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 // put our transport first, in case there's any conflict (like OMB)
130 array_unshift($transports, 'ostatus');
136 * Add a link header for LRDD Discovery
138 function onStartShowHTML($action)
140 if ($action instanceof ShowstreamAction) {
141 $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
142 $url = common_local_url('userxrd');
143 $url.= '?uri='. $acct;
145 header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="application/xrd+xml"');
150 * Set up a PuSH hub link to our internal link for canonical timeline
151 * Atom feeds for users and groups.
153 function onStartApiAtom($feed)
157 if ($feed instanceof AtomUserNoticeFeed) {
158 $salmonAction = 'usersalmon';
159 $user = $feed->getUser();
161 $profile = $user->getProfile();
162 } else if ($feed instanceof AtomGroupNoticeFeed) {
163 $salmonAction = 'groupsalmon';
164 $group = $feed->getGroup();
166 } else if ($feed instanceof AtomListNoticeFeed) {
167 $salmonAction = 'peopletagsalmon';
168 $peopletag = $feed->getList();
169 $id = $peopletag->id;
175 $hub = common_config('ostatus', 'hub');
177 // Updates will be handled through our internal PuSH hub.
178 $hub = common_local_url('pushhub');
180 $feed->addLink($hub, array('rel' => 'hub'));
182 // Also, we'll add in the salmon link
183 $salmon = common_local_url($salmonAction, array('id' => $id));
184 $feed->addLink($salmon, array('rel' => Salmon::REL_SALMON));
186 // XXX: these are deprecated
187 $feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
188 $feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
195 * Automatically load the actions and libraries used by the plugin
197 * @param Class $cls the class
199 * @return boolean hook return
202 function onAutoload($cls)
204 $base = dirname(__FILE__);
205 $lower = strtolower($cls);
206 $map = array('activityverb' => 'activity',
207 'activityobject' => 'activity',
208 'activityutils' => 'activity');
209 if (isset($map[$lower])) {
210 $lower = $map[$lower];
212 $files = array("$base/classes/$cls.php",
213 "$base/lib/$lower.php");
214 if (substr($lower, -6) == 'action') {
215 $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
217 foreach ($files as $file) {
218 if (file_exists($file)) {
227 * Add in an OStatus subscribe button
229 function onStartProfileRemoteSubscribe($output, $profile)
231 $this->onStartProfileListItemActionElements($output, $profile);
235 function onStartGroupSubscribe($output, $group)
237 $cur = common_current_user();
240 // Add an OStatus subscribe
241 $url = common_local_url('ostatusinit',
242 array('group' => $group->nickname));
243 $output->element('a', array('href' => $url,
244 'class' => 'entity_remote_subscribe'),
252 function onStartSubscribePeopletagForm($output, $peopletag)
254 $cur = common_current_user();
257 $output->elementStart('li', 'entity_subscribe');
258 $profile = $peopletag->getTagger();
259 $url = common_local_url('ostatusinit',
260 array('tagger' => $profile->nickname, 'peopletag' => $peopletag->tag));
261 $output->element('a', array('href' => $url,
262 'class' => 'entity_remote_subscribe'),
265 $output->elementEnd('li');
272 function onStartShowTagProfileForm($action, $profile)
274 $action->elementStart('form', array('method' => 'post',
275 'id' => 'form_tag_user',
276 'class' => 'form_settings',
277 'name' => 'tagprofile',
278 'action' => common_local_url('tagprofile', array('id' => @$profile->id))));
280 $action->elementStart('fieldset');
281 $action->element('legend', null, _('Tag remote profile'));
282 $action->hidden('token', common_session_token());
284 $user = common_current_user();
286 $action->elementStart('ul', 'form_data');
287 $action->elementStart('li');
289 $action->input('uri', _('Remote profile'), $action->trimmed('uri'),
290 _('OStatus user\'s address, like nickname@example.com or http://example.net/nickname'));
291 $action->elementEnd('li');
292 $action->elementEnd('ul');
293 $action->submit('fetch', _('Fetch'));
294 $action->elementEnd('fieldset');
295 $action->elementEnd('form');
298 function onStartTagProfileAction($action, $profile)
301 $uri = $action->trimmed('uri');
303 if (!$profile && $uri) {
305 if (Validate::email($uri)) {
306 $oprofile = Ostatus_profile::ensureWebfinger($uri);
307 } else if (Validate::uri($uri)) {
308 $oprofile = Ostatus_profile::ensureProfileURL($uri);
310 throw new Exception('Invalid URI');
313 // redirect to the new profile.
314 common_redirect(common_local_url('tagprofile', array('id' => $oprofile->profile_id)), 303);
317 } catch (Exception $e) {
318 $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");
321 $action->showForm($err);
328 * If the field being looked for is URI look for the profile
330 function onStartProfileCompletionSearch($action, $profile, $search_engine) {
331 if ($action->field == 'uri') {
333 $profile->joinAdd($user);
334 $profile->whereAdd('uri LIKE "%' . $profile->escape($q) . '%"');
337 if ($profile->N == 0) {
339 if (Validate::email($q)) {
340 $oprofile = Ostatus_profile::ensureWebfinger($q);
341 } else if (Validate::uri($q)) {
342 $oprofile = Ostatus_profile::ensureProfileURL($q);
344 throw new Exception('Invalid URI');
346 return $this->filter(array($oprofile->localProfile()));
348 } catch (Exception $e) {
349 $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");
359 * Find any explicit remote mentions. Accepted forms:
360 * Webfinger: @user@example.com
361 * Profile link: @example.com/mublog/user
362 * @param Profile $sender (os user?)
363 * @param string $text input markup text
364 * @param array &$mention in/out param: set of found mentions
365 * @return boolean hook return value
368 function onEndFindMentions($sender, $text, &$mentions)
372 // Webfinger matches: @user@example.com
373 if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\-?\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
376 PREG_OFFSET_CAPTURE)) {
377 foreach ($wmatches[1] as $wmatch) {
378 list($target, $pos) = $wmatch;
379 $this->log(LOG_INFO, "Checking webfinger '$target'");
381 $oprofile = Ostatus_profile::ensureWebfinger($target);
382 if ($oprofile && !$oprofile->isGroup()) {
383 $profile = $oprofile->localProfile();
384 $matches[$pos] = array('mentioned' => array($profile),
387 'url' => $profile->profileurl);
389 } catch (Exception $e) {
390 $this->log(LOG_ERR, "Webfinger check failed: " . $e->getMessage());
395 // Profile matches: @example.com/mublog/user
396 if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)!',
399 PREG_OFFSET_CAPTURE)) {
400 foreach ($wmatches[1] as $wmatch) {
401 list($target, $pos) = $wmatch;
402 $schemes = array('http', 'https');
403 foreach ($schemes as $scheme) {
404 $url = "$scheme://$target";
405 $this->log(LOG_INFO, "Checking profile address '$url'");
407 $oprofile = Ostatus_profile::ensureProfileURL($url);
408 if ($oprofile && !$oprofile->isGroup()) {
409 $profile = $oprofile->localProfile();
410 $matches[$pos] = array('mentioned' => array($profile),
413 'url' => $profile->profileurl);
416 } catch (Exception $e) {
417 $this->log(LOG_ERR, "Profile check failed: " . $e->getMessage());
423 foreach ($mentions as $i => $other) {
424 // If we share a common prefix with a local user, override it!
425 $pos = $other['position'];
426 if (isset($matches[$pos])) {
427 $mentions[$i] = $matches[$pos];
428 unset($matches[$pos]);
431 foreach ($matches as $mention) {
432 $mentions[] = $mention;
439 * Allow remote profile references to be used in commands:
440 * sub update@status.net
441 * whois evan@identi.ca
442 * reply http://identi.ca/evan hey what's up
444 * @param Command $command
446 * @param Profile &$profile
447 * @return hook return code
449 function onStartCommandGetProfile($command, $arg, &$profile)
451 $oprofile = $this->pullRemoteProfile($arg);
452 if ($oprofile && !$oprofile->isGroup()) {
453 $profile = $oprofile->localProfile();
461 * Allow remote group references to be used in commands:
462 * join group+statusnet@identi.ca
463 * join http://identi.ca/group/statusnet
464 * drop identi.ca/group/statusnet
466 * @param Command $command
468 * @param User_group &$group
469 * @return hook return code
471 function onStartCommandGetGroup($command, $arg, &$group)
473 $oprofile = $this->pullRemoteProfile($arg);
474 if ($oprofile && $oprofile->isGroup()) {
475 $group = $oprofile->localGroup();
482 protected function pullRemoteProfile($arg)
485 if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) {
488 return Ostatus_profile::ensureWebfinger($arg);
489 } catch (Exception $e) {
490 common_log(LOG_ERR, 'Webfinger lookup failed for ' .
491 $arg . ': ' . $e->getMessage());
495 // Look for profile URLs, with or without scheme:
497 if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
500 if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
501 $schemes = array('http', 'https');
502 foreach ($schemes as $scheme) {
503 $urls[] = "$scheme://$arg";
507 foreach ($urls as $url) {
509 return Ostatus_profile::ensureProfileURL($url);
510 } catch (Exception $e) {
511 common_log(LOG_ERR, 'Profile lookup failed for ' .
512 $arg . ': ' . $e->getMessage());
519 * Make sure necessary tables are filled out.
521 function onCheckSchema() {
522 $schema = Schema::get();
523 $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
524 $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
525 $schema->ensureTable('feedsub', FeedSub::schemaDef());
526 $schema->ensureTable('hubsub', HubSub::schemaDef());
527 $schema->ensureTable('magicsig', Magicsig::schemaDef());
531 function onEndShowStatusNetStyles($action) {
532 $action->cssLink($this->path('theme/base/css/ostatus.css'));
536 function onEndShowStatusNetScripts($action) {
537 $action->script($this->path('js/ostatus.js'));
542 * Override the "from ostatus" bit in notice lists to link to the
543 * original post and show the domain it came from.
545 * @param Notice in $notice
546 * @param string out &$name
547 * @param string out &$url
548 * @param string out &$title
549 * @return mixed hook return code
551 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
553 if ($notice->source == 'ostatus') {
555 $bits = parse_url($notice->url);
556 $domain = $bits['host'];
557 if (substr($domain, 0, 4) == 'www.') {
558 $name = substr($domain, 4);
564 // TRANSLATE: %s is a domain.
565 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
573 * Send incoming PuSH feeds for OStatus endpoints in for processing.
575 * @param FeedSub $feedsub
576 * @param DOMDocument $feed
577 * @return mixed hook return code
579 function onStartFeedSubReceive($feedsub, $feed)
581 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
583 $oprofile->processFeed($feed, 'push');
585 common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
590 * Tell the FeedSub infrastructure whether we have any active OStatus
591 * usage for the feed; if not it'll be able to garbage-collect the
594 * @param FeedSub $feedsub
595 * @param integer $count in/out
596 * @return mixed hook return code
598 function onFeedSubSubscriberCount($feedsub, &$count)
600 $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
602 $count += $oprofile->subscriberCount();
608 * When about to subscribe to a remote user, start a server-to-server
609 * PuSH subscription if needed. If we can't establish that, abort.
611 * @fixme If something else aborts later, we could end up with a stray
612 * PuSH subscription. This is relatively harmless, though.
614 * @param Profile $subscriber
615 * @param Profile $other
617 * @return hook return code
621 function onStartSubscribe($subscriber, $other)
623 $user = User::staticGet('id', $subscriber->id);
629 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
631 if (empty($oprofile)) {
635 if (!$oprofile->subscribe()) {
637 throw new Exception(_m('Could not set up remote subscription.'));
642 * Having established a remote subscription, send a notification to the
643 * remote OStatus profile's endpoint.
645 * @param Profile $subscriber
646 * @param Profile $other
648 * @return hook return code
652 function onEndSubscribe($subscriber, $other)
654 $user = User::staticGet('id', $subscriber->id);
660 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
662 if (empty($oprofile)) {
666 $sub = Subscription::pkeyGet(array('subscriber' => $subscriber->id,
667 'subscribed' => $other->id));
669 $act = $sub->asActivity();
671 $oprofile->notifyActivity($act, $subscriber);
677 * Notify remote server and garbage collect unused feeds on unsubscribe.
678 * @fixme send these operations to background queues
681 * @param Profile $other
682 * @return hook return value
684 function onEndUnsubscribe($profile, $other)
686 $user = User::staticGet('id', $profile->id);
692 $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
694 if (empty($oprofile)) {
698 // Drop the PuSH subscription if there are no other subscribers.
699 $oprofile->garbageCollect();
701 $act = new Activity();
703 $act->verb = ActivityVerb::UNFOLLOW;
705 $act->id = TagURI::mint('unfollow:%d:%d:%s',
708 common_date_iso8601(time()));
711 $act->title = _m('Unfollow');
712 // TRANS: Success message for unsubscribe from user attempt through OStatus.
713 // TRANS: %1$s is the unsubscriber's name, %2$s is the unsubscribed user's name.
714 $act->content = sprintf(_m('%1$s stopped following %2$s.'),
715 $profile->getBestName(),
716 $other->getBestName());
718 $act->actor = ActivityObject::fromProfile($profile);
719 $act->object = ActivityObject::fromProfile($other);
721 $oprofile->notifyActivity($act, $profile);
727 * When one of our local users tries to join a remote group,
728 * notify the remote server. If the notification is rejected,
731 * @param User_group $group
734 * @return mixed hook return value
737 function onStartJoinGroup($group, $user)
739 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
741 if (!$oprofile->subscribe()) {
742 throw new Exception(_m('Could not set up remote group membership.'));
745 // NOTE: we don't use Group_member::asActivity() since that record
746 // has not yet been created.
748 $member = Profile::staticGet($user->id);
750 $act = new Activity();
751 $act->id = TagURI::mint('join:%d:%d:%s',
754 common_date_iso8601(time()));
756 $act->actor = ActivityObject::fromProfile($member);
757 $act->verb = ActivityVerb::JOIN;
758 $act->object = $oprofile->asActivityObject();
761 $act->title = _m("Join");
762 // TRANS: Success message for subscribe to group attempt through OStatus.
763 // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
764 $act->content = sprintf(_m('%1$s has joined group %2$s.'),
765 $member->getBestName(),
766 $oprofile->getBestName());
768 if ($oprofile->notifyActivity($act, $member)) {
771 $oprofile->garbageCollect();
773 throw new Exception(_m("Failed joining remote group."));
779 * When one of our local users leaves a remote group, notify the remote
782 * @fixme Might be good to schedule a resend of the leave notification
783 * if it failed due to a transitory error. We've canceled the local
784 * membership already anyway, but if the remote server comes back up
785 * it'll be left with a stray membership record.
787 * @param User_group $group
790 * @return mixed hook return value
793 function onEndLeaveGroup($group, $user)
795 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
797 // Drop the PuSH subscription if there are no other subscribers.
798 $oprofile->garbageCollect();
800 $member = Profile::staticGet($user->id);
802 $act = new Activity();
803 $act->id = TagURI::mint('leave:%d:%d:%s',
806 common_date_iso8601(time()));
808 $act->actor = ActivityObject::fromProfile($member);
809 $act->verb = ActivityVerb::LEAVE;
810 $act->object = $oprofile->asActivityObject();
813 $act->title = _m("Leave");
814 // TRANS: Success message for unsubscribe from group attempt through OStatus.
815 // TRANS: %1$s is the member name, %2$s is the unsubscribed group's name.
816 $act->content = sprintf(_m('%1$s has left group %2$s.'),
817 $member->getBestName(),
818 $oprofile->getBestName());
820 $oprofile->notifyActivity($act, $member);
825 * When one of our local users tries to subscribe to a remote peopletag,
826 * notify the remote server. If the notification is rejected,
827 * deny the subscription.
829 * @param Profile_list $peopletag
832 * @return mixed hook return value
835 function onStartSubscribePeopletag($peopletag, $user)
837 $oprofile = Ostatus_profile::staticGet('peopletag_id', $peopletag->id);
839 if (!$oprofile->subscribe()) {
840 throw new Exception(_m('Could not set up remote peopletag subscription.'));
843 $sub = $user->getProfile();
844 $tagger = Profile::staticGet($peopletag->tagger);
846 $act = new Activity();
847 $act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s',
850 common_date_iso8601(time()));
852 $act->actor = ActivityObject::fromProfile($sub);
853 $act->verb = ActivityVerb::FOLLOW;
854 $act->object = $oprofile->asActivityObject();
857 $act->title = _m("Follow list");
858 $act->content = sprintf(_m("%s is now following people tagged %s by %s."),
860 $oprofile->getBestName(),
861 $tagger->getBestName());
863 if ($oprofile->notifyActivity($act, $sub)) {
866 $oprofile->garbageCollect();
867 throw new Exception(_m("Failed subscribing to remote peopletag."));
873 * When one of our local users unsubscribes to a remote peopletag, notify the remote
876 * @param Profile_list $peopletag
879 * @return mixed hook return value
882 function onEndUnsubscribePeopletag($peopletag, $user)
884 $oprofile = Ostatus_profile::staticGet('peopletag_id', $peopletag->id);
886 // Drop the PuSH subscription if there are no other subscribers.
887 $oprofile->garbageCollect();
889 $sub = Profile::staticGet($user->id);
890 $tagger = Profile::staticGet($peopletag->tagger);
892 $act = new Activity();
893 $act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
896 common_date_iso8601(time()));
898 $act->actor = ActivityObject::fromProfile($member);
899 $act->verb = ActivityVerb::UNFOLLOW;
900 $act->object = $oprofile->asActivityObject();
903 $act->title = _m("Unfollow peopletag");
904 $act->content = sprintf(_m("%s stopped following the list %s by %s."),
906 $oprofile->getBestName(),
907 $tagger->getBestName());
909 $oprofile->notifyActivity($act, $user);
914 * Notify remote users when their notices get favorited.
916 * @param Profile or User $profile of local user doing the faving
917 * @param Notice $notice being favored
918 * @return hook return value
920 function onEndFavorNotice(Profile $profile, Notice $notice)
922 $user = User::staticGet('id', $profile->id);
928 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
930 if (empty($oprofile)) {
934 $fav = Fave::pkeyGet(array('user_id' => $user->id,
935 'notice_id' => $notice->id));
942 $act = $fav->asActivity();
944 $oprofile->notifyActivity($act, $profile);
949 function onEndTagProfile($ptag)
951 $oprofile = Ostatus_profile::staticGet('profile_id', $ptag->tagged);
953 if (empty($oprofile)) {
957 $plist = $ptag->getMeta();
958 if ($plist->private) {
962 $act = new Activity();
964 $tagger = $plist->getTagger();
965 $tagged = Profile::staticGet('id', $ptag->tagged);
967 $act->verb = ActivityVerb::TAG;
968 $act->id = TagURI::mint('tag_profile:%d:%d:%s',
969 $plist->tagger, $plist->id,
970 common_date_iso8601(time()));
972 $act->title = _("Tag");
973 $act->content = sprintf(_("%s tagged %s in the list %s"),
974 $tagger->getBestName(),
975 $tagged->getBestName(),
976 $plist->getBestName());
978 $act->actor = ActivityObject::fromProfile($tagger);
979 $act->objects = array(ActivityObject::fromProfile($tagged));
980 $act->target = ActivityObject::fromPeopletag($plist);
982 $oprofile->notifyActivity($act, $tagger);
984 // initiate a PuSH subscription for the person being tagged
985 if (!$oprofile->subscribe()) {
986 throw new Exception(sprintf(_('Could not complete subscription to remote '.
987 'profile\'s feed. Tag %s could not be saved.'), $ptag->tag));
993 function onEndUntagProfile($ptag)
995 $oprofile = Ostatus_profile::staticGet('profile_id', $ptag->tagged);
997 if (empty($oprofile)) {
1001 $plist = $ptag->getMeta();
1002 if ($plist->private) {
1006 $act = new Activity();
1008 $tagger = $plist->getTagger();
1009 $tagged = Profile::staticGet('id', $ptag->tagged);
1011 $act->verb = ActivityVerb::UNTAG;
1012 $act->id = TagURI::mint('untag_profile:%d:%d:%s',
1013 $plist->tagger, $plist->id,
1014 common_date_iso8601(time()));
1015 $act->time = time();
1016 $act->title = _("Untag");
1017 $act->content = sprintf(_("%s untagged %s from the list %s"),
1018 $tagger->getBestName(),
1019 $tagged->getBestName(),
1020 $plist->getBestName());
1022 $act->actor = ActivityObject::fromProfile($tagger);
1023 $act->objects = array(ActivityObject::fromProfile($tagged));
1024 $act->target = ActivityObject::fromPeopletag($plist);
1026 $oprofile->notifyActivity($act, $tagger);
1028 // unsubscribe to PuSH feed if no more required
1029 $oprofile->garbageCollect();
1035 * Notify remote users when their notices get de-favorited.
1037 * @param Profile $profile Profile person doing the de-faving
1038 * @param Notice $notice Notice being favored
1040 * @return hook return value
1043 function onEndDisfavorNotice(Profile $profile, Notice $notice)
1045 $user = User::staticGet('id', $profile->id);
1051 $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
1053 if (empty($oprofile)) {
1057 $act = new Activity();
1059 $act->verb = ActivityVerb::UNFAVORITE;
1060 $act->id = TagURI::mint('disfavor:%d:%d:%s',
1063 common_date_iso8601(time()));
1064 $act->time = time();
1065 $act->title = _m('Disfavor');
1066 // TRANS: Success message for remove a favorite notice through OStatus.
1067 // TRANS: %1$s is the unfavoring user's name, %2$s is URI to the no longer favored notice.
1068 $act->content = sprintf(_m('%1$s marked notice %2$s as no longer a favorite.'),
1069 $profile->getBestName(),
1072 $act->actor = ActivityObject::fromProfile($profile);
1073 $act->object = ActivityObject::fromNotice($notice);
1075 $oprofile->notifyActivity($act, $profile);
1080 function onStartGetProfileUri($profile, &$uri)
1082 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
1083 if (!empty($oprofile)) {
1084 $uri = $oprofile->uri;
1090 function onStartUserGroupHomeUrl($group, &$url)
1092 return $this->onStartUserGroupPermalink($group, $url);
1095 function onStartUserGroupPermalink($group, &$url)
1097 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
1099 // @fixme this should probably be in the user_group table
1100 // @fixme this uri not guaranteed to be a profile page
1101 $url = $oprofile->uri;
1106 function onStartShowSubscriptionsContent($action)
1108 $this->showEntityRemoteSubscribe($action);
1113 function onStartShowUserGroupsContent($action)
1115 $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
1120 function onEndShowSubscriptionsMiniList($action)
1122 $this->showEntityRemoteSubscribe($action);
1127 function onEndShowGroupsMiniList($action)
1129 $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
1134 function showEntityRemoteSubscribe($action, $target='ostatussub')
1136 $user = common_current_user();
1137 if ($user && ($user->id == $action->profile->id)) {
1138 $action->elementStart('div', 'entity_actions');
1139 $action->elementStart('p', array('id' => 'entity_remote_subscribe',
1140 'class' => 'entity_subscribe'));
1141 $action->element('a', array('href' => common_local_url($target),
1142 'class' => 'entity_remote_subscribe'),
1143 // TRANS: Link text for link to remote subscribe.
1145 $action->elementEnd('p');
1146 $action->elementEnd('div');
1151 * Ping remote profiles with updates to this profile.
1152 * Salmon pings are queued for background processing.
1154 function onEndBroadcastProfile(Profile $profile)
1156 $user = User::staticGet('id', $profile->id);
1158 // Find foreign accounts I'm subscribed to that support Salmon pings.
1160 // @fixme we could run updates through the PuSH feed too,
1161 // in which case we can skip Salmon pings to folks who
1162 // are also subscribed to me.
1163 $sql = "SELECT * FROM ostatus_profile " .
1164 "WHERE profile_id IN " .
1165 "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
1167 "(SELECT group_id FROM group_member WHERE profile_id=%d)";
1168 $oprofile = new Ostatus_profile();
1169 $oprofile->query(sprintf($sql, $profile->id, $profile->id));
1171 if ($oprofile->N == 0) {
1172 common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
1176 $act = new Activity();
1178 $act->verb = ActivityVerb::UPDATE_PROFILE;
1179 $act->id = TagURI::mint('update-profile:%d:%s',
1181 common_date_iso8601(time()));
1182 $act->time = time();
1183 // TRANS: Title for activity.
1184 $act->title = _m("Profile update");
1185 // TRANS: Ping text for remote profile update through OStatus.
1186 // TRANS: %s is user that updated their profile.
1187 $act->content = sprintf(_m("%s has updated their profile page."),
1188 $profile->getBestName());
1190 $act->actor = ActivityObject::fromProfile($profile);
1191 $act->object = $act->actor;
1193 while ($oprofile->fetch()) {
1194 $oprofile->notifyDeferred($act, $profile);
1200 function onStartProfileListItemActionElements($item, $profile=null)
1202 if (!common_logged_in()) {
1204 $profileUser = User::staticGet('id', $item->profile->id);
1206 if (!empty($profileUser)) {
1208 if ($item instanceof Action) {
1210 $profile = $item->profile;
1212 $output = $item->out;
1215 // Add an OStatus subscribe
1216 $output->elementStart('li', 'entity_subscribe');
1217 $url = common_local_url('ostatusinit',
1218 array('nickname' => $profileUser->nickname));
1219 $output->element('a', array('href' => $url,
1220 'class' => 'entity_remote_subscribe'),
1221 // TRANS: Link text for a user to subscribe to an OStatus user.
1223 $output->elementEnd('li');
1225 $output->elementStart('li', 'entity_tag');
1226 $url = common_local_url('ostatustag',
1227 array('nickname' => $profileUser->nickname));
1228 $output->element('a', array('href' => $url,
1229 'class' => 'entity_remote_tag'),
1231 $output->elementEnd('li');
1238 function onPluginVersion(&$versions)
1240 $versions[] = array('name' => 'OStatus',
1241 'version' => STATUSNET_VERSION,
1242 'author' => 'Evan Prodromou, James Walker, Brion Vibber, Zach Copley',
1243 'homepage' => 'http://status.net/wiki/Plugin:OStatus',
1244 // TRANS: Plugin description.
1245 'rawdescription' => _m('Follow people across social networks that implement '.
1246 '<a href="http://ostatus.org/">OStatus</a>.'));
1252 * Utility function to check if the given URI is a canonical group profile
1253 * page, and if so return the ID number.
1255 * @param string $url
1256 * @return mixed int or false
1258 public static function localGroupFromUrl($url)
1260 $group = User_group::staticGet('uri', $url);
1262 $local = Local_group::staticGet('group_id', $group->id);
1267 // To find local groups which haven't had their uri fields filled out...
1268 // If the domain has changed since a subscriber got the URI, it'll
1270 $template = common_local_url('groupbyid', array('id' => '31337'));
1271 $template = preg_quote($template, '/');
1272 $template = str_replace('31337', '(\d+)', $template);
1273 if (preg_match("/$template/", $url, $matches)) {
1274 return intval($matches[1]);
1280 public function onStartProfileGetAtomFeed($profile, &$feed)
1282 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
1284 if (empty($oprofile)) {
1288 $feed = $oprofile->feeduri;
1292 function onStartGetProfileFromURI($uri, &$profile)
1294 // Don't want to do Web-based discovery on our own server,
1295 // so we check locally first.
1297 $user = User::staticGet('uri', $uri);
1299 if (!empty($user)) {
1300 $profile = $user->getProfile();
1304 // Now, check remotely
1306 $oprofile = Ostatus_profile::ensureProfileURI($uri);
1308 if (!empty($oprofile)) {
1309 $profile = $oprofile->localProfile();
1313 // Still not a hit, so give up.
1318 function onEndXrdActionLinks(&$xrd, $user)
1320 $xrd->links[] = array('rel' => Discovery::UPDATESFROM,
1321 'href' => common_local_url('ApiTimelineUser',
1322 array('id' => $user->id,
1323 'format' => 'atom')),
1324 'type' => 'application/atom+xml');
1327 $salmon_url = common_local_url('usersalmon',
1328 array('id' => $user->id));
1330 $xrd->links[] = array('rel' => Salmon::REL_SALMON,
1331 'href' => $salmon_url);
1332 // XXX : Deprecated - to be removed.
1333 $xrd->links[] = array('rel' => Salmon::NS_REPLIES,
1334 'href' => $salmon_url);
1336 $xrd->links[] = array('rel' => Salmon::NS_MENTIONS,
1337 'href' => $salmon_url);
1339 // Get this user's keypair
1340 $magickey = Magicsig::staticGet('user_id', $user->id);
1342 // No keypair yet, let's generate one.
1343 $magickey = new Magicsig();
1344 $magickey->generate($user->id);
1347 $xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,
1348 'href' => 'data:application/magic-public-key,'. $magickey->toString(false));
1350 // TODO - finalize where the redirect should go on the publisher
1351 $url = common_local_url('ostatussub') . '?profile={uri}';
1352 $xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe',
1353 'template' => $url );