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.
This commit is contained in:
Brion Vibber 2011-03-29 16:58:15 -07:00
parent 23436ad83d
commit 20ca5027cc
5 changed files with 44 additions and 180 deletions

View File

@ -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?
*

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
/**