3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Show a single favorite in Atom Activity Streams format
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Show a single favorite in Atom Activity Streams format.
40 * Can also be used to delete a favorite.
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2010 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47 * @link http://status.net/
49 class AtompubshowfavoriteAction extends ApiAuthAction
51 private $_profile = null;
52 private $_notice = null;
53 private $_fave = null;
56 * For initializing members of the class.
58 * @param array $argarray misc. arguments
60 * @return boolean true
62 function prepare($argarray)
64 parent::prepare($argarray);
66 $profileId = $this->trimmed('profile');
67 $noticeId = $this->trimmed('notice');
69 $this->_profile = Profile::getKV('id', $profileId);
71 if (empty($this->_profile)) {
72 // TRANS: Client exception.
73 throw new ClientException(_('No such profile.'), 404);
76 $this->_notice = Notice::getKV('id', $noticeId);
78 if (empty($this->_notice)) {
79 // TRANS: Client exception thrown when referencing a non-existing notice.
80 throw new ClientException(_('No such notice.'), 404);
83 $this->_fave = Fave::pkeyGet(array('user_id' => $profileId,
84 'notice_id' => $noticeId));
86 if (empty($this->_fave)) {
87 // TRANS: Client exception thrown when referencing a non-existing favorite.
88 throw new ClientException(_('No such favorite.'), 404);
97 * @param array $argarray is ignored since it's now passed in in prepare()
101 function handle($argarray=null)
103 parent::handle($argarray);
105 switch ($_SERVER['REQUEST_METHOD']) {
114 // TRANS: Client exception thrown using an unsupported HTTP method.
115 throw new ClientException(_('HTTP method not supported.'),
122 * Show a single favorite, in ActivityStreams format
128 $activity = $this->_fave->asActivity();
130 header('Content-Type: application/atom+xml; charset=utf-8');
133 $this->raw($activity->asString(true, true, true));
140 * Delete the favorite
144 function deleteFave()
146 if (empty($this->auth_user) ||
147 $this->auth_user->id != $this->_profile->id) {
148 // TRANS: Client exception thrown when trying to remove a favorite notice of another user.
149 throw new ClientException(_("Cannot delete someone else's".
153 $this->_fave->delete();
159 * Return true if read only.
163 * @param array $args other arguments
165 * @return boolean is read only action?
167 function isReadOnly($args)
169 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
170 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
178 * Return last modified, if applicable.
182 * @return string last modified http header
184 function lastModified()
186 return max(strtotime($this->_profile->modified),
187 strtotime($this->_notice->modified),
188 strtotime($this->_fave->modified));
192 * Return etag, if applicable.
196 * @return string etag http header
200 $mtime = strtotime($this->_fave->modified);
202 return 'W/"' . implode(':', array('AtomPubShowFavorite',
209 * Does this require authentication?
211 * @return boolean true if delete, else false
213 function requiresAuth()
215 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
216 $_SERVER['REQUEST_METHOD'] == 'HEAD') {