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

ActivityPlugin.php 17.8 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  = 'system';
51

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

60 61 62 63 64 65
    function onAutoload($cls)
    {
        $dir = dirname(__FILE__);

        switch ($cls)
        {
66
        case 'JoinListItem':
67 68 69
        case 'LeaveListItem':
        case 'FollowListItem':
        case 'UnfollowListItem':
70
        case 'SystemListItem':
71 72
            include_once $dir . '/'.strtolower($cls).'.php';
            return false;
73 74 75 76 77 78 79
        default:
            return true;
        }
    }

    function onEndSubscribe($subscriber, $other)
    {
80 81
        // Only do this if config is enabled
        if(!$this->StartFollowUser) return true;
82
        $user = $subscriber->getUser();
83
        if (!empty($user)) {
84 85
            $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
                                               'subscribed' => $other->id));
86 87 88
            // 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.
89 90 91
            $rendered = sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%2$s">%4$s</a>.'),
                                $subscriber->profileurl,
                                $subscriber->getBestName(),
92 93
                                $other->profileurl,
                                $other->getBestName());
94 95 96
            // 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.
97 98 99
            $content  = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
                                $subscriber->getBestName(),
                                $subscriber->profileurl,
100
                                $other->getBestName(),
101
                                $other->profileurl);
102 103 104

            $notice = Notice::saveNew($user->id,
                                      $content,
105
                                      ActivityPlugin::SOURCE,
106
                                      array('rendered' => $rendered,
107
                                            'urls' => array(),
108
                                            'replies' => array($other->getUri()),
109 110 111
                                            'verb' => ActivityVerb::FOLLOW,
                                            'object_type' => ActivityObject::PERSON,
                                            'uri' => $sub->uri));
112 113 114 115 116 117
        }
        return true;
    }

    function onEndUnsubscribe($subscriber, $other)
    {
118 119
        // Only do this if config is enabled
        if(!$this->StopFollowUser) return true;
120
        $user = $subscriber->getUser();
121
        if (!empty($user)) {
122 123 124
            // 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.
125 126 127
            $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
                                $subscriber->profileurl,
                                $subscriber->getBestName(),
128 129
                                $other->profileurl,
                                $other->getBestName());
130 131 132
            // 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.
133 134 135
            $content  = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
                                $subscriber->getBestName(),
                                $subscriber->profileurl,
136
                                $other->getBestName(),
137 138 139 140 141 142
                                $other->profileurl);

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

            $notice = Notice::saveNew($user->id,
                                      $content,
146
                                      ActivityPlugin::SOURCE,
147
                                      array('rendered' => $rendered,
148
                                            'urls' => array(),
149
                                            'replies' => array($other->getUri()),
150 151 152
                                            'uri' => $uri,
                                            'verb' => ActivityVerb::UNFOLLOW,
                                            'object_type' => ActivityObject::PERSON));
153 154 155 156 157 158
        }
        return true;
    }

    function onEndFavorNotice($profile, $notice)
    {
159 160
        //  Only do this if config is enabled
        if(!$this->StartLike) return true;
161

162
        $user = $profile->getUser();
163

164
        if (!empty($user)) {
165

166
            $author = $notice->getProfile();
167
            $fave   = Fave::pkeyGet(array('user_id' => $user->id,
168 169
                                          'notice_id' => $notice->id));

170 171 172
            // TRANS: Text for "liked" 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.
173 174 175
            $rendered = sprintf(_m('<a href="%1$s">%2$s</a> liked <a href="%3$s">%4$s\'s update</a>.'),
                                $profile->profileurl,
                                $profile->getBestName(),
176 177
                                $notice->bestUrl(),
                                $author->getBestName());
178 179 180 181
            // TRANS: Text for "liked" 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) liked %3$s\'s status (%4$s).'),
182 183 184 185
                                $profile->getBestName(),
                                $profile->profileurl,
                                $author->getBestName(),
                                $notice->bestUrl());
186 187 188

            $notice = Notice::saveNew($user->id,
                                      $content,
189
                                      ActivityPlugin::SOURCE,
190
                                      array('rendered' => $rendered,
191
                                            'urls' => array(),
192
                                            'replies' => array($author->getUri()),
193 194 195 196
                                            'uri' => $fave->getURI(),
                                            'verb' => ActivityVerb::FAVORITE,
                                            'object_type' => (($notice->verb == ActivityVerb::POST) ?
                                                             $notice->object_type : ActivityObject::ACTIVITY)));
197 198 199 200 201 202
        }
        return true;
    }

    function onEndDisfavorNotice($profile, $notice)
    {
203 204
        // Only do this if config is enabled
        if(!$this->StopLike) return true;
205 206 207 208
        $user = User::staticGet('id', $profile->id);

        if (!empty($user)) {
            $author = Profile::staticGet('id', $notice->profile_id);
209 210 211
            // 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.
212 213 214
            $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
                                $profile->profileurl,
                                $profile->getBestName(),
215 216
                                $notice->bestUrl(),
                                $author->getBestName());
217 218 219 220
            // 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).'),
221 222 223 224 225 226 227 228 229 230
                                $profile->getBestName(),
                                $profile->profileurl,
                                $author->getBestName(),
                                $notice->bestUrl());

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

231 232
            $notice = Notice::saveNew($user->id,
                                      $content,
233
                                      ActivityPlugin::SOURCE,
234
                                      array('rendered' => $rendered,
235
                                            'urls' => array(),
236
                                            'replies' => array($author->getUri()),
237 238 239 240
                                            'uri' => $uri,
                                            'verb' => ActivityVerb::UNFAVORITE,
                                            'object_type' => (($notice->verb == ActivityVerb::POST) ?
                                                             $notice->object_type : ActivityObject::ACTIVITY)));
