]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Activity/ActivityPlugin.php
Merge commit 'refs/merge-requests/165' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / plugins / Activity / ActivityPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Shows social activities in the output feed
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Activity
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Activity plugin main class
39  *
40  * @category  Activity
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class ActivityPlugin extends Plugin
48 {
49     const VERSION = '0.1';
50     const SOURCE  = 'activity';
51
52     // Flags to switch off certain activity notices
53     public $StartFollowUser = true;
54     public $StopFollowUser  = false;
55     public $JoinGroup = true;
56     public $LeaveGroup = false;
57     public $StartLike = false;
58     public $StopLike = false;
59
60     function onAutoload($cls)
61     {
62         $dir = dirname(__FILE__);
63
64         switch ($cls)
65         {
66         case 'JoinListItem':
67         case 'LeaveListItem':
68         case 'FollowListItem':
69         case 'UnfollowListItem':
70         case 'SystemListItem':
71             include_once $dir . '/'.strtolower($cls).'.php';
72             return false;
73         default:
74             return true;
75         }
76     }
77
78     function onEndSubscribe($subscriber, $other)
79     {
80         // Only do this if config is enabled
81         if(!$this->StartFollowUser) return true;
82         $user = $subscriber->getUser();
83         if (!empty($user)) {
84             $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
85                                                'subscribed' => $other->id));
86             // TRANS: Text for "started following" item in activity plugin.
87             // TRANS: %1$s is a profile URL, %2$s is a profile name,
88             // TRANS: %3$s is a profile URL, %4$s is a profile name.
89             $rendered = sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%3$s">%4$s</a>.'),
90                                 $subscriber->profileurl,
91                                 $subscriber->getBestName(),
92                                 $other->profileurl,
93                                 $other->getBestName());
94             // TRANS: Text for "started following" item in activity plugin.
95             // TRANS: %1$s is a profile name, %2$s is a profile URL,
96             // TRANS: %3$s is a profile name, %4$s is a profile URL.
97             $content  = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
98                                 $subscriber->getBestName(),
99                                 $subscriber->profileurl,
100                                 $other->getBestName(),
101                                 $other->profileurl);
102
103             $notice = Notice::saveNew($user->id,
104                                       $content,
105                                       ActivityPlugin::SOURCE,
106                                       array('rendered' => $rendered,
107                                             'urls' => array(),
108                                             'replies' => array($other->getUri()),
109                                             'verb' => ActivityVerb::FOLLOW,
110                                             'object_type' => ActivityObject::PERSON,
111                                             'uri' => $sub->uri));
112         }
113         return true;
114     }
115
116     function onEndUnsubscribe($subscriber, $other)
117     {
118         // Only do this if config is enabled
119         if(!$this->StopFollowUser) return true;
120         $user = $subscriber->getUser();
121         if (!empty($user)) {
122             // TRANS: Text for "stopped following" item in activity plugin.
123             // TRANS: %1$s is a profile URL, %2$s is a profile name,
124             // TRANS: %3$s is a profile URL, %4$s is a profile name.
125             $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
126                                 $subscriber->profileurl,
127                                 $subscriber->getBestName(),
128                                 $other->profileurl,
129                                 $other->getBestName());
130             // TRANS: Text for "stopped following" item in activity plugin.
131             // TRANS: %1$s is a profile name, %2$s is a profile URL,
132             // TRANS: %3$s is a profile name, %4$s is a profile URL.
133             $content  = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
134                                 $subscriber->getBestName(),
135                                 $subscriber->profileurl,
136                                 $other->getBestName(),
137                                 $other->profileurl);
138
139             $uri = TagURI::mint('stop-following:%d:%d:%s',
140                                 $subscriber->id,
141                                 $other->id,
142                                 common_date_iso8601(common_sql_now()));
143
144             $notice = Notice::saveNew($user->id,
145                                       $content,
146                                       ActivityPlugin::SOURCE,
147                                       array('rendered' => $rendered,
148                                             'urls' => array(),
149                                             'replies' => array($other->getUri()),
150                                             'uri' => $uri,
151                                             'verb' => ActivityVerb::UNFOLLOW,
152                                             'object_type' => ActivityObject::PERSON));
153         }
154         return true;
155     }
156
157     function onEndFavorNotice($profile, $notice)
158     {
159         //  Only do this if config is enabled
160         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                                           'notice_id' => $notice->id));
169
170             // TRANS: Text for "liked" item in activity plugin.
171             // TRANS: %1$s is a profile URL, %2$s is a profile name,
172             // TRANS: %3$s is a notice URL, %4$s is an author name.
173             $rendered = sprintf(_m('<a href="%1$s">%2$s</a> liked <a href="%3$s">%4$s\'s update</a>.'),
174                                 $profile->profileurl,
175                                 $profile->getBestName(),
176                                 $notice->bestUrl(),
177                                 $author->getBestName());
178             // TRANS: Text for "liked" item in activity plugin.
179             // TRANS: %1$s is a profile name, %2$s is a profile URL,
180             // TRANS: %3$s is an author name, %4$s is a notice URL.
181             $content  = sprintf(_m('%1$s (%2$s) liked %3$s\'s status (%4$s).'),
182                                 $profile->getBestName(),
183                                 $profile->profileurl,
184                                 $author->getBestName(),
185                                 $notice->bestUrl());
186
187             $notice = Notice::saveNew($user->id,
188                                       $content,
189                                       ActivityPlugin::SOURCE,
190                                       array('rendered' => $rendered,
191                                             'urls' => array(),
192                                             'replies' => array($author->getUri()),
193                                             'uri' => $fave->getURI(),
194                                             'verb' => ActivityVerb::FAVORITE,
195                                             'object_type' => (($notice->verb == ActivityVerb::POST) ?
196                                                              $notice->object_type : ActivityObject::ACTIVITY)));
197         }
198         return true;
199     }
200
201     function onEndDisfavorNotice($profile, $notice)
202     {
203         // Only do this if config is enabled
204         if(!$this->StopLike) return true;
205         $user = User::staticGet('id', $profile->id);
206
207         if (!empty($user)) {
208             $author = Profile::staticGet('id', $notice->profile_id);
209             // TRANS: Text for "stopped liking" item in activity plugin.
210             // TRANS: %1$s is a profile URL, %2$s is a profile name,
211             // TRANS: %3$s is a notice URL, %4$s is an author name.
212             $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
213                                 $profile->profileurl,
214                                 $profile->getBestName(),
215                                 $notice->bestUrl(),
216                                 $author->getBestName());
217             // TRANS: Text for "stopped liking" item in activity plugin.
218             // TRANS: %1$s is a profile name, %2$s is a profile URL,
219             // TRANS: %3$s is an author name, %4$s is a notice URL.
220             $content  = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s).'),
221                                 $profile->getBestName(),
222                                 $profile->profileurl,
223                                 $author->getBestName(),
224                                 $notice->bestUrl());
225
226             $uri = TagURI::mint('unlike:%d:%d:%s',
227                                 $profile->id,
228                                 $notice->id,
229                                 common_date_iso8601(common_sql_now()));
230
231             $notice = Notice::saveNew($user->id,
232                                       $content,
233                                       ActivityPlugin::SOURCE,
234                                       array('rendered' => $rendered,
235                                             'urls' => array(),
236                                             'replies' => array($author->getUri()),
237                                             'uri' => $uri,
238                                             'verb' => ActivityVerb::UNFAVORITE,
239                                             'object_type' => (($notice->verb == ActivityVerb::POST) ?
240                                                              $notice->object_type : ActivityObject::ACTIVITY)));
241         }
242         return true;
243     }
244
245     function onEndJoinGroup($group, $profile)
246     {
247         // Only do this if config is enabled
248         if(!$this->JoinGroup) return true;
249
250         $user = $profile->getUser();
251
252         if (empty($user)) {
253             return true;
254         }
255
256         // TRANS: Text for "joined group" item in activity plugin.
257         // TRANS: %1$s is a profile URL, %2$s is a profile name,
258         // TRANS: %3$s is a group URL, %4$s is a group name.
259         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
260                             $profile->profileurl,
261                             $profile->getBestName(),
262                             $group->homeUrl(),
263                             $group->getBestName());
264         // TRANS: Text for "joined group" item in activity plugin.
265         // TRANS: %1$s is a profile name, %2$s is a profile URL,
266         // TRANS: %3$s is a group name, %4$s is a group URL.
267         $content  = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
268                             $profile->getBestName(),
269                             $profile->profileurl,
270                             $group->getBestName(),
271                             $group->homeUrl());
272
273         $mem = Group_member::pkeyGet(array('group_id' => $group->id,
274                                            'profile_id' => $profile->id));
275
276         $notice = Notice::saveNew($user->id,
277                                   $content,
278                                   ActivityPlugin::SOURCE,
279                                   array('rendered' => $rendered,
280                                         'urls' => array(),
281                                         'groups' => array($group->id),
282                                         'uri' => $mem->getURI(),
283                                         'verb' => ActivityVerb::JOIN,
284                                         'object_type' => ActivityObject::GROUP));
285         return true;
286     }
287
288     function onEndLeaveGroup($group, $profile)
289     {
290         // Only do this if config is enabled
291         if(!$this->LeaveGroup) return true;
292
293         $user = $profile->getUser();
294
295         if (empty($user)) {
296             return true;
297         }
298
299         // TRANS: Text for "left group" item in activity plugin.
300         // TRANS: %1$s is a profile URL, %2$s is a profile name,
301         // TRANS: %3$s is a group URL, %4$s is a group name.
302         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
303                             $profile->profileurl,
304                             $profile->getBestName(),
305                             $group->homeUrl(),
306                             $group->getBestName());
307         // TRANS: Text for "left group" item in activity plugin.
308         // TRANS: %1$s is a profile name, %2$s is a profile URL,
309         // TRANS: %3$s is a group name, %4$s is a group URL.
310         $content  = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'),
311                             $profile->getBestName(),
312                             $profile->profileurl,
313                             $group->getBestName(),
314                             $group->homeUrl());
315
316         $uri = TagURI::mint('leave:%d:%d:%s',
317                             $user->id,
318                             $group->id,
319                             common_date_iso8601(common_sql_now()));
320
321         $notice = Notice::saveNew($user->id,
322                                   $content,
323                                   ActivityPlugin::SOURCE,
324                                   array('rendered' => $rendered,
325                                         'urls' => array(),
326                                         'groups' => array($group->id),
327                                         'uri' => $uri,
328                                         'verb' => ActivityVerb::LEAVE,
329                                         'object_type' => ActivityObject::GROUP));
330         return true;
331     }
332
333     function onStartShowNoticeItem($nli)
334     {
335         $notice = $nli->notice;
336
337         $adapter = null;
338
339         switch ($notice->verb) {
340         case ActivityVerb::FAVORITE:
341         case ActivityVerb::UNFAVORITE:
342             $adapter = new SystemListItem($nli);
343             break;
344         case ActivityVerb::JOIN:
345             $adapter = new JoinListItem($nli);
346             break;
347         case ActivityVerb::LEAVE:
348             $adapter = new JoinListItem($nli);
349             break;
350         case ActivityVerb::FOLLOW:
351             $adapter = new FollowListItem($nli);
352             break;
353         case ActivityVerb::UNFOLLOW:
354             $adapter = new UnfollowListItem($nli);
355             break;
356         }
357
358         if (!empty($adapter)) {
359             $adapter->showNotice();
360             $adapter->showNoticeAttachments();
361             $adapter->showNoticeInfo();
362             $adapter->showNoticeOptions();
363             return false;
364         }
365
366         return true;
367     }
368
369     function onEndNoticeAsActivity($notice, &$activity)
370     {
371         switch ($notice->verb) {
372         case ActivityVerb::FAVORITE:
373             $fave = Fave::staticGet('uri', $notice->uri);
374             if (!empty($fave)) {
375                 $notice = Notice::staticGet('id', $fave->notice_id);
376                 if (!empty($notice)) {
377                     $cur = common_current_user();
378                     $target = $notice->asActivity($cur);
379                     if ($target->verb == ActivityVerb::POST) {
380                         // "I like the thing you posted"
381                         $activity->objects = $target->objects;
382                     } else {
383                         // "I like that you did whatever you did"
384                         $activity->objects = array($target);
385                     }
386                 }
387             }
388             break;
389         case ActivityVerb::UNFAVORITE:
390             // FIXME: do something here
391             break;
392         case ActivityVerb::JOIN:
393             $mem = Group_member::staticGet('uri', $notice->uri);
394             if (!empty($mem)) {
395                 $group = $mem->getGroup();
396                 $activity->objects = array(ActivityObject::fromGroup($group));
397             }
398             break;
399         case ActivityVerb::LEAVE:
400             // FIXME: ????
401             break;
402         case ActivityVerb::FOLLOW:
403             $sub = Subscription::staticGet('uri', $notice->uri);
404             if (!empty($sub)) {
405                 $profile = Profile::staticGet('id', $sub->subscribed);
406                 if (!empty($profile)) {
407                     $activity->objects = array(ActivityObject::fromProfile($profile));
408                 }
409             }
410             break;
411         case ActivityVerb::UNFOLLOW:
412             // FIXME: ????
413             break;
414         }
415
416         return true;
417     }
418
419     function onPluginVersion(&$versions)
420     {
421         $versions[] = array('name' => 'Activity',
422                             'version' => self::VERSION,
423                             'author' => 'Evan Prodromou',
424                             'homepage' => 'http://status.net/wiki/Plugin:Activity',
425                             'rawdescription' =>
426                             // TRANS: Plugin description.
427                             _m('Emits notices when social activities happen.'));
428         return true;
429     }
430 }