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