git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

ActivityPlugin.php 14.2 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 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 49
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * Shows social activities in the output feed
 *
 * 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  Activity
 * @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/
 */

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

/**
 * Activity plugin main class
 *
 * @category  Activity
 * @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/
 */
class ActivityPlugin extends Plugin
{
    const VERSION = '0.1';
50
    const SOURCE  = 'activity';
51

52 53
    // Flags to switch off certain activity notices
    public $StartFollowUser = true;
54
    public $StopFollowUser  = false;
55
    public $JoinGroup = true;
56 57 58
    public $LeaveGroup = false;
    public $StartLike = false;
    public $StopLike = false;
59

60
    function onEndSubscribe(Profile $profile, Profile $other)
61
    {
62 63
        // Only do this if config is enabled
        if(!$this->StartFollowUser) return true;
64 65 66 67

        if (!$profile->isLocal()) {
            // can't do anything with remote user anyway
            return true;
68
        }
69 70 71 72 73 74

        $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
                                           'subscribed' => $other->id));
        // TRANS: Text for "started following" item in activity plugin.
        // TRANS: %1$s is a profile URL, %2$s is a profile name,
        // TRANS: %3$s is a profile URL, %4$s is a profile name.
75
        $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%3$s">%4$s</a>.'),
76
                            $profile->getUrl(),
77
                            $profile->getBestName(),
78
                            $other->getUrl(),
79 80 81 82 83 84
                            $other->getBestName());
        // TRANS: Text for "started following" item in activity plugin.
        // TRANS: %1$s is a profile name, %2$s is a profile URL,
        // TRANS: %3$s is a profile name, %4$s is a profile URL.
        $content  = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
                            $profile->getBestName(),
85
                            $profile->getUrl(),
86
                            $other->getBestName(),
87
                            $other->getUrl());
88 89 90 91 92 93 94 95 96 97

        $notice = Notice::saveNew($profile->id,
                                  $content,
                                  ActivityPlugin::SOURCE,
                                  array('rendered' => $rendered,
                                        'urls' => array(),
                                        'replies' => array($other->getUri()),
                                        'verb' => ActivityVerb::FOLLOW,
                                        'object_type' => ActivityObject::PERSON,
                                        'uri' => $sub->uri));
98 99 100
        return true;
    }

101
    function onEndUnsubscribe(Profile $profile, Profile $other)
102
    {
103 104
        // Only do this if config is enabled
        if(!$this->StopFollowUser) return true;
105 106 107

        if (!$profile->isLocal()) {
            return true;
108
        }
109 110 111 112

        // TRANS: Text for "stopped following" item in activity plugin.
        // TRANS: %1$s is a profile URL, %2$s is a profile name,
        // TRANS: %3$s is a profile URL, %4$s is a profile name.
113
        $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
114
                            $profile->getUrl(),
115
                            $profile->getBestName(),
116
                            $other->getUrl(),
117 118 119 120 121 122
                            $other->getBestName());
        // TRANS: Text for "stopped following" item in activity plugin.
        // TRANS: %1$s is a profile name, %2$s is a profile URL,
        // TRANS: %3$s is a profile name, %4$s is a profile URL.
        $content  = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
                            $profile->getBestName(),
123
                            $profile->getUrl(),
124
                            $other->getBestName(),
125
                            $other->getUrl());
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

        $uri = TagURI::mint('stop-following:%d:%d:%s',
                            $profile->id,
                            $other->id,
                            common_date_iso8601(common_sql_now()));

        $notice = Notice::saveNew($profile->id,
                                  $content,
                                  ActivityPlugin::SOURCE,
                                  array('rendered' => $rendered,
                                        'urls' => array(),
                                        'replies' => array($other->getUri()),
                                        'uri' => $uri,
                                        'verb' => ActivityVerb::UNFOLLOW,
                                        'object_type' => ActivityObject::PERSON));

142 143 144 145 146
        return true;
    }

    function onEndDisfavorNotice($profile, $notice)
    {
147 148
        // Only do this if config is enabled
        if(!$this->StopLike) return true;
149 150 151

        if (!$profile->isLocal()) {
            return true;
152
        }
153 154 155 156 157

        $author = Profile::getKV('id', $notice->profile_id);
        // TRANS: Text for "stopped liking" item in activity plugin.
        // TRANS: %1$s is a profile URL, %2$s is a profile name,
        // TRANS: %3$s is a notice URL, %4$s is an author name.
158
        $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
159
                            $profile->getUrl(),
160
                            $profile->getBestName(),
161
                            $notice->getUrl(),
162 163 164 165 166 167
                            $author->getBestName());
        // TRANS: Text for "stopped liking" item in activity plugin.
        // TRANS: %1$s is a profile name, %2$s is a profile URL,
        // TRANS: %3$s is an author name, %4$s is a notice URL.
        $content  = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s).'),
                            $profile->getBestName(),
168
                            $profile->getUrl(),
169
                            $author->getBestName(),
170
                            $notice->getUrl());
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

        $uri = TagURI::mint('unlike:%d:%d:%s',
                            $profile->id,
                            $notice->id,
                            common_date_iso8601(common_sql_now()));

        $notice = Notice::saveNew($profile->id,
                                  $content,
                                  ActivityPlugin::SOURCE,
                                  array('rendered' => $rendered,
                                        'urls' => array(),
                                        'replies' => array($author->getUri()),
                                        'uri' => $uri,
                                        'verb' => ActivityVerb::UNFAVORITE,
                                        'object_type' => (($notice->verb == ActivityVerb::POST) ?
186
                                                         $notice->object_type : null)));
187

188 189 190
        return true;
    }

191
    function onEndJoinGroup($group, $profile)
