]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/OStatusPlugin.php
Merge commit 'origin/testing' into 0.9.x
[quix0rs-gnu-social.git] / plugins / OStatus / OStatusPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009-2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/');
28
29 class FeedSubException extends Exception
30 {
31 }
32
33 class OStatusPlugin extends Plugin
34 {
35     /**
36      * Hook for RouterInitialized event.
37      *
38      * @param Net_URL_Mapper $m path-to-action mapper
39      * @return boolean hook return
40      */
41     function onRouterInitialized($m)
42     {
43         // Discovery actions
44         $m->connect('.well-known/host-meta',
45                     array('action' => 'hostmeta'));
46         $m->connect('main/xrd',
47                     array('action' => 'userxrd'));
48         $m->connect('main/ownerxrd',
49                     array('action' => 'ownerxrd'));
50         $m->connect('main/ostatus',
51                     array('action' => 'ostatusinit'));
52         $m->connect('main/ostatus?nickname=:nickname',
53                   array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
54         $m->connect('main/ostatus?group=:group',
55                   array('action' => 'ostatusinit'), array('group' => '[A-Za-z0-9_-]+'));
56         $m->connect('main/ostatussub',
57                     array('action' => 'ostatussub'));
58         $m->connect('main/ostatusgroup',
59                     array('action' => 'ostatusgroup'));
60
61         // PuSH actions
62         $m->connect('main/push/hub', array('action' => 'pushhub'));
63
64         $m->connect('main/push/callback/:feed',
65                     array('action' => 'pushcallback'),
66                     array('feed' => '[0-9]+'));
67
68         // Salmon endpoint
69         $m->connect('main/salmon/user/:id',
70                     array('action' => 'usersalmon'),
71                     array('id' => '[0-9]+'));
72         $m->connect('main/salmon/group/:id',
73                     array('action' => 'groupsalmon'),
74                     array('id' => '[0-9]+'));
75         return true;
76     }
77
78     /**
79      * Set up queue handlers for outgoing hub pushes
80      * @param QueueManager $qm
81      * @return boolean hook return
82      */
83     function onEndInitializeQueueManager(QueueManager $qm)
84     {
85         // Prepare outgoing distributions after notice save.
86         $qm->connect('ostatus', 'OStatusQueueHandler');
87
88         // Outgoing from our internal PuSH hub
89         $qm->connect('hubconf', 'HubConfQueueHandler');
90         $qm->connect('hubout', 'HubOutQueueHandler');
91
92         // Outgoing Salmon replies (when we don't need a return value)
93         $qm->connect('salmon', 'SalmonQueueHandler');
94
95         // Incoming from a foreign PuSH hub
96         $qm->connect('pushin', 'PushInQueueHandler');
97         return true;
98     }
99
100     /**
101      * Put saved notices into the queue for pubsub distribution.
102      */
103     function onStartEnqueueNotice($notice, &$transports)
104     {
105         $transports[] = 'ostatus';
106         return true;
107     }
108
109     /**
110      * Add a link header for LRDD Discovery
111      */
112     function onStartShowHTML($action)
113     {
114         if ($action instanceof ShowstreamAction) {
115             $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
116             $url = common_local_url('userxrd');
117             $url.= '?uri='. $acct;
118
119             header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="application/xrd+xml"');
120         }
121     }
122
123     /**
124      * Set up a PuSH hub link to our internal link for canonical timeline
125      * Atom feeds for users and groups.
126      */
127     function onStartApiAtom($feed)
128     {
129         $id = null;
130
131         if ($feed instanceof AtomUserNoticeFeed) {
132             $salmonAction = 'usersalmon';
133             $user = $feed->getUser();
134             $id   = $user->id;
135             $profile = $user->getProfile();
136             $feed->setActivitySubject($profile->asActivityNoun('subject'));
137         } else if ($feed instanceof AtomGroupNoticeFeed) {
138             $salmonAction = 'groupsalmon';
139             $group = $feed->getGroup();
140             $id = $group->id;
141             $feed->setActivitySubject($group->asActivitySubject());
142         } else {
143             return true;
144         }
145
146         if (!empty($id)) {
147             $hub = common_config('ostatus', 'hub');
148             if (empty($hub)) {
149                 // Updates will be handled through our internal PuSH hub.
150                 $hub = common_local_url('pushhub');
151             }
152             $feed->addLink($hub, array('rel' => 'hub'));
153
154             // Also, we'll add in the salmon link
155             $salmon = common_local_url($salmonAction, array('id' => $id));
156             $feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
157             $feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
158         }
159
160         return true;
161     }
162
163     /**
164      * Automatically load the actions and libraries used by the plugin
165      *
166      * @param Class $cls the class
167      *
168      * @return boolean hook return
169      *
170      */
171     function onAutoload($cls)
172     {
173         $base = dirname(__FILE__);
174         $lower = strtolower($cls);
175         $map = array('activityverb' => 'activity',
176                      'activityobject' => 'activity',
177                      'activityutils' => 'activity');
178         if (isset($map[$lower])) {
179             $lower = $map[$lower];
180         }
181         $files = array("$base/classes/$cls.php",
182                        "$base/lib/$lower.php");
183         if (substr($lower, -6) == 'action') {
184             $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
185         }
186         foreach ($files as $file) {
187             if (file_exists($file)) {
188                 include_once $file;
189                 return false;
190             }
191         }
192         return true;
193     }
194
195     /**
196      * Add in an OStatus subscribe button
197      */
198     function onStartProfileRemoteSubscribe($output, $profile)
199     {
200         $cur = common_current_user();
201
202         if (empty($cur)) {
203             // Add an OStatus subscribe
204             $output->elementStart('li', 'entity_subscribe');
205             $url = common_local_url('ostatusinit',
206                                     array('nickname' => $profile->nickname));
207             $output->element('a', array('href' => $url,
208                                         'class' => 'entity_remote_subscribe'),
209                                 _m('Subscribe'));
210
211             $output->elementEnd('li');
212         }
213
214         return false;
215     }
216
217     function onStartGroupSubscribe($output, $group)
218     {
219         $cur = common_current_user();
220
221         if (empty($cur)) {
222             // Add an OStatus subscribe
223             $url = common_local_url('ostatusinit',
224                                     array('group' => $group->nickname));
225             $output->element('a', array('href' => $url,
226                                         'class' => 'entity_remote_subscribe'),
227                                 _m('Join'));
228         }
229
230         return true;
231     }
232
233     /**
234      * Check if we've got remote replies to send via Salmon.
235      *
236      * @fixme push webfinger lookup & sending to a background queue
237      * @fixme also detect short-form name for remote subscribees where not ambiguous
238      */
239
240     function onEndNoticeSave($notice)
241     {
242     }
243
244     /**
245      * Find any explicit remote mentions. Accepted forms:
246      *   Webfinger: @user@example.com
247      *   Profile link: @example.com/mublog/user
248      * @param Profile $sender (os user?)
249      * @param string $text input markup text
250      * @param array &$mention in/out param: set of found mentions
251      * @return boolean hook return value
252      */
253
254     function onEndFindMentions($sender, $text, &$mentions)
255     {
256         $matches = array();
257
258         // Webfinger matches: @user@example.com
259         if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
260                        $text,
261                        $wmatches,
262                        PREG_OFFSET_CAPTURE)) {
263             foreach ($wmatches[1] as $wmatch) {
264                 list($target, $pos) = $wmatch;
265                 $this->log(LOG_INFO, "Checking webfinger '$target'");
266                 try {
267                     $oprofile = Ostatus_profile::ensureWebfinger($target);
268                     if ($oprofile && !$oprofile->isGroup()) {
269                         $profile = $oprofile->localProfile();
270                         $matches[$pos] = array('mentioned' => array($profile),
271                                                'text' => $target,
272                                                'position' => $pos,
273                                                'url' => $profile->profileurl);
274                     }
275                 } catch (Exception $e) {
276                     $this->log(LOG_ERR, "Webfinger check failed: " . $e->getMessage());
277                 }
278             }
279         }
280
281         // Profile matches: @example.com/mublog/user
282         if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)!',
283                        $text,
284                        $wmatches,
285                        PREG_OFFSET_CAPTURE)) {
286             foreach ($wmatches[1] as $wmatch) {
287                 list($target, $pos) = $wmatch;
288                 $schemes = array('http', 'https');
289                 foreach ($schemes as $scheme) {
290                     $url = "$scheme://$target";
291                     $this->log(LOG_INFO, "Checking profile address '$url'");
292                     try {
293                         $oprofile = Ostatus_profile::ensureProfile($url);
294                         if ($oprofile && !$oprofile->isGroup()) {
295                             $profile = $oprofile->localProfile();
296                             $matches[$pos] = array('mentioned' => array($profile),
297                                                    'text' => $target,
298                                                    'position' => $pos,
299                                                    'url' => $profile->profileurl);
300                             break;
301                         }
302                     } catch (Exception $e) {
303                         $this->log(LOG_ERR, "Profile check failed: " . $e->getMessage());
304                     }
305                 }
306             }
307         }
308
309         foreach ($mentions as $i => $other) {
310             // If we share a common prefix with a local user, override it!
311             $pos = $other['position'];
312             if (isset($matches[$pos])) {
313                 $mentions[$i] = $matches[$pos];
314                 unset($matches[$pos]);
315             }
316         }
317         foreach ($matches as $mention) {
318             $mentions[] = $mention;
319         }
320
321         return true;
322     }
323
324     /**
325      * Make sure necessary tables are filled out.
326      */
327     function onCheckSchema() {
328         $schema = Schema::get();
329         $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
330         $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
331         $schema->ensureTable('feedsub', FeedSub::schemaDef());
332         $schema->ensureTable('hubsub', HubSub::schemaDef());
333         $schema->ensureTable('magicsig', Magicsig::schemaDef());
334         return true;
335     }
336
337     function onEndShowStatusNetStyles($action) {
338         $action->cssLink('plugins/OStatus/theme/base/css/ostatus.css');
339         return true;
340     }
341
342     function onEndShowStatusNetScripts($action) {
343         $action->script('plugins/OStatus/js/ostatus.js');
344         return true;
345     }
346
347     /**
348      * Override the "from ostatus" bit in notice lists to link to the
349      * original post and show the domain it came from.
350      *
351      * @param Notice in $notice
352      * @param string out &$name
353      * @param string out &$url
354      * @param string out &$title
355      * @return mixed hook return code
356      */
357     function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
358     {
359         if ($notice->source == 'ostatus') {
360             if ($notice->url) {
361                 $bits = parse_url($notice->url);
362                 $domain = $bits['host'];
363                 if (substr($domain, 0, 4) == 'www.') {
364                     $name = substr($domain, 4);
365                 } else {
366                     $name = $domain;
367                 }
368
369                 $url = $notice->url;
370                 $title = sprintf(_m("Sent from %s via OStatus"), $domain);
371                 return false;
372             }
373         }
374     }
375
376     /**
377      * Send incoming PuSH feeds for OStatus endpoints in for processing.
378      *
379      * @param FeedSub $feedsub
380      * @param DOMDocument $feed
381      * @return mixed hook return code
382      */
383     function onStartFeedSubReceive($feedsub, $feed)
384     {
385         $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
386         if ($oprofile) {
387             $oprofile->processFeed($feed, 'push');
388         } else {
389             common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
390         }
391     }
392
393     /**
394      * When about to subscribe to a remote user, start a server-to-server
395      * PuSH subscription if needed. If we can't establish that, abort.
396      *
397      * @fixme If something else aborts later, we could end up with a stray
398      *        PuSH subscription. This is relatively harmless, though.
399      *
400      * @param Profile $subscriber
401      * @param Profile $other
402      *
403      * @return hook return code
404      *
405      * @throws Exception
406      */
407     function onStartSubscribe($subscriber, $other)
408     {
409         $user = User::staticGet('id', $subscriber->id);
410
411         if (empty($user)) {
412             return true;
413         }
414
415         $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
416
417         if (empty($oprofile)) {
418             return true;
419         }
420
421         if (!$oprofile->subscribe()) {
422             throw new Exception(_m('Could not set up remote subscription.'));
423         }
424     }
425
426     /**
427      * Having established a remote subscription, send a notification to the
428      * remote OStatus profile's endpoint.
429      *
430      * @param Profile $subscriber
431      * @param Profile $other
432      *
433      * @return hook return code
434      *
435      * @throws Exception
436      */
437     function onEndSubscribe($subscriber, $other)
438     {
439         $user = User::staticGet('id', $subscriber->id);
440
441         if (empty($user)) {
442             return true;
443         }
444
445         $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
446
447         if (empty($oprofile)) {
448             return true;
449         }
450
451         $act = new Activity();
452
453         $act->verb = ActivityVerb::FOLLOW;
454
455         $act->id   = TagURI::mint('follow:%d:%d:%s',
456                                   $subscriber->id,
457                                   $other->id,
458                                   common_date_iso8601(time()));
459
460         $act->time    = time();
461         $act->title   = _("Follow");
462         $act->content = sprintf(_("%s is now following %s."),
463                                $subscriber->getBestName(),
464                                $other->getBestName());
465
466         $act->actor   = ActivityObject::fromProfile($subscriber);
467         $act->object  = ActivityObject::fromProfile($other);
468
469         $oprofile->notifyActivity($act, $subscriber);
470
471         return true;
472     }
473
474     /**
475      * Notify remote server and garbage collect unused feeds on unsubscribe.
476      * @fixme send these operations to background queues
477      *
478      * @param User $user
479      * @param Profile $other
480      * @return hook return value
481      */
482     function onEndUnsubscribe($profile, $other)
483     {
484         $user = User::staticGet('id', $profile->id);
485
486         if (empty($user)) {
487             return true;
488         }
489
490         $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
491
492         if (empty($oprofile)) {
493             return true;
494         }
495
496         // Drop the PuSH subscription if there are no other subscribers.
497         $oprofile->garbageCollect();
498
499         $act = new Activity();
500
501         $act->verb = ActivityVerb::UNFOLLOW;
502
503         $act->id   = TagURI::mint('unfollow:%d:%d:%s',
504                                   $profile->id,
505                                   $other->id,
506                                   common_date_iso8601(time()));
507
508         $act->time    = time();
509         $act->title   = _("Unfollow");
510         $act->content = sprintf(_("%s stopped following %s."),
511                                $profile->getBestName(),
512                                $other->getBestName());
513
514         $act->actor   = ActivityObject::fromProfile($profile);
515         $act->object  = ActivityObject::fromProfile($other);
516
517         $oprofile->notifyActivity($act, $profile);
518
519         return true;
520     }
521
522     /**
523      * When one of our local users tries to join a remote group,
524      * notify the remote server. If the notification is rejected,
525      * deny the join.
526      *
527      * @param User_group $group
528      * @param User $user
529      *
530      * @return mixed hook return value
531      */
532
533     function onStartJoinGroup($group, $user)
534     {
535         $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
536         if ($oprofile) {
537             if (!$oprofile->subscribe()) {
538                 throw new Exception(_m('Could not set up remote group membership.'));
539             }
540
541             $member = Profile::staticGet($user->id);
542
543             $act = new Activity();
544             $act->id = TagURI::mint('join:%d:%d:%s',
545                                     $member->id,
546                                     $group->id,
547                                     common_date_iso8601(time()));
548
549             $act->actor = ActivityObject::fromProfile($member);
550             $act->verb = ActivityVerb::JOIN;
551             $act->object = $oprofile->asActivityObject();
552
553             $act->time = time();
554             $act->title = _m("Join");
555             $act->content = sprintf(_m("%s has joined group %s."),
556                                     $member->getBestName(),
557                                     $oprofile->getBestName());
558
559             if ($oprofile->notifyActivity($act, $member)) {
560                 return true;
561             } else {
562                 $oprofile->garbageCollect();
563                 throw new Exception(_m("Failed joining remote group."));
564             }
565         }
566     }
567
568     /**
569      * When one of our local users leaves a remote group, notify the remote
570      * server.
571      *
572      * @fixme Might be good to schedule a resend of the leave notification
573      * if it failed due to a transitory error. We've canceled the local
574      * membership already anyway, but if the remote server comes back up
575      * it'll be left with a stray membership record.
576      *
577      * @param User_group $group
578      * @param User $user
579      *
580      * @return mixed hook return value
581      */
582
583     function onEndLeaveGroup($group, $user)
584     {
585         $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
586         if ($oprofile) {
587             // Drop the PuSH subscription if there are no other subscribers.
588             $oprofile->garbageCollect();
589
590             $member = Profile::staticGet($user->id);
591
592             $act = new Activity();
593             $act->id = TagURI::mint('leave:%d:%d:%s',
594                                     $member->id,
595                                     $group->id,
596                                     common_date_iso8601(time()));
597
598             $act->actor = ActivityObject::fromProfile($member);
599             $act->verb = ActivityVerb::LEAVE;
600             $act->object = $oprofile->asActivityObject();
601
602             $act->time = time();
603             $act->title = _m("Leave");
604             $act->content = sprintf(_m("%s has left group %s."),
605                                     $member->getBestName(),
606                                     $oprofile->getBestName());
607
608             $oprofile->notifyActivity($act, $member);
609         }
610     }
611
612     /**
613      * Notify remote users when their notices get favorited.
614      *
615      * @param Profile or User $profile of local user doing the faving
616      * @param Notice $notice being favored
617      * @return hook return value
618      */
619
620     function onEndFavorNotice(Profile $profile, Notice $notice)
621     {
622         $user = User::staticGet('id', $profile->id);
623
624         if (empty($user)) {
625             return true;
626         }
627
628         $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
629
630         if (empty($oprofile)) {
631             return true;
632         }
633
634         $act = new Activity();
635
636         $act->verb = ActivityVerb::FAVORITE;
637         $act->id   = TagURI::mint('favor:%d:%d:%s',
638                                   $profile->id,
639                                   $notice->id,
640                                   common_date_iso8601(time()));
641
642         $act->time    = time();
643         $act->title   = _("Favor");
644         $act->content = sprintf(_("%s marked notice %s as a favorite."),
645                                $profile->getBestName(),
646                                $notice->uri);
647
648         $act->actor   = ActivityObject::fromProfile($profile);
649         $act->object  = ActivityObject::fromNotice($notice);
650
651         $oprofile->notifyActivity($act, $profile);
652
653         return true;
654     }
655
656     /**
657      * Notify remote users when their notices get de-favorited.
658      *
659      * @param Profile $profile Profile person doing the de-faving
660      * @param Notice  $notice  Notice being favored
661      *
662      * @return hook return value
663      */
664
665     function onEndDisfavorNotice(Profile $profile, Notice $notice)
666     {
667         $user = User::staticGet('id', $profile->id);
668
669         if (empty($user)) {
670             return true;
671         }
672
673         $oprofile = Ostatus_profile::staticGet('profile_id', $notice->profile_id);
674
675         if (empty($oprofile)) {
676             return true;
677         }
678
679         $act = new Activity();
680
681         $act->verb = ActivityVerb::UNFAVORITE;
682         $act->id   = TagURI::mint('disfavor:%d:%d:%s',
683                                   $profile->id,
684                                   $notice->id,
685                                   common_date_iso8601(time()));
686         $act->time    = time();
687         $act->title   = _("Disfavor");
688         $act->content = sprintf(_("%s marked notice %s as no longer a favorite."),
689                                $profile->getBestName(),
690                                $notice->uri);
691
692         $act->actor   = ActivityObject::fromProfile($profile);
693         $act->object  = ActivityObject::fromNotice($notice);
694
695         $oprofile->notifyActivity($act, $profile);
696
697         return true;
698     }
699
700     function onStartGetProfileUri($profile, &$uri)
701     {
702         $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
703         if (!empty($oprofile)) {
704             $uri = $oprofile->uri;
705             return false;
706         }
707         return true;
708     }
709
710     function onStartUserGroupHomeUrl($group, &$url)
711     {
712         return $this->onStartUserGroupPermalink($group, $url);
713     }
714
715     function onStartUserGroupPermalink($group, &$url)
716     {
717         $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
718         if ($oprofile) {
719             // @fixme this should probably be in the user_group table
720             // @fixme this uri not guaranteed to be a profile page
721             $url = $oprofile->uri;
722             return false;
723         }
724     }
725
726     function onStartShowSubscriptionsContent($action)
727     {
728         $this->showEntityRemoteSubscribe($action);
729
730         return true;
731     }
732
733     function onStartShowUserGroupsContent($action)
734     {
735         $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
736
737         return true;
738     }
739
740     function onEndShowSubscriptionsMiniList($action)
741     {
742         $this->showEntityRemoteSubscribe($action);
743
744         return true;
745     }
746
747     function onEndShowGroupsMiniList($action)
748     {
749         $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
750
751         return true;
752     }
753
754     function showEntityRemoteSubscribe($action, $target='ostatussub')
755     {
756         $user = common_current_user();
757         if ($user && ($user->id == $action->profile->id)) {
758             $action->elementStart('div', 'entity_actions');
759             $action->elementStart('p', array('id' => 'entity_remote_subscribe',
760                                              'class' => 'entity_subscribe'));
761             $action->element('a', array('href' => common_local_url($target),
762                                         'class' => 'entity_remote_subscribe')
763                                 , _m('Remote'));
764             $action->elementEnd('p');
765             $action->elementEnd('div');
766         }
767     }
768
769     /**
770      * Ping remote profiles with updates to this profile.
771      * Salmon pings are queued for background processing.
772      */
773     function onEndBroadcastProfile(Profile $profile)
774     {
775         $user = User::staticGet('id', $profile->id);
776
777         // Find foreign accounts I'm subscribed to that support Salmon pings.
778         //
779         // @fixme we could run updates through the PuSH feed too,
780         // in which case we can skip Salmon pings to folks who
781         // are also subscribed to me.
782         $sql = "SELECT * FROM ostatus_profile " .
783                "WHERE profile_id IN " .
784                "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
785                "OR group_id IN " .
786                "(SELECT group_id FROM group_member WHERE profile_id=%d)";
787         $oprofile = new Ostatus_profile();
788         $oprofile->query(sprintf($sql, $profile->id, $profile->id));
789
790         if ($oprofile->N == 0) {
791             common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
792             return true;
793         }
794
795         $act = new Activity();
796
797         $act->verb = ActivityVerb::UPDATE_PROFILE;
798         $act->id   = TagURI::mint('update-profile:%d:%s',
799                                   $profile->id,
800                                   common_date_iso8601(time()));
801         $act->time    = time();
802         $act->title   = _m("Profile update");
803         $act->content = sprintf(_m("%s has updated their profile page."),
804                                $profile->getBestName());
805
806         $act->actor   = ActivityObject::fromProfile($profile);
807         $act->object  = $act->actor;
808
809         while ($oprofile->fetch()) {
810             $oprofile->notifyDeferred($act, $profile);
811         }
812
813         return true;
814     }
815
816     function onStartProfileListItemActionElements($item)
817     {
818         if (!common_logged_in()) {
819
820             $profileUser = User::staticGet('id', $item->profile->id);
821
822             if (!empty($profileUser)) {
823
824                 $output = $item->out;
825
826                 // Add an OStatus subscribe
827                 $output->elementStart('li', 'entity_subscribe');
828                 $url = common_local_url('ostatusinit',
829                                         array('nickname' => $profileUser->nickname));
830                 $output->element('a', array('href' => $url,
831                                             'class' => 'entity_remote_subscribe'),
832                                  _m('Subscribe'));
833                 $output->elementEnd('li');
834             }
835         }
836
837         return true;
838     }
839 }