]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/actions/usersalmon.php
Notify Twitter when StatusNet user faves/disfaves a Twitter notice
[quix0rs-gnu-social.git] / plugins / OStatus / actions / usersalmon.php
index 12c74798f20d8d2b401c59c31008fc014db94e9e..06a72bf0244331be868a790eb38b6bd3f7697ae3 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->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
+
+        // @fixme: process all activity objects?
+        switch ($this->activity->objects[0]->type) {
         case ActivityObject::ARTICLE:
         case ActivityObject::BLOGENTRY:
         case ActivityObject::NOTE:
@@ -68,25 +71,36 @@ class UsersalmonAction extends SalmonAction
 
         // Notice must either be a) in reply to a notice by this user
         // or b) to the attention of this user
+        // or c) in reply to a notice to the attention of this user
 
-        $context = $this->act->context;
+        $context = $this->activity->context;
 
         if (!empty($context->replyToID)) {
             $notice = Notice::staticGet('uri', $context->replyToID);
             if (empty($notice)) {
                 throw new ClientException("In reply to unknown notice");
             }
-            if ($notice->profile_id != $this->user->id) {
-                throw new ClientException("In reply to a notice not by this user");
+            if ($notice->profile_id != $this->user->id &&
+                !in_array($this->user->id, $notice->getReplies())) {
+                throw new ClientException("In reply to a notice not by this user and not mentioning 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!");
         }
 
+        $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
+
+        if (!empty($existing)) {
+            common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
+            return;
+        }
+
         $this->saveNotice();
     }
 
@@ -131,7 +145,7 @@ class UsersalmonAction extends SalmonAction
 
     function handleFavorite()
     {
-        $notice = $this->getNotice($this->act->object);
+        $notice = $this->getNotice($this->activity->objects[0]);
         $profile = $this->ensureProfile()->localProfile();
 
         $old = Fave::pkeyGet(array('user_id' => $profile->id,
@@ -152,7 +166,7 @@ class UsersalmonAction extends SalmonAction
      */
     function handleUnfavorite()
     {
-        $notice = $this->getNotice($this->act->object);
+        $notice = $this->getNotice($this->activity->objects[0]);
         $profile = $this->ensureProfile()->localProfile();
 
         $fave = Fave::pkeyGet(array('user_id' => $profile->id,