Move AlreadyFulfilled check to Fave::addNew

This commit is contained in:
Mikael Nordfeldth 2015-03-10 11:50:16 +01:00
parent fa080328cf
commit 8a273eef20
5 changed files with 11 additions and 34 deletions

View File

@ -62,11 +62,6 @@ class AnonFavorAction extends RedirectingAction
$notice = Notice::getKV($id); $notice = Notice::getKV($id);
$token = $this->checkSessionToken(); $token = $this->checkSessionToken();
if (Fave::existsForProfile($notice, $profile)) {
// TRANS: Client error.
throw new AlreadyFulfilledException(_m('This notice is already a favorite!'));
}
// Throws exception // Throws exception
$stored = Fave::addNew($profile, $notice); $stored = Fave::addNew($profile, $notice);

View File

@ -89,19 +89,12 @@ class ApiFavoriteCreateAction extends ApiAuthAction
); );
} }
// Note: Twitter lets you fave things repeatedly via API. try {
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
);
}
// throws exception on failure
$stored = Fave::addNew($this->scoped, $this->notice); $stored = Fave::addNew($this->scoped, $this->notice);
} catch (AlreadyFulfilledException $e) {
// Note: Twitter lets you fave things repeatedly via API.
$this->clientError($e->getMessage(), 403);
}
if ($this->format == 'xml') { if ($this->format == 'xml') {
$this->showSingleXmlStatus($this->notice); $this->showSingleXmlStatus($this->notice);

View File

@ -61,12 +61,7 @@ class FavorAction extends FormAction
protected function doPost() protected function doPost()
{ {
if (Fave::existsForProfile($this->target, $this->scoped)) { // throws exception on failure, might be an AlreadyFulfilledException
// 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
$stored = Fave::addNew($this->scoped, $this->target); $stored = Fave::addNew($this->scoped, $this->target);
// TRANS: Message when a favor action has been taken for a notice. // TRANS: Message when a favor action has been taken for a notice.

View File

@ -48,6 +48,11 @@ class Fave extends Managed_DataObject
* @throws Exception on failure * @throws Exception on failure
*/ */
static function addNew(Profile $actor, Notice $target) { 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 = new Activity();
$act->type = ActivityObject::ACTIVITY; $act->type = ActivityObject::ACTIVITY;
$act->verb = ActivityVerb::FAVORITE; $act->verb = ActivityVerb::FAVORITE;

View File

@ -14,17 +14,6 @@ class FavCommand extends Command
{ {
$notice = $this->getNotice($this->other); $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 { try {
$fave = Fave::addNew($this->user->getProfile(), $notice); $fave = Fave::addNew($this->user->getProfile(), $notice);
} catch (Exception $e) { } catch (Exception $e) {