]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Favorite/actions/apifavoritecreate.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Favorite / actions / apifavoritecreate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Add a notice to a user's list of favorite notices via the API
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Zach Copley <zach@status.net>
27  * @author   Mikael Nordfeldth <mmn@hethane.se>
28  * @copyright 2009 StatusNet, Inc.
29  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://gnu.io/
32  */
33
34 if (!defined('GNUSOCIAL')) { exit(1); }
35
36 /**
37  * Favorites the status specified in the ID parameter as the authenticating user.
38  * Returns the favorite status when successful.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Craig Andrews <candrews@integralblue.com>
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Zach Copley <zach@status.net>
45  * @author   Mikael Nordfeldth <mmn@hethane.se>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://gnu.io/
48  */
49 class ApiFavoriteCreateAction extends ApiAuthAction
50 {
51     var $notice = null;
52
53     protected $needPost = true;
54
55     /**
56      * Take arguments for running
57      *
58      * @return boolean success flag
59      */
60     protected function prepare(array $args=array())
61     {
62         parent::prepare($args);
63
64         $this->notice = Notice::getKV($this->arg('id'));
65         if (!empty($this->notice->repeat_of)) {
66                 common_debug('Trying to Fave '.$this->notice->id.', repeat of '.$this->notice->repeat_of);
67                 common_debug('Will Fave '.$this->notice->repeat_of.' instead');
68                 $real_notice_id = $this->notice->repeat_of;
69                 $this->notice = Notice::getKV($real_notice_id);
70         }
71
72         return true;
73     }
74
75     /**
76      * Handle the request
77      *
78      * Check the format and show the user info
79      *
80      * @return void
81      */
82     protected function handle()
83     {
84         parent::handle();
85
86         if (!in_array($this->format, array('xml', 'json'))) {
87             $this->clientError(
88                 // TRANS: Client error displayed when coming across a non-supported API method.
89                 _('API method not found.'),
90                 404,
91                 $this->format
92             );
93         }
94
95         if (empty($this->notice)) {
96             $this->clientError(
97                 // TRANS: Client error displayed when requesting a status with a non-existing ID.
98                 _('No status found with that ID.'),
99                 404,
100                 $this->format
101             );
102         }
103
104         try {
105             $stored = Fave::addNew($this->scoped, $this->notice);
106         } catch (AlreadyFulfilledException $e) {
107             // Note: Twitter lets you fave things repeatedly via API.
108             $this->clientError($e->getMessage(), 403);
109         }
110
111         if ($this->format == 'xml') {
112             $this->showSingleXmlStatus($this->notice);
113         } elseif ($this->format == 'json') {
114             $this->show_single_json_status($this->notice);
115         }
116     }
117 }