Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Added timeline echo for photos and timeline notifications #1

Merged
merged 6 commits into from
May 30, 2013
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

$base_url = "http://localhost/mirror-quickstart-php";

$sqlite_database = "/tmp/database.sqlite";
$sqlite_database = "/tmp/database.sqlite";
14 changes: 7 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

// But first, handle POST data from the form (if there is any)
switch ($_POST['operation']) {
case "insertItem":
case 'insertItem':
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText($_POST['message']);

Expand All @@ -54,7 +54,7 @@

$message = "Timeline Item inserted!";
break;
case "insertItemWithAction":
case 'insertItemWithAction':
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setText("What did you have for lunch?");

Expand Down Expand Up @@ -92,7 +92,7 @@

$message = "Inserted a timeline item you can reply to";
break;
case "insertTimelineAllUsers":
case 'insertTimelineAllUsers':
$credentials = list_credentials();
if (count($credentials) > 10) {
$message = "Found " . count($credentials) . " users. Aborting to save your quota.";
Expand All @@ -111,19 +111,19 @@
$message = "Sent a cat fact to " . count($credentials) . " users.";
}
break;
case "insertSubscription":
case 'insertSubscription':
$message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'],
$_SESSION['userid'], $base_url . "/notify.php");
break;
case "deleteSubscription":
case 'deleteSubscription':
$message = $mirror_service->subscriptions->delete($_POST['subscriptionId']);
break;
case "insertContact":
case 'insertContact':
insertContact($mirror_service, $_POST['id'], $_POST['name'],
$base_url . "/static/images/chipotle-tube-640x360.jpg");
$message = "Contact inserted. Enable it on MyGlass.";
break;
case "deleteContact":
case 'deleteContact':
deleteContact($mirror_service, $_POST['id']);
$message = "Contact deleted.";
break;
Expand Down
78 changes: 71 additions & 7 deletions notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,53 @@
* limitations under the License.
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok pushed!

*/
// 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')) {
$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") {
http_send_status(400);
if ($_SERVER['REQUEST_METHOD'] != "POST") {
echo "method not supported";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the status to 405 Method not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Expand All @@ -40,8 +70,42 @@
// 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']) {
case 'timeline':
// Verify that it's a share
foreach ($request['userActions'] as $i => $user_action) {
if ($user_action['type'] == 'SHARE') {

$timeline_item_id = $request['itemId'];

$timeline_item = $mirror_service->timeline->get($timeline_item_id);

foreach($timeline_item->getAttachments() as $j => $attachment) {
$attachment = $mirror_service->timeline->attachments->get($timeline_item_id, $attachment.getId());
$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;
}
}

break;
case 'locations':
$location = $mirror_service->locations->get("latest");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would recommend using $request['itemId'] here to be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
24 changes: 0 additions & 24 deletions util.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,6 @@
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';

// Returns an unauthenticated service
function get_google_api_client() {
global $api_client_id, $api_client_secret, $api_simple_key, $base_url;
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();

$client = new Google_Client();

$client->setApplicationName('Google Mirror API PHP Quick Start');

// These are set in config.php
$client->setClientId($api_client_id);
$client->setClientSecret($api_client_secret);
$client->setDeveloperKey($api_simple_key);
$client->setRedirectUri($base_url."/oauth2callback.php");

$client->setScopes(array(
'https://www.googleapis.com/auth/glass.timeline',
'https://www.googleapis.com/auth/glass.location',
'https://www.googleapis.com/auth/userinfo.profile'));

return $client;
}

function store_credentials($user_id, $credentials) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! I moved it to mirror-client.php

$db = init_db();
Expand Down