192
    {
193 194
        // Only do this if config is enabled
        if(!$this->JoinGroup) return true;
195

196
        if (!$profile->isLocal()) {
197 198
            return true;
        }
199

200 201 202
        // TRANS: Text for "joined group" item in activity plugin.
        // TRANS: %1$s is a profile URL, %2$s is a profile name,
        // TRANS: %3$s is a group URL, %4$s is a group name.
203
        $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
204
                            $profile->getUrl(),
205
                            $profile->getBestName(),
206 207
                            $group->homeUrl(),
                            $group->getBestName());
208 209 210
        // TRANS: Text for "joined group" item in activity plugin.
        // TRANS: %1$s is a profile name, %2$s is a profile URL,
        // TRANS: %3$s is a group name, %4$s is a group URL.
211 212
        $content  = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
                            $profile->getBestName(),
213
                            $profile->getUrl(),
214
                            $group->getBestName(),
215 216 217 218
                            $group->homeUrl());

        $mem = Group_member::pkeyGet(array('group_id' => $group->id,
                                           'profile_id' => $profile->id));
219

220
        $notice = Notice::saveNew($profile->id,
221
                                  $content,
222
                                  ActivityPlugin::SOURCE,
223
                                  array('rendered' => $rendered,
224
                                        'urls' => array(),
225
                                        'groups' => array($group->id),
226 227 228
                                        'uri' => $mem->getURI(),
                                        'verb' => ActivityVerb::JOIN,
                                        'object_type' => ActivityObject::GROUP));
229 230 231
        return true;
    }

232
    function onEndLeaveGroup($group, $profile)
233
    {
234 235
        // Only do this if config is enabled
        if(!$this->LeaveGroup) return true;
236

237
        if (!$profile->isLocal()) {
238 239
            return true;
        }
240

241 242 243
        // TRANS: Text for "left group" item in activity plugin.
        // TRANS: %1$s is a profile URL, %2$s is a profile name,
        // TRANS: %3$s is a group URL, %4$s is a group name.
244
        $rendered = html_sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
245
                            $profile->getUrl(),
246
                            $profile->getBestName(),
247 248
                            $group->homeUrl(),
                            $group->getBestName());
249 250 251 252
        // TRANS: Text for "left group" item in activity plugin.
        // TRANS: %1$s is a profile name, %2$s is a profile URL,
        // TRANS: %3$s is a group name, %4$s is a group URL.
        $content  = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'),
253
                            $profile->getBestName(),
254
                            $profile->getUrl(),
255
                            $group->getBestName(),
256 257 258
                            $group->homeUrl());

        $uri = TagURI::mint('leave:%d:%d:%s',
259
                            $profile->id,
260 261
                            $group->id,
                            common_date_iso8601(common_sql_now()));
262

263
        $notice = Notice::saveNew($profile->id,
264
                                  $content,
265
                                  ActivityPlugin::SOURCE,
266
                                  array('rendered' => $rendered,
267
                                        'urls' => array(),
268
                                        'groups' => array($group->id),
269 270 271
                                        'uri' => $uri,
                                        'verb' => ActivityVerb::LEAVE,
                                        'object_type' => ActivityObject::GROUP));
272 273
        return true;
    }
274

275 276
    function onStartShowNoticeItem($nli)
    {
277 278 279 280 281 282 283 284 285
        $notice = $nli->notice;

        $adapter = null;

        switch ($notice->verb) {
        case ActivityVerb::JOIN:
            $adapter = new JoinListItem($nli);
            break;
        case ActivityVerb::LEAVE:
286
            $adapter = new LeaveListItem($nli);
287 288 289 290 291 292 293 294
            break;
        case ActivityVerb::FOLLOW:
            $adapter = new FollowListItem($nli);
            break;
        case ActivityVerb::UNFOLLOW:
            $adapter = new UnfollowListItem($nli);
            break;
        }
295 296 297 298 299 300 301 302

        if (!empty($adapter)) {
            $adapter->showNotice();
            $adapter->showNoticeAttachments();
            $adapter->showNoticeInfo();
            $adapter->showNoticeOptions();
            return false;
        }
303

304 305
        return true;
    }
306

307
    public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
308
    {
309
        switch ($stored->verb) {
310 311
        case ActivityVerb::UNFAVORITE:
            // FIXME: do something here
312 313
            break;
        case ActivityVerb::JOIN:
314 315
            $mem = Group_member::getKV('uri', $stored->getUri());
            if ($mem instanceof Group_member) {
316
                $group = $mem->getGroup();
317
                $act->title = $stored->getTitle();
318
                $act->objects = array(ActivityObject::fromGroup($group));
319 320
            }
            break;
321
        case ActivityVerb::LEAVE:
322
            // FIXME: ????
323 324
            break;
        case ActivityVerb::FOLLOW:
325
            $sub = Subscription::getKV('uri', $stored->uri);
326
            if ($sub instanceof Subscription) {
327
                $profile = Profile::getKV('id', $sub->subscribed);
328 329
                if ($profile instanceof Profile) {
                    $act->title = $stored->getTitle();
330
                    $act->objects = array($profile->asActivityObject());
331 332
                }
            }
333 334
            break;
        case ActivityVerb::UNFOLLOW:
335
            // FIXME: ????
336 337
            break;
        }
338

339
        return true;
340 341
    }

342
    function onPluginVersion(array &$versions)
343 344 345 346 347 348
    {
        $versions[] = array('name' => 'Activity',
                            'version' => self::VERSION,
                            'author' => 'Evan Prodromou',
                            'homepage' => 'http://status.net/wiki/Plugin:Activity',
                            'rawdescription' =>
349
                            // TRANS: Plugin description.
350 351 352 353
                            _m('Emits notices when social activities happen.'));
        return true;
    }
}