3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package OStatusPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET')) {
29 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/');
31 class FeedSubException extends Exception
33 function __construct($msg=null)
35 $type = get_class($this);
37 parent::__construct("$type: $msg");
39 parent::__construct($type);
44 class OStatusPlugin extends Plugin
47 * Hook for RouterInitialized event.
49 * @param Net_URL_Mapper $m path-to-action mapper
50 * @return boolean hook return
52 function onRouterInitialized($m)
55 $m->connect('main/ownerxrd',
56 array('action' => 'ownerxrd'));
57 $m->connect('main/ostatus',
58 array('action' => 'ostatusinit'));
59 $m->connect('main/ostatustag',
60 array('action' => 'ostatustag'));
61 $m->connect('main/ostatustag?nickname=:nickname',
62 array('action' => 'ostatustag'), array('nickname' => '[A-Za-z0-9_-]+'));
63 $m->connect('main/ostatus?nickname=:nickname',
64 array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
65 $m->connect('main/ostatus?group=:group',
66 array('action' => 'ostatusinit'), array('group' => '[A-Za-z0-9_-]+'));
67 $m->connect('main/ostatus?peopletag=:peopletag&tagger=:tagger',
68 array('action' => 'ostatusinit'), array('tagger' => '[A-Za-z0-9_-]+',
69 'peopletag' => '[A-Za-z0-9_-]+'));
71 // Remote subscription actions
72 $m->connect('main/ostatussub',
73 array('action' => 'ostatussub'));
74 $m->connect('main/ostatusgroup',
75 array('action' => 'ostatusgroup'));
76 $m->connect('main/ostatuspeopletag',
77 array('action' => 'ostatuspeopletag'));
80 $m->connect('main/push/hub', array('action' => 'pushhub'));
82 $m->connect('main/push/callback/:feed',
83 array('action' => 'pushcallback'),
84 array('feed' => '[0-9]+'));
87 $m->connect('main/salmon/user/:id',
88 array('action' => 'usersalmon'),
89 array('id' => '[0-9]+'));
90 $m->connect('main/salmon/group/:id',
91 array('action' => 'groupsalmon'),
92 array('id' => '[0-9]+'));
93 $m->connect('main/salmon/peopletag/:id',
94 array('action' => 'peopletagsalmon'),
95 array('id' => '[0-9]+'));
100 * Set up queue handlers for outgoing hub pushes
101 * @param QueueManager $qm
102 * @return boolean hook return
104 function onEndInitializeQueueManager(QueueManager $qm)
106 // Prepare outgoing distributions after notice save.
107 $qm->connect('ostatus', 'OStatusQueueHandler');
109 // Outgoing from our internal PuSH hub
110 $qm->connect('hubconf', 'HubConfQueueHandler');
111 $qm->connect('hubprep', 'HubPrepQueueHandler');
113 $qm->connect('hubout', 'HubOutQueueHandler');
115 // Outgoing Salmon replies (when we don't need a return value)
116 $qm->connect('salmon', 'SalmonQueueHandler');
118 // Incoming from a foreign PuSH hub
119 $qm->connect('pushin', 'PushInQueueHandler');
124 * Put saved notices into the queue for pubsub distribution.
126 function onStartEnqueueNotice($notice, &$transports)
128 if ($notice->isLocal()) {
129 if ($notice->inScope(null)) {
130 // put our transport first, in case there's any conflict (like OMB)
131 array_unshift($transports, 'ostatus');
132 $this->log(LOG_INFO, "Notice {$notice->id} queued for OStatus processing");
134 // FIXME: we don't do privacy-controlled OStatus updates yet.
135 // once that happens, finer grain of control here.
136 $this->log(LOG_NOTICE, "Not queueing notice {$notice->id} for OStatus because of privacy; scope = {$notice->scope}");
139 $this->log(LOG_NOTICE, "Not queueing notice {$notice->id} for OStatus because it's not local.");
145 * Add a link header for LRDD Discovery
147 function onStartShowHTML($action)
149 if ($action instanceof ShowstreamAction) {
150 $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
151 $url = common_local_url('userxrd');
152 $url.= '?uri='. $acct;
154 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="application/xrd+xml"');
159 * Set up a PuSH hub link to our internal link for canonical timeline
160 * Atom feeds for users and groups.
162 function onStartApiAtom($feed)
166 if ($feed instanceof AtomUserNoticeFeed) {
167 $salmonAction = 'usersalmon';
168 $user = $feed->getUser();
170 $profile = $user->getProfile();
171 } else if ($feed instanceof AtomGroupNoticeFeed) {
172 $salmonAction = 'groupsalmon';
173 $group = $feed->getGroup();
175 } else if ($feed instanceof AtomListNoticeFeed) {
176 $salmonAction = 'peopletagsalmon';
177 $peopletag = $feed->getList();
178 $id = $peopletag->id;
184 $hub = common_config('ostatus', 'hub');
186 // Updates will be handled through our internal PuSH hub.
187 $hub = common_local_url('pushhub');
189 $feed->addLink($hub, array('rel' => 'hub'));
191 // Also, we'll add in the salmon link
192 $salmon = common_local_url($salmonAction, array('id' => $id));
193 $feed->addLink($salmon, array('rel' => Salmon::REL_SALMON));
195 // XXX: these are deprecated
196 $feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
197 $feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
204 * Automatically load the actions and libraries used by the plugin
206 * @param Class $cls the class
208 * @return boolean hook return
211 function onAutoload($cls)
213 $base = dirname(__FILE__);
214 $lower = strtolower($cls);
215 $map = array('activityverb' => 'activity',
216 'activityobject' => 'activity',
217 'activityutils' => 'activity');
218 if (isset($map[$lower])) {
219 $lower = $map[$lower];
221 $files = array("$base/classes/$cls.php",
222 "$base/lib/$lower.php");
223 if (substr($lower, -6) == 'action') {
224 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
226 foreach ($files as $file) {
227 if (file_exists($file)) {
236 * Add in an OStatus subscribe button
238 function onStartProfileRemoteSubscribe($output, $profile)
240 $this->onStartProfileListItemActionElements($output, $profile);
244 function onStartGroupSubscribe($widget, $group)
246 $cur = common_current_user();
249 $widget->out->elementStart('li', 'entity_subscribe');
250 $profile = $peopletag->getTagger();
251 $url = common_local_url('ostatusinit',
252 array('group' => $group->nickname));
253 $widget->out->element('a', array('href' => $url,
254 'class' => 'entity_remote_subscribe'),
255 // TRANS: Link to subscribe to a remote entity.
258 $widget->out->elementEnd('li');
265 function onStartSubscribePeopletagForm($output, $peopletag)
267 $cur = common_current_user();
270 $output->elementStart('li', 'entity_subscribe');
271 $profile = $peopletag->getTagger();
272 $url = common_local_url('ostatusinit',
273 array('tagger' => $profile->nickname, 'peopletag' => $peopletag->tag));
274 $output->element('a', array('href' => $url,
275 'class' => 'entity_remote_subscribe'),
276 // TRANS: Link to subscribe to a remote entity.
279 $output->elementEnd('li');
286 function onStartShowTagProfileForm($action, $profile)
288 $action->elementStart('form', array('method' => 'post',
289 'id' => 'form_tag_user',
290 'class' => 'form_settings',
291 'name' => 'tagprofile',
292 'action' => common_local_url('tagprofile', array('id' => @$profile->id))));
294 $action->elementStart('fieldset');
295 // TRANS: Fieldset legend.
296 $action->element('legend', null, _m('List remote profile'));
297 $action->hidden('token', common_session_token());
299 $user = common_current_user();
301 $action->elementStart('ul', 'form_data');
302 $action->elementStart('li');
304 // TRANS: Field label.
305 $action->input('uri', _m('LABEL','Remote profile'), $action->trimmed('uri'),
306 // TRANS: Field title.
307 _m('OStatus user\'s address, like nickname@example.com or http://example.net/nickname.'));
308 $action->elementEnd('li');
309 $action->elementEnd('ul');
310 // TRANS: Button text to fetch remote profile.
311 $action->submit('fetch', _m('BUTTON','Fetch'));
312 $action->elementEnd('fieldset');
313 $action->elementEnd('form');
316 function onStartTagProfileAction($action, $profile)
319 $uri = $action->trimmed('uri');
321 if (!$profile && $uri) {
323 if (Validate::email($uri)) {
324 $oprofile = Ostatus_profile::ensureWebfinger($uri);
325 } else if (Validate::uri($uri)) {
326 $oprofile = Ostatus_profile::ensureProfileURL($uri);
328 // TRANS: Exception in OStatus when invalid URI was entered.
329 throw new Exception(_m('Invalid URI.'));
332 // redirect to the new profile.
333 common_redirect(common_local_url('tagprofile', array('id' => $oprofile->profile_id)), 303);
336 } catch (Exception $e) {
337 // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
338 // TRANS: and example.net, as these are official standard domain names for use in examples.
339 $err = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
342 $action->showForm($err);
349 * If the field being looked for is URI look for the profile
351 function onStartProfileCompletionSearch($action, $profile, $search_engine) {
352 if ($action->field == 'uri') {
353 $profile->joinAdd(array('id', 'user:id'));
354 $profile->whereAdd('uri LIKE "%' . $profile->escape($q) . '%"');
357 if ($profile->N == 0) {
359 if (Validate::email($q)) {
360 $oprofile = Ostatus_profile::ensureWebfinger($q);
361 } else if (Validate::uri($q)) {
362 $oprofile = Ostatus_profile::ensureProfileURL($q);
364 // TRANS: Exception in OStatus when invalid URI was entered.
365 throw new Exception(_m('Invalid URI.'));
367 return $this->filter(array($oprofile->localProfile()));
369 } catch (Exception $e) {
370 // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
371 // TRANS: and example.net, as these are official standard domain names for use in examples.
372 $this->msg = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
382 * Find any explicit remote mentions. Accepted forms:
383 * Webfinger: @user@example.com
384 * Profile link: @example.com/mublog/user
385 * @param Profile $sender (os user?)
386 * @param string $text input markup text
387 * @param array &$mention in/out param: set of found mentions
388 * @return boolean hook return value
390 function onEndFindMentions($sender, $text, &$mentions)
394 // Webfinger matches: @user@example.com
395 if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\-?\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
398 PREG_OFFSET_CAPTURE)) {
399 foreach ($wmatches[1] as $wmatch) {
400 list($target, $pos) = $wmatch;
401 $this->log(LOG_INFO, "Checking webfinger '$target'");
403 $oprofile = Ostatus_profile::ensureWebfinger($target);
404 if ($oprofile && !$oprofile->isGroup()) {
405 $profile = $oprofile->localProfile();
406 $matches[$pos] = array('mentioned' => array($profile),
409 'url' => $profile->profileurl);
411 } catch (Exception $e) {
412 $this->log(LOG_ERR, "Webfinger check failed: " . $e->getMessage());
417 // Profile matches: @example.com/mublog/user
418 if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)!',
421 PREG_OFFSET_CAPTURE)) {
422 foreach ($wmatches[1] as $wmatch) {
423 list($target, $pos) = $wmatch;
424 $schemes = array('http', 'https');
425 foreach ($schemes as $scheme) {
426 $url = "$scheme://$target";
427 $this->log(LOG_INFO, "Checking profile address '$url'");
429 $oprofile = Ostatus_profile::ensureProfileURL($url);
430 if ($oprofile && !$oprofile->isGroup()) {
431 $profile = $oprofile->localProfile();
432 $matches[$pos] = array('mentioned' => array($profile),
435 'url' => $profile->profileurl);
438 } catch (Exception $e) {
439 $this->log(LOG_ERR, "Profile check failed: " . $e->getMessage());
445 foreach ($mentions as $i => $other) {
446 // If we share a common prefix with a local user, override it!
447 $pos = $other['position'];
448 if (isset($matches[$pos])) {
449 $mentions[$i] = $matches[$pos];
450 unset($matches[$pos]);
453 foreach ($matches as $mention) {
454 $mentions[] = $mention;
461 * Allow remote profile references to be used in commands:
462 * sub update@status.net
463 * whois evan@identi.ca
464 * reply http://identi.ca/evan hey what's up
466 * @param Command $command
468 * @param Profile &$profile
469 * @return hook return code
471 function onStartCommandGetProfile($command, $arg, &$profile)
473 $oprofile = $this->pullRemoteProfile($arg);
474 if ($oprofile && !$oprofile->isGroup()) {
475 $profile = $oprofile->localProfile();
483 * Allow remote group references to be used in commands:
484 * join group+statusnet@identi.ca
485 * join http://identi.ca/group/statusnet
486 * drop identi.ca/group/statusnet
488 * @param Command $command
490 * @param User_group &$group
491 * @return hook return code
493 function onStartCommandGetGroup($command, $arg, &$group)
495 $oprofile = $this->pullRemoteProfile($arg);
496 if ($oprofile && $oprofile->isGroup()) {
497 $group = $oprofile->localGroup();
504 protected function pullRemoteProfile($arg)
507 if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) {
510 return Ostatus_profile::ensureWebfinger($arg);
511 } catch (Exception $e) {
512 common_log(LOG_ERR, 'Webfinger lookup failed for ' .
513 $arg . ': ' . $e->getMessage());
517 // Look for profile URLs, with or without scheme:
519 if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
522 if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
523 $schemes = array('http', 'https');
524 foreach ($schemes as $scheme) {
525 $urls[] = "$scheme://$arg";
529 foreach ($urls as $url) {
531 return Ostatus_profile::ensureProfileURL($url);
532 } catch (Exception $e) {
533 common_log(LOG_ERR, 'Profile lookup failed for ' .
534 $arg . ': ' . $e->getMessage());
541 * Make sure necessary tables are filled out.
543 function onCheckSchema() {
544 $schema = Schema::get();
545 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
546 $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
547 $schema->ensureTable('feedsub', FeedSub::schemaDef());
548 $schema->ensureTable('hubsub', HubSub::schemaDef());
549 $schema->ensureTable('magicsig', Magicsig::schemaDef());
553 function onEndShowStatusNetStyles($action) {
554 $action->cssLink($this->path('theme/base/css/ostatus.css'));
558 function onEndShowStatusNetScripts($action) {
559 $action->script($this->path('js/ostatus.js'));
564 * Override the "from ostatus" bit in notice lists to link to the
565 * original post and show the domain it came from.
567 * @param Notice in $notice
568 * @param string out &$name
569 * @param string out &$url
570 * @param string out &$title
571 * @return mixed hook return code
573 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
575 if ($notice->source == 'ostatus') {
577 $bits = parse_url($notice->url);
578 $domain = $bits['host'];
579 if (substr($domain, 0, 4) == 'www.') {
580 $name = substr($domain, 4);
586 // TRANS: Title. %s is a domain name.
587 $title = sprintf(_m('Sent from %s via OStatus'), $domain);
595 * Send incoming PuSH feeds for OStatus endpoints in for processing.
597 * @param FeedSub $feedsub
598 * @param DOMDocument $feed
599 * @return mixed hook return code
601 function onStartFeedSubReceive($feedsub, $feed)
603 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
605 $oprofile->processFeed($feed, 'push');
607 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
612 * Tell the FeedSub infrastructure whether we have any active OStatus
613 * usage for the feed; if not it'll be able to garbage-collect the
616 * @param FeedSub $feedsub
617 * @param integer $count in/out
618 * @return mixed hook return code
620 function onFeedSubSubscriberCount($feedsub, &$count)
622 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
624 $count += $oprofile->subscriberCount();
630 * When about to subscribe to a remote user, start a server-to-server
631 * PuSH subscription if needed. If we can't establish that, abort.
633 * @fixme If something else aborts later, we could end up with a stray
634 * PuSH subscription. This is relatively harmless, though.
636 * @param Profile $subscriber
637 * @param Profile $other
639 * @return hook return code
643 function onStartSubscribe($subscriber, $other)
645 $user = User::staticGet('id', $subscriber->id);
651 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
653 if (empty($oprofile)) {
657 if (!$oprofile->subscribe()) {
658 // TRANS: Exception thrown when setup of remote subscription fails.
659 throw new Exception(_m('Could not set up remote subscription.'));
664 * Having established a remote subscription, send a notification to the
665 * remote OStatus profile's endpoint.
667 * @param Profile $subscriber
668 * @param Profile $other
670 * @return hook return code
674 function onEndSubscribe($subscriber, $other)
676 $user = User::staticGet('id', $subscriber->id);
682 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
684 if (empty($oprofile)) {
688 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
689 'subscribed' => $other->id));
691 $act = $sub->asActivity();
693 $oprofile->notifyActivity($act, $subscriber);
699 * Notify remote server and garbage collect unused feeds on unsubscribe.
700 * @todo FIXME: Send these operations to background queues
703 * @param Profile $other
704 * @return hook return value
706 function onEndUnsubscribe($profile, $other)
708 $user = User::staticGet('id', $profile->id);
714 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
716 if (empty($oprofile)) {
720 // Drop the PuSH subscription if there are no other subscribers.
721 $oprofile->garbageCollect();
723 $act = new Activity();
725 $act->verb = ActivityVerb::UNFOLLOW;
727 $act->id = TagURI::mint('unfollow:%d:%d:%s',
730 common_date_iso8601(time()));
733 // TRANS: Title for unfollowing a remote profile.
734 $act->title = _m('TITLE','Unfollow');
735 // TRANS: Success message for unsubscribe from user attempt through OStatus.
736 // TRANS: %1$s is the unsubscriber's name, %2$s is the unsubscribed user's name.
737 $act->content = sprintf(_m('%1$s stopped following %2$s.'),
738 $profile->getBestName(),
739 $other->getBestName());
741 $act->actor = ActivityObject::fromProfile($profile);
742 $act->object = ActivityObject::fromProfile($other);
744 $oprofile->notifyActivity($act, $profile);
750 * When one of our local users tries to join a remote group,
751 * notify the remote server. If the notification is rejected,
754 * @param User_group $group
757 * @return mixed hook return value
759 function onStartJoinGroup($group, $user)
761 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
763 if (!$oprofile->subscribe()) {
764 // TRANS: Exception thrown when setup of remote group membership fails.
765 throw new Exception(_m('Could not set up remote group membership.'));
768 // NOTE: we don't use Group_member::asActivity() since that record
769 // has not yet been created.
771 $member = Profile::staticGet($user->id);
773 $act = new Activity();
774 $act->id = TagURI::mint('join:%d:%d:%s',
777 common_date_iso8601(time()));
779 $act->actor = ActivityObject::fromProfile($member);
780 $act->verb = ActivityVerb::JOIN;
781 $act->object = $oprofile->asActivityObject();
784 // TRANS: Title for joining a remote groep.
785 $act->title = _m('TITLE','Join');
786 // TRANS: Success message for subscribe to group attempt through OStatus.
787 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
788 $act->content = sprintf(_m('%1$s has joined group %2$s.'),
789 $member->getBestName(),
790 $oprofile->getBestName());
792 if ($oprofile->notifyActivity($act, $member)) {
795 $oprofile->garbageCollect();
796 // TRANS: Exception thrown when joining a remote group fails.
797 throw new Exception(_m('Failed joining remote group.'));
803 * When one of our local users leaves a remote group, notify the remote
806 * @fixme Might be good to schedule a resend of the leave notification
807 * if it failed due to a transitory error. We've canceled the local
808 * membership already anyway, but if the remote server comes back up
809 * it'll be left with a stray membership record.
811 * @param User_group $group
812 * @param Profile $user
814 * @return mixed hook return value
816 function onEndLeaveGroup($group, $user)
818 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
820 // Drop the PuSH subscription if there are no other subscribers.
821 $oprofile->garbageCollect();
823 $member = Profile::staticGet($user->id);
825 $act = new Activity();
826 $act->id = TagURI::mint('leave:%d:%d:%s',
829 common_date_iso8601(time()));
831 $act->actor = ActivityObject::fromProfile($member);
832 $act->verb = ActivityVerb::LEAVE;
833 $act->object = $oprofile->asActivityObject();
836 // TRANS: Title for leaving a remote group.
837 $act->title = _m('TITLE','Leave');
838 // TRANS: Success message for unsubscribe from group attempt through OStatus.
839 // TRANS: %1$s is the member name, %2$s is the unsubscribed group's name.
840 $act->content = sprintf(_m('%1$s has left group %2$s.'),
841 $member->getBestName(),
842 $oprofile->getBestName());
844 $oprofile->notifyActivity($act, $member);
849 * When one of our local users tries to subscribe to a remote peopletag,
850 * notify the remote server. If the notification is rejected,
851 * deny the subscription.
853 * @param Profile_list $peopletag
856 * @return mixed hook return value
859 function onStartSubscribePeopletag($peopletag, $user)
861 $oprofile = Ostatus_profile::staticGet('peopletag_id', $peopletag->id);
863 if (!$oprofile->subscribe()) {
864 // TRANS: Exception thrown when setup of remote list subscription fails.
865 throw new Exception(_m('Could not set up remote list subscription.'));
868 $sub = $user->getProfile();
869 $tagger = Profile::staticGet($peopletag->tagger);
871 $act = new Activity();
872 $act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s',
875 common_date_iso8601(time()));
877 $act->actor = ActivityObject::fromProfile($sub);
878 $act->verb = ActivityVerb::FOLLOW;
879 $act->object = $oprofile->asActivityObject();
882 // TRANS: Title for following a remote list.
883 $act->title = _m('TITLE','Follow list');
884 // TRANS: Success message for remote list follow through OStatus.
885 // TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
886 $act->content = sprintf(_m('%1$s is now following people listed in %2$s by %3$s.'),
888 $oprofile->getBestName(),
889 $tagger->getBestName());
891 if ($oprofile->notifyActivity($act, $sub)) {
894 $oprofile->garbageCollect();
895 // TRANS: Exception thrown when subscription to remote list fails.
896 throw new Exception(_m('Failed subscribing to remote list.'));
902 * When one of our local users unsubscribes to a remote peopletag, notify the remote
905 * @param Profile_list $peopletag
908 * @return mixed hook return value
911 function onEndUnsubscribePeopletag($peopletag, $user)
913 $oprofile = Ostatus_profile::staticGet('peopletag_id', $peopletag->id);
915 // Drop the PuSH subscription if there are no other subscribers.
916 $oprofile->garbageCollect();
918 $sub = Profile::staticGet($user->id);
919 $tagger = Profile::staticGet($peopletag->tagger);
921 $act = new Activity();
922 $act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
925 common_date_iso8601(time()));
927 $act->actor = ActivityObject::fromProfile($member);
928 $act->verb = ActivityVerb::UNFOLLOW;
929 $act->object = $oprofile->asActivityObject();
932 // TRANS: Title for unfollowing a remote list.
933 $act->title = _m('Unfollow list');
934 // TRANS: Success message for remote list unfollow through OStatus.
935 // TRANS: %1$s is the subscriber name, %2$s is the list, %3$s is the lister's name.
936 $act->content = sprintf(_m('%1$s stopped following the list %2$s by %3$s.'),
938 $oprofile->getBestName(),
939 $tagger->getBestName());
941 $oprofile->notifyActivity($act, $user);
946 * Notify remote users when their notices get favorited.
948 * @param Profile or User $profile of local user doing the faving
949 * @param Notice $notice being favored
950 * @return hook return value
952 function onEndFavorNotice(Profile $profile, Notice $notice)
954 $user = User::staticGet('id', $profile->id);
960 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
962 if (empty($oprofile)) {
966 $fav = Fave::pkeyGet(array('user_id' => $user->id,
967 'notice_id' => $notice->id));
974 $act = $fav->asActivity();
976 $oprofile->notifyActivity($act, $profile);
982 * Notify remote user it has got a new people tag
983 * - tag verb is queued
984 * - the subscription is done immediately if not present
986 * @param Profile_tag $ptag the people tag that was created
987 * @return hook return value
989 function onEndTagProfile($ptag)
991 $oprofile = Ostatus_profile::staticGet('profile_id', $ptag->tagged);
993 if (empty($oprofile)) {
997 $plist = $ptag->getMeta();
998 if ($plist->private) {
1002 $act = new Activity();
1004 $tagger = $plist->getTagger();
1005 $tagged = Profile::staticGet('id', $ptag->tagged);
1007 $act->verb = ActivityVerb::TAG;
1008 $act->id = TagURI::mint('tag_profile:%d:%d:%s',
1009 $plist->tagger, $plist->id,
1010 common_date_iso8601(time()));
1011 $act->time = time();
1012 // TRANS: Title for listing a remote profile.
1013 $act->title = _m('TITLE','List');
1014 // TRANS: Success message for remote list addition through OStatus.
1015 // TRANS: %1$s is the list creator's name, %2$s is the added list member, %3$s is the list name.
1016 $act->content = sprintf(_m('%1$s listed %2$s in the list %3$s.'),
1017 $tagger->getBestName(),
1018 $tagged->getBestName(),
1019 $plist->getBestName());
1021 $act->actor = ActivityObject::fromProfile($tagger);
1022 $act->objects = array(ActivityObject::fromProfile($tagged));
1023 $act->target = ActivityObject::fromPeopletag($plist);
1025 $oprofile->notifyDeferred($act, $tagger);
1027 // initiate a PuSH subscription for the person being tagged
1028 if (!$oprofile->subscribe()) {
1029 // TRANS: Exception thrown when subscribing to a remote list fails.
1030 throw new Exception(sprintf(_m('Could not complete subscription to remote '.
1031 'profile\'s feed. List %s could not be saved.'), $ptag->tag));
1038 * Notify remote user that a people tag has been removed
1039 * - untag verb is queued
1040 * - the subscription is undone immediately if not required
1041 * i.e garbageCollect()'d
1043 * @param Profile_tag $ptag the people tag that was deleted
1044 * @return hook return value
1046 function onEndUntagProfile($ptag)
1048 $oprofile = Ostatus_profile::staticGet('profile_id', $ptag->tagged);
1050 if (empty($oprofile)) {
1054 $plist = $ptag->getMeta();
1055 if ($plist->private) {
1059 $act = new Activity();
1061 $tagger = $plist->getTagger();
1062 $tagged = Profile::staticGet('id', $ptag->tagged);
1064 $act->verb = ActivityVerb::UNTAG;
1065 $act->id = TagURI::mint('untag_profile:%d:%d:%s',
1066 $plist->tagger, $plist->id,
1067 common_date_iso8601(time()));
1068 $act->time = time();
1069 // TRANS: Title for unlisting a remote profile.
1070 $act->title = _m('TITLE','Unlist');
1071 // TRANS: Success message for remote list removal through OStatus.
1072 // TRANS: %1$s is the list creator's name, %2$s is the removed list member, %3$s is the list name.
1073 $act->content = sprintf(_m('%1$s removed %2$s from the list %3$s.'),
1074 $tagger->getBestName(),
1075 $tagged->getBestName(),
1076 $plist->getBestName());
1078 $act->actor = ActivityObject::fromProfile($tagger);
1079 $act->objects = array(ActivityObject::fromProfile($tagged));
1080 $act->target = ActivityObject::fromPeopletag($plist);
1082 $oprofile->notifyDeferred($act, $tagger);
1084 // unsubscribe to PuSH feed if no more required
1085 $oprofile->garbageCollect();
1091 * Notify remote users when their notices get de-favorited.
1093 * @param Profile $profile Profile person doing the de-faving
1094 * @param Notice $notice Notice being favored
1096 * @return hook return value
1098 function onEndDisfavorNotice(Profile $profile, Notice $notice)
1100 $user = User::staticGet('id', $profile->id);
1106 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
1108 if (empty($oprofile)) {
1112 $act = new Activity();
1114 $act->verb = ActivityVerb::UNFAVORITE;
1115 $act->id = TagURI::mint('disfavor:%d:%d:%s',
1118 common_date_iso8601(time()));
1119 $act->time = time();
1120 // TRANS: Title for unliking a remote notice.
1121 $act->title = _m('Unlike');
1122 // TRANS: Success message for remove a favorite notice through OStatus.
1123 // TRANS: %1$s is the unfavoring user's name, %2$s is URI to the no longer favored notice.
1124 $act->content = sprintf(_m('%1$s no longer likes %2$s.'),
1125 $profile->getBestName(),
1128 $act->actor = ActivityObject::fromProfile($profile);
1129 $act->object = ActivityObject::fromNotice($notice);
1131 $oprofile->notifyActivity($act, $profile);
1136 function onStartGetProfileUri($profile, &$uri)
1138 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
1139 if (!empty($oprofile)) {
1140 $uri = $oprofile->uri;
1146 function onStartUserGroupHomeUrl($group, &$url)
1148 return $this->onStartUserGroupPermalink($group, $url);
1151 function onStartUserGroupPermalink($group, &$url)
1153 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
1155 // @fixme this should probably be in the user_group table
1156 // @fixme this uri not guaranteed to be a profile page
1157 $url = $oprofile->uri;
1162 function onStartShowSubscriptionsContent($action)
1164 $this->showEntityRemoteSubscribe($action);
1169 function onStartShowUserGroupsContent($action)
1171 $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
1176 function onEndShowSubscriptionsMiniList($action)
1178 $this->showEntityRemoteSubscribe($action);
1183 function onEndShowGroupsMiniList($action)
1185 $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
1190 function showEntityRemoteSubscribe($action, $target='ostatussub')
1192 $user = common_current_user();
1193 if ($user && ($user->id == $action->profile->id)) {
1194 $action->elementStart('div', 'entity_actions');
1195 $action->elementStart('p', array('id' => 'entity_remote_subscribe',
1196 'class' => 'entity_subscribe'));
1197 $action->element('a', array('href' => common_local_url($target),
1198 'class' => 'entity_remote_subscribe'),
1199 // TRANS: Link text for link to remote subscribe.
1201 $action->elementEnd('p');
1202 $action->elementEnd('div');
1207 * Ping remote profiles with updates to this profile.
1208 * Salmon pings are queued for background processing.
1210 function onEndBroadcastProfile(Profile $profile)
1212 $user = User::staticGet('id', $profile->id);
1214 // Find foreign accounts I'm subscribed to that support Salmon pings.
1216 // @fixme we could run updates through the PuSH feed too,
1217 // in which case we can skip Salmon pings to folks who
1218 // are also subscribed to me.
1219 $sql = "SELECT * FROM ostatus_profile " .
1220 "WHERE profile_id IN " .
1221 "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
1223 "(SELECT group_id FROM group_member WHERE profile_id=%d)";
1224 $oprofile = new Ostatus_profile();
1225 $oprofile->query(sprintf($sql, $profile->id, $profile->id));
1227 if ($oprofile->N == 0) {
1228 common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
1232 $act = new Activity();
1234 $act->verb = ActivityVerb::UPDATE_PROFILE;
1235 $act->id = TagURI::mint('update-profile:%d:%s',
1237 common_date_iso8601(time()));
1238 $act->time = time();
1239 // TRANS: Title for activity.
1240 $act->title = _m('Profile update');
1241 // TRANS: Ping text for remote profile update through OStatus.
1242 // TRANS: %s is user that updated their profile.
1243 $act->content = sprintf(_m('%s has updated their profile page.'),
1244 $profile->getBestName());
1246 $act->actor = ActivityObject::fromProfile($profile);
1247 $act->object = $act->actor;
1249 while ($oprofile->fetch()) {
1250 $oprofile->notifyDeferred($act, $profile);
1256 function onStartProfileListItemActionElements($item, $profile=null)
1258 if (!common_logged_in()) {
1260 $profileUser = User::staticGet('id', $item->profile->id);
1262 if (!empty($profileUser)) {
1264 if ($item instanceof Action) {
1266 $profile = $item->profile;
1268 $output = $item->out;
1271 // Add an OStatus subscribe
1272 $output->elementStart('li', 'entity_subscribe');
1273 $url = common_local_url('ostatusinit',
1274 array('nickname' => $profileUser->nickname));
1275 $output->element('a', array('href' => $url,
1276 'class' => 'entity_remote_subscribe'),
1277 // TRANS: Link text for a user to subscribe to an OStatus user.
1279 $output->elementEnd('li');
1281 $output->elementStart('li', 'entity_tag');
1282 $url = common_local_url('ostatustag',
1283 array('nickname' => $profileUser->nickname));
1284 $output->element('a', array('href' => $url,
1285 'class' => 'entity_remote_tag'),
1286 // TRANS: Link text for a user to list an OStatus user.
1288 $output->elementEnd('li');
1295 function onPluginVersion(&$versions)
1297 $versions[] = array('name' => 'OStatus',
1298 'version' => STATUSNET_VERSION,
1299 'author' => 'Evan Prodromou, James Walker, Brion Vibber, Zach Copley',
1300 'homepage' => 'http://status.net/wiki/Plugin:OStatus',
1301 // TRANS: Plugin description.
1302 'rawdescription' => _m('Follow people across social networks that implement '.
1303 '<a href="http://ostatus.org/">OStatus</a>.'));
1309 * Utility function to check if the given URI is a canonical group profile
1310 * page, and if so return the ID number.
1312 * @param string $url
1313 * @return mixed int or false
1315 public static function localGroupFromUrl($url)
1317 $group = User_group::staticGet('uri', $url);
1319 $local = Local_group::staticGet('group_id', $group->id);
1324 // To find local groups which haven't had their uri fields filled out...
1325 // If the domain has changed since a subscriber got the URI, it'll
1327 $template = common_local_url('groupbyid', array('id' => '31337'));
1328 $template = preg_quote($template, '/');
1329 $template = str_replace('31337', '(\d+)', $template);
1330 if (preg_match("/$template/", $url, $matches)) {
1331 return intval($matches[1]);
1337 public function onStartProfileGetAtomFeed($profile, &$feed)
1339 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
1341 if (empty($oprofile)) {
1345 $feed = $oprofile->feeduri;
1349 function onStartGetProfileFromURI($uri, &$profile)
1351 // Don't want to do Web-based discovery on our own server,
1352 // so we check locally first.
1354 $user = User::staticGet('uri', $uri);
1356 if (!empty($user)) {
1357 $profile = $user->getProfile();
1361 // Now, check remotely
1363 $oprofile = Ostatus_profile::ensureProfileURI($uri);
1365 if (!empty($oprofile)) {
1366 $profile = $oprofile->localProfile();
1370 // Still not a hit, so give up.
1375 function onEndXrdActionLinks(&$xrd, $user)
1377 $xrd->links[] = array('rel' => Discovery::UPDATESFROM,
1378 'href' => common_local_url('ApiTimelineUser',
1379 array('id' => $user->id,
1380 'format' => 'atom')),
1381 'type' => 'application/atom+xml');
1384 $salmon_url = common_local_url('usersalmon',
1385 array('id' => $user->id));
1387 $xrd->links[] = array('rel' => Salmon::REL_SALMON,
1388 'href' => $salmon_url);
1389 // XXX : Deprecated - to be removed.
1390 $xrd->links[] = array('rel' => Salmon::NS_REPLIES,
1391 'href' => $salmon_url);
1393 $xrd->links[] = array('rel' => Salmon::NS_MENTIONS,
1394 'href' => $salmon_url);
1396 // Get this user's keypair
1397 $magickey = Magicsig::staticGet('user_id', $user->id);
1399 // No keypair yet, let's generate one.
1400 $magickey = new Magicsig();
1401 $magickey->generate($user->id);
1404 $xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,
1405 'href' => 'data:application/magic-public-key,'. $magickey->toString(false));
1407 // TODO - finalize where the redirect should go on the publisher
1408 $url = common_local_url('ostatussub') . '?profile={uri}';
1409 $xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe',
1410 'template' => $url );