Add attachments to the TwAPI.

This commit is contained in:
Roger Braun 2017-03-30 17:07:03 +02:00
parent 42c90855ba
commit 73df2f8e5e
7 changed files with 73 additions and 13 deletions

View File

@ -1,6 +1,6 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
alias Pleroma.Activity alias Pleroma.Activity
def to_map(%Activity{} = activity, %{user: user} = opts) do def to_map(%Activity{} = activity, %{user: user} = opts) do
@ -16,7 +16,8 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do
"is_post_verb" => true, "is_post_verb" => true,
"created_at" => published, "created_at" => published,
"in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"], "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
end end

View File

@ -8,8 +8,13 @@ def to_map(%Object{} = object, _opts) do
%{ %{
url: url["href"], url: url["href"],
mimetype: url["mediaType"], mimetype: url["mediaType"],
id: object.id, id: data["uuid"],
oembed: false oembed: false
} }
end 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 end

View File

@ -1,5 +1,5 @@
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do 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.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
@ -8,6 +8,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def create_status(user = %User{}, data = %{}) do def create_status(user = %User{}, data = %{}) do
date = DateTime.utc_now() |> DateTime.to_iso8601 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 context = ActivityPub.generate_context_id
activity = %{ activity = %{
"type" => "Create", "type" => "Create",
@ -23,7 +27,8 @@ def create_status(user = %User{}, data = %{}) do
"context" => context "context" => context
}, },
"published" => date, "published" => date,
"context" => context "context" => context,
"attachment" => attachments
} }
# Wire up reply info. # Wire up reply info.

View File

@ -11,11 +11,22 @@ def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
end end
def status_update(%{assigns: %{user: user}} = conn, status_data) do 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 conn
|> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user})) |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
end 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 def public_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_public_statuses(user, params) statuses = TwitterAPI.fetch_public_statuses(user, params)
{:ok, json} = Poison.encode(statuses) {:ok, json} = Poison.encode(statuses)

View File

@ -1,13 +1,27 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.{User, Activity} alias Pleroma.{User, Activity, Object}
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter, ObjectRepresenter}
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
test "an activity" do test "an activity" do
{:ok, user} = UserBuilder.insert {:ok, user} = UserBuilder.insert
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) {: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" content = "Some content"
date = DateTime.utc_now() |> DateTime.to_iso8601 date = DateTime.utc_now() |> DateTime.to_iso8601
@ -19,6 +33,9 @@ test "an activity" do
User.ap_followers(user), User.ap_followers(user),
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"attachment" => [
object
],
"actor" => User.ap_id(user), "actor" => User.ap_id(user),
"object" => %{ "object" => %{
"published" => date, "published" => date,
@ -42,7 +59,10 @@ test "an activity" do
"is_post_verb" => true, "is_post_verb" => true,
"created_at" => date, "created_at" => date,
"in_reply_to_status_id" => 213123, "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 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower}) == expected_status

View File

@ -14,12 +14,13 @@ test "represent an image attachment" do
"mediaType" => "sometype", "mediaType" => "sometype",
"href" => "someurl" "href" => "someurl"
} }
] ],
"uuid" => 6
} }
} }
expected_object = %{ expected_object = %{
id: 5, id: 6,
url: "someurl", url: "someurl",
mimetype: "sometype", mimetype: "sometype",
oembed: false oembed: false

View File

@ -2,13 +2,28 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Builders.{UserBuilder, ActivityBuilder} alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.{Activity, User} alias Pleroma.{Activity, User, Object, Repo}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
test "create a status" do test "create a status" do
user = UserBuilder.build 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 = %{ input = %{
"status" => "Hello again." "status" => "Hello again.",
"media_ids" => [object.id]
} }
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input) { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
@ -24,6 +39,8 @@ test "create a status" do
assert is_binary(get_in(activity.data, ["object", "context"])) assert is_binary(get_in(activity.data, ["object", "context"]))
assert get_in(activity.data, ["object", "statusnetConversationId"]) == activity.id assert get_in(activity.data, ["object", "statusnetConversationId"]) == activity.id
assert get_in(activity.data, ["statusnetConversationId"]) == activity.id assert get_in(activity.data, ["statusnetConversationId"]) == activity.id
assert is_list(activity.data["attachment"])
end end
test "create a status that is a reply" do test "create a status that is a reply" do