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