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 = 'system';
52 // Flags to switch off certain activity notices
53 public $StartFollowUser = true;
54 public $StopFollowUser = true;
55 public $JoinGroup = true;
56 public $LeaveGroup = true;
57 public $StartLike = true;
58 public $StopLike = true;
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 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> started following <a href="%2$s">%4$s</a>.'),
87 $subscriber->profileurl,
88 $subscriber->getBestName(),
90 $other->getBestName());
91 $content = sprintf(_m('%1$s (%2$s) started following %3$s (%4$s).'),
92 $subscriber->getBestName(),
93 $subscriber->profileurl,
94 $other->getBestName(),
97 $notice = Notice::saveNew($user->id,
99 ActivityPlugin::SOURCE,
100 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="%1$s">%2$s</a> stopped following <a href="%3$s">%4$s</a>.'),
117 $subscriber->profileurl,
118 $subscriber->getBestName(),
120 $other->getBestName());
121 $content = sprintf(_m('%1$s (%2$s) stopped following %3$s (%4$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,
137 'replies' => array($other->getUri()),
139 'verb' => ActivityVerb::UNFOLLOW,
140 'object_type' => ActivityObject::PERSON));
145 function onEndFavorNotice($profile, $notice)
147 // Only do this if config is enabled
148 if(!$this->StartLike) return true;
150 $user = $profile->getUser();
154 $author = $notice->getProfile();
155 $fave = Fave::pkeyGet(array('user_id' => $user->id,
156 'notice_id' => $notice->id));
158 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> liked <a href="%3$s">%4$s\'s update</a>.'),
159 $profile->profileurl,
160 $profile->getBestName(),
162 $author->getBestName());
163 $content = sprintf(_m('%1$s (%2$s) liked %3$s\'s status (%4$s)'),
164 $profile->getBestName(),
165 $profile->profileurl,
166 $author->getBestName(),
169 $notice = Notice::saveNew($user->id,
171 ActivityPlugin::SOURCE,
172 array('rendered' => $rendered,
174 'replies' => array($author->getUri()),
175 'uri' => $fave->getURI(),
176 'verb' => ActivityVerb::FAVORITE,
177 'object_type' => (($notice->verb == ActivityVerb::POST) ?
178 $notice->object_type : ActivityObject::ACTIVITY)));
183 function onEndDisfavorNotice($profile, $notice)
185 // Only do this if config is enabled
186 if(!$this->StopLike) return true;
187 $user = User::staticGet('id', $profile->id);
190 $author = Profile::staticGet('id', $notice->profile_id);
191 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> stopped liking <a href="%3$s">%4$s\'s update</a>.'),
192 $profile->profileurl,
193 $profile->getBestName(),
195 $author->getBestName());
196 $content = sprintf(_m('%1$s (%2$s) stopped liking %3$s\'s status (%4$s)'),
197 $profile->getBestName(),
198 $profile->profileurl,
199 $author->getBestName(),
202 $uri = TagURI::mint('unlike:%d:%d:%s',
205 common_date_iso8601(common_sql_now()));
207 $notice = Notice::saveNew($user->id,
209 ActivityPlugin::SOURCE,
210 array('rendered' => $rendered,
212 'replies' => array($author->getUri()),
214 'verb' => ActivityVerb::UNFAVORITE,
215 'object_type' => (($notice->verb == ActivityVerb::POST) ?
216 $notice->object_type : ActivityObject::ACTIVITY)));
221 function onEndJoinGroup($group, $profile)
223 // Only do this if config is enabled
224 if(!$this->JoinGroup) return true;
226 $user = $profile->getUser();
232 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> joined the group <a href="%3$s">%4$s</a>.'),
233 $profile->profileurl,
234 $profile->getBestName(),
236 $group->getBestName());
237 $content = sprintf(_m('%1$s (%2$s) joined the group %3$s (%4$s).'),
238 $profile->getBestName(),
239 $profile->profileurl,
240 $group->getBestName(),
243 $mem = Group_member::pkeyGet(array('group_id' => $group->id,
244 'profile_id' => $profile->id));
246 $notice = Notice::saveNew($user->id,
248 ActivityPlugin::SOURCE,
249 array('rendered' => $rendered,
251 'groups' => array($group->id),
252 'uri' => $mem->getURI(),
253 'verb' => ActivityVerb::JOIN,
254 'object_type' => ActivityObject::GROUP));
258 function onEndLeaveGroup($group, $profile)
260 // Only do this if config is enabled
261 if(!$this->LeaveGroup) return true;
263 $user = $profile->getUser();
269 $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'),
270 $profile->profileurl,
271 $profile->getBestName(),
273 $group->getBestName());
274 $content = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s)'),
275 $profile->getBestName(),
276 $profile->profileurl,
277 $group->getBestName(),
280 $uri = TagURI::mint('leave:%d:%d:%s',
283 common_date_iso8601(common_sql_now()));
285 $notice = Notice::saveNew($user->id,
287 ActivityPlugin::SOURCE,
288 array('rendered' => $rendered,
290 'groups' => array($group->id),
292 'verb' => ActivityVerb::LEAVE,
293 'object_type' => ActivityObject::GROUP));
297 function onStartShowNoticeItem($nli)
299 $notice = $nli->notice;
303 switch ($notice->verb) {
304 case ActivityVerb::FAVORITE:
305 case ActivityVerb::UNFAVORITE:
306 $adapter = new SystemListItem($nli);
308 case ActivityVerb::JOIN:
309 $adapter = new JoinListItem($nli);
311 case ActivityVerb::LEAVE:
312 $adapter = new JoinListItem($nli);
314 case ActivityVerb::FOLLOW:
315 $adapter = new FollowListItem($nli);
317 case ActivityVerb::UNFOLLOW:
318 $adapter = new UnfollowListItem($nli);
322 if (!empty($adapter)) {
323 $adapter->showNotice();
324 $adapter->showNoticeAttachments();
325 $adapter->showNoticeInfo();
326 $adapter->showNoticeOptions();
333 function onEndNoticeAsActivity($notice, &$activity)
335 switch ($notice->verb) {
336 case ActivityVerb::FAVORITE:
337 $fave = Fave::staticGet('uri', $notice->uri);
339 $notice = Notice::staticGet('id', $fave->notice_id);
340 if (!empty($notice)) {
341 $target = $notice->asActivity();
342 if ($target->verb == ActivityVerb::POST) {
343 // "I like the thing you posted"
344 $activity->objects = $target->objects;
346 // "I like that you did whatever you did"
347 $activity->objects = array($target);
352 case ActivityVerb::UNFAVORITE:
353 // FIXME: do something here
355 case ActivityVerb::JOIN:
356 $mem = Group_member::staticGet('uri', $notice->uri);
358 $group = $mem->getGroup();
359 $activity->objects = array(ActivityObject::fromGroup($group));
362 case ActivityVerb::LEAVE:
365 case ActivityVerb::FOLLOW:
366 $sub = Subscription::staticGet('uri', $notice->uri);
368 $profile = Profile::staticGet('id', $sub->subscribed);
369 if (!empty($profile)) {
370 $activity->objects = array(ActivityObject::fromProfile($profile));
374 case ActivityVerb::UNFOLLOW:
382 function onPluginVersion(&$versions)
384 $versions[] = array('name' => 'Activity',
385 'version' => self::VERSION,
386 'author' => 'Evan Prodromou',
387 'homepage' => 'http://status.net/wiki/Plugin:Activity',
389 // TRANS: Plugin description.
390 _m('Emits notices when social activities happen.'));