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

apiconversation.php 6.5 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2 3 4 5 6
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2011, StatusNet, Inc.
 *
 * Show a stream of notices in a particular conversation
7
 *
Evan Prodromou's avatar
Evan Prodromou committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * PHP version 5
 *
 * 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
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    // This check helps protect against security problems;
    // your code file can't be executed directly from the web.
    exit(1);
}

/**
 * Show a stream of notices in a particular conversation
 *
 * @category  API
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */
class ApiconversationAction extends ApiAuthAction
{
49 50 51
    protected $conversation = null;
    protected $notices      = null;

52
    protected function prepare(array $args=array())
Evan Prodromou's avatar
Evan Prodromou committed
53
    {
54
        parent::prepare($args);
55

Evan Prodromou's avatar
Evan Prodromou committed
56
        $convId = $this->trimmed('id');
57

Evan Prodromou's avatar
Evan Prodromou committed
58
        if (empty($convId)) {
59 60
            // TRANS: Client exception thrown when no conversation ID is given.
            throw new ClientException(_('No conversation ID.'));
Evan Prodromou's avatar
Evan Prodromou committed
61
        }
62

63
        $this->conversation = Conversation::getKV('id', $convId);
64

Evan Prodromou's avatar
Evan Prodromou committed
65
        if (empty($this->conversation)) {
66 67 68
            // TRANS: Client exception thrown when referring to a non-existing conversation ID (%d).
            throw new ClientException(sprintf(_('No conversation with ID %d.'), $convId),
                                      404);
Evan Prodromou's avatar
Evan Prodromou committed
69
        }
70

71
        $stream = new ConversationNoticeStream($convId, $this->scoped);
72

Evan Prodromou's avatar
Evan Prodromou committed
73 74 75 76
        $notice = $stream->getNotices(($this->page-1) * $this->count,
                                      $this->count,
                                      $this->since_id,
                                      $this->max_id);
77

Evan Prodromou's avatar
Evan Prodromou committed
78
        $this->notices = $notice->fetchAll();
79

Evan Prodromou's avatar
Evan Prodromou committed
80 81 82 83 84 85 86 87 88 89 90 91 92
        return true;
    }

    /**
     * Handler method
     *
     * @param array $argarray is ignored since it's now passed in in prepare()
     *
     * @return void
     */
    function handle($argarray=null)
    {
        $sitename   = common_config('site', 'name');
93
        // TRANS: Title for conversion timeline.
94
        $title      = _m('TITLE', 'Conversation');
Evan Prodromou's avatar
Evan Prodromou committed
95 96 97 98
        $id         = common_local_url('apiconversation', array('id' => $this->conversation->id, 'format' => $this->format));
        $link       = common_local_url('conversation', array('id' => $this->conversation->id));

        $self       = $id;
99

Evan Prodromou's avatar
Evan Prodromou committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        switch($this->format) {
        case 'xml':
            $this->showXmlTimeline($this->notices);
            break;
        case 'rss':
            $this->showRssTimeline(
                $this->notices,
                $title,
                $link,
                null,
                null,
                null,
                $self
            );
            break;
        case 'atom':

            header('Content-Type: application/atom+xml; charset=utf-8');

            $atom = new AtomNoticeFeed($this->auth_user);

            $atom->setId($id);
            $atom->setTitle($title);
            $atom->setUpdated('now');

            $atom->addLink($link);
            $atom->setSelfLink($self);

            $atom->addEntryFromNotices($this->notices);
            $this->raw($atom->getString());

            break;
        case 'json':
            $this->showJsonTimeline($this->notices);
            break;
        case 'as':
            header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
            $doc = new ActivityStreamJSONDocument($this->auth_user);
            $doc->setTitle($title);
            $doc->addLink($link, 'alternate', 'text/html');
            $doc->addItemsFromNotices($this->notices);
            $this->raw($doc->asString());
            break;
        default:
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), $code = 404);
            break;
        }
    }

    /**
     * Return true if read only.
     *
     * MAY override
     *
     * @param array $args other arguments
     *
     * @return boolean is read only action?
     */
    function isReadOnly($args)
    {
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Return last modified, if applicable.
     *
     * MAY override
     *
     * @return string last modified http header
     */
    function lastModified()
    {
        if (!empty($this->notices) && (count($this->notices) > 0)) {
            return strtotime($this->notices[0]->created);
        }

        return null;
    }

    /**
     * Return etag, if applicable.
     *
     * MAY override
     *
     * @return string etag http header
     */
    function etag()
    {
        if (!empty($this->notices) && (count($this->notices) > 0)) {

            $last = count($this->notices) - 1;

            return '"' . implode(
                ':',
                array($this->arg('action'),
                      common_user_cache_hash($this->auth_user),
                      common_language(),
                      $this->user->id,
                      strtotime($this->notices[0]->created),
                      strtotime($this->notices[$last]->created))
            )
            . '"';
        }
209

Evan Prodromou's avatar
Evan Prodromou committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        return null;
    }

    /**
     * Does this require authentication?
     *
     * @return boolean true if delete, else false
     */
    function requiresAuth()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return false;
        } else {
            return true;
        }
    }
227
}