atompubsubscriptionfeed.php 8.5 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * AtomPub subscription feed
Siebrand Mazeland's avatar
Siebrand Mazeland committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * 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  Cache
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2010 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */

31
if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32 33 34 35 36

/**
 * Subscription feed class for AtomPub
 *
 * Generates a list of the user's subscriptions
Siebrand Mazeland's avatar
Siebrand Mazeland committed
37
 *
38 39 40 41 42 43 44
 * @category  AtomPub
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2010 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */
45
class AtompubsubscriptionfeedAction extends AtompubAction
46
{
47 48
    private $_profile       = null;
    private $_subscriptions = null;
49

50
    protected function atompubPrepare()
51 52 53
    {
        $subscriber = $this->trimmed('subscriber');

54
        $this->_profile = Profile::getKV('id', $subscriber);
55

56
        if (!$this->_profile instanceof Profile) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
57 58 59
            // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
            // TRANS: %d is the non-existing profile ID number.
            throw new ClientException(sprintf(_('No such profile id: %d.'),
60 61 62
                                              $subscriber), 404);
        }

63 64 65
        $this->_subscriptions = Subscription::bySubscriber($this->_profile->id,
                                                           $this->offset,
                                                           $this->limit);
66 67 68 69

        return true;
    }

70
    protected function handleGet()
71
    {
72 73
        $this->showFeed();
    }
74

75 76 77
    protected function handlePost()
    {
        $this->addSubscription();
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    /**
     * Show the feed of subscriptions
     *
     * @return void
     */
    function showFeed()
    {
        header('Content-Type: application/atom+xml; charset=utf-8');

        $url = common_local_url('AtomPubSubscriptionFeed',
                                array('subscriber' => $this->_profile->id));

        $feed = new Atom10Feed(true);

        $feed->addNamespace('activity',
                            'http://activitystrea.ms/spec/1.0/');

        $feed->addNamespace('poco',
                            'http://portablecontacts.net/spec/1.0');

        $feed->addNamespace('media',
                            'http://purl.org/syndication/atommedia');

103 104 105
        $feed->addNamespace('georss',
                            'http://www.georss.org/georss');

106 107 108 109 110 111 112
        $feed->id = $url;

        $feed->setUpdated('now');

        $feed->addAuthor($this->_profile->getBestName(),
                         $this->_profile->getURI());

Siebrand Mazeland's avatar
Siebrand Mazeland committed
113 114
        // TRANS: Title for Atom subscription feed.
        // TRANS: %s is a user nickname.
115 116 117
        $feed->setTitle(sprintf(_("%s subscriptions"),
                                $this->_profile->getBestName()));

Siebrand Mazeland's avatar
Siebrand Mazeland committed
118 119
        // TRANS: Subtitle for Atom subscription feed.
        // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
120
        $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
121 122
                                   $this->_profile->getBestName(),
                                   common_config('site', 'name')));
123 124

        $feed->addLink(common_local_url('subscriptions',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
125
                                        array('nickname' =>
126 127 128 129 130
                                              $this->_profile->nickname)));

        $feed->addLink($url,
                       array('rel' => 'self',
                             'type' => 'application/atom+xml'));
Siebrand Mazeland's avatar
Siebrand Mazeland committed
131

132 133 134 135 136 137 138 139
        // If there's more...

        if ($this->page > 1) {
            $feed->addLink($url,
                           array('rel' => 'first',
                                 'type' => 'application/atom+xml'));

            $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
140
                                            array('subscriber' =>
141
                                                  $this->_profile->id),
Siebrand Mazeland's avatar
Siebrand Mazeland committed
142
                                            array('page' =>
143 144 145 146 147
                                                  $this->page - 1)),
                           array('rel' => 'prev',
                                 'type' => 'application/atom+xml'));
        }

148
        if ($this->_subscriptions->N > $this->count) {
149 150 151

            $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
                                            array('subscriber' =>
152 153
                                                  $this->_profile->id),
                                            array('page' =>
154 155 156 157 158 159 160 161 162
                                                  $this->page + 1)),
                           array('rel' => 'next',
                                 'type' => 'application/atom+xml'));
        }

        $i = 0;

        // XXX: This is kind of inefficient

163
        while ($this->_subscriptions->fetch()) {
164 165 166 167 168 169 170 171 172

            // We get one more than needed; skip that one

            $i++;

            if ($i > $this->count) {
                break;
            }

173
            $act = $this->_subscriptions->asActivity();
174 175 176 177 178 179
            $feed->addEntryRaw($act->asString(false, false, false));
        }

        $this->raw($feed->getString());
    }

180 181 182 183 184 185 186
    /**
     * Add a new subscription
     *
     * Handling the POST method for AtomPub
     *
     * @return void
     */
187 188 189 190
    function addSubscription()
    {
        if (empty($this->auth_user) ||
            $this->auth_user->id != $this->_profile->id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
191 192 193
            // TRANS: Client exception thrown when trying to subscribe another user.
            throw new ClientException(_("Cannot add someone else's".
                                        " subscription."), 403);
194
        }
Siebrand Mazeland's avatar
Siebrand Mazeland committed
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        $xml = file_get_contents('php://input');

        $dom = DOMDocument::loadXML($xml);

        if ($dom->documentElement->namespaceURI != Activity::ATOM ||
            $dom->documentElement->localName != 'entry') {
            // TRANS: Client error displayed when not using an Atom entry.
            $this->clientError(_('Atom post must be an Atom entry.'));
        }

        $activity = new Activity($dom->documentElement);

        $sub = null;

        if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {

            if ($activity->verb != ActivityVerb::FOLLOW) {
213
                // TRANS: Client error displayed when not using the follow verb.
214 215 216 217 218 219
                $this->clientError(_('Can only handle Follow activities.'));
            }

            $person = $activity->objects[0];

            if ($person->type != ActivityObject::PERSON) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
220
                // TRANS: Client exception thrown when subscribing to an object that is not a person.
221 222 223 224
                $this->clientError(_('Can only follow people.'));
            }

            // XXX: OStatus discovery (maybe)
225 226 227
            try {
                $profile = Profile::fromUri($person->id);
            } catch (UnknownUriException $e) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
228
                // TRANS: Client exception thrown when subscribing to a non-existing profile.
229
                // TRANS: %s is the unknown profile ID.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
230
                $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
231 232
            }

233 234 235
            try {
                $sub = Subscription::start($this->_profile, $profile);
            } catch (AlreadyFulfilledException $e) {
236
                // 409 Conflict
237
                $this->clientError($e->getMessage(), 409);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            }

            Event::handle('EndAtomPubNewActivity', array($activity, $sub));
        }

        if (!empty($sub)) {
            $act = $sub->asActivity();

            header('Content-Type: application/atom+xml; charset=utf-8');
            header('Content-Location: ' . $act->selfLink);

            $this->startXML();
            $this->raw($act->asString(true, true, true));
            $this->endXML();
        }
    }
254
}