]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Activity/ActivityPlugin.php
ce6f114bfb1b42918e3e3afd8960d14b4ae69568
[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 onEndSubscribe(Profile $profile, Profile $other)
61     {
62         // Only do this if config is enabled
63         if(!$this->StartFollowUser) return true;
64
65         if (!$profile->isLocal()) {
66             // can't do anything with remote user anyway
67             return true;
68         }
69
70         $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
71                                            'subscribed' => $other->id));
72         // TRANS: Text for "started following" item in activity plugin.
73         // TRANS: %1$s is a profile URL, %2$s is a profile name,
74         // TRANS: %3$s is a profile URL, %4$s is a profile name.
75         $rendered = 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                             $other->getBestName());
80         // TRANS: Text for "started following" item in activity plugin.
81         // TRANS: %1$s is a profile name, %2$s is a profile URL,
82         // TRANS: %3$s is a profile name, %4$s is a profile URL.
83         $content  = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
84                             $profile->getBestName(),
85                             $profile->getUrl(),
86                             $other->getBestName(),
87                             $other->getUrl());
88
89         $notice = Notice::saveNew($profile->id,
90                                   $content,
91                                   ActivityPlugin::SOURCE,
92                                   array('rendered' => $rendered,
93                                         'urls' => array(),
94                                         'replies' => array($other->getUri()),
95                                         'verb' => ActivityVerb::FOLLOW,
96                                         'object_type' => ActivityObject::PERSON,
97                                         'uri' => $sub->uri));
98         return true;
99     }
100
101     function onEndUnsubscribe(Profile $profile, Profile $other)
102     {
103         // Only do this if config is enabled
104         if(!$this->StopFollowUser) return true;
105
106         if (!$profile->isLocal()) {
107             return true;
108         }
109
110         // TRANS: Text for "stopped following" item in activity plugin.
111         // TRANS: %1$s is a profile URL, %2$s is a profile name,
112         // TRANS: %3$s is a profile URL, %4$s is a profile name.
113         $rendered = 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                             $other->getBestName());
118         // TRANS: Text for "stopped following" item in activity plugin.
119         // TRANS: %1$s is a profile name, %2$s is a profile URL,
120         // TRANS: %3$s is a profile name, %4$s is a profile URL.
121         $content  = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
122                             $profile->getBestName(),
123                             $profile->getUrl(),
124                             $other->getBestName(),
125                             $other->getUrl());
126
127         $uri = TagURI::mint('stop-following:%d:%d:%s',
128                             $profile->id,
129                             $other->id,
130                             common_date_iso8601(common_sql_now()));
131
132         $notice = Notice::saveNew($profile->id,
133                                   $content,
134                                   ActivityPlugin::SOURCE,
135                                   array('rendered' => $rendered,
136                                         'urls' => array(),
137                                         'replies' => array($other->getUri()),
138                                         'uri' => $uri,
139                                         'verb' => ActivityVerb::UNFOLLOW,
140                                         'object_type' => ActivityObject::PERSON));
141
142         return true;
143     }
144
145     function onEndFavorNotice($profile, $notice)
146     {
147         //  Only do this if config is enabled
148         if(!$this->StartLike) return true;
149
150         if (!$profile->isLocal()) {
151             return true;
152         }
153
154         $author = $notice->getProfile();
155         $fave   = Fave::pkeyGet(array('user_id' => $profile->id,
156                                       'notice_id' => $notice->id));
157
158         // TRANS: Text for "liked" item in activity plugin.
159         // TRANS: %1$s is a profile URL, %2$s is a profile name,
160         // TRANS: %3$s is a notice URL, %4$s is an author name.
161         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> liked <a href="%3$s">%4$s\'s update</a>.'),
162                             $profile->getUrl(),
163                             $profile->getBestName(),
164                             $notice->getUrl(),
165                             $author->getBestName());
166         // TRANS: Text for "liked" item in activity plugin.
167         // TRANS: %1$s is a profile name, %2$s is a profile URL,
168         // TRANS: %3$s is an author name, %4$s is a notice URL.
169         $content  = sprintf(_m('%1$s (%2$s) liked %3$s\'s status (%4$s).'),
170                             $profile->getBestName(),
171                             $profile->getUrl(),
172                             $author->getBestName(),
173                             $notice->getUrl());
174
175         $notice = Notice::saveNew($profile->id,
176                                   $content,
177                                   ActivityPlugin::SOURCE,
178                                   array('rendered' => $rendered,
179                                         'urls' => array(),
180                                         'replies' => array($author->getUri()),
181                                         'uri' => $fave->getURI(),
182                                         'verb' => ActivityVerb::FAVORITE,
183                                         'object_type' => (($notice->verb == ActivityVerb::POST) ?
184                                                          $notice->object_type : ActivityObject::ACTIVITY)));
185
186         return true;
187     }
188
189     function onEndDisfavorNotice($profile, $notice)
190     {
191         // Only do this if config is enabled
192         if(!$this->StopLike) return true;
193
194         if (!$profile->isLocal()) {
195             return true;
196         }
197
198         $author = Profile::getKV('id', $notice->profile_id);
199         // TRANS: Text for "stopped liking" item in activity plugin.
200         // TRANS: %1$s is a profile URL, %2$s is a profile name,
201         // TRANS: %3$s is a notice URL, %4$s is an author name.
202         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
203                             $profile->getUrl(),
204                             $profile->getBestName(),
205                             $notice->getUrl(),
206                             $author->getBestName());
207         // TRANS: Text for "stopped liking" item in activity plugin.
208         // TRANS: %1$s is a profile name, %2$s is a profile URL,
209         // TRANS: %3$s is an author name, %4$s is a notice URL.
210         $content  = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s).'),
211                             $profile->getBestName(),
212                             $profile->getUrl(),
213                             $author->getBestName(),
214                             $notice->getUrl());
215
216         $uri = TagURI::mint('unlike:%d:%d:%s',
217                             $profile->id,
218                             $notice->id,
219                             common_date_iso8601(common_sql_now()));
220
221         $notice = Notice::saveNew($profile->id,
222                                   $content,
223                                   ActivityPlugin::SOURCE,
224                                   array('rendered' => $rendered,
225                                         'urls' => array(),
226                                         'replies' => array($author->getUri()),
227                                         'uri' => $uri,
228                                         'verb' => ActivityVerb::UNFAVORITE,
229                                         'object_type' => (($notice->verb == ActivityVerb::POST) ?
230                                                          $notice->object_type : ActivityObject::ACTIVITY)));
231
232         return true;
233     }
234
235     function onEndJoinGroup($group, $profile)
236     {
237         // Only do this if config is enabled
238         if(!$this->JoinGroup) return true;
239
240         if (!$profile->isLocal()) {
241             return true;
242         }
243
244         // TRANS: Text for "joined group" item in activity plugin.
245         // TRANS: %1$s is a profile URL, %2$s is a profile name,
246         // TRANS: %3$s is a group URL, %4$s is a group name.
247         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
248                             $profile->getUrl(),
249                             $profile->getBestName(),
250                             $group->homeUrl(),
251                             $group->getBestName());
252         // TRANS: Text for "joined group" item in activity plugin.
253         // TRANS: %1$s is a profile name, %2$s is a profile URL,
254         // TRANS: %3$s is a group name, %4$s is a group URL.
255         $content  = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
256                             $profile->getBestName(),
257                             $profile->getUrl(),
258                             $group->getBestName(),
259                             $group->homeUrl());
260
261         $mem = Group_member::pkeyGet(array('group_id' => $group->id,
262                                            'profile_id' => $profile->id));
263
264         $notice = Notice::saveNew($profile->id,
265                                   $content,
266                                   ActivityPlugin::SOURCE,
267                                   array('rendered' => $rendered,
268                                         'urls' => array(),
269                                         'groups' => array($group->id),
270                                         'uri' => $mem->getURI(),
271                                         'verb' => ActivityVerb::JOIN,
272                                         'object_type' => ActivityObject::GROUP));
273         return true;
274     }
275
276     function onEndLeaveGroup($group, $profile)
277     {
278         // Only do this if config is enabled
279         if(!$this->LeaveGroup) return true;
280
281         if (!$profile->isLocal()) {
282             return true;
283         }
284
285         // TRANS: Text for "left group" item in activity plugin.
286         // TRANS: %1$s is a profile URL, %2$s is a profile name,
287         // TRANS: %3$s is a group URL, %4$s is a group name.
288         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
289                             $profile->getUrl(),
290                             $profile->getBestName(),
291                             $group->homeUrl(),
292                             $group->getBestName());
293         // TRANS: Text for "left group" item in activity plugin.
294         // TRANS: %1$s is a profile name, %2$s is a profile URL,
295         // TRANS: %3$s is a group name, %4$s is a group URL.
296         $content  = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'),
297                             $profile->getBestName(),
298                             $profile->getUrl(),
299                             $group->getBestName(),
300                             $group->homeUrl());
301
302         $uri = TagURI::mint('leave:%d:%d:%s',
303                             $profile->id,
304                             $group->id,
305                             common_date_iso8601(common_sql_now()));
306
307         $notice = Notice::saveNew($profile->id,
308                                   $content,
309                                   ActivityPlugin::SOURCE,
310                                   array('rendered' => $rendered,
311                                         'urls' => array(),
312                                         'groups' => array($group->id),
313                                         'uri' => $uri,
314                                         'verb' => ActivityVerb::LEAVE,
315                                         'object_type' => ActivityObject::GROUP));
316         return true;
317     }
318
319     function onStartShowNoticeItem($nli)
320     {
321         $notice = $nli->notice;
322
323         $adapter = null;
324
325         switch ($notice->verb) {
326         case ActivityVerb::FAVORITE:
327         case ActivityVerb::UNFAVORITE:
328             $adapter = new SystemListItem($nli);
329             break;
330         case ActivityVerb::JOIN:
331             $adapter = new JoinListItem($nli);
332             break;
333         case ActivityVerb::LEAVE:
334             $adapter = new JoinListItem($nli);
335             break;
336         case ActivityVerb::FOLLOW:
337             $adapter = new FollowListItem($nli);
338             break;
339         case ActivityVerb::UNFOLLOW:
340             $adapter = new UnfollowListItem($nli);
341             break;
342         }
343
344         if (!empty($adapter)) {
345             $adapter->showNotice();
346             $adapter->showNoticeAttachments();
347             $adapter->showNoticeInfo();
348             $adapter->showNoticeOptions();
349             return false;
350         }
351
352         return true;
353     }
354
355     function onEndNoticeAsActivity($notice, &$activity)
356     {
357         switch ($notice->verb) {
358         case ActivityVerb::FAVORITE:
359             $fave = Fave::getKV('uri', $notice->uri);
360             if (!empty($fave)) {
361                 $notice = Notice::getKV('id', $fave->notice_id);
362                 if (!empty($notice)) {
363                     $cur = common_current_user();
364                     $target = $notice->asActivity($cur);
365                     if ($target->verb == ActivityVerb::POST) {
366                         // "I like the thing you posted"
367                         $activity->objects = $target->objects;
368                     } else {
369                         // "I like that you did whatever you did"
370                         $activity->objects = array($target);
371                     }
372                 }
373             }
374             break;
375         case ActivityVerb::UNFAVORITE:
376             // FIXME: do something here
377             break;
378         case ActivityVerb::JOIN:
379             $mem = Group_member::getKV('uri', $notice->uri);
380             if (!empty($mem)) {
381                 $group = $mem->getGroup();
382                 $activity->objects = array(ActivityObject::fromGroup($group));
383             }
384             break;
385         case ActivityVerb::LEAVE:
386             // FIXME: ????
387             break;
388         case ActivityVerb::FOLLOW:
389             $sub = Subscription::getKV('uri', $notice->uri);
390             if (!empty($sub)) {
391                 $profile = Profile::getKV('id', $sub->subscribed);
392                 if (!empty($profile)) {
393                     $activity->objects = array($profile->asActivityObject());
394                 }
395             }
396             break;
397         case ActivityVerb::UNFOLLOW:
398             // FIXME: ????
399             break;
400         }
401
402         return true;
403     }
404
405     function onPluginVersion(&$versions)
406     {
407         $versions[] = array('name' => 'Activity',
408                             'version' => self::VERSION,
409                             'author' => 'Evan Prodromou',
410                             'homepage' => 'http://status.net/wiki/Plugin:Activity',
411                             'rawdescription' =>
412                             // TRANS: Plugin description.
413                             _m('Emits notices when social activities happen.'));
414         return true;
415     }
416 }