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);
$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);

View File

@ -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') {

View File

@ -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.

View File

@ -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;

View File

@ -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) {