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

usersalmon.php 8.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * 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/>.
 */

20
if (!defined('GNUSOCIAL')) { exit(1); }
21

Siebrand Mazeland's avatar
Siebrand Mazeland committed
22 23 24 25
/**
 * @package OStatusPlugin
 * @author James Walker <james@status.net>
 */
26 27
class UsersalmonAction extends SalmonAction
{
28
    protected function prepare(array $args=array())
29 30 31 32 33 34
    {
        parent::prepare($args);

        $id = $this->trimmed('id');

        if (!$id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
35
            // TRANS: Client error displayed trying to perform an action without providing an ID.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
36
            $this->clientError(_m('No ID.'));
37 38
        }

39
        $this->user = User::getKV('id', $id);
40

41
        if (!$this->user instanceof User) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
42
            // TRANS: Client error displayed when referring to a non-existing user.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
43
            $this->clientError(_m('No such user.'));
44 45
        }

mattl's avatar
mattl committed
46
        $this->target = $this->user->getProfile();
47

48 49 50 51 52 53 54 55 56 57 58
        return true;
    }

    /**
     * We've gotten a post event on the Salmon backchannel, probably a reply.
     *
     * @todo validate if we need to handle this post, then call into
     * ostatus_profile's general incoming-post handling.
     */
    function handlePost()
    {
59
        common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
60

Zach Copley's avatar
Zach Copley committed
61
        // @fixme: process all activity objects?
62
        switch ($this->activity->objects[0]->type) {
63 64 65 66 67 68 69
        case ActivityObject::ARTICLE:
        case ActivityObject::BLOGENTRY:
        case ActivityObject::NOTE:
        case ActivityObject::STATUS:
        case ActivityObject::COMMENT:
            break;
        default:
Siebrand Mazeland's avatar
Siebrand Mazeland committed
70
            // TRANS: Client exception thrown when an undefied activity is performed.
71
            throw new ClientException(_m('Cannot handle that kind of post.'));
72 73 74
        }

        // Notice must either be a) in reply to a notice by this user
75 76
        // or b) in reply to a notice to the attention of this user
        // or c) to the attention of this user
77

78
        $context = $this->activity->context;
79
        $notice = false;
80 81

        if (!empty($context->replyToID)) {
82
            $notice = Notice::getKV('uri', $context->replyToID);
83 84
        }

85
        if ($notice instanceof Notice &&
mattl's avatar
mattl committed
86 87
            ($notice->profile_id == $this->target->id ||
             array_key_exists($this->target->id, $notice->getReplies())))
88 89
        {
            // In reply to a notice either from or mentioning this user.
90
        } elseif (!empty($context->attention) &&
mattl's avatar
mattl committed
91
                   array_key_exists($this->target->getUri(), $context->attention)) {
92
            // To the attention of this user.
93
        } else {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
94
            // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
95
            throw new ClientException(_m('Not to anyone in reply to anything.'));
96 97
        }

98 99 100
        try {
            $this->saveNotice();
        } catch AlreadyFulfilledException($e) {
101
            return;
102
        }
103 104 105 106 107 108 109 110
    }

    /**
     * We've gotten a follow/subscribe notification from a remote user.
     * Save a subscription relationship for them.
     */
    function handleFollow()
    {
mattl's avatar
mattl committed
111 112
        common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
        Subscription::start($this->actor, $this->target);
113 114 115 116 117 118 119 120 121 122
    }

    /**
     * We've gotten an unfollow/unsubscribe notification from a remote user.
     * Check if we have a subscription relationship for them and kill it.
     *
     * @fixme probably catch exceptions on fail?
     */
    function handleUnfollow()
    {
mattl's avatar
mattl committed
123 124 125 126 127
        common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
        try {
            Subscription::cancel($this->actor, $this->target);
        } catch (NoProfileException $e) {
            common_debug('Could not find profile for Subscription: '.$e->getMessage());
128 129 130
        }
    }

Shashi Gowda's avatar
Shashi Gowda committed
131 132 133 134
    function handleTag()
    {
        if ($this->activity->target->type == ActivityObject::_LIST) {
            if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
135 136
                // TRANS: Client exception.
                throw new ClientException(_m('Not a person object.'));
Shashi Gowda's avatar
Shashi Gowda committed
137 138
            }
            // this is a peopletag
139
            $tagged = User::getKV('uri', $this->activity->objects[0]->id);
Shashi Gowda's avatar
Shashi Gowda committed
140

141
            if (!$tagged instanceof User) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
142
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
143
                throw new ClientException(_m('Unidentified profile being listed.'));
Shashi Gowda's avatar
Shashi Gowda committed
144 145
            }

mattl's avatar
mattl committed
146
            if ($tagged->id !== $this->target->id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
147
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
148
                throw new ClientException(_m('This user is not the one being listed.'));
Shashi Gowda's avatar
Shashi Gowda committed
149 150 151 152 153 154 155 156
            }

            // save the list
            $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);

            $ptag = $list->localPeopletag();
            $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
            if (!$result) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
157
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
158
                throw new ClientException(_m('The listing could not be saved.'));
Shashi Gowda's avatar
Shashi Gowda committed
159 160 161 162 163 164 165 166
            }
        }
    }

    function handleUntag()
    {
        if ($this->activity->target->type == ActivityObject::_LIST) {
            if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
167 168
                // TRANS: Client exception.
                throw new ClientException(_m('Not a person object.'));
Shashi Gowda's avatar
Shashi Gowda committed
169 170
            }
            // this is a peopletag
171
            $tagged = User::getKV('uri', $this->activity->objects[0]->id);
Shashi Gowda's avatar
Shashi Gowda committed
172

173
            if (!$tagged instanceof User) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
174
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
175
                throw new ClientException(_m('Unidentified profile being unlisted.'));
Shashi Gowda's avatar
Shashi Gowda committed
176 177
            }

mattl's avatar
mattl committed
178
            if ($tagged->id !== $this->target->id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
179
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
180
                throw new ClientException(_m('This user is not the one being unlisted.'));
Shashi Gowda's avatar
Shashi Gowda committed
181 182 183 184 185 186 187 188 189
            }

            // save the list
            $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);

            $ptag = $list->localPeopletag();
            $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);

            if (!$result) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
190
                // TRANS: Client exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
191
                throw new ClientException(_m('The listing could not be deleted.'));
Shashi Gowda's avatar
Shashi Gowda committed
192 193 194 195
            }
        }
    }

196 197 198 199 200
    /**
     * @param ActivityObject $object
     * @return Notice
     * @throws ClientException on invalid input
     */
201
    function getNotice(ActivityObject $object)
202 203
    {
        switch ($object->type) {
204 205 206 207 208 209 210
        case ActivityObject::ARTICLE:
        case ActivityObject::BLOGENTRY:
        case ActivityObject::NOTE:
        case ActivityObject::STATUS:
        case ActivityObject::COMMENT:
            break;
        default:
Siebrand Mazeland's avatar
Siebrand Mazeland committed
211
            // TRANS: Client exception.
212
            throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
213 214
        }

215
        $notice = Notice::getKV('uri', $object->id);
216

217
        if (!$notice instanceof Notice) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
218 219
            // TRANS: Client exception. %s is an object ID.
            throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
220 221
        }

mattl's avatar
mattl committed
222
        if ($notice->profile_id != $this->target->id) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
223
            // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
mattl's avatar
mattl committed
224
            throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'), $object->id, $this->target->id));
225 226
        }

227
        return $notice;
228 229
    }
}