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,
102 'replies' => array($other->getUri()),
103 'verb' => ActivityVerb::FOLLOW,
104 'object_type' => ActivityObject::PERSON,
105 'uri' => $sub->uri));
110 function onEndUnsubscribe($subscriber, $other)
112 // Only do this if config is enabled
113 if(!$this->StopFollowUser) return true;
114 $user = $subscriber->getUser();
116 $rendered = sprintf(_m('<a href="%s">%s</a> stopped following <a href="%s">%s</a>.'),
117 $subscriber->profileurl,
118 $subscriber->getBestName(),
120 $other->getBestName());
121 $content = sprintf(_m('%s (%s) stopped following %s (%s).'),
122 $subscriber->getBestName(),
123 $subscriber->profileurl,
124 $other->getBestName(),
127 $uri = TagURI::mint('stop-following:%d:%d:%s',
130 common_date_iso8601(common_sql_now()));
132 $notice = Notice::saveNew($user->id,
134 ActivityPlugin::SOURCE,
135 array('rendered' => $rendered,
136 'replies' => array($other->getUri()),
138 'verb' => ActivityVerb::UNFOLLOW,
139 'object_type' => ActivityObject::PERSON));
144 function onEndFavorNotice($profile, $notice)
146 // Only do this if config is enabled
147 if(!$this->StartLike) return true;
149 $user = $profile->getUser();
153 $author = $notice->getProfile();
154 $fave = Fave::pkeyGet(array('user_id' => $user->id,
155 'notice_id' => $notice->id));
157 $rendered = sprintf(_m('<a href="%s">%s</a> liked <a href="%s">%s\'s update</a>.'),
158 $profile->profileurl,
159 $profile->getBestName(),
161 $author->getBestName());
162 $content = sprintf(_m('%s (%s) liked %s\'s status (%s)'),
163 $profile->getBestName(),
164 $profile->profileurl,
165 $author->getBestName(),
168 $notice = Notice::saveNew($user->id,
170 ActivityPlugin::SOURCE,
171 array('rendered' => $rendered,
172 'replies' => array($author->getUri()),
173 'uri' => $fave->getURI(),
174 'verb' => ActivityVerb::FAVORITE,
175 'object_type' => (($notice->verb == ActivityVerb::POST) ?
176 $notice->object_type : ActivityObject::ACTIVITY)));
181 function onEndDisfavorNotice($profile, $notice)
183 // Only do this if config is enabled
184 if(!$this->StopLike) return true;
185 $user = User::staticGet('id', $profile->id);
188 $author = Profile::staticGet('id', $notice->profile_id);
189 $rendered = sprintf(_m('<a href="%s">%s</a> stopped liking <a href="%s">%s\'s update</a>.'),
190 $profile->profileurl,
191 $profile->getBestName(),
193 $author->getBestName());
194 $content = sprintf(_m('%s (%s) stopped liking %s\'s status (%s)'),
195 $profile->getBestName(),
196 $profile->profileurl,
197 $author->getBestName(),
200 $uri = TagURI::mint('unlike:%d:%d:%s',
203 common_date_iso8601(common_sql_now()));
205 $notice = Notice::saveNew($user->id,
207 ActivityPlugin::SOURCE,
208 array('rendered' => $rendered,
209 'replies' => array($author->getUri()),
211 'verb' => ActivityVerb::UNFAVORITE,
212 'object_type' => (($notice->verb == ActivityVerb::POST) ?
213 $notice->object_type : ActivityObject::ACTIVITY)));
218 function onEndJoinGroup($group, $profile)
220 // Only do this if config is enabled
221 if(!$this->JoinGroup) return true;
223 $user = $profile->getUser();
229 $rendered = sprintf(_m('<a href="%s">%s</a> joined the group <a href="%s">%s</a>.'),
230 $profile->profileurl,
231 $profile->getBestName(),
233 $group->getBestName());
234 $content = sprintf(_m('%s (%s) joined the group %s (%s).'),
235 $profile->getBestName(),
236 $profile->profileurl,
237 $group->getBestName(),
240 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
241 'profile_id' => $profile->id));
243 $notice = Notice::saveNew($user->id,
245 ActivityPlugin::SOURCE,
246 array('rendered' => $rendered,
247 'groups' => array($group->id),
248 'uri' => $mem->getURI(),
249 'verb' => ActivityVerb::JOIN,
250 'object_type' => ActivityObject::GROUP));
254 function onEndLeaveGroup($group, $profile)
256 // Only do this if config is enabled
257 if(!$this->LeaveGroup) return true;
259 $user = $profile->getUser();
265 $rendered = sprintf(_m('<a href="%s">%s</a> left the group <a href="%s">%s</a>.'),
266 $profile->profileurl,
267 $profile->getBestName(),
269 $group->getBestName());
270 $content = sprintf(_m('%s (%s) left the group %s (%s)'),
271 $profile->getBestName(),
272 $profile->profileurl,
273 $group->getBestName(),
276 $uri = TagURI::mint('leave:%d:%d:%s',
279 common_date_iso8601(common_sql_now()));
281 $notice = Notice::saveNew($user->id,
283 ActivityPlugin::SOURCE,
284 array('rendered' => $rendered,
285 'groups' => array($group->id),
287 'verb' => ActivityVerb::LEAVE,
288 'object_type' => ActivityObject::GROUP));
292 function onStartShowNoticeItem($nli)
294 $notice = $nli->notice;
298 switch ($notice->verb) {
299 case ActivityVerb::FAVORITE:
300 case ActivityVerb::UNFAVORITE:
301 $adapter = new SystemListItem($nli);
303 case ActivityVerb::JOIN:
304 $adapter = new JoinListItem($nli);
306 case ActivityVerb::LEAVE:
307 $adapter = new JoinListItem($nli);
309 case ActivityVerb::FOLLOW:
310 $adapter = new FollowListItem($nli);
312 case ActivityVerb::UNFOLLOW:
313 $adapter = new UnfollowListItem($nli);
317 if (!empty($adapter)) {
318 $adapter->showNotice();
319 $adapter->showNoticeAttachments();
320 $adapter->showNoticeInfo();
321 $adapter->showNoticeOptions();
328 function onEndNoticeAsActivity($notice, &$activity)
330 switch ($notice->verb) {
331 case ActivityVerb::FAVORITE:
332 $fave = Fave::staticGet('uri', $notice->uri);
334 $notice = Notice::staticGet('id', $fave->notice_id);
335 if (!empty($notice)) {
336 $target = $notice->asActivity();
337 if ($target->verb == ActivityVerb::POST) {
338 // "I like the thing you posted"
339 $activity->objects = $target->objects;
341 // "I like that you did whatever you did"
342 $activity->objects = array($target);
347 case ActivityVerb::UNFAVORITE:
348 // FIXME: do something here
350 case ActivityVerb::JOIN:
351 $mem = Group_member::staticGet('uri', $notice->uri);
353 $group = $mem->getGroup();
354 $activity->objects = array(ActivityObject::fromGroup($group));
357 case ActivityVerb::LEAVE:
360 case ActivityVerb::FOLLOW:
361 $sub = Subscription::staticGet('uri', $notice->uri);
363 $profile = Profile::staticGet('id', $sub->subscribed);
364 if (!empty($profile)) {
365 $activity->objects = array(ActivityObject::fromProfile($profile));
369 case ActivityVerb::UNFOLLOW:
377 function onPluginVersion(&$versions)
379 $versions[] = array('name' => 'Activity',
380 'version' => self::VERSION,
381 'author' => 'Evan Prodromou',
382 'homepage' => 'http://status.net/wiki/Plugin:Activity',
384 _m('Emits notices when social activities happen.'));