-
Notifications
You must be signed in to change notification settings - Fork 73
Added timeline echo for photos and timeline notifications #1
Changes from 1 commit
958e63a
b1ab44b
c46d2a7
d3eb49a
68ecbbd
d3caf0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,53 @@ | |
* limitations under the License. | ||
*/ | ||
// Author: Jenny Murphy - http://google.com/+JennyMurphy | ||
// Modified by: Winnie Tong | ||
|
||
|
||
|
||
// Always respond with a 200 right away and then terminate the connection to prevent notification | ||
// retries. How this is done depends on your HTTP server configs. I'll try a few common techniques | ||
// here, but if none of these work, start troubleshooting here. | ||
|
||
// First try: the content length header | ||
header("Content-length: 0"); | ||
|
||
// Next, assuming it didn't work, attempt to close the output buffer by setting the time limit. | ||
ignore_user_abort(true); | ||
set_time_limit(0); | ||
|
||
// And one more thing to try: forking the heavy lifting into a new process. Yeah, crazy eh? | ||
if(function_exists('pcntl_fork')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/if(/if (/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
$pid = pcntl_fork(); | ||
if ($pid == -1) { | ||
error_log("could not fork!"); | ||
exit(); | ||
} else if ($pid) { | ||
// fork worked! but I'm the parent. time to exit. | ||
exit(); | ||
} | ||
} | ||
|
||
// In the child process (hopefully). Do the processing. | ||
require_once 'config.php'; | ||
require_once 'mirror-client.php'; | ||
require_once 'google-api-php-client/src/Google_Client.php'; | ||
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php'; | ||
require_once 'util.php'; | ||
|
||
if($_SERVER['REQUEST_METHOD'] != "POST") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/if(/if (/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
http_send_status(400); | ||
echo "method not supported"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set the status to 405 Method not supported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
exit(); | ||
} | ||
|
||
// Parse the request body | ||
$request = json_decode(http_get_request_body()); | ||
$request_bytes = @file_get_contents('php://input'); | ||
$request = json_decode($request_bytes, true); | ||
|
||
// A notification has come in. If there's an attached photo, bounce it back | ||
// to the user | ||
$user_id = $request['userToken']; | ||
|
||
$access_token = get_credentials($user_id); | ||
|
||
$client = get_google_api_client(); | ||
|
@@ -40,8 +70,34 @@ | |
// A glass service for interacting with the Mirror API | ||
$mirror_service = new Google_MirrorService($client); | ||
|
||
$timeline_item = new Google_TimelineItem(); | ||
$timeline_item->setText("Got a notification " . $request); | ||
switch($request['collection']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/switch(/switch (/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, you should check that the userActions array contains the "SHARE" action. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
case "timeline": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You use double quotes here but single quotes in other places, try to be consistent as much as you can. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Cleaned 'em up by moving more to single quotes in places I don't expect anyone to supply a variable. |
||
$timeline_item_id = $request['itemId']; | ||
|
||
$timeline_item = new Google_TimelineItem($mirror_service->timeline->get($timeline_item_id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to instantiate a new Object? You can set the global "useObject" flag to tell the client library to instantiate objects for you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
$attachments = $timeline_item->getAttachments(); | ||
$attachment = $attachments[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check that attachment actually exists. Also, the $attachment might not contain the "contentUrl" property: you should send a GET request to the attachment resource to actually retrieve the full body: |
||
|
||
$bytes = downloadAttachment($timeline_item_id, $attachment); | ||
|
||
// Insert a new timeline card, with a copy of that photo attached | ||
$echo_timeline_item = new Google_TimelineItem(); | ||
$echo_timeline_item->setText("Echoing your shared photo"); | ||
$echo_timeline_item->setNotification( | ||
new google_NotificationConfig(array("level"=>"DEFAULT"))); | ||
insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); | ||
break; | ||
case "locations": | ||
$location = new Google_Location($mirror_service->locations->get("latest")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment regarding object instantiation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
// Insert a new timeline card, with a copy of that photo attached | ||
$loc_timeline_item = new Google_TimelineItem(); | ||
$loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " . | ||
$location->getLongitude()); | ||
|
||
insertTimelineItem($mirror_service, $loc_timeline_item, null, null); | ||
break; | ||
default: | ||
error_log("I don't know how to process this notification:" . $request); | ||
} | ||
|
||
insertTimelineItem($mirror_service, $timeline_item, null, null); | ||
//TODO: if there's an attached photo, echo it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall comment on the rest of the Quckstart that I didn't notice before: some variable names are camelCased whereas some are underscored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any of the changes, did you push them somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haven't pushed them yet :) Will once I fix the variables issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok pushed!