apitimelinementions.php 7.51 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
 *
 * Show notices mentioning a user (@nickname)
 *
 * 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 26 27 28 29
 * @author    Craig Andrews <candrews@integralblue.com>
 * @author    Evan Prodromou <evan@status.net>
 * @author    Jeffery To <jeffery.to@gmail.com>
 * @author    mac65 <mac65@mac65.com>
 * @author    Mike Cochrane <mikec@mikenz.geek.nz>
 * @author    Robin Millette <robin@millette.info>
30 31
 * @author    Zach Copley <zach@status.net>
 * @copyright 2009 StatusNet, Inc.
32
 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33 34 35 36 37 38 39 40 41 42 43 44 45
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Returns the most recent (default 20) mentions (status containing @nickname)
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
46 47 48 49 50 51
 * @author   Craig Andrews <candrews@integralblue.com>
 * @author   Evan Prodromou <evan@status.net>
 * @author   Jeffery To <jeffery.to@gmail.com>
 * @author   mac65 <mac65@mac65.com>
 * @author   Mike Cochrane <mikec@mikenz.geek.nz>
 * @author   Robin Millette <robin@millette.info>
52 53 54 55
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
56
class ApiTimelineMentionsAction extends ApiBareAuthAction
57 58 59 60 61 62 63 64 65 66
{
    var $notices = null;

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
67
    protected function prepare(array $args=array())
68 69 70
    {
        parent::prepare($args);

71
        $this->target = $this->getTargetProfile($this->arg('id'));
72

73
        if (!($this->target instanceof Profile)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
74
            // TRANS: Client error displayed when requesting most recent mentions for a non-existing user.
75
            $this->clientError(_('No such user.'), 404);
76 77 78 79 80 81 82 83 84 85 86 87 88 89
        }

        $this->notices = $this->getNotices();

        return true;
    }

    /**
     * Handle the request
     *
     * Just show the notices
     *
     * @return void
     */
90
    protected function handle()
91
    {
92
        parent::handle();
93 94 95 96 97 98 99 100 101 102 103 104
        $this->showTimeline();
    }

    /**
     * Show the timeline of notices
     *
     * @return void
     */
    function showTimeline()
    {
        $sitename   = common_config('site', 'name');
        $title      = sprintf(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
105 106
            // TRANS: Title for timeline of most recent mentions of a user.
            // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
107
            _('%1$s / Updates mentioning %2$s'),
108
            $sitename, $this->target->nickname
109
        );
110
        $taguribase = TagURI::base();
111
        $id         = "tag:$taguribase:Mentions:" . $this->target->id;
112

113
        $logo = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
114
        $link = common_local_url('replies',
115
                    array('nickname' => $this->target->nickname));
116 117
        $self = $this->getSelfUri();

118
        $subtitle   = sprintf(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
119 120 121
            // TRANS: Subtitle for timeline of most recent mentions of a user.
            // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname,
            // TRANS: %3$s is a user's full name.
122 123
            _('%1$s updates that reply to updates from %3$s / %2$s.'),
            $sitename, $this->target->nickname, $this->target->getBestName()
124 125
        );

Zach Copley's avatar
Zach Copley committed
126
        switch($this->format) {
127
        case 'xml':
128
            $this->showXmlTimeline($this->notices);
129 130
            break;
        case 'rss':
131 132 133 134 135 136 137 138 139
            $this->showRssTimeline(
                $this->notices,
                $title,
                $link,
                $subtitle,
                null,
                $logo,
                $self
            );
140 141
            break;
        case 'atom':
142 143
            header('Content-Type: application/atom+xml; charset=utf-8');

144
            $atom = new AtomNoticeFeed($this->auth_user);
145 146 147 148 149 150 151

            $atom->setId($id);
            $atom->setTitle($title);
            $atom->setSubtitle($subtitle);
            $atom->setLogo($logo);
            $atom->setUpdated('now');

152 153
            $atom->addLink($link);
            $atom->setSelfLink($self);
154 155 156 157

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

158 159
            break;
        case 'json':
160
            $this->showJsonTimeline($this->notices);
161
            break;
162
        case 'as':
163
            header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
164 165 166 167 168 169
            $doc = new ActivityStreamJSONDocument($this->auth_user);
            $doc->setTitle($title);
            $doc->addLink($link, 'alternate', 'text/html');
            $doc->addItemsFromNotices($this->notices);
            $this->raw($doc->asString());
            break;
170
        default:
171
            // TRANS: Client error displayed when coming across a non-supported API method.
172
            $this->clientError(_('API method not found.'), $code = 404);
173 174 175 176 177 178 179 180 181 182 183 184 185
            break;
        }
    }

    /**
     * Get notices
     *
     * @return array notices
     */
    function getNotices()
    {
        $notices = array();

186
        $stream = new ReplyNoticeStream($this->target->id, $this->scoped);
187 188 189 190 191

        $notice = $stream->getNotices(($this->page - 1) * $this->count,
                                      $this->count,
                                      $this->since_id,
                                      $this->max_id);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

        while ($notice->fetch()) {
            $notices[] = clone($notice);
        }

        return $notices;
    }

    /**
     * Is this action read only?
     *
     * @param array $args other arguments
     *
     * @return boolean true
     */
    function isReadOnly($args)
    {
        return true;
    }

    /**
     * When was this feed last modified?
     *
     * @return string datestamp of the latest notice in the stream
     */
    function lastModified()
    {
        if (!empty($this->notices) && (count($this->notices) > 0)) {
            return strtotime($this->notices[0]->created);
        }

        return null;
    }

    /**
     * An entity tag for this stream
     *
     * Returns an Etag based on the action name, language, user ID, and
     * timestamps of the first and last notice in the timeline
     *
     * @return string etag
     */
    function etag()
    {
        if (!empty($this->notices) && (count($this->notices) > 0)) {

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

240
            return '"' . implode(
241 242
                ':',
                array($this->arg('action'),
243
                      common_user_cache_hash($this->auth_user),
244
                      common_language(),
245
                      $this->target->id,
246 247 248 249 250 251 252 253 254
                      strtotime($this->notices[0]->created),
                      strtotime($this->notices[$last]->created))
            )
            . '"';
        }

        return null;
    }
}