]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/OStatusPlugin.php
OStatus: do PuSH subscription setup from subscribe/join event hooks, so resubscribing...
[quix0rs-gnu-social.git] / plugins / OStatus / OStatusPlugin.php
index 60bb144a830c09f7343a03ba6cf02fcab0be0e72..629645767f13294ed09a03d574e055de989a99fd 100644 (file)
@@ -24,6 +24,8 @@
 
 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
 
+set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/');
+
 class FeedSubException extends Exception
 {
 }
@@ -58,8 +60,6 @@ class OStatusPlugin extends Plugin
         $m->connect('main/push/callback/:feed',
                     array('action' => 'pushcallback'),
                     array('feed' => '[0-9]+'));
-        $m->connect('settings/feedsub',
-                    array('action' => 'feedsubsettings'));
 
         // Salmon endpoint
         $m->connect('main/salmon/user/:id',
@@ -201,16 +201,18 @@ class OStatusPlugin extends Plugin
     {
         $mentioned = $notice->getReplies();
 
-        foreach ($mentioned as $profile) {
+        foreach ($mentioned as $profile_id) {
 
-            $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
+            $oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
 
             if (!empty($oprofile) && !empty($oprofile->salmonuri)) {
 
+                common_log(LOG_INFO, "Sending notice '{$notice->uri}' to remote profile '{$oprofile->uri}'.");
+
                 // FIXME: this needs to go out in a queue handler
 
-                $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
-                $xml .= $notice->asAtomEntry();
+                $xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
+                $xml .= $notice->asAtomEntry(true, true);
 
                 $salmon = new Salmon();
                 $salmon->post($oprofile->salmonuri, $xml);
@@ -249,66 +251,16 @@ class OStatusPlugin extends Plugin
         return true;
     }
 
-    /**
-     * Notify remote server and garbage collect unused feeds on unsubscribe.
-     * @fixme send these operations to background queues
-     *
-     * @param User $user
-     * @param Profile $other
-     * @return hook return value
-     */
-    function onEndUnsubscribe($profile, $other)
-    {
-        $user = User::staticGet('id', $profile->id);
-
-        if (empty($user)) {
-            return true;
-        }
-
-        $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
-
-        if (empty($oprofile)) {
-            return true;
-        }
-
-        // Drop the PuSH subscription if there are no other subscribers.
-
-        if ($other->subscriberCount() == 0) {
-            common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri");
-            $oprofile->unsubscribe();
-        }
-
-        $act = new Activity();
-
-        $act->verb = ActivityVerb::UNFOLLOW;
-
-        $act->id   = TagURI::mint('unfollow:%d:%d:%s',
-                                  $profile->id,
-                                  $other->id,
-                                  common_date_iso8601(time()));
-
-        $act->time    = time();
-        $act->title   = _("Unfollow");
-        $act->content = sprintf(_("%s stopped following %s."),
-                               $profile->getBestName(),
-                               $other->getBestName());
-
-        $act->actor   = ActivityObject::fromProfile($profile);
-        $act->object  = ActivityObject::fromProfile($other);
-
-        $oprofile->notifyActivity($act);
-
-        return true;
-    }
-
     /**
      * Make sure necessary tables are filled out.
      */
     function onCheckSchema() {
         $schema = Schema::get();
         $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
+        $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
         $schema->ensureTable('feedsub', FeedSub::schemaDef());
         $schema->ensureTable('hubsub', HubSub::schemaDef());
+        $schema->ensureTable('magicsig', Magicsig::schemaDef());
         return true;
     }
 
@@ -362,6 +314,50 @@ class OStatusPlugin extends Plugin
         }
     }
 
+    /**
+     * When about to subscribe to a remote user, start a server-to-server
+     * PuSH subscription if needed. If we can't establish that, abort.
+     *
+     * @fixme If something else aborts later, we could end up with a stray
+     *        PuSH subscription. This is relatively harmless, though.
+     *
+     * @param Profile $subscriber
+     * @param Profile $other
+     *
+     * @return hook return code
+     *
+     * @throws Exception
+     */
+    function onStartSubscribe($subscriber, $other)
+    {
+        $user = User::staticGet('id', $subscriber->id);
+
+        if (empty($user)) {
+            return true;
+        }
+
+        $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
+
+        if (empty($oprofile)) {
+            return true;
+        }
+
+        if (!$oprofile->subscribe()) {
+            throw new Exception(_m('Could not set up remote subscription.'));
+        }
+    }
+
+    /**
+     * Having established a remote subscription, send a notification to the
+     * remote OStatus profile's endpoint.
+     *
+     * @param Profile $subscriber
+     * @param Profile $other
+     *
+     * @return hook return code
+     *
+     * @throws Exception
+     */
     function onEndSubscribe($subscriber, $other)
     {
         $user = User::staticGet('id', $subscriber->id);
@@ -399,6 +395,145 @@ class OStatusPlugin extends Plugin
         return true;
     }
 
+    /**
+     * Notify remote server and garbage collect unused feeds on unsubscribe.
+     * @fixme send these operations to background queues
+     *
+     * @param User $user
+     * @param Profile $other
+     * @return hook return value
+     */
+    function onEndUnsubscribe($profile, $other)
+    {
+        $user = User::staticGet('id', $profile->id);
+
+        if (empty($user)) {
+            return true;
+        }
+
+        $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
+
+        if (empty($oprofile)) {
+            return true;
+        }
+
+        // Drop the PuSH subscription if there are no other subscribers.
+        $oprofile->garbageCollect();
+
+        $act = new Activity();
+
+        $act->verb = ActivityVerb::UNFOLLOW;
+
+        $act->id   = TagURI::mint('unfollow:%d:%d:%s',
+                                  $profile->id,
+                                  $other->id,
+                                  common_date_iso8601(time()));
+
+        $act->time    = time();
+        $act->title   = _("Unfollow");
+        $act->content = sprintf(_("%s stopped following %s."),
+                               $profile->getBestName(),
+                               $other->getBestName());
+
+        $act->actor   = ActivityObject::fromProfile($profile);
+        $act->object  = ActivityObject::fromProfile($other);
+
+        $oprofile->notifyActivity($act);
+
+        return true;
+    }
+
+    /**
+     * When one of our local users tries to join a remote group,
+     * notify the remote server. If the notification is rejected,
+     * deny the join.
+     *
+     * @param User_group $group
+     * @param User $user
+     *
+     * @return mixed hook return value
+     */
+
+    function onStartJoinGroup($group, $user)
+    {
+        $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
+        if ($oprofile) {
+            if (!$oprofile->subscribe()) {
+                throw new Exception(_m('Could not set up remote group membership.'));
+            }
+
+            $member = Profile::staticGet($user->id);
+
+            $act = new Activity();
+            $act->id = TagURI::mint('join:%d:%d:%s',
+                                    $member->id,
+                                    $group->id,
+                                    common_date_iso8601(time()));
+
+            $act->actor = ActivityObject::fromProfile($member);
+            $act->verb = ActivityVerb::JOIN;
+            $act->object = $oprofile->asActivityObject();
+
+            $act->time = time();
+            $act->title = _m("Join");
+            $act->content = sprintf(_m("%s has joined group %s."),
+                                    $member->getBestName(),
+                                    $oprofile->getBestName());
+
+            if ($oprofile->notifyActivity($act)) {
+                return true;
+            } else {
+                $oprofile->garbageCollect();
+                throw new Exception(_m("Failed joining remote group."));
+            }
+        }
+    }
+
+    /**
+     * When one of our local users leaves a remote group, notify the remote
+     * server.
+     *
+     * @fixme Might be good to schedule a resend of the leave notification
+     * if it failed due to a transitory error. We've canceled the local
+     * membership already anyway, but if the remote server comes back up
+     * it'll be left with a stray membership record.
+     *
+     * @param User_group $group
+     * @param User $user
+     *
+     * @return mixed hook return value
+     */
+
+    function onEndLeaveGroup($group, $user)
+    {
+        $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
+        if ($oprofile) {
+            // Drop the PuSH subscription if there are no other subscribers.
+            $oprofile->garbageCollect();
+
+
+            $member = Profile::staticGet($user->id);
+
+            $act = new Activity();
+            $act->id = TagURI::mint('leave:%d:%d:%s',
+                                    $member->id,
+                                    $group->id,
+                                    common_date_iso8601(time()));
+
+            $act->actor = ActivityObject::fromProfile($member);
+            $act->verb = ActivityVerb::LEAVE;
+            $act->object = $oprofile->asActivityObject();
+
+            $act->time = time();
+            $act->title = _m("Leave");
+            $act->content = sprintf(_m("%s has left group %s."),
+                                    $member->getBestName(),
+                                    $oprofile->getBestName());
+
+            $oprofile->notifyActivity($act);
+        }
+    }
+
     /**
      * Notify remote users when their notices get favorited.
      *
@@ -486,4 +621,47 @@ class OStatusPlugin extends Plugin
 
         return true;
     }
+
+    function onStartGetProfileUri($profile, &$uri)
+    {
+        $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
+        if (!empty($oprofile)) {
+            $uri = $oprofile->uri;
+            return false;
+        }
+        return true;
+    }
+
+    function onStartUserGroupHomeUrl($group, &$url)
+    {
+        return $this->onStartUserGroupPermalink($group, &$url);
+    }
+
+    function onStartUserGroupPermalink($group, &$url)
+    {
+        $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
+        if ($oprofile) {
+            // @fixme this should probably be in the user_group table
+            // @fixme this uri not guaranteed to be a profile page
+            $url = $oprofile->uri;
+            return false;
+        }
+    }
+
+    function onStartShowSubscriptionsContent($action)
+    {
+        $user = common_current_user();
+        if ($user && ($user->id == $action->profile->id)) {
+            $action->elementStart('div', 'entity_actions');
+            $action->elementStart('p', array('id' => 'entity_remote_subscribe',
+                                             'class' => 'entity_subscribe'));
+            $action->element('a', array('href' => common_local_url('ostatussub'),
+                                        'class' => 'entity_remote_subscribe')
+                                , _m('Subscribe to remote user'));
+            $action->elementEnd('p');
+            $action->elementEnd('div');
+        }
+
+        return true;
+    }
 }