3 * StatusNet, the distributed open-source microblogging tool
5 * Add a notice to a user's list of favorite notices via the API
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Evan Prodromou <evan@status.net>
26 * @author Zach Copley <zach@status.net>
27 * @copyright 2009 StatusNet, Inc.
28 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
38 * Favorites the status specified in the ID parameter as the authenticating user.
39 * Returns the favorite status when successful.
43 * @author Craig Andrews <candrews@integralblue.com>
44 * @author Evan Prodromou <evan@status.net>
45 * @author Zach Copley <zach@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class ApiFavoriteCreateAction extends ApiAuthAction
54 * Take arguments for running
56 * @param array $args $_REQUEST args
58 * @return boolean success flag
60 function prepare(array $args=array())
62 parent::prepare($args);
64 $this->user = $this->auth_user;
65 $this->notice = Notice::getKV($this->arg('id'));
66 if ($this->notice->repeat_of != '' ) {
67 common_debug('Trying to Fave '.$this->notice->id.', repeat of '.$this->notice->repeat_of);
68 common_debug('Will Fave '.$this->notice->repeat_of.' instead');
69 $real_notice_id = $this->notice->repeat_of;
70 $this->notice = Notice::getKV($real_notice_id);
79 * Check the format and show the user info
81 * @param array $args $_REQUEST data (unused)
85 function handle(array $args=array())
87 parent::handle($args);
89 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
91 // TRANS: Client error. POST is a HTTP command. It should not be translated.
92 _('This method requires a POST.'),
99 if (!in_array($this->format, array('xml', 'json'))) {
101 // TRANS: Client error displayed when coming across a non-supported API method.
102 _('API method not found.'),
109 if (empty($this->notice)) {
111 // TRANS: Client error displayed when requesting a status with a non-existing ID.
112 _('No status found with that ID.'),
119 // Note: Twitter lets you fave things repeatedly via API.
121 if (Fave::existsForProfile($this->notice, $this->scoped)) {
123 // TRANS: Client error displayed when trying to mark a notice favourite that already is a favourite.
124 _('This status is already a favorite.'),
131 $fave = Fave::addNew($this->user->getProfile(), $this->notice);
135 // TRANS: Client error displayed when marking a notice as favourite fails.
136 _('Could not create favorite.'),
143 $this->notify($fave, $this->notice, $this->user);
144 Fave::blowCacheForProfileId($this->user->id);
146 if ($this->format == 'xml') {
147 $this->showSingleXmlStatus($this->notice);
148 } elseif ($this->format == 'json') {
149 $this->show_single_json_status($this->notice);
154 * Notify the author of the favorite that the user likes their notice
156 * @param Favorite $fave the favorite in question
157 * @param Notice $notice the notice that's been faved
158 * @param User $user the user doing the favoriting
162 function notify($fave, $notice, $user)
164 $other = User::getKV('id', $notice->profile_id);
165 if ($other && $other->id != $user->id && !empty($other->email)) {
166 require_once INSTALLDIR.'/lib/mail.php';
168 mail_notify_fave($other, $user->getProfile(), $notice);
170 // XXX: notify by SMS