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