]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/OStatusPlugin.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / OStatus / OStatusPlugin.php
index 0b0317316c8ae672d2974d00ddcf6dbae91d6b8c..3a9e77c2a6ca95324ef61dd3b24e4b5ec7ded9d4 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
 {
 }
@@ -209,7 +211,7 @@ class OStatusPlugin extends Plugin
 
                 // FIXME: this needs to go out in a queue handler
 
-                $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
+                $xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
                 $xml .= $notice->asAtomEntry(true, true);
 
                 $salmon = new Salmon();
@@ -222,7 +224,7 @@ class OStatusPlugin extends Plugin
      *
      */
 
-    function onEndFindMentions($sender, $text, &$mentions)
+    function onStartFindMentions($sender, $text, &$mentions)
     {
         preg_match_all('/(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)/',
                        $text,
@@ -249,58 +251,6 @@ 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.
      */
@@ -310,6 +260,7 @@ class OStatusPlugin extends Plugin
         $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
         $schema->ensureTable('feedsub', FeedSub::schemaDef());
         $schema->ensureTable('hubsub', HubSub::schemaDef());
+        $schema->ensureTable('magicsig', Magicsig::schemaDef());
         return true;
     }
 
@@ -363,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);
@@ -400,6 +395,149 @@ 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.
+
+            $members = $group->getMembers(0, 1);
+            if ($members->N == 0) {
+                common_log(LOG_INFO, "Unsubscribing from now-unused group feed $oprofile->feeduri");
+                $oprofile->unsubscribe();
+            }
+
+            $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.
      *
@@ -498,6 +636,22 @@ class OStatusPlugin extends Plugin
         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();