]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Activity/ActivityPlugin.php
Merge activity plugin into mainline
[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     /**
61      * Database schema setup
62      *
63      * @see Schema
64      * @see ColumnDef
65      *
66      * @return boolean hook value; true means continue processing, false means stop.
67      */
68
69     function onCheckSchema()
70     {
71         $schema = Schema::get();
72
73         // For storing the activity part of a notice
74
75         $schema->ensureTable('notice_activity',
76                              array(new ColumnDef('notice_id', 'integer', null,
77                                                  false, 'PRI'),
78                                    new ColumnDef('verb', 'varchar', 255,
79                                                  false, 'MUL'),
80                                    new ColumnDef('object', 'varchar', 255,
81                                                  true, 'MUL')));
82
83         return true;
84     }
85
86     function onAutoload($cls)
87     {
88         $dir = dirname(__FILE__);
89
90         switch ($cls)
91         {
92         case 'Notice_activity':
93             include_once $dir . '/'.$cls.'.php';
94             return false;
95         default:
96             return true;
97         }
98     }
99
100     function onEndSubscribe($subscriber, $other)
101     {
102         // Only do this if config is enabled
103         if(!$this->StartFollowUser) return true;
104         $user = User::staticGet('id', $subscriber->id);
105         if (!empty($user)) {
106             $rendered = sprintf(_m('<em>Started following <a href="%s">%s</a></em>.'),
107                                 $other->profileurl,
108                                 $other->getBestName());
109             $content  = sprintf(_m('Started following %s : %s'),
110                                 $other->getBestName(),
111                                 $other->profileurl);
112
113             $notice = Notice::saveNew($user->id,
114                                       $content,
115                                       'activity',
116                                       array('rendered' => $rendered));
117
118             Notice_activity::setActivity($notice->id,
119                                          ActivityVerb::FOLLOW,
120                                          $other->getUri());
121         }
122         return true;
123     }
124
125     function onEndUnsubscribe($subscriber, $other)
126     {
127         // Only do this if config is enabled
128         if(!$this->StopFollowUser) return true;
129         $user = User::staticGet('id', $subscriber->id);
130         if (!empty($user)) {
131             $rendered = sprintf(_m('<em>Stopped following <a href="%s">%s</a></em>.'),
132                                 $other->profileurl,
133                                 $other->getBestName());
134             $content  = sprintf(_m('Stopped following %s : %s'),
135                                 $other->getBestName(),
136                                 $other->profileurl);
137
138             $notice = Notice::saveNew($user->id,
139                                       $content,
140                                       'activity',
141                                       array('rendered' => $rendered));
142
143             Notice_activity::setActivity($notice->id,
144                                          ActivityVerb::UNFOLLOW,
145                                          $other->getUri());
146         }
147         return true;
148     }
149
150     function onEndFavorNotice($profile, $notice)
151     {
152         //  Only do this if config is enabled
153         if(!$this->StartLike) return true;
154         $user = User::staticGet('id', $profile->id);
155
156         if (!empty($user)) {
157             $author = Profile::staticGet('id', $notice->profile_id);
158             $rendered = sprintf(_m('<em>Liked <a href="%s">%s\'s status</a></em>.'),
159                                 $notice->bestUrl(),
160                                 $author->getBestName());
161             $content  = sprintf(_m('Liked %s\'s status: %s'),
162                                 $author->getBestName(), 
163                                 $notice->bestUrl());
164
165             $notice = Notice::saveNew($user->id,
166                                       $content,
167                                       'activity',
168                                       array('rendered' => $rendered));
169
170             Notice_activity::setActivity($notice->id,
171                                          ActivityVerb::FAVORITE,
172                                          $notice->uri);
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>Stopped liking <a href="%s">%s\'s status</a></em>.'),
186                                 $notice->bestUrl(),
187                                 $author->getBestName());
188             $content  = sprintf(_m('Stopped liking %s\'s status: %s'),
189                                 $author->getBestName(),
190                                 $notice->bestUrl());
191
192             $notice = Notice::saveNew($user->id,
193                                       $content,
194                                       'activity',
195                                       array('rendered' => $rendered));
196
197             Notice_activity::setActivity($notice->id,
198                                          ActivityVerb::UNFAVORITE,
199                                          $notice->uri);
200         }
201         return true;
202     }
203
204     function onEndJoinGroup($group, $user)
205     {
206         // Only do this if config is enabled
207         if(!$this->JoinGroup) return true;
208         $rendered = sprintf(_m('<em>Joined the group &quot;<a href="%s">%s</a>&quot;</em>.'),
209                             $group->homeUrl(),
210                             $group->getBestName());
211         $content  = sprintf(_m('Joined the group %s : %s'),
212                             $group->getBestName(),
213                             $group->homeUrl());
214
215         $notice = Notice::saveNew($user->id,
216                                   $content,
217                                   'activity',
218                                   array('rendered' => $rendered));
219
220         Notice_activity::setActivity($notice->id,
221                                      ActivityVerb::JOIN,
222                                      $group->getUri());
223         return true;
224     }
225
226     function onEndLeaveGroup($group, $user)
227     {
228         // Only do this if config is enabled
229         if(!$this->LeaveGroup) return true;
230         $rendered = sprintf(_m('<em>Left the group &quot;<a href="%s">%s</a>&quot;</em>.'),
231                             $group->homeUrl(),
232                             $group->getBestName());
233         $content  = sprintf(_m('Left the group "%s" : %s'),
234                             $group->getBestName(),
235                             $group->homeUrl());
236
237         $notice = Notice::saveNew($user->id,
238                                   $content,
239                                   'activity',
240                                   array('rendered' => $rendered));
241
242         Notice_activity::setActivity($notice->id,
243                                      ActivityVerb::LEAVE,
244                                      $group->getUri());
245         return true;
246     }
247
248     function onEndNoticeAsActivity($notice, &$activity)
249     {
250         $na = Notice_activity::staticGet('notice_id', $notice->id);
251
252         if (!empty($na)) {
253
254             $activity->verb = $na->verb;
255
256             // wipe the old object!
257
258             $activity->objects = array();
259
260             switch ($na->verb)
261             {
262             case ActivityVerb::FOLLOW:
263             case ActivityVerb::UNFOLLOW:
264                 $profile = Profile::fromURI($na->object);
265                 if (!empty($profile)) {
266                     $activity->objects[] = ActivityObject::fromProfile($profile);
267                 }
268                 break;
269             case ActivityVerb::FAVORITE:
270             case ActivityVerb::UNFAVORITE:
271                 $target = Notice::staticGet('uri', $na->object);
272                 if (!empty($target)) {
273                     $activity->objects[] = ActivityObject::fromNotice($target);
274                 }
275                 break;
276             case ActivityVerb::JOIN:
277             case ActivityVerb::LEAVE:
278                 $group = User_group::staticGet('uri', $na->object);
279                 if (!empty($notice)) {
280                     $activity->objects[] = ActivityObject::fromGroup($group);
281                 }
282                 break;
283             default:
284                 break;
285             }
286         }
287
288         return true;
289     }
290
291
292     function onPluginVersion(&$versions)
293     {
294         $versions[] = array('name' => 'Activity',
295                             'version' => self::VERSION,
296                             'author' => 'Evan Prodromou',
297                             'homepage' => 'http://status.net/wiki/Plugin:Activity',
298                             'rawdescription' =>
299                             _m('Emits notices when social activities happen.'));
300         return true;
301     }
302 }