Handle incoming Question objects
authorrinpatch <rinpatch@sdf.org>
Wed, 15 May 2019 17:10:16 +0000 (20:10 +0300)
committerrinpatch <rinpatch@sdf.org>
Wed, 15 May 2019 17:10:16 +0000 (20:10 +0300)
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/activity_pub/utils.ex
test/fixtures/httpoison_mock/rinpatch.json [new file with mode: 0644]
test/fixtures/mastodon-question-activity.json [new file with mode: 0644]
test/support/http_request_mock.ex
test/web/activity_pub/transmogrifier_test.exs

index 508f3532f2de46398976c5672e0ffdceff171567..c2596cfecb9e2d640fb3b171573610561c73382f 100644 (file)
@@ -399,7 +399,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   # - tags
   # - emoji
   def handle_incoming(%{"type" => "Create", "object" => %{"type" => objtype} = object} = data)
   # - tags
   # - emoji
   def handle_incoming(%{"type" => "Create", "object" => %{"type" => objtype} = object} = data)
-      when objtype in ["Article", "Note", "Video", "Page"] do
+      when objtype in ["Article", "Note", "Video", "Page", "Question"] do
     actor = Containment.get_actor(data)
 
     data =
     actor = Containment.get_actor(data)
 
     data =
index 581b9d1ab3e0f232b4e35e954dd5dbeffb4999a6..de91fa03f69137af43b9be0ec4bf37d155cb36ca 100644 (file)
@@ -19,7 +19,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   require Logger
 
 
   require Logger
 
-  @supported_object_types ["Article", "Note", "Video", "Page"]
+  @supported_object_types ["Article", "Note", "Video", "Page", "Question"]
 
   # Some implementations send the actor URI as the actor field, others send the entire actor object,
   # so figure out what the actor's URI is based on what we have.
 
   # Some implementations send the actor URI as the actor field, others send the entire actor object,
   # so figure out what the actor's URI is based on what we have.
