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('<a href="%s">%s</a> started following <a href="%s">%s</a>.'),
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,
103 'replies' => array($other->getUri()),
104 'verb' => ActivityVerb::FOLLOW,
105 'object_type' => ActivityObject::PERSON,
106 'uri' => $sub->uri));
111 function onEndUnsubscribe($subscriber, $other)
113 // Only do this if config is enabled
114 if(!$this->StopFollowUser) return true;
115 $user = $subscriber->getUser();
117 $rendered = sprintf(_m('<a href="%s">%s</a> stopped following <a href="%s">%s</a>.'),
118 $subscriber->profileurl,
119 $subscriber->getBestName(),
121 $other->getBestName());
122 $content = sprintf(_m('%s (%s) stopped following %s (%s).'),
123 $subscriber->getBestName(),
124 $subscriber->profileurl,
125 $other->getBestName(),
128 $uri = TagURI::mint('stop-following:%d:%d:%s',
131 common_date_iso8601(common_sql_now()));
133 $notice = Notice::saveNew($user->id,
135 ActivityPlugin::SOURCE,
136 array('rendered' => $rendered,
138 'replies' => array($other->getUri()),
140 'verb' => ActivityVerb::UNFOLLOW,
141 'object_type' => ActivityObject::PERSON));
146 function onEndFavorNotice($profile, $notice)
148 // Only do this if config is enabled
149 if(!$this->StartLike) return true;
151 $user = $profile->getUser();
155 $author = $notice->getProfile();
156 $fave = Fave::pkeyGet(array('user_id' => $user->id,
157 'notice_id' => $notice->id));
159 $rendered = sprintf(_m('<a href="%s">%s</a> liked <a href="%s">%s\'s update</a>.'),
160 $profile->profileurl,
161 $profile->getBestName(),
163 $author->getBestName());
164 $content = sprintf(_m('%s (%s) liked %s\'s status (%s)'),
165 $profile->getBestName(),
166 $profile->profileurl,
167 $author->getBestName(),
170 $notice = Notice::saveNew($user->id,
172 ActivityPlugin::SOURCE,
173 array('rendered' => $rendered,
175 'replies' => array($author->getUri()),
176 'uri' => $fave->getURI(),
177 'verb' => ActivityVerb::FAVORITE,
178 'object_type' => (($notice->verb == ActivityVerb::POST) ?
179 $notice->object_type : ActivityObject::ACTIVITY)));
184 function onEndDisfavorNotice($profile, $notice)
186 // Only do this if config is enabled
187 if(!$this->StopLike) return true;
188 $user = User::staticGet('id', $profile->id);
191 $author = Profile::staticGet('id', $notice->profile_id);
192 $rendered = sprintf(_m('<a href="%s">%s</a> stopped liking <a href="%s">%s\'s update</a>.'),
193 $profile->profileurl,
194 $profile->getBestName(),
196 $author->getBestName());
197 $content = sprintf(_m('%s (%s) stopped liking %s\'s status (%s)'),
198 $profile->getBestName(),
199 $profile->profileurl,
200 $author->getBestName(),
203 $uri = TagURI::mint('unlike:%d:%d:%s',
206 common_date_iso8601(common_sql_now()));
208 $notice = Notice::saveNew($user->id,
210 ActivityPlugin::SOURCE,
211 array('rendered' => $rendered,
213 'replies' => array($author->getUri()),
215 'verb' => ActivityVerb::UNFAVORITE,
216 'object_type' => (($notice->verb == ActivityVerb::POST) ?
217 $notice->object_type : ActivityObject::ACTIVITY)));
222 function onEndJoinGroup($group, $profile)
224 // Only do this if config is enabled
225 if(!$this->JoinGroup) return true;
227 $user = $profile->getUser();
233 $rendered = sprintf(_m('<a href="%s">%s</a> joined the group <a href="%s">%s</a>.'),
234 $profile->profileurl,
235 $profile->getBestName(),
237 $group->getBestName());
238 $content = sprintf(_m('%s (%s) joined the group %s (%s).'),
239 $profile->getBestName(),
240 $profile->profileurl,
241 $group->getBestName(),
244 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
245 'profile_id' => $profile->id));
247 $notice = Notice::saveNew($user->id,
249 ActivityPlugin::SOURCE,
250 array('rendered' => $rendered,
252 'groups' => array($group->id),
253 'uri' => $mem->getURI(),
254 'verb' => ActivityVerb::JOIN,
255 'object_type' => ActivityObject::GROUP));
259 function onEndLeaveGroup($group, $profile)
261 // Only do this if config is enabled
262 if(!$this->LeaveGroup) return true;
264 $user = $profile->getUser();
270 $rendered = sprintf(_m('<a href="%s">%s</a> left the group <a href="%s">%s</a>.'),
271 $profile->profileurl,
272 $profile->getBestName(),
274 $group->getBestName());
275 $content = sprintf(_m('%s (%s) left the group %s (%s)'),
276 $profile->getBestName(),
277 $profile->profileurl,
278 $group->getBestName(),
281 $uri = TagURI::mint('leave:%d:%d:%s',
284 common_date_iso8601(common_sql_now()));
286 $notice = Notice::saveNew($user->id,
288 ActivityPlugin::SOURCE,
289 array('rendered' => $rendered,
291 'groups' => array($group->id),
293 'verb' => ActivityVerb::LEAVE,
294 'object_type' => ActivityObject::GROUP));
298 function onStartShowNoticeItem($nli)
300 $notice = $nli->notice;
304 switch ($notice->verb) {
305 case ActivityVerb::FAVORITE:
306 case ActivityVerb::UNFAVORITE:
307 $adapter = new SystemListItem($nli);
309 case ActivityVerb::JOIN:
310 $adapter = new JoinListItem($nli);
312 case ActivityVerb::LEAVE:
313 $adapter = new JoinListItem($nli);
315 case ActivityVerb::FOLLOW:
316 $adapter = new FollowListItem($nli);
318 case ActivityVerb::UNFOLLOW:
319 $adapter = new UnfollowListItem($nli);
323 if (!empty($adapter)) {
324 $adapter->showNotice();
325 $adapter->showNoticeAttachments();
326 $adapter->showNoticeInfo();
327 $adapter->showNoticeOptions();
334 function onEndNoticeAsActivity($notice, &$activity)
336 switch ($notice->verb) {
337 case ActivityVerb::FAVORITE:
338 $fave = Fave::staticGet('uri', $notice->uri);
340 $notice = Notice::staticGet('id', $fave->notice_id);
341 if (!empty($notice)) {
342 $target = $notice->asActivity();
343 if ($target->verb == ActivityVerb::POST) {
344 // "I like the thing you posted"
345 $activity->objects = $target->objects;
347 // "I like that you did whatever you did"
348 $activity->objects = array($target);
353 case ActivityVerb::UNFAVORITE:
354 // FIXME: do something here
356 case ActivityVerb::JOIN:
357 $mem = Group_member::staticGet('uri', $notice->uri);
359 $group = $mem->getGroup();
360 $activity->objects = array(ActivityObject::fromGroup($group));
363 case ActivityVerb::LEAVE:
366 case ActivityVerb::FOLLOW:
367 $sub = Subscription::staticGet('uri', $notice->uri);
369 $profile = Profile::staticGet('id', $sub->subscribed);
370 if (!empty($profile)) {
371 $activity->objects = array(ActivityObject::fromProfile($profile));
375 case ActivityVerb::UNFOLLOW:
383 function onPluginVersion(&$versions)
385 $versions[] = array('name' => 'Activity',
386 'version' => self::VERSION,
387 'author' => 'Evan Prodromou',
388 'homepage' => 'http://status.net/wiki/Plugin:Activity',
390 _m('Emits notices when social activities happen.'));