We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

apifavoritecreate.php 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Add a notice to a user's list of favorite notices via the API
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  API
 * @package   StatusNet
Zach Copley's avatar
Zach Copley committed
24 25
 * @author    Craig Andrews <candrews@integralblue.com>
 * @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 38 39 40 41

/**
 * Favorites the status specified in the ID parameter as the authenticating user.
 * Returns the favorite status when successful.
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
42 43
 * @author   Craig Andrews <candrews@integralblue.com>
 * @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 50 51 52
 */
class ApiFavoriteCreateAction extends ApiAuthAction
{
    var $notice = null;

53 54 55
    protected $needPost = true;

    protected function prepare(array $args=array())
56 57 58
    {
        parent::prepare($args);

59
        $this->notice = Notice::getKV($this->arg('id'));
60
        if (!empty($this->notice->repeat_of)) {
Jean Baptiste Favre's avatar
Jean Baptiste Favre committed
61 62 63
                common_log(LOG_DEBUG, 'Trying to Fave '.$this->notice->id.', repeat of '.$this->notice->repeat_of);
                common_log(LOG_DEBUG, 'Will Fave '.$this->notice->repeat_of.' instead');
                $real_notice_id = $this->notice->repeat_of;
64
                $this->notice = Notice::getKV($real_notice_id);
Jean Baptiste Favre's avatar
Jean Baptiste Favre committed
65
        }
66 67 68 69

        return true;
    }

70
    protected function handle()
71
    {
72
        parent::handle();
73 74 75

        if (!in_array($this->format, array('xml', 'json'))) {
            $this->clientError(
76
                // TRANS: Client error displayed when coming across a non-supported API method.
77
                _('API method not found.'),
78 79 80 81 82 83 84
                404,
                $this->format
            );
        }

        if (empty($this->notice)) {
            $this->clientError(
85
                // TRANS: Client error displayed when requesting a status with a non-existing ID.
86 87 88 89 90 91 92 93
                _('No status found with that ID.'),
                404,
                $this->format
            );
        }

        // Note: Twitter lets you fave things repeatedly via API.

94
        if (Fave::existsForProfile($this->notice, $this->scoped)) {
95
            $this->clientError(
96
                // TRANS: Client error displayed when trying to mark a notice favourite that already is a favourite.
97
                _('This status is already a favorite.'),
98 99 100 101 102
                403,
                $this->format
            );
        }

103 104
        // throws exception on failure
        $stored = Fave::addNew($this->scoped, $this->notice);
105 106

        if ($this->format == 'xml') {
107
            $this->showSingleXmlStatus($this->notice);
108 109 110 111 112
        } elseif ($this->format == 'json') {
            $this->show_single_json_status($this->notice);
        }
    }
}