]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Activity/ActivityPlugin.php
Add custom list items for follow, unfollow, leave activities
[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
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             include_once $dir . '/'.strtolower($cls).'.php';
71             return false;
72         default:
73             return true;
74         }
75     }
76
77     function onEndSubscribe($subscriber, $other)
78     {
79         // Only do this if config is enabled
80         if(!$this->StartFollowUser) return true;
81         $user = $subscriber->getUser();
82         if (!empty($user)) {
83                 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
84                                                    'subscribed' => $other->id));
85             $rendered = sprintf(_m('<em><a href="%s">%s</a> started following <a href="%s">%s</a></em>.'),
86                                                 $subscriber->profileurl,
87                                                 $subscriber->getBestName(),
88                                 $other->profileurl,
89                                 $other->getBestName());
90             $content  = sprintf(_m('%s (%s) started following %s (%s).'),
91                                                 $subscriber->getBestName(),
92                                                 $subscriber->profileurl,
93                                 $other->getBestName(),
94                                                                 $other->profileurl);
95
96             $notice = Notice::saveNew($user->id,
97                                       $content,
98                                       'activity',
99                                       array('rendered' => $rendered,
100                                                 'verb' => ActivityVerb::FOLLOW,
101                                                 'object_type' => ActivityObject::PERSON,
102                                                 'uri' => $sub->uri));
103         }
104         return true;
105     }
106
107     function onEndUnsubscribe($subscriber, $other)
108     {
109         // Only do this if config is enabled
110         if(!$this->StopFollowUser) return true;
111         $user = $subscriber->getUser();
112         if (!empty($user)) {
113             $rendered = sprintf(_m('<em><a href="%s">%s</a> stopped following <a href="%s">%s</a></em>.'),
114                                                 $subscriber->profileurl,
115                                                 $subscriber->getBestName(),
116                                 $other->profileurl,
117                                 $other->getBestName());
118             $content  = sprintf(_m('%s (%s) stopped following %s (%s).'),
119                                                 $subscriber->getBestName(),
120                                                 $subscriber->profileurl,
121                                 $other->getBestName(),
122                                                                 $other->profileurl);
123
124                         $uri = TagURI::mint('stop-following:%d:%d:%s',
125                                 $subscriber->id,
126                                 $other->id,
127                                 common_date_iso8601(common_sql_now()));
128                             
129             $notice = Notice::saveNew($user->id,
130                                       $content,
131                                       'activity',
132                                       array('rendered' => $rendered,
133                                                 'uri' => $uri,
134                                                 'verb' => ActivityVerb::UNFOLLOW,
135                                                 'object_type' => ActivityObject::PERSON));
136         }
137         return true;
138     }
139
140     function onEndFavorNotice($profile, $notice)
141     {
142         //  Only do this if config is enabled
143         if(!$this->StartLike) return true;
144         
145         $user = $profile->getUser();
146         
147         if (!empty($user)) {
148                 
149             $author = $notice->getProfile();
150             $fave   = Fave::pkeyGet(array('user_id' => $user->id,
151                                                                   'notice_id' => $notice->id));
152             
153             $rendered = sprintf(_m('<em><a href="%s">%s</a> liked <a href="%s">%s\'s update</a></em>.'),
154                                                 $profile->profileurl,
155                                                 $profile->getBestName(),
156                                 $notice->bestUrl(),
157                                 $author->getBestName());
158             $content  = sprintf(_m('%s (%s) liked %s\'s status (%s)'),
159                                                 $profile->getBestName(),
160                                                 $profile->profileurl,
161                                 $author->getBestName(), 
162                                                                 $notice->bestUrl());
163
164             $notice = Notice::saveNew($user->id,
165                                       $content,
166                                       'activity',
167                                       array('rendered' => $rendered,
168                                                 'uri' => $fave->getURI(),
169                                                 'verb' => ActivityVerb::FAVORITE,
170                                                 'object_type' => (($notice->verb == ActivityVerb::POST) ?
171                                                                                  $notice->object_type : ActivityObject::ACTIVITY)));
172         }
173         return true;
174     }
175
176     function onEndDisfavorNotice($profile, $notice)
177     {
178         // Only do this if config is enabled
179         if(!$this->StopLike) return true;
180         $user = User::staticGet('id', $profile->id);
181
182         if (!empty($user)) {
183             $author = Profile::staticGet('id', $notice->profile_id);
184             $rendered = sprintf(_m('<em><a href="%s">%s</a> stopped liking <a href="%s">%s\'s update</a></em>.'),
185                                                 $profile->profileurl,
186                                                 $profile->getBestName(),
187                                 $notice->bestUrl(),
188                                 $author->getBestName());
189             $content  = sprintf(_m('%s (%s) stopped liking %s\'s status (%s)'),
190                                                 $profile->getBestName(),
191                                                 $profile->profileurl,
192                                 $author->getBestName(), 
193                                                                 $notice->bestUrl());
194                                                                 
195                         $uri = TagURI::mint('unlike:%d:%d:%s',
196                                 $profile->id,
197                                 $notice->id,
198                                 common_date_iso8601(common_sql_now()));
199                                 
200             $notice = Notice::saveNew($user->id,
201                                       $content,
202                                       'activity',
203                                       array('rendered' => $rendered,
204                                                 'uri' => $uri,
205                                                 'verb' => ActivityVerb::UNFAVORITE,
206                                                 'object_type' => (($notice->verb == ActivityVerb::POST) ?
207                                                                                  $notice->object_type : ActivityObject::ACTIVITY)));
208         }
209         return true;
210     }
211
212     function onEndJoinGroup($group, $profile)
213     {
214         // Only do this if config is enabled
215         if(!$this->JoinGroup) return true;
216         
217         $user = $profile->getUser();
218         
219         if (empty($user)) {
220             return true;
221         }
222         
223         $rendered = sprintf(_m('<em><a href="%s">%s</a> joined the group <a href="%s">%s</a></em>.'),
224                                         $profile->profileurl,
225                                         $profile->getBestName(),
226                             $group->homeUrl(),
227                             $group->getBestName());
228         $content  = sprintf(_m('%s (%s) joined the group %s (%s).'),
229                                         $profile->getBestName(),
230                                         $profile->profileurl,
231                             $group->getBestName(),
232                                                 $group->homeUrl());
233
234                 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
235                                                                                    'profile_id' => $profile->id));
236                                                                                          
237         $notice = Notice::saveNew($user->id,
238                                   $content,
239                                   'activity',
240                                   array('rendered' => $rendered,
241                                                 'uri' => $mem->getURI(),
242                                                 'verb' => ActivityVerb::JOIN,
243                                                 'object_type' => ActivityObject::GROUP));
244         return true;
245     }
246
247     function onEndLeaveGroup($group, $profile)
248     {
249         // Only do this if config is enabled
250         if(!$this->LeaveGroup) return true;
251         
252         $user = $profile->getUser();
253         
254         if (empty($user)) {
255             return true;
256         }
257         
258         $rendered = sprintf(_m('<em><a href="%s">%s</a> left the group <a href="%s">%s</a></em>.'),
259                                         $profile->profileurl,
260                                         $profile->getBestName(),
261                             $group->homeUrl(),
262                             $group->getBestName());
263         $content  = sprintf(_m('%s (%s) left the group %s (%s)'),
264                                         $profile->getBestName(),
265                                         $profile->profileurl,
266                             $group->getBestName(),
267                                                 $group->homeUrl());
268                             
269                 $uri = TagURI::mint('leave:%d:%d:%s',
270                             $user->id,
271                             $group->id,
272                             common_date_iso8601(common_sql_now()));
273
274         $notice = Notice::saveNew($user->id,
275                                   $content,
276                                   'activity',
277                                   array('rendered' => $rendered,
278                                                 'uri' => $uri,
279                                                 'verb' => ActivityVerb::LEAVE,
280                                                 'object_type' => ActivityObject::GROUP));
281         return true;
282     }
283     
284     function onStartShowNoticeItem($nli)
285     {
286                 $notice = $nli->notice;
287                 
288                 $adapter = null;
289                 
290                 switch ($notice->verb) {
291                 case ActivityVerb::JOIN:
292                         $adapter = new JoinListItem($nli);
293                         break;
294                 case ActivityVerb::LEAVE:
295                         $adapter = new JoinListItem($nli);
296                         break;
297                 case ActivityVerb::FOLLOW:
298                         $adapter = new FollowListItem($nli);
299                         break;
300                 case ActivityVerb::UNFOLLOW:
301                         $adapter = new UnfollowListItem($nli);
302                         break;
303                 }
304
305         if (!empty($adapter)) {
306             $adapter->showNotice();
307             $adapter->showNoticeAttachments();
308             $adapter->showNoticeInfo();
309             $adapter->showNoticeOptions();
310             return false;
311         }
312         
313         return true;
314     }
315
316     function onEndNoticeAsActivity($notice, &$activity)
317     {
318         return true;
319     }
320
321     function onPluginVersion(&$versions)
322     {
323         $versions[] = array('name' => 'Activity',
324                             'version' => self::VERSION,
325                             'author' => 'Evan Prodromou',
326                             'homepage' => 'http://status.net/wiki/Plugin:Activity',
327                             'rawdescription' =>
328                             _m('Emits notices when social activities happen.'));
329         return true;
330     }
331 }