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/
47 class ActivityPlugin extends Plugin
49 const VERSION = '0.1';
50 const SOURCE = 'activity';
52 // Flags to switch off certain activity notices
53 public $StartFollowUser = true;
54 public $StopFollowUser = false;
55 public $JoinGroup = true;
56 public $LeaveGroup = false;
57 public $StartLike = false;
58 public $StopLike = false;
60 function onAutoload($cls)
62 $dir = dirname(__FILE__);
68 case 'FollowListItem':
69 case 'UnfollowListItem':
70 case 'SystemListItem':
71 include_once $dir . '/'.strtolower($cls).'.php';
78 function onEndSubscribe($subscriber, $other)
80 // Only do this if config is enabled
81 if(!$this->StartFollowUser) return true;
82 $user = $subscriber->getUser();
84 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
85 'subscribed' => $other->id));
86 // TRANS: Text for "started following" item in activity plugin.
87 // TRANS: %1$s is a profile URL, %2$s is a profile name,
88 // TRANS: %3$s is a profile URL, %4$s is a profile name.
89 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%3$s">%4$s</a>.'),
90 $subscriber->profileurl,
91 $subscriber->getBestName(),
93 $other->getBestName());
94 // TRANS: Text for "started following" item in activity plugin.
95 // TRANS: %1$s is a profile name, %2$s is a profile URL,
96 // TRANS: %3$s is a profile name, %4$s is a profile URL.
97 $content = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
98 $subscriber->getBestName(),
99 $subscriber->profileurl,
100 $other->getBestName(),
103 $notice = Notice::saveNew($user->id,
105 ActivityPlugin::SOURCE,
106 array('rendered' => $rendered,
108 'replies' => array($other->getUri()),
109 'verb' => ActivityVerb::FOLLOW,
110 'object_type' => ActivityObject::PERSON,
111 'uri' => $sub->uri));
116 function onEndUnsubscribe($subscriber, $other)
118 // Only do this if config is enabled
119 if(!$this->StopFollowUser) return true;
120 $user = $subscriber->getUser();
122 // TRANS: Text for "stopped following" item in activity plugin.
123 // TRANS: %1$s is a profile URL, %2$s is a profile name,
124 // TRANS: %3$s is a profile URL, %4$s is a profile name.
125 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
126 $subscriber->profileurl,
127 $subscriber->getBestName(),
129 $other->getBestName());
130 // TRANS: Text for "stopped following" item in activity plugin.
131 // TRANS: %1$s is a profile name, %2$s is a profile URL,
132 // TRANS: %3$s is a profile name, %4$s is a profile URL.
133 $content = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$s).'),
134 $subscriber->getBestName(),
135 $subscriber->profileurl,
136 $other->getBestName(),
139 $uri = TagURI::mint('stop-following:%d:%d:%s',
142 common_date_iso8601(common_sql_now()));
144 $notice = Notice::saveNew($user->id,
146 ActivityPlugin::SOURCE,
147 array('rendered' => $rendered,
149 'replies' => array($other->getUri()),
151 'verb' => ActivityVerb::UNFOLLOW,
152 'object_type' => ActivityObject::PERSON));
157 function onEndFavorNotice($profile, $notice)
159 // Only do this if config is enabled
160 if(!$this->StartLike) return true;
162 $user = $profile->getUser();
166 $author = $notice->getProfile();
167 $fave = Fave::pkeyGet(array('user_id' => $user->id,
168 'notice_id' => $notice->id));
170 // TRANS: Text for "liked" item in activity plugin.
171 // TRANS: %1$s is a profile URL, %2$s is a profile name,
172 // TRANS: %3$s is a notice URL, %4$s is an author name.
173 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> liked <a href="%3$s">%4$s\'s update</a>.'),
174 $profile->profileurl,
175 $profile->getBestName(),
177 $author->getBestName());
178 // TRANS: Text for "liked" item in activity plugin.
179 // TRANS: %1$s is a profile name, %2$s is a profile URL,
180 // TRANS: %3$s is an author name, %4$s is a notice URL.
181 $content = sprintf(_m('%1$s (%2$s) liked %3$s\'s status (%4$s).'),
182 $profile->getBestName(),
183 $profile->profileurl,
184 $author->getBestName(),
187 $notice = Notice::saveNew($user->id,
189 ActivityPlugin::SOURCE,
190 array('rendered' => $rendered,
192 'replies' => array($author->getUri()),
193 'uri' => $fave->getURI(),
194 'verb' => ActivityVerb::FAVORITE,
195 'object_type' => (($notice->verb == ActivityVerb::POST) ?
196 $notice->object_type : ActivityObject::ACTIVITY)));
201 function onEndDisfavorNotice($profile, $notice)
203 // Only do this if config is enabled
204 if(!$this->StopLike) return true;
205 $user = User::getKV('id', $profile->id);
208 $author = Profile::getKV('id', $notice->profile_id);
209 // TRANS: Text for "stopped liking" item in activity plugin.
210 // TRANS: %1$s is a profile URL, %2$s is a profile name,
211 // TRANS: %3$s is a notice URL, %4$s is an author name.
212 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
213 $profile->profileurl,
214 $profile->getBestName(),
216 $author->getBestName());
217 // TRANS: Text for "stopped liking" item in activity plugin.
218 // TRANS: %1$s is a profile name, %2$s is a profile URL,
219 // TRANS: %3$s is an author name, %4$s is a notice URL.
220 $content = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s).'),
221 $profile->getBestName(),
222 $profile->profileurl,
223 $author->getBestName(),
226 $uri = TagURI::mint('unlike:%d:%d:%s',
229 common_date_iso8601(common_sql_now()));
231 $notice = Notice::saveNew($user->id,
233 ActivityPlugin::SOURCE,
234 array('rendered' => $rendered,
236 'replies' => array($author->getUri()),
238 'verb' => ActivityVerb::UNFAVORITE,
239 'object_type' => (($notice->verb == ActivityVerb::POST) ?
240 $notice->object_type : ActivityObject::ACTIVITY)));
245 function onEndJoinGroup($group, $profile)
247 // Only do this if config is enabled
248 if(!$this->JoinGroup) return true;
250 $user = $profile->getUser();
256 // TRANS: Text for "joined group" item in activity plugin.
257 // TRANS: %1$s is a profile URL, %2$s is a profile name,
258 // TRANS: %3$s is a group URL, %4$s is a group name.
259 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
260 $profile->profileurl,
261 $profile->getBestName(),
263 $group->getBestName());
264 // TRANS: Text for "joined group" item in activity plugin.
265 // TRANS: %1$s is a profile name, %2$s is a profile URL,
266 // TRANS: %3$s is a group name, %4$s is a group URL.
267 $content = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
268 $profile->getBestName(),
269 $profile->profileurl,
270 $group->getBestName(),
273 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
274 'profile_id' => $profile->id));
276 $notice = Notice::saveNew($user->id,
278 ActivityPlugin::SOURCE,
279 array('rendered' => $rendered,
281 'groups' => array($group->id),
282 'uri' => $mem->getURI(),
283 'verb' => ActivityVerb::JOIN,
284 'object_type' => ActivityObject::GROUP));
288 function onEndLeaveGroup($group, $profile)
290 // Only do this if config is enabled
291 if(!$this->LeaveGroup) return true;
293 $user = $profile->getUser();
299 // TRANS: Text for "left group" item in activity plugin.
300 // TRANS: %1$s is a profile URL, %2$s is a profile name,
301 // TRANS: %3$s is a group URL, %4$s is a group name.
302 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
303 $profile->profileurl,
304 $profile->getBestName(),
306 $group->getBestName());
307 // TRANS: Text for "left group" item in activity plugin.
308 // TRANS: %1$s is a profile name, %2$s is a profile URL,
309 // TRANS: %3$s is a group name, %4$s is a group URL.
310 $content = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'),
311 $profile->getBestName(),
312 $profile->profileurl,
313 $group->getBestName(),
316 $uri = TagURI::mint('leave:%d:%d:%s',
319 common_date_iso8601(common_sql_now()));
321 $notice = Notice::saveNew($user->id,
323 ActivityPlugin::SOURCE,
324 array('rendered' => $rendered,
326 'groups' => array($group->id),
328 'verb' => ActivityVerb::LEAVE,
329 'object_type' => ActivityObject::GROUP));
333 function onStartShowNoticeItem($nli)
335 $notice = $nli->notice;
339 switch ($notice->verb) {
340 case ActivityVerb::FAVORITE:
341 case ActivityVerb::UNFAVORITE:
342 $adapter = new SystemListItem($nli);
344 case ActivityVerb::JOIN:
345 $adapter = new JoinListItem($nli);
347 case ActivityVerb::LEAVE:
348 $adapter = new JoinListItem($nli);
350 case ActivityVerb::FOLLOW:
351 $adapter = new FollowListItem($nli);
353 case ActivityVerb::UNFOLLOW:
354 $adapter = new UnfollowListItem($nli);
358 if (!empty($adapter)) {
359 $adapter->showNotice();
360 $adapter->showNoticeAttachments();
361 $adapter->showNoticeInfo();
362 $adapter->showNoticeOptions();
369 function onEndNoticeAsActivity($notice, &$activity)
371 switch ($notice->verb) {
372 case ActivityVerb::FAVORITE:
373 $fave = Fave::getKV('uri', $notice->uri);
375 $notice = Notice::getKV('id', $fave->notice_id);
376 if (!empty($notice)) {
377 $cur = common_current_user();
378 $target = $notice->asActivity($cur);
379 if ($target->verb == ActivityVerb::POST) {
380 // "I like the thing you posted"
381 $activity->objects = $target->objects;
383 // "I like that you did whatever you did"
384 $activity->objects = array($target);
389 case ActivityVerb::UNFAVORITE:
390 // FIXME: do something here
392 case ActivityVerb::JOIN:
393 $mem = Group_member::getKV('uri', $notice->uri);
395 $group = $mem->getGroup();
396 $activity->objects = array(ActivityObject::fromGroup($group));
399 case ActivityVerb::LEAVE:
402 case ActivityVerb::FOLLOW:
403 $sub = Subscription::getKV('uri', $notice->uri);
405 $profile = Profile::getKV('id', $sub->subscribed);
406 if (!empty($profile)) {
407 $activity->objects = array(ActivityObject::fromProfile($profile));
411 case ActivityVerb::UNFOLLOW:
419 function onPluginVersion(&$versions)
421 $versions[] = array('name' => 'Activity',
422 'version' => self::VERSION,
423 'author' => 'Evan Prodromou',
424 'homepage' => 'http://status.net/wiki/Plugin:Activity',
426 // TRANS: Plugin description.
427 _m('Emits notices when social activities happen.'));