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