241 242 243 244
        }
        return true;
    }

245
    function onEndJoinGroup($group, $profile)
246
    {
247 248
        // Only do this if config is enabled
        if(!$this->JoinGroup) return true;
249

250
        $user = $profile->getUser();
251

252 253 254
        if (empty($user)) {
            return true;
        }
255

256 257 258
        // 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.
259 260 261
        $rendered = sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
                            $profile->profileurl,
                            $profile->getBestName(),
262 263
                            $group->homeUrl(),
                            $group->getBestName());
264 265 266
        // 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.
267 268 269
        $content  = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
                            $profile->getBestName(),
                            $profile->profileurl,
270
                            $group->getBestName(),
271 272 273 274
                            $group->homeUrl());

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

        $notice = Notice::saveNew($user->id,
                                  $content,
278
                                  ActivityPlugin::SOURCE,
279
                                  array('rendered' => $rendered,
280
                                        'urls' => array(),
281
                                        'groups' => array($group->id),
282 283 284
                                        'uri' => $mem->getURI(),
                                        'verb' => ActivityVerb::JOIN,
                                        'object_type' => ActivityObject::GROUP));
285 286 287
        return true;
    }

288
    function onEndLeaveGroup($group, $profile)
289
    {
290 291
        // Only do this if config is enabled
        if(!$this->LeaveGroup) return true;
292

293
        $user = $profile->getUser();
294

295 296 297
        if (empty($user)) {
            return true;
        }
298

299 300 301
        // 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.
302 303 304
        $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
                            $profile->profileurl,
                            $profile->getBestName(),
305 306
                            $group->homeUrl(),
                            $group->getBestName());
307 308 309 310
        // 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).'),
311 312
                            $profile->getBestName(),
                            $profile->profileurl,
313
                            $group->getBestName(),
314 315 316
                            $group->homeUrl());

        $uri = TagURI::mint('leave:%d:%d:%s',
317 318 319
                            $user->id,
                            $group->id,
                            common_date_iso8601(common_sql_now()));
320 321 322

        $notice = Notice::saveNew($user->id,
                                  $content,
323
                                  ActivityPlugin::SOURCE,
324
                                  array('rendered' => $rendered,
325
                                        'urls' => array(),
326
                                        'groups' => array($group->id),
327 328 329
                                        'uri' => $uri,
                                        'verb' => ActivityVerb::LEAVE,
                                        'object_type' => ActivityObject::GROUP));
330 331
        return true;
    }
332

333 334
    function onStartShowNoticeItem($nli)
    {
335 336 337 338 339
        $notice = $nli->notice;

        $adapter = null;

        switch ($notice->verb) {
340 341
        case ActivityVerb::FAVORITE:
        case ActivityVerb::UNFAVORITE:
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
            $adapter = new SystemListItem($nli);
            break;
        case ActivityVerb::JOIN:
            $adapter = new JoinListItem($nli);
            break;
        case ActivityVerb::LEAVE:
            $adapter = new JoinListItem($nli);
            break;
        case ActivityVerb::FOLLOW:
            $adapter = new FollowListItem($nli);
            break;
        case ActivityVerb::UNFOLLOW:
            $adapter = new UnfollowListItem($nli);
            break;
        }
357 358 359 360 361 362 363 364

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

366 367
        return true;
    }
368

369
    function onEndNoticeAsActivity($notice, &$activity)
370
    {
371
        switch ($notice->verb) {
372 373 374 375 376
        case ActivityVerb::FAVORITE:
            $fave = Fave::staticGet('uri', $notice->uri);
            if (!empty($fave)) {
                $notice = Notice::staticGet('id', $fave->notice_id);
                if (!empty($notice)) {
377 378
                    $cur = common_current_user();
                    $target = $notice->asActivity($cur);
379 380 381 382 383 384 385 386 387 388 389 390
                    if ($target->verb == ActivityVerb::POST) {
                        // "I like the thing you posted"
                        $activity->objects = $target->objects;
                    } else {
                        // "I like that you did whatever you did"
                        $activity->objects = array($target);
                    }
                }
            }
            break;
        case ActivityVerb::UNFAVORITE:
            // FIXME: do something here
391 392
            break;
        case ActivityVerb::JOIN:
393 394 395 396 397 398
            $mem = Group_member::staticGet('uri', $notice->uri);
            if (!empty($mem)) {
                $group = $mem->getGroup();
                $activity->objects = array(ActivityObject::fromGroup($group));
            }
            break;
399
        case ActivityVerb::LEAVE:
400
            // FIXME: ????
401 402
            break;
        case ActivityVerb::FOLLOW:
403 404 405 406 407 408 409
            $sub = Subscription::staticGet('uri', $notice->uri);
            if (!empty($sub)) {
                $profile = Profile::staticGet('id', $sub->subscribed);
                if (!empty($profile)) {
                    $activity->objects = array(ActivityObject::fromProfile($profile));
                }
            }
410 411
            break;
        case ActivityVerb::UNFOLLOW:
412
            // FIXME: ????
413 414
            break;
        }
415

416
        return true;
417 418 419 420 421 422 423 424 425
    }

    function onPluginVersion(&$versions)
    {
        $versions[] = array('name' => 'Activity',
                            'version' => self::VERSION,
                            'author' => 'Evan Prodromou',
                            'homepage' => 'http://status.net/wiki/Plugin:Activity',
                            'rawdescription' =>
426
                            // TRANS: Plugin description.
427 428 429 430
                            _m('Emits notices when social activities happen.'));
        return true;
    }
}