From bda30a92bc41912ac60dbd417aae00fc27eaa4fc Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 3 Jan 2016 22:35:49 +0100 Subject: [PATCH] Fave deletion would fail in some cases with missing profiles or notices --- plugins/Favorite/classes/Fave.php | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/plugins/Favorite/classes/Fave.php b/plugins/Favorite/classes/Fave.php index ad7cc67df8..7fa7705e8f 100644 --- a/plugins/Favorite/classes/Fave.php +++ b/plugins/Favorite/classes/Fave.php @@ -114,24 +114,38 @@ class Fave extends Managed_DataObject public function delete($useWhere=false) { - $profile = Profile::getKV('id', $this->user_id); - $notice = Notice::getKV('id', $this->notice_id); - $result = null; - if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) { + try { + $profile = $this->getActor(); + $notice = $this->getTarget(); + + if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) { + + $result = parent::delete($useWhere); + + self::blowCacheForProfileId($this->user_id); + self::blowCacheForNoticeId($this->notice_id); + self::blow('popular'); + + if ($result !== false) { + Event::handle('EndDisfavorNotice', array($profile, $notice)); + } + } + + } catch (NoResultException $e) { + common_log(LOG_INFO, '"'.get_class($e->obj).'" with id=='.var_export($e->obj->id, true).' object not found when deleting favorite, ignoring...'); + // Delete it without the event, as something is wrong and we don't want it anyway. $result = parent::delete($useWhere); self::blowCacheForProfileId($this->user_id); self::blowCacheForNoticeId($this->notice_id); self::blow('popular'); - - if ($result) { - Event::handle('EndDisfavorNotice', array($profile, $notice)); - } } + + return $result; } @@ -391,14 +405,7 @@ class Fave extends Managed_DataObject public function getTarget() { - // throws exception on failure - $target = new Notice(); - $target->id = $this->notice_id; - if (!$target->find(true)) { - throw new NoResultException($target); - } - - return $target; + return Notice::getByID($this->notice_id); } public function getTargetObject() -- 2.39.2