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