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