3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Shows social activities in the output feed
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Activity plugin main class
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/
48 class ActivityPlugin extends Plugin
50 const VERSION = '0.1';
51 const SOURCE = 'system';
53 // Flags to switch off certain activity notices
54 public $StartFollowUser = true;
55 public $StopFollowUser = true;
56 public $JoinGroup = true;
57 public $LeaveGroup = true;
58 public $StartLike = true;
59 public $StopLike = true;
61 function onAutoload($cls)
63 $dir = dirname(__FILE__);
69 case 'FollowListItem':
70 case 'UnfollowListItem':
71 case 'SystemListItem':
72 include_once $dir . '/'.strtolower($cls).'.php';
79 function onEndSubscribe($subscriber, $other)
81 // Only do this if config is enabled
82 if(!$this->StartFollowUser) return true;
83 $user = $subscriber->getUser();
85 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
86 'subscribed' => $other->id));
87 $rendered = sprintf(_m('<em><a href="%s">%s</a> started following <a href="%s">%s</a></em>.'),
88 $subscriber->profileurl,
89 $subscriber->getBestName(),
91 $other->getBestName());
92 $content = sprintf(_m('%s (%s) started following %s (%s).'),
93 $subscriber->getBestName(),
94 $subscriber->profileurl,
95 $other->getBestName(),
98 $notice = Notice::saveNew($user->id,
100 ActivityPlugin::SOURCE,
101 array('rendered' => $rendered,
102 'verb' => ActivityVerb::FOLLOW,
103 'object_type' => ActivityObject::PERSON,
104 'uri' => $sub->uri));
109 function onEndUnsubscribe($subscriber, $other)
111 // Only do this if config is enabled
112 if(!$this->StopFollowUser) return true;
113 $user = $subscriber->getUser();
115 $rendered = sprintf(_m('<em><a href="%s">%s</a> stopped following <a href="%s">%s</a></em>.'),
116 $subscriber->profileurl,
117 $subscriber->getBestName(),
119 $other->getBestName());
120 $content = sprintf(_m('%s (%s) stopped following %s (%s).'),
121 $subscriber->getBestName(),
122 $subscriber->profileurl,
123 $other->getBestName(),
126 $uri = TagURI::mint('stop-following:%d:%d:%s',
129 common_date_iso8601(common_sql_now()));
131 $notice = Notice::saveNew($user->id,
133 ActivityPlugin::SOURCE,
134 array('rendered' => $rendered,
136 'verb' => ActivityVerb::UNFOLLOW,
137 'object_type' => ActivityObject::PERSON));
142 function onEndFavorNotice($profile, $notice)
144 // Only do this if config is enabled
145 if(!$this->StartLike) return true;
147 $user = $profile->getUser();
151 $author = $notice->getProfile();
152 $fave = Fave::pkeyGet(array('user_id' => $user->id,
153 'notice_id' => $notice->id));
155 $rendered = sprintf(_m('<em><a href="%s">%s</a> liked <a href="%s">%s\'s update</a></em>.'),
156 $profile->profileurl,
157 $profile->getBestName(),
159 $author->getBestName());
160 $content = sprintf(_m('%s (%s) liked %s\'s status (%s)'),
161 $profile->getBestName(),
162 $profile->profileurl,
163 $author->getBestName(),
166 $notice = Notice::saveNew($user->id,
168 ActivityPlugin::SOURCE,
169 array('rendered' => $rendered,
170 'uri' => $fave->getURI(),
171 'verb' => ActivityVerb::FAVORITE,
172 'object_type' => (($notice->verb == ActivityVerb::POST) ?
173 $notice->object_type : ActivityObject::ACTIVITY)));
178 function onEndDisfavorNotice($profile, $notice)
180 // Only do this if config is enabled
181 if(!$this->StopLike) return true;
182 $user = User::staticGet('id', $profile->id);
185 $author = Profile::staticGet('id', $notice->profile_id);
186 $rendered = sprintf(_m('<em><a href="%s">%s</a> stopped liking <a href="%s">%s\'s update</a></em>.'),
187 $profile->profileurl,
188 $profile->getBestName(),
190 $author->getBestName());
191 $content = sprintf(_m('%s (%s) stopped liking %s\'s status (%s)'),
192 $profile->getBestName(),
193 $profile->profileurl,
194 $author->getBestName(),
197 $uri = TagURI::mint('unlike:%d:%d:%s',
200 common_date_iso8601(common_sql_now()));
202 $notice = Notice::saveNew($user->id,
204 ActivityPlugin::SOURCE,
205 array('rendered' => $rendered,
207 'verb' => ActivityVerb::UNFAVORITE,
208 'object_type' => (($notice->verb == ActivityVerb::POST) ?
209 $notice->object_type : ActivityObject::ACTIVITY)));
214 function onEndJoinGroup($group, $profile)
216 // Only do this if config is enabled
217 if(!$this->JoinGroup) return true;
219 $user = $profile->getUser();
225 $rendered = sprintf(_m('<em><a href="%s">%s</a> joined the group <a href="%s">%s</a></em>.'),
226 $profile->profileurl,
227 $profile->getBestName(),
229 $group->getBestName());
230 $content = sprintf(_m('%s (%s) joined the group %s (%s).'),
231 $profile->getBestName(),
232 $profile->profileurl,
233 $group->getBestName(),
236 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
237 'profile_id' => $profile->id));
239 $notice = Notice::saveNew($user->id,
241 ActivityPlugin::SOURCE,
242 array('rendered' => $rendered,
243 'uri' => $mem->getURI(),
244 'verb' => ActivityVerb::JOIN,
245 'object_type' => ActivityObject::GROUP));
249 function onEndLeaveGroup($group, $profile)
251 // Only do this if config is enabled
252 if(!$this->LeaveGroup) return true;
254 $user = $profile->getUser();
260 $rendered = sprintf(_m('<em><a href="%s">%s</a> left the group <a href="%s">%s</a></em>.'),
261 $profile->profileurl,
262 $profile->getBestName(),
264 $group->getBestName());
265 $content = sprintf(_m('%s (%s) left the group %s (%s)'),
266 $profile->getBestName(),
267 $profile->profileurl,
268 $group->getBestName(),
271 $uri = TagURI::mint('leave:%d:%d:%s',
274 common_date_iso8601(common_sql_now()));
276 $notice = Notice::saveNew($user->id,
278 ActivityPlugin::SOURCE,
279 array('rendered' => $rendered,
281 'verb' => ActivityVerb::LEAVE,
282 'object_type' => ActivityObject::GROUP));
286 function onStartShowNoticeItem($nli)
288 $notice = $nli->notice;
292 switch ($notice->verb) {
293 case ActivityVerb::FAVORITE:
294 case ActivityVerb::UNFAVORITE:
295 $adapter = new SystemListItem($nli);
297 case ActivityVerb::JOIN:
298 $adapter = new JoinListItem($nli);
300 case ActivityVerb::LEAVE:
301 $adapter = new JoinListItem($nli);
303 case ActivityVerb::FOLLOW:
304 $adapter = new FollowListItem($nli);
306 case ActivityVerb::UNFOLLOW:
307 $adapter = new UnfollowListItem($nli);
311 if (!empty($adapter)) {
312 $adapter->showNotice();
313 $adapter->showNoticeAttachments();
314 $adapter->showNoticeInfo();
315 $adapter->showNoticeOptions();
322 function onEndNoticeAsActivity($notice, &$activity)
324 switch ($notice->verb) {
325 case ActivityVerb::FAVORITE:
326 $fave = Fave::staticGet('uri', $notice->uri);
328 $notice = Notice::staticGet('id', $fave->notice_id);
329 if (!empty($notice)) {
330 $target = $notice->asActivity();
331 if ($target->verb == ActivityVerb::POST) {
332 // "I like the thing you posted"
333 $activity->objects = $target->objects;
335 // "I like that you did whatever you did"
336 $activity->objects = array($target);
341 case ActivityVerb::UNFAVORITE:
342 // FIXME: do something here
344 case ActivityVerb::JOIN:
345 $mem = Group_member::staticGet('uri', $notice->uri);
347 $group = $mem->getGroup();
348 $activity->objects = array(ActivityObject::fromGroup($group));
351 case ActivityVerb::LEAVE:
354 case ActivityVerb::FOLLOW:
355 $sub = Subscription::staticGet('uri', $notice->uri);
357 $profile = Profile::staticGet('id', $sub->subscribed);
358 if (!empty($profile)) {
359 $activity->objects = array(ActivityObject::fromProfile($profile));
363 case ActivityVerb::UNFOLLOW:
371 function onPluginVersion(&$versions)
373 $versions[] = array('name' => 'Activity',
374 'version' => self::VERSION,
375 'author' => 'Evan Prodromou',
376 'homepage' => 'http://status.net/wiki/Plugin:Activity',
378 _m('Emits notices when social activities happen.'));