From 0ad7411343bae32fbc60c6d35ea3a7f53f3bf386 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 20 Jul 2011 11:33:28 -0400 Subject: [PATCH] Make process*() functions return existing or new notice --- lib/microappplugin.php | 4 +- plugins/OStatus/classes/Ostatus_profile.php | 49 ++++++++++++--------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/microappplugin.php b/lib/microappplugin.php index 0ef2d50141..1c886d5a69 100644 --- a/lib/microappplugin.php +++ b/lib/microappplugin.php @@ -345,7 +345,7 @@ abstract class MicroAppPlugin extends Plugin * * @return boolean hook value */ - function onStartHandleFeedEntryWithProfile($activity, $oprofile) + function onStartHandleFeedEntryWithProfile($activity, $oprofile, &$notice) { if ($this->isMyActivity($activity)) { @@ -364,7 +364,7 @@ abstract class MicroAppPlugin extends Plugin 'source' => 'ostatus'); // $actor is an ostatus_profile - $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options); + $notice = $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options); return false; } diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 06eefde292..2412638ecc 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -463,18 +463,23 @@ class Ostatus_profile extends Managed_DataObject * @param DOMElement $entry * @param DOMElement $feed for context * @param string $source identifier ("push" or "salmon") + * + * @return Notice Notice representing the new (or existing) activity */ + public function processEntry($entry, $feed, $source) { $activity = new Activity($entry, $feed); - $this->processActivity($activity, $source); + return $this->processActivity($activity, $source); } public function processActivity($activity, $source) { + $notice = null; + // The "WithProfile" events were added later. - if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this)) && + if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this, &$notice)) && Event::handle('StartHandleFeedEntry', array($activity))) { switch ($activity->verb) { @@ -487,7 +492,7 @@ class Ostatus_profile extends Managed_DataObject case ActivityObject::STATUS: case ActivityObject::COMMENT: case null: - $this->processPost($activity, $source); + $notice = $this->processPost($activity, $source); break; default: // TRANS: Client exception. @@ -495,24 +500,28 @@ class Ostatus_profile extends Managed_DataObject } break; case ActivityVerb::SHARE: - $result = $this->processShare($activity, $source); + $notice = $this->processShare($activity, $source); break; default: common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb"); } Event::handle('EndHandleFeedEntry', array($activity)); - Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this)); + Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice)); } + + return $notice; } public function processShare($activity, $method) { + $notice = null; + $oprofile = $this->checkAuthorship($activity); if (empty($oprofile)) { common_log(LOG_INFO, "No author matched share activity"); - return false; + return null; } if (count($activity->objects) != 1) { @@ -529,16 +538,10 @@ class Ostatus_profile extends Managed_DataObject // Save the item (or check for a dupe) - $other->processActivity($shared, $method); + $sharedNotice = $other->processActivity($shared, $method); - // XXX: process*() should return the new or existing notice. They don't, so we have to - // go fishing for it now. - - $sharedId = ($shared->id) ? $shared->id : $shared->objects[0]->id; - - $sharedNotice = Notice::staticGet('uri', $sharedId); - if (empty($sharedNotice)) { + $sharedId = ($shared->id) ? $shared->id : $shared->objects[0]->id; throw new ClientException(sprintf(_m("Failed to save activity %s"), $sharedId)); } @@ -552,7 +555,7 @@ class Ostatus_profile extends Managed_DataObject $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri"); - return false; + return $dupe; } // We'll also want to save a web link to the original notice, if provided. @@ -689,10 +692,12 @@ class Ostatus_profile extends Managed_DataObject $options['urls'][] = $href; } - return Notice::saveNew($oprofile->profile_id, - $content, - 'ostatus', - $content); + $notice = Notice::saveNew($oprofile->profile_id, + $content, + 'ostatus', + $content); + + return $notice; } /** @@ -704,10 +709,12 @@ class Ostatus_profile extends Managed_DataObject */ public function processPost($activity, $method) { + $notice = null; + $oprofile = $this->checkAuthorship($activity); if (empty($oprofile)) { - return false; + return null; } // It's not always an ActivityObject::NOTE, but... let's just say it is. @@ -721,7 +728,7 @@ class Ostatus_profile extends Managed_DataObject $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri"); - return false; + return $dupe; } // We'll also want to save a web link to the original notice, if provided.