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