]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
OStatus: hooked up follow/unfollow events on Salmon endpoint to create/destroy remote...
authorBrion Vibber <brion@pobox.com>
Fri, 19 Feb 2010 20:08:07 +0000 (12:08 -0800)
committerBrion Vibber <brion@pobox.com>
Fri, 19 Feb 2010 20:08:07 +0000 (12:08 -0800)
plugins/OStatus/OStatusPlugin.php
plugins/OStatus/actions/feedsubsettings.php
plugins/OStatus/actions/salmon.php
plugins/OStatus/classes/Ostatus_profile.php

index 4ebe4551ecbaf2991fd2079c851e87413f944b71..4c43d8a303e3c4878316f830dacabb50fbe3bc3f 100644 (file)
@@ -253,17 +253,22 @@ class OStatusPlugin extends Plugin
      */
     function onEndUnsubscribe($user, $other)
     {
+        if ($user instanceof Profile) {
+            $profile = $user;
+        } else if ($user instanceof Profile) {
+            $profile = $user->getProfile();
+        }
         $oprofile = Ostatus_profile::staticGet('profile_id', $other->id);
         if ($oprofile) {
             // Notify the remote server of the unsub, if supported.
-            $oprofile->notify($user->getProfile(), ActivityVerb::UNFOLLOW, $oprofile);
+            $oprofile->notify($profile, ActivityVerb::UNFOLLOW, $oprofile);
 
             // Drop the PuSH subscription if there are no other subscribers.
             $sub = new Subscription();
             $sub->subscribed = $other->id;
             $sub->limit(1);
             if (!$sub->find(true)) {
-                common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri on hub $oprofile->huburi");
+                common_log(LOG_INFO, "Unsubscribing from now-unused feed $oprofile->feeduri");
                 $oprofile->unsubscribe();
             }
         }
index 3e1d0aa823effed5be9a17564648d396c8a7c34f..aee4cee9af0942bb77e3f99bfa0e651aea2353e3 100644 (file)
@@ -68,14 +68,6 @@ class FeedSubSettingsAction extends ConnectSettingsAction
 
         $profile = $user->getProfile();
 
-        $fuser = null;
-
-        $flink = Foreign_link::getByUserID($user->id, FEEDSUB_SERVICE);
-
-        if (!empty($flink)) {
-            $fuser = $flink->getForeignUser();
-        }
-
         $this->elementStart('form', array('method' => 'post',
                                           'id' => 'form_settings_feedsub',
                                           'class' => 'form_settings',
index 2da9db9eb74beca203fcb1d6be87ffeda54bf36e..1ed322ed28f89cf4142e371f6cb058382777ab87 100644 (file)
@@ -63,13 +63,17 @@ class SalmonAction extends Action
         // XXX: check that document element is Atom entry
         // XXX: check the signature
 
-        $this->act = new Activity($dom->documentElement);
+        $entries = $dom->getElementsByTagNameNS(Activity::ATOM, 'entry');
+        if ($entries && $entries->length) {
+            // @fixme is it legit to have multiple entries?
+            $this->act = new Activity($entries->item(0), $dom->documentElement);
+        }
 
         return true;
     }
 
     /**
-     * @fixme probably call Ostatus_profile::processFeed
+     * Check the posted activity type and break out to appropriate processing.
      */
 
     function handle($args)
@@ -94,13 +98,19 @@ class SalmonAction extends Action
             case ActivityVerb::FRIEND:
                 $this->handleFollow();
                 break;
+            case ActivityVerb::UNFOLLOW:
+                $this->handleUnfollow();
+                break;
             }
             Event::handle('EndHandleSalmon', array($this->user, $this->activity));
         }
     }
 
     /**
-     * @fixme probably call Ostatus_profile::processFeed
+     * We've gotten a post event on the Salmon backchannel, probably a reply.
+     *
+     * @todo validate if we need to handle this post, then call into
+     * ostatus_profile's general incoming-post handling.
      */
     function handlePost()
     {
@@ -116,43 +126,81 @@ class SalmonAction extends Action
         }
 
         $profile = $this->ensureProfile();
