From 30815645fc8b2cfb80e734b97f79f9ffda5833fd Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Mon, 20 Dec 2010 22:02:50 -0500 Subject: [PATCH] Photo federation is basically working now, could use some polish. --- .../GNUsocialPhotos/GNUsocialPhotosPlugin.php | 102 +++++++++++------- .../GNUsocialPhotos/actions/photoupload.php | 4 +- .../classes/gnusocialphoto.php | 31 +++--- plugins/GNUsocialPhotos/lib/tempphoto.php | 37 +++++++ 4 files changed, 112 insertions(+), 62 deletions(-) create mode 100644 plugins/GNUsocialPhotos/lib/tempphoto.php diff --git a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php index 8aa10c7f70..8e603c2fe0 100644 --- a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php +++ b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php @@ -39,6 +39,7 @@ class GNUsocialPhotosPlugin extends Plugin { $dir = dirname(__FILE__); + include_once $dir . '/lib/tempphoto.php'; switch ($cls) { case 'PhotosAction': @@ -81,53 +82,72 @@ class GNUsocialPhotosPlugin extends Plugin return true; } - function onStartActivityDefaultObjectType(&$notice, &$xs, &$type) + function onStartNoticeDistribute($notice) { - $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id); - if($photo) { - $type = ActivityObject::PHOTO; - } - } - - function onStartActivityObjects(&$notice, &$xs, &$objects) - { - $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id); - if($photo) { - $object = new ActivityObject(); - $object->thumbnail = $photo->thumb_uri; - $object->largerImage = $photo->uri; - $object->type = ActivityObject::PHOTO; - - $object->id = $notice->id; - $objects[0] = $object; - } - } - - function onStartHandleFeedEntry($activity) - { - if ($activity->verb == ActivityVerb::POST) { - $oprofile = Ostatus_profile::ensureActorProfile($activity); - foreach ($activity->objects as $object) { - if ($object->type == ActivityObject::PHOTO) { - $uri = $object->largerImage; - $thumb_uri = $object->thumbnail; - $profile_id = $oprofile->profile_id; - $source = 'unknown'; // TODO: put something better here. - - $uri = filter_var($uri, FILTER_SANITIZE_URL); - $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL); - $uri = filter_var($uri, FILTER_VALIDATE_URL); - $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL); - if (!empty($uri) && !empty($thumb_uri)) { - GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source); - } - return false; - } + common_log(LOG_INFO, "event: StartNoticeDistribute"); + if (GNUsocialPhotoTemp::$tmp) { + GNUsocialPhotoTemp::$tmp->notice_id = $notice->id; + $photo_id = GNUsocialPhotoTemp::$tmp->insert(); + if (!$photo_id) { + common_log_db_error($photo, 'INSERT', __FILE__); + throw new ServerException(_m('Problem saving photo.')); } } return true; } + function onEndNoticeAsActivity($notice, &$activity) + { + common_log(LOG_INFO, 'photo plugin: EndNoticeAsActivity'); + $photo = GNUsocialPhoto::staticGet('notice_id', $notice->id); + if(!$photo) { + common_log(LOG_INFO, 'not a photo.'); + return true; + } + + $activity->objects[0]->type = ActivityObject::PHOTO; + $activity->objects[0]->thumbnail = $photo->thumb_uri; + $activity->objects[0]->largerImage = $photo->uri; + return false; + } + + + function onStartHandleFeedEntry($activity) + { + common_log(LOG_INFO, 'photo plugin: onEndAtomPubNewActivity'); + $oprofile = Ostatus_profile::ensureActorProfile($activity); + foreach ($activity->objects as $object) { + if($object->type == ActivityObject::PHOTO) { + $uri = $object->largerImage; + $thumb_uri = $object->thumbnail; + $profile_id = $oprofile->profile_id; + $source = 'unknown'; // TODO: put something better here. + + common_log(LOG_INFO, 'uri : ' . $uri); + common_log(LOG_INFO, 'thumb_uri : ' . $thumb_uri); + + // It's possible this is validated elsewhere, but I'm not sure and + // would rather be safe. + $uri = filter_var($uri, FILTER_SANITIZE_URL); + $thumb_uri = filter_var($thumb_uri, FILTER_SANITIZE_URL); + $uri = filter_var($uri, FILTER_VALIDATE_URL); + $thumb_uri = filter_var($thumb_uri, FILTER_VALIDATE_URL); + + if(empty($thumb_uri)) { + // We need a thumbnail, so if we aren't given one, use the actual picture for now. + $thumb_uri = $uri; + } + + if (!empty($uri) && !empty($thumb_uri)) { + GNUsocialPhoto::saveNew($profile_id, $thumb_uri, $uri, $source, false); + } else { + common_log(LOG_INFO, 'bad URI for photo'); + } + return false; + } + } + return true; + } function onStartShowNoticeItem($action) { diff --git a/plugins/GNUsocialPhotos/actions/photoupload.php b/plugins/GNUsocialPhotos/actions/photoupload.php index 209b31ab4f..b4e7771a18 100644 --- a/plugins/GNUsocialPhotos/actions/photoupload.php +++ b/plugins/GNUsocialPhotos/actions/photoupload.php @@ -142,9 +142,9 @@ class PhotouploadAction extends Action $album = GNUsocialPhotoAlbum::staticGet('profile_id', $profile_id); if(!$album) { $album = GNUsocialPhotoAlbum::newAlbum($profile_id, 'Default'); - GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web'); + GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false); } else { - GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web'); + GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false); } } diff --git a/plugins/GNUsocialPhotos/classes/gnusocialphoto.php b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php index 1314ba87e0..3df160822b 100644 --- a/plugins/GNUsocialPhotos/classes/gnusocialphoto.php +++ b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php @@ -89,19 +89,24 @@ class GNUsocialPhoto extends Memcached_DataObject return array(false, false, false); } - function saveNew($profile_id, $album_id, $thumb_uri, $uri, $source) + function saveNew($profile_id, $album_id, $thumb_uri, $uri, $source, $insert_now) { $photo = new GNUsocialPhoto(); $photo->thumb_uri = $thumb_uri; $photo->uri = $uri; $photo->album_id = $album_id; - $notice = Notice::saveNew($profile_id, $uri, $source); - $photo->notice_id = $notice->id; - $photo_id = $photo->insert(); - if (!$photo_id) { - common_log_db_error($photo, 'INSERT', __FILE__); - throw new ServerException(_m('Problem Saving Photo.')); + if($insert_now) { + $notice = Notice::saveNew($profile_id, $uri, $source); + $photo->notice_id = $notice->id; + $photo_id = $photo->insert(); + if (!$photo_id) { + common_log_db_error($photo, 'INSERT', __FILE__); + throw new ServerException(_m('Problem Saving Photo.')); + } + } else { + GNUsocialPhotoTemp::$tmp = $photo; + Notice::saveNew($profile_id, $uri, $source); } } @@ -128,16 +133,4 @@ class GNUsocialPhoto extends Memcached_DataObject return $photos; } - - /* - function asActivityNoun($element) - { - $object = new ActivityObject(); - - $object->type = ActivityObject::PHOTO; - $object->title = ""; - $object->thumbnail = 'http://' . common_config('site', 'server') . $this->thumb_uri; - $object->largerImage = 'http://' . common_config('site', 'server') . $this->path; - return $object; - } */ } diff --git a/plugins/GNUsocialPhotos/lib/tempphoto.php b/plugins/GNUsocialPhotos/lib/tempphoto.php new file mode 100644 index 0000000000..119c301083 --- /dev/null +++ b/plugins/GNUsocialPhotos/lib/tempphoto.php @@ -0,0 +1,37 @@ +. + * + * @package GNU Social + * @author Ian Denhardt + * @copyright 2010 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +// XXX: This entire file is an ugly hack and needs to be replaced. It is essentially a global variable +// used to store information about a photo we want to insert in onStartNoticeDistribute(), which we +// can't actually pass to that function. +class GNUsocialPhotoTemp { + public static $tmp = null; +}