diff --git a/test/fixtures/httpoison_mock/rinpatch.json b/test/fixtures/httpoison_mock/rinpatch.json
new file mode 100644 (file)
index 0000000..59311ec
--- /dev/null
@@ -0,0 +1,64 @@
+{
+  "@context": [
+    "https://www.w3.org/ns/activitystreams",
+    "https://w3id.org/security/v1",
+    {
+      "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
+      "toot": "http://joinmastodon.org/ns#",
+      "featured": {
+        "@id": "toot:featured",
+        "@type": "@id"
+      },
+      "alsoKnownAs": {
+        "@id": "as:alsoKnownAs",
+        "@type": "@id"
+      },
+      "movedTo": {
+        "@id": "as:movedTo",
+        "@type": "@id"
+      },
+      "schema": "http://schema.org#",
+      "PropertyValue": "schema:PropertyValue",
+      "value": "schema:value",
+      "Hashtag": "as:Hashtag",
+      "Emoji": "toot:Emoji",
+      "IdentityProof": "toot:IdentityProof",
+      "focalPoint": {
+        "@container": "@list",
+        "@id": "toot:focalPoint"
+      }
+    }
+  ],
+  "id": "https://mastodon.sdf.org/users/rinpatch",
+  "type": "Person",
+  "following": "https://mastodon.sdf.org/users/rinpatch/following",
+  "followers": "https://mastodon.sdf.org/users/rinpatch/followers",
+  "inbox": "https://mastodon.sdf.org/users/rinpatch/inbox",
+  "outbox": "https://mastodon.sdf.org/users/rinpatch/outbox",
+  "featured": "https://mastodon.sdf.org/users/rinpatch/collections/featured",
+  "preferredUsername": "rinpatch",
+  "name": "rinpatch",
+  "summary": "<p>umu</p>",
+  "url": "https://mastodon.sdf.org/@rinpatch",
+  "manuallyApprovesFollowers": false,
+  "publicKey": {
+    "id": "https://mastodon.sdf.org/users/rinpatch#main-key",
+    "owner": "https://mastodon.sdf.org/users/rinpatch",
+    "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1vbhYKDopb5xzfJB2TZY\n0ZvgxqdAhbSKKkQC5Q2b0ofhvueDy2AuZTnVk1/BbHNlqKlwhJUSpA6LiTZVvtcc\nMn6cmSaJJEg30gRF5GARP8FMcuq8e2jmceiW99NnUX17MQXsddSf2JFUwD0rUE8H\nBsgD7UzE9+zlA/PJOTBO7fvBEz9PTQ3r4sRMTJVFvKz2MU/U+aRNTuexRKMMPnUw\nfp6VWh1F44VWJEQOs4tOEjGiQiMQh5OfBk1w2haT3vrDbQvq23tNpUP1cRomLUtx\nEBcGKi5DMMBzE1RTVT1YUykR/zLWlA+JSmw7P6cWtsHYZovs8dgn8Po3X//6N+ng\nTQIDAQAB\n-----END PUBLIC KEY-----\n"
+  },
+  "tag": [],
+  "attachment": [],
+  "endpoints": {
+    "sharedInbox": "https://mastodon.sdf.org/inbox"
+  },
+  "icon": {
+    "type": "Image",
+    "mediaType": "image/jpeg",
+    "url": "https://mastodon.sdf.org/system/accounts/avatars/000/067/580/original/bf05521bf711b7a0.jpg?1533238802"
+  },
+  "image": {
+    "type": "Image",
+    "mediaType": "image/gif",
+    "url": "https://mastodon.sdf.org/system/accounts/headers/000/067/580/original/a99b987e798f7063.gif?1533278217"
+  }
+}
diff --git a/test/fixtures/mastodon-question-activity.json b/test/fixtures/mastodon-question-activity.json
new file mode 100644 (file)
index 0000000..ac329c7
--- /dev/null
@@ -0,0 +1,99 @@
+{
+  "@context": [
+    "https://www.w3.org/ns/activitystreams",
+    {
+      "ostatus": "http://ostatus.org#",
+      "atomUri": "ostatus:atomUri",
+      "inReplyToAtomUri": "ostatus:inReplyToAtomUri",
+      "conversation": "ostatus:conversation",
+      "sensitive": "as:sensitive",
+      "Hashtag": "as:Hashtag",
+      "toot": "http://joinmastodon.org/ns#",
+      "Emoji": "toot:Emoji",
+      "focalPoint": {
+        "@container": "@list",
+        "@id": "toot:focalPoint"
+      }
+    }
+  ],
+  "id": "https://mastodon.sdf.org/users/rinpatch/statuses/102070944809637304/activity",
+  "type": "Create",
+  "actor": "https://mastodon.sdf.org/users/rinpatch",
+  "published": "2019-05-10T09:03:36Z",
+  "to": [
+    "https://www.w3.org/ns/activitystreams#Public"
+  ],
+  "cc": [
+    "https://mastodon.sdf.org/users/rinpatch/followers"
+  ],
+  "object": {
+    "id": "https://mastodon.sdf.org/users/rinpatch/statuses/102070944809637304",
+    "type": "Question",
+    "summary": null,
+    "inReplyTo": null,
+    "published": "2019-05-10T09:03:36Z",
+    "url": "https://mastodon.sdf.org/@rinpatch/102070944809637304",
+    "attributedTo": "https://mastodon.sdf.org/users/rinpatch",
+    "to": [
+      "https://www.w3.org/ns/activitystreams#Public"
+    ],
+    "cc": [
+      "https://mastodon.sdf.org/users/rinpatch/followers"
+    ],
+    "sensitive": false,
+    "atomUri": "https://mastodon.sdf.org/users/rinpatch/statuses/102070944809637304",
+    "inReplyToAtomUri": null,
+    "conversation": "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation",
+    "content": "<p>Why is Tenshi eating a corndog so cute?</p>",
+    "contentMap": {
+      "en": "<p>Why is Tenshi eating a corndog so cute?</p>"
+    },
+    "endTime": "2019-05-11T09:03:36Z",
+    "closed": "2019-05-11T09:03:36Z",
+    "attachment": [],
+    "tag": [],
+    "replies": {
+      "id": "https://mastodon.sdf.org/users/rinpatch/statuses/102070944809637304/replies",
+      "type": "Collection",
+      "first": {
+        "type": "CollectionPage",
+        "partOf": "https://mastodon.sdf.org/users/rinpatch/statuses/102070944809637304/replies",
+        "items": []
+      }
+    },
+    "oneOf": [
+      {
+        "type": "Note",
+        "name": "Dunno",
+        "replies": {
+          "type": "Collection",
+          "totalItems": 0
+        }
+      },
+      {
+        "type": "Note",
+        "name": "Everyone knows that!",
+        "replies": {
+          "type": "Collection",
+          "totalItems": 1
+        }
+      },
+      {
+        "type": "Note",
+        "name": "25 char limit is dumb",
+        "replies": {
+          "type": "Collection",
+          "totalItems": 0
+        }
+      },
+      {
+        "type": "Note",
+        "name": "I can't even fit a funny",
+        "replies": {
+          "type": "Collection",
+          "totalItems": 1
+        }
+      }
+    ]
+  }
+}
index 5b355bfe6088c8f87f214baa992116a88eed05e9..3064c032b133e48ad0313e075ae007b7da056a37 100644 (file)
@@ -52,6 +52,14 @@ defmodule HttpRequestMock do
      }}
   end
 
      }}
   end
 
+  def get("https://mastodon.sdf.org/users/rinpatch", _, _, _) do
+    {:ok,
+     %Tesla.Env{
+       status: 200,
+       body: File.read!("test/fixtures/httpoison_mock/rinpatch.json")
+     }}
+  end
+
   def get(
         "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie",
         _,
   def get(
         "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie",
         _,
index c24b50f8c833a4f1513edb8841b51cb7153fdc25..727abbd17c7f50f82aa162a816a5299fc6617c38 100644 (file)
@@ -113,6 +113,23 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert Enum.at(object.data["tag"], 2) == "moo"
     end
 
       assert Enum.at(object.data["tag"], 2) == "moo"
     end
 
+    test "it works for incoming questions" do
+      data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
+
+      {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
+
+      object = Object.normalize(activity)
+
+      assert Enum.all?(object.data["oneOf"], fn choice ->
+               choice["name"] in [
+                 "Dunno",
+                 "Everyone knows that!",
+                 "25 char limit is dumb",
+                 "I can't even fit a funny"
+               ]
+             end)
+    end
+
     test "it works for incoming notices with contentMap" do
       data =
         File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()
     test "it works for incoming notices with contentMap" do
       data =
         File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Poison.decode!()