From 8a273eef2078d77faad027ce46a4cfca0ada8225 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Tue, 10 Mar 2015 11:50:16 +0100 Subject: [PATCH] Move AlreadyFulfilled check to Fave::addNew --- plugins/AnonymousFave/actions/anonfavor.php | 5 ----- plugins/Favorite/actions/apifavoritecreate.php | 17 +++++------------ plugins/Favorite/actions/favor.php | 7 +------ plugins/Favorite/classes/Fave.php | 5 +++++ plugins/Favorite/lib/favcommand.php | 11 ----------- 5 files changed, 11 insertions(+), 34 deletions(-) diff --git a/plugins/AnonymousFave/actions/anonfavor.php b/plugins/AnonymousFave/actions/anonfavor.php index 4029154d72..287c25cbe9 100644 --- a/plugins/AnonymousFave/actions/anonfavor.php +++ b/plugins/AnonymousFave/actions/anonfavor.php @@ -62,11 +62,6 @@ class AnonFavorAction extends RedirectingAction $notice = Notice::getKV($id); $token = $this->checkSessionToken(); - if (Fave::existsForProfile($notice, $profile)) { - // TRANS: Client error. - throw new AlreadyFulfilledException(_m('This notice is already a favorite!')); - } - // Throws exception $stored = Fave::addNew($profile, $notice); diff --git a/plugins/Favorite/actions/apifavoritecreate.php b/plugins/Favorite/actions/apifavoritecreate.php index 736bd7b0a1..7e63b199e2 100644 --- a/plugins/Favorite/actions/apifavoritecreate.php +++ b/plugins/Favorite/actions/apifavoritecreate.php @@ -89,20 +89,13 @@ class ApiFavoriteCreateAction extends ApiAuthAction ); } - // Note: Twitter lets you fave things repeatedly via API. - - if (Fave::existsForProfile($this->notice, $this->scoped)) { - $this->clientError( - // TRANS: Client error displayed when trying to mark a notice favourite that already is a favourite. - _('This status is already a favorite.'), - 403, - $this->format - ); + try { + $stored = Fave::addNew($this->scoped, $this->notice); + } catch (AlreadyFulfilledException $e) { + // Note: Twitter lets you fave things repeatedly via API. + $this->clientError($e->getMessage(), 403); } - // throws exception on failure - $stored = Fave::addNew($this->scoped, $this->notice); - if ($this->format == 'xml') { $this->showSingleXmlStatus($this->notice); } elseif ($this->format == 'json') { diff --git a/plugins/Favorite/actions/favor.php b/plugins/Favorite/actions/favor.php index f659078450..0d52f77961 100644 --- a/plugins/Favorite/actions/favor.php +++ b/plugins/Favorite/actions/favor.php @@ -61,12 +61,7 @@ class FavorAction extends FormAction protected function doPost() { - if (Fave::existsForProfile($this->target, $this->scoped)) { - // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite. - throw new AlreadyFulfilledException(_('You have already favorited this!')); - } - - // throws exception on failure + // throws exception on failure, might be an AlreadyFulfilledException $stored = Fave::addNew($this->scoped, $this->target); // TRANS: Message when a favor action has been taken for a notice. diff --git a/plugins/Favorite/classes/Fave.php b/plugins/Favorite/classes/Fave.php index 094b65feaa..ba0d04ba25 100644 --- a/plugins/Favorite/classes/Fave.php +++ b/plugins/Favorite/classes/Fave.php @@ -48,6 +48,11 @@ class Fave extends Managed_DataObject * @throws Exception on failure */ static function addNew(Profile $actor, Notice $target) { + if (self::existsForProfile($target, $actor)) { + // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite. + throw new AlreadyFulfilledException(_('You have already favorited this!')); + } + $act = new Activity(); $act->type = ActivityObject::ACTIVITY; $act->verb = ActivityVerb::FAVORITE; diff --git a/plugins/Favorite/lib/favcommand.php b/plugins/Favorite/lib/favcommand.php index cf5ca79218..6c7d0d1946 100644 --- a/plugins/Favorite/lib/favcommand.php +++ b/plugins/Favorite/lib/favcommand.php @@ -14,17 +14,6 @@ class FavCommand extends Command { $notice = $this->getNotice($this->other); - $fave = new Fave(); - $fave->user_id = $this->user->id; - $fave->notice_id = $notice->id; - $fave->find(); - - if ($fave->fetch()) { - // TRANS: Error message text shown when a favorite could not be set because it has already been favorited. - $channel->error($this->user, _('Could not create favorite: Already favorited.')); - return; - } - try { $fave = Fave::addNew($this->user->getProfile(), $notice); } catch (Exception $e) { -- 2.39.5