From 20ca5027ccb30f094bc87189ef19ecdb59156137 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 29 Mar 2011 16:58:15 -0700 Subject: [PATCH] Consolidate common code in micro-apps custom notice type display actions. The ShowNoticeAction subclasses were cut-n-pasting a lot of prepare() code from ShowNoticeAction, though the only part that's different is how we look up the notice. Broke that out to a getNotice() method, so only that needs to be copied. Avoids extra copies of permission checks and other common code in this spot. --- actions/shownotice.php | 44 ++++++++++++++++++----------- plugins/Bookmark/showbookmark.php | 43 +++-------------------------- plugins/Event/showevent.php | 46 +++---------------------------- plugins/Event/showrsvp.php | 46 +++---------------------------- plugins/Poll/showpoll.php | 45 +++--------------------------- 5 files changed, 44 insertions(+), 180 deletions(-) diff --git a/actions/shownotice.php b/actions/shownotice.php index 15358fd082..b6d0625e13 100644 --- a/actions/shownotice.php +++ b/actions/shownotice.php @@ -77,22 +77,7 @@ class ShownoticeAction extends OwnerDesignAction StatusNet::setApi(true); } - $id = $this->arg('notice'); - - $this->notice = Notice::staticGet('id', $id); - - if (empty($this->notice)) { - // Did we used to have it, and it got deleted? - $deleted = Deleted_notice::staticGet($id); - if (!empty($deleted)) { - // TRANS: Client error displayed trying to show a deleted notice. - $this->clientError(_('Notice deleted.'), 410); - } else { - // TRANS: Client error displayed trying to show a non-existing notice. - $this->clientError(_('No such notice.'), 404); - } - return false; - } + $this->notice = $this->getNotice(); $cur = common_current_user(); @@ -122,6 +107,33 @@ class ShownoticeAction extends OwnerDesignAction return true; } + /** + * Fetch the notice to show. This may be overridden by child classes to + * customize what we fetch without duplicating all of the prepare() method. + * + * @return Notice + */ + function getNotice() + { + $id = $this->arg('notice'); + + $notice = Notice::staticGet('id', $id); + + if (empty($notice)) { + // Did we used to have it, and it got deleted? + $deleted = Deleted_notice::staticGet($id); + if (!empty($deleted)) { + // TRANS: Client error displayed trying to show a deleted notice. + $this->clientError(_('Notice deleted.'), 410); + } else { + // TRANS: Client error displayed trying to show a non-existing notice. + $this->clientError(_('No such notice.'), 404); + } + return false; + } + return $notice; + } + /** * Is this action read-only? * diff --git a/plugins/Bookmark/showbookmark.php b/plugins/Bookmark/showbookmark.php index 9eb5778b2c..27f24876a7 100644 --- a/plugins/Bookmark/showbookmark.php +++ b/plugins/Bookmark/showbookmark.php @@ -48,16 +48,8 @@ class ShowbookmarkAction extends ShownoticeAction { protected $bookmark = null; - /** - * For initializing members of the class. - * - * @param array $argarray misc. arguments - * - * @return boolean true - */ - function prepare($argarray) + function getNotice() { - OwnerDesignAction::prepare($argarray); $this->id = $this->trimmed('id'); @@ -68,42 +60,15 @@ class ShowbookmarkAction extends ShownoticeAction throw new ClientException(_m('No such bookmark.'), 404); } - $this->notice = Notice::staticGet('uri', $this->bookmark->uri); + $notice = Notice::staticGet('uri', $this->bookmark->uri); - if (empty($this->notice)) { + if (empty($notice)) { // Did we used to have it, and it got deleted? // TRANS: Client exception thrown when referring to a non-existing bookmark. throw new ClientException(_m('No such bookmark.'), 404); } - if (!empty($cur)) { - $curProfile = $cur->getProfile(); - } else { - $curProfile = null; - } - - if (!$this->notice->inScope($curProfile)) { - // TRANS: Client exception thrown when referring to a bookmark the user has no access to. - throw new ClientException(_m('Not available.'), 403); - } - - $this->user = User::staticGet('id', $this->bookmark->profile_id); - - if (empty($this->user)) { - // TRANS: Client exception thrown when referring to a bookmark for a non-existing user. - throw new ClientException(_m('No such user.'), 404); - } - - $this->profile = $this->user->getProfile(); - - if (empty($this->profile)) { - // TRANS: Client exception thrown when referring to a bookmark for a non-existing profile. - throw new ServerException(_m('User without a profile.')); - } - - $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE); - - return true; + return $notice; } /** diff --git a/plugins/Event/showevent.php b/plugins/Event/showevent.php index b31360aa5c..e5afd47d00 100644 --- a/plugins/Event/showevent.php +++ b/plugins/Event/showevent.php @@ -49,17 +49,8 @@ class ShoweventAction extends ShownoticeAction protected $id = null; protected $event = null; - /** - * For initializing members of the class. - * - * @param array $argarray misc. arguments - * - * @return boolean true - */ - function prepare($argarray) + function getNotice() { - OwnerDesignAction::prepare($argarray); - $this->id = $this->trimmed('id'); $this->event = Happening::staticGet('id', $this->id); @@ -69,44 +60,15 @@ class ShoweventAction extends ShownoticeAction throw new ClientException(_m('No such event.'), 404); } - $this->notice = $this->event->getNotice(); + $notice = $this->event->getNotice(); - if (empty($this->notice)) { + if (empty($notice)) { // Did we used to have it, and it got deleted? // TRANS: Client exception thrown when referring to a non-existing event. throw new ClientException(_m('No such event.'), 404); } - $cur = common_current_user(); - - if (!empty($cur)) { - $curProfile = $cur->getProfile(); - } else { - $curProfile = null; - } - - if (!$this->notice->inScope($curProfile)) { - // TRANS: Client exception thrown when referring to an event the user has no access to. - throw new ClientException(_m('Not available.'), 403); - } - - $this->user = User::staticGet('id', $this->event->profile_id); - - if (empty($this->user)) { - // TRANS: Client exception thrown when referring to a non-existing user. - throw new ClientException(_m('No such user.'), 404); - } - - $this->profile = $this->user->getProfile(); - - if (empty($this->profile)) { - // TRANS: Server exception thrown when referring to a user without a profile. - throw new ServerException(_m('User without a profile.')); - } - - $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE); - - return true; + return $notice; } /** diff --git a/plugins/Event/showrsvp.php b/plugins/Event/showrsvp.php index 1f96b394df..145788feea 100644 --- a/plugins/Event/showrsvp.php +++ b/plugins/Event/showrsvp.php @@ -49,17 +49,8 @@ class ShowrsvpAction extends ShownoticeAction protected $rsvp = null; protected $event = null; - /** - * For initializing members of the class. - * - * @param array $argarray misc. arguments - * - * @return boolean true - */ - function prepare($argarray) + function getNotice() { - OwnerDesignAction::prepare($argarray); - $this->id = $this->trimmed('id'); $this->rsvp = RSVP::staticGet('id', $this->id); @@ -77,45 +68,16 @@ class ShowrsvpAction extends ShownoticeAction throw new ClientException(_m('No such Event.'), 404); } - $this->notice = $this->rsvp->getNotice(); + $notice = $this->rsvp->getNotice(); - if (empty($this->notice)) { + if (empty($notice)) { // Did we used to have it, and it got deleted? // TRANS: Client exception thrown when referring to a non-existing RSVP. // TRANS: RSVP stands for "Please reply". throw new ClientException(_m('No such RSVP.'), 404); } - $cur = common_current_user(); - - if (!empty($cur)) { - $curProfile = $cur->getProfile(); - } else { - $curProfile = null; - } - - if (!$this->notice->inScope($curProfile)) { - // TRANS: Client exception thrown when referring to an event the user has no access to. - throw new ClientException(_m('Not available.'), 403); - } - - $this->user = User::staticGet('id', $this->rsvp->profile_id); - - if (empty($this->user)) { - // TRANS: Client exception thrown when referring to a non-existing user. - throw new ClientException(_m('No such user.'), 404); - } - - $this->profile = $this->user->getProfile(); - - if (empty($this->profile)) { - // TRANS: Server exception thrown when referring to a user without a profile. - throw new ServerException(_m('User without a profile.')); - } - - $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE); - - return true; + return $notice; } /** diff --git a/plugins/Poll/showpoll.php b/plugins/Poll/showpoll.php index d59d9e28f3..edfe3fe245 100644 --- a/plugins/Poll/showpoll.php +++ b/plugins/Poll/showpoll.php @@ -48,17 +48,8 @@ class ShowPollAction extends ShownoticeAction { protected $poll = null; - /** - * For initializing members of the class. - * - * @param array $argarray misc. arguments - * - * @return boolean true - */ - function prepare($argarray) + function getNotice() { - OwnerDesignAction::prepare($argarray); - $this->id = $this->trimmed('id'); $this->poll = Poll::staticGet('id', $this->id); @@ -68,43 +59,15 @@ class ShowPollAction extends ShownoticeAction throw new ClientException(_m('No such poll.'), 404); } - $this->notice = $this->poll->getNotice(); + $notice = $this->poll->getNotice(); - if (empty($this->notice)) { + if (empty($notice)) { // Did we used to have it, and it got deleted? // TRANS: Client exception thrown trying to view a non-existing poll notice. throw new ClientException(_m('No such poll notice.'), 404); } - $cur = common_current_user(); - - if (!empty($cur)) { - $curProfile = $cur->getProfile(); - } else { - $curProfile = null; - } - - if (!$this->notice->inScope($curProfile)) { - throw new ClientException(_('Not available.'), 403); - } - - $this->user = User::staticGet('id', $this->poll->profile_id); - - if (empty($this->user)) { - // TRANS: Client exception thrown trying to view a poll of a non-existing user. - throw new ClientException(_m('No such user.'), 404); - } - - $this->profile = $this->user->getProfile(); - - if (empty($this->profile)) { - // TRANS: Server exception thrown trying to view a poll for a user for which the profile could not be loaded. - throw new ServerException(_m('User without a profile.')); - } - - $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE); - - return true; + return $notice; } /** -- 2.39.2