Add attachments to the TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Thu, 30 Mar 2017 15:07:03 +0000 (17:07 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 30 Mar 2017 15:07:22 +0000 (17:07 +0200)
lib/pleroma/web/twitter_api/representers/activity_representer.ex
lib/pleroma/web/twitter_api/representers/object_representer.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/representers/activity_representer_test.exs
test/web/twitter_api/representers/object_representer_test.exs
test/web/twitter_api/twitter_api_test.exs

index 05d1306390592abc836374a13be8363c5808c148..bcb819200f93ba5308a292c7c1b44b586c790cf5 100644 (file)
@@ -1,6 +1,6 @@
 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
   use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
-  alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
   alias Pleroma.Activity
 
   def to_map(%Activity{} = activity, %{user: user} = opts) do
@@ -16,7 +16,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       "is_post_verb" => true,
       "created_at" => published,
       "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
-      "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"]
+      "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
+      "attachments" => (activity.data["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts)
     }
   end
 end
index 57ab24caa4f2632b472602063402d17182af7b7c..c39b607605421449f49d4015be75920e49bcd7c9 100644 (file)
@@ -8,8 +8,13 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter do
     %{
       url: url["href"],
       mimetype: url["mediaType"],
-      id: object.id,
+      id: data["uuid"],
       oembed: false
     }
   end
+
+  # If we only get the naked data, wrap in an object
+  def to_map(%{} = data, opts) do
+    to_map(%Object{data: data}, opts)
+  end
 end
index 0624e73aa0cd19bf860bda2268cb30eb9a6467ba..ab2b1aab792cd6123d297f8cdae7dad70c2dd543 100644 (file)
@@ -1,5 +1,5 @@
 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
-  alias Pleroma.{User, Activity, Repo}
+  alias Pleroma.{User, Activity, Repo, Object}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
@@ -8,6 +8,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   def create_status(user = %User{}, data = %{}) do
     date = DateTime.utc_now() |> DateTime.to_iso8601
 
+    attachments = Enum.map(data["media_ids"] || [], fn (media_id) ->
+      Repo.get(Object, media_id).data
+    end)
+
     context = ActivityPub.generate_context_id
     activity = %{
       "type" => "Create",
@@ -23,7 +27,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
         "context" => context
       },
       "published" => date,
-      "context" => context
+      "context" => context,
+      "attachment" => attachments
     }
 
     # Wire up reply info.
index a26885bf148c44729fe948559a113840ce84a2a1..2154f34f7de828aa55c2b385ef1f6fd6f1e1fc28 100644 (file)
@@ -11,11 +11,22 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   end
 
   def status_update(%{assigns: %{user: user}} = conn, status_data) do
-    {:ok, activity} = TwitterAPI.create_status(user, status_data)
+    media_ids = extract_media_ids(status_data)
+    {:ok, activity} = TwitterAPI.create_status(user, %{ "status" => status_data["status"], "media_ids" => media_ids })
     conn
     |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
   end
 
+  defp extract_media_ids(status_data) do
+    with media_ids when not is_nil(media_ids) <- status_data["media_ids"],
+         split_ids <- String.split(media_ids, ","),
+         clean_ids <- Enum.reject(split_ids, fn (id) -> String.length(id) == 0 end)
+      do
+        clean_ids
+      else _e -> []
+    end
+  end
+
   def public_timeline(%{assigns: %{user: user}} = conn, params) do
     statuses = TwitterAPI.fetch_public_statuses(user, params)
     {:ok, json} = Poison.encode(statuses)
index 376d315755149542c3032dad3e4655a3b60f327a..96e6dc98ec76956d84778b45943a31e7a94c2097 100644 (file)
@@ -1,13 +1,27 @@
 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
   use Pleroma.DataCase
-  alias Pleroma.{User, Activity}
-  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
+  alias Pleroma.{User, Activity, Object}
+  alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter, ObjectRepresenter}
   alias Pleroma.Builders.UserBuilder
 
   test "an activity" do
     {:ok, user} = UserBuilder.insert
     {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
 
+    object = %Object{
+      data: %{
+        "type" => "Image",
+        "url" => [
+          %{
+            "type" => "Link",
+            "mediaType" => "image/jpg",
+            "href" => "http://example.org/image.jpg"
+          }
+        ],
+        "uuid" => 1
+      }
+    }
+
     content = "Some content"
     date = DateTime.utc_now() |> DateTime.to_iso8601
 
@@ -19,6 +33,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
           User.ap_followers(user),
           "https://www.w3.org/ns/activitystreams#Public"
         ],
+        "attachment" => [
+          object
+        ],
         "actor" => User.ap_id(user),
         "object" => %{
           "published" => date,
@@ -42,7 +59,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
       "is_post_verb" => true,
       "created_at" => date,
       "in_reply_to_status_id" => 213123,
-      "statusnet_conversation_id" => 4711
+      "statusnet_conversation_id" => 4711,
+      "attachments" => [
+        ObjectRepresenter.to_map(object)
+      ]
     }
 
     assert ActivityRepresenter.to_map(activity, %{user: user, for: follower}) == expected_status
index fc6e0aff988877b35556948b87cb7d6de8e48f2c..791b30237b3032cdc033a71e2739d37818ee6a1e 100644 (file)
@@ -14,12 +14,13 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectReprenterTest do
             "mediaType" => "sometype",
             "href" => "someurl"
           }
-        ]
+        ],
+        "uuid" => 6
       }
     }
 
     expected_object = %{
-      id: 5,
+      id: 6,
       url: "someurl",
       mimetype: "sometype",
       oembed: false
index dcffab2b87907fd03d58785aab89ed3c826c8efc..97657eae07e194a7619bcd1d8c5f5f17e5872f9c 100644 (file)
@@ -2,13 +2,28 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
   use Pleroma.DataCase
   alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
   alias Pleroma.Web.TwitterAPI.TwitterAPI
-  alias Pleroma.{Activity, User}
+  alias Pleroma.{Activity, User, Object, Repo}
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
   test "create a status" do
     user = UserBuilder.build
+    object_data = %{
+      "type" => "Image",
+      "url" => [
+        %{
+          "type" => "Link",
+          "mediaType" => "image/jpg",
+          "href" => "http://example.org/image.jpg"
+        }
+      ],
+      "uuid" => 1
+    }
+
+    object = Repo.insert!(%Object{data: object_data})
+
     input = %{
-      "status" => "Hello again."
+      "status" => "Hello again.",
+      "media_ids" => [object.id]
     }
 
     { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
@@ -24,6 +39,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert is_binary(get_in(activity.data, ["object", "context"]))
     assert get_in(activity.data, ["object", "statusnetConversationId"]) == activity.id
     assert get_in(activity.data, ["statusnetConversationId"]) == activity.id
+
+    assert is_list(activity.data["attachment"])
   end
 
   test "create a status that is a reply" do