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