]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/actions/usersalmon.php
Merge remote branch 'statusnet/1.0.x' into idle-irc-plugin
[quix0rs-gnu-social.git] / plugins / OStatus / actions / usersalmon.php
index 4363488ddc333ef7acd53b086c4ff50e21bec647..6c360c49f96c442ba4e40e8d820bcf78ed4e4457 100644 (file)
@@ -55,7 +55,10 @@ class UsersalmonAction extends SalmonAction
      */
     function handlePost()
     {
-        switch ($this->act->object->type) {
+        common_log(LOG_INFO, "Received post of '{$this->act->objects[0]->id}' from '{$this->act->actor->id}'");
+
+        // @fixme: process all activity objects?
+        switch ($this->act->objects[0]->type) {
         case ActivityObject::ARTICLE:
         case ActivityObject::BLOGENTRY:
         case ActivityObject::NOTE:
@@ -80,15 +83,23 @@ class UsersalmonAction extends SalmonAction
                 throw new ClientException("In reply to a notice not by this user");
             }
         } else if (!empty($context->attention)) {
-            if (!in_array($context->attention, $this->user->uri)) {
+            if (!in_array($this->user->uri, $context->attention) &&
+                !in_array(common_profile_url($this->user->nickname), $context->attention)) {
+                common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
                 throw new ClientException("To the attention of user(s) not including this one!");
             }
         } else {
             throw new ClientException("Not to anyone in reply to anything!");
         }
 
-        $profile = $this->ensureProfile();
-        // @fixme do something with the post
+        $existing = Notice::staticGet('uri', $this->act->objects[0]->id);
+
+        if (!empty($existing)) {
+            common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
+            return;
+        }
+
+        $this->saveNotice();
     }
 
     /**
@@ -101,7 +112,8 @@ class UsersalmonAction extends SalmonAction
         $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);
+            Subscription::start($oprofile->localProfile(),
+                                $this->user->getProfile());
         } else {
             common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
         }
@@ -131,10 +143,51 @@ class UsersalmonAction extends SalmonAction
 
     function handleFavorite()
     {
-        // WORST VARIABLE NAME EVER
-        $object = $this->act->object;
+        $notice = $this->getNotice($this->act->objects[0]);
+        $profile = $this->ensureProfile()->localProfile();
+
+        $old = Fave::pkeyGet(array('user_id' => $profile->id,
+                                   'notice_id' => $notice->id));
+
+        if (!empty($old)) {
+            throw new ClientException("We already know that's a fave!");
+        }
+
+        if (!Fave::addNew($profile, $notice)) {
+            throw new ClientException("Could not save new favorite.");
+        }
+    }
+
+    /**
+     * Remote user doesn't like one of our posts after all!
+     * Confirm the post is ours, and save a local favorite event.
+     */
+    function handleUnfavorite()
+    {
+        $notice = $this->getNotice($this->act->objects[0]);
+        $profile = $this->ensureProfile()->localProfile();
+
+        $fave = Fave::pkeyGet(array('user_id' => $profile->id,
+                                   'notice_id' => $notice->id));
+        if (empty($fave)) {
+            throw new ClientException("Notice wasn't favorited!");
+        }
+
+        $fave->delete();
+    }
 
-        switch ($this->act->object->type) {
+    /**
+     * @param ActivityObject $object
+     * @return Notice
+     * @throws ClientException on invalid input
+     */
+    function getNotice($object)
+    {
+        if (!$object) {
+            throw new ClientException("Can't favorite/unfavorite without an object.");
+        }
+
+        switch ($object->type) {
         case ActivityObject::ARTICLE:
         case ActivityObject::BLOGENTRY:
         case ActivityObject::NOTE:
@@ -155,35 +208,7 @@ class UsersalmonAction extends SalmonAction
             throw new ClientException("Notice with ID $object->id not posted by $this->user->id.");
         }
 
-        $profile = $this->ensureProfile();
-
-        $old = Fave::pkeyGet(array('user_id' => $profile->id,
-                                   'notice_id' => $notice->id));
-
-        if (!empty($old)) {
-            throw new ClientException("We already know that's a fave!");
-        }
-
-        $fave = new Fave();
-
-        // @fixme need to change this attribute name, maybe references
-        $fave->user_id   = $profile->id;
-        $fave->notice_id = $notice->id;
-
-        $result = $fave->insert();
-
-        if (!$result) {
-            common_log_db_error($fave, 'INSERT', __FILE__);
-            throw new ServerException('Could not save new favorite.');
-        }
-    }
-
-    /**
-     * Remote user doesn't like one of our posts after all!
-     * Confirm the post is ours, and save a local favorite event.
-     */
-    function handleUnfavorite()
-    {
+        return $notice;
     }
 
 }