+        // @fixme do something with the post
     }
 
     /**
-     * @fixme probably call Ostatus_profile::processFeed
+     * We've gotten a follow/subscribe notification from a remote user.
+     * Save a subscription relationship for them.
      */
     function handleFollow()
     {
+        $oprofile = $this->ensureProfile();
+        if ($oprofile) {
+            common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
+            $oprofile->subscribeRemoteToLocal($this->user);
+        } else {
+            common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
+        }
+    }
+
+    /**
+     * We've gotten an unfollow/unsubscribe notification from a remote user.
+     * Check if we have a subscription relationship for them and kill it.
+     *
+     * @fixme probably catch exceptions on fail?
+     */
+    function handleUnfollow()
+    {
+        $oprofile = $this->ensureProfile();
+        if ($oprofile) {
+            common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
+            Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
+        } else {
+            common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
+        }
     }
 
     /**
-     * @fixme probably call Ostatus_profile::processFeed
+     * Remote user likes one of our posts.
+     * Confirm the post is ours, and save a local favorite event.
      */
     function handleFavorite()
     {
     }
 
     /**
-     * @fixme probably call Ostatus_profile::processFeed
+     * Remote user doesn't like one of our posts after all!
+     * Confirm the post is ours, and save a local favorite event.
+     */
+    function handleUnfavorite()
+    {
+    }
+
+    /**
+     * Hmmmm
      */
     function handleShare()
     {
     }
 
+    /**
+     * @return Ostatus_profile
+     */
     function ensureProfile()
     {
         $actor = $this->act->actor;
         common_log(LOG_DEBUG, "Received salmon bit: " . var_export($this->act, true));
         if (empty($actor->id)) {
+            common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
             throw new Exception("Received a salmon slap from unidentified actor.");
         }
 
-        $ostatusProfile = Ostatus_profile::ensureActorProfile($this->act);
-        return $oprofile->localProfile();
+        return Ostatus_profile::ensureActorProfile($this->act);
     }
 
     /**
-     * @fixme anything new in here probably should be merged into Ostatus_profile::ensureActorProfile and friends
+     * @fixme merge into Ostatus_profile::ensureActorProfile and friends
      */
     function createProfile()
     {
index 319b76a661c5802b09d3c0c91d7d5894912abee5..97d8eec10130832a06aab4b2aab6b3660aaddc74 100644 (file)
@@ -310,8 +310,15 @@ class Ostatus_profile extends Memcached_DataObject
      * @param $verb eg Activity::SUBSCRIBE or Activity::JOIN
      * @param $object object of the action; if null, the remote entity itself is assumed
      */
-    public function notify(Profile $actor, $verb, $object=null)
+    public function notify($actor, $verb, $object=null)
     {
+        if (!($actor instanceof Profile)) {
+            $type = gettype($actor);
+            if ($type == 'object') {
+                $type = get_class($actor);
+            }
+            throw new ServerException("Invalid actor passed to " . __METHOD__ . ": " . $type);
+        }
         if ($object == null) {
             $object = $this;
         }
@@ -340,7 +347,7 @@ class Ostatus_profile extends Memcached_DataObject
             $feed->addEntry($entry);
 
             $xml = $feed->getString();
-            common_log(LOG_INFO, "Posting to Salmon endpoint $salmon: $xml");
+            common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
 
             $salmon = new Salmon(); // ?
             $salmon->post($this->salmonuri, $xml);
@@ -531,9 +538,14 @@ class Ostatus_profile extends Memcached_DataObject
         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
         copy($url, $temp_filename);
 
+        if ($this->isGroup()) {
+            $id = $this->group_id;
+        } else {
+            $id = $this->profile_id;
+        }
         // @fixme should we be using different ids?
-        $imagefile = new ImageFile($this->id, $temp_filename);
-        $filename = Avatar::filename($this->id,
+        $imagefile = new ImageFile($id, $temp_filename);
+        $filename = Avatar::filename($id,
                                      image_type_to_extension($imagefile->type),
                                      null,
                                      common_timestamp());
@@ -646,7 +658,7 @@ class Ostatus_profile extends Memcached_DataObject
         $actor = $activity->actor;
         $homeuri = self::getActorProfileURI($activity);
         $nickname = self::getAuthorNick($activity);
-        $avatar = self::getAvatar($actor, $feed);
+        $avatar = self::getAvatar($actor, $activity->feed);
 
         if (!$homeuri) {
             common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));