[#210] TwitterAPI: implemented /api/media/metadata/create

to allow uploads description (alt text) setting.
This commit is contained in:
Ivan Tashkinov 2018-12-04 18:35:57 +03:00
parent 88b05aeabb
commit 826fc446d5
3 changed files with 38 additions and 1 deletions

View file

@ -324,6 +324,7 @@ defmodule Pleroma.Web.Router do
post("/statusnet/media/upload", TwitterAPI.Controller, :upload)
post("/media/upload", TwitterAPI.Controller, :upload_json)
post("/media/metadata/create", TwitterAPI.Controller, :update_media)
post("/favorites/create/:id", TwitterAPI.Controller, :favorite)
post("/favorites/create", TwitterAPI.Controller, :favorite)

View file

@ -4,7 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView}
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils
alias Pleroma.{Repo, Activity, User, Notification}
alias Pleroma.{Repo, Activity, Object, User, Notification}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
alias Ecto.Changeset
@ -226,6 +226,22 @@ def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
end
end
@doc "https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-metadata-create"
def update_media(%{assigns: %{user: _}} = conn, %{"media_id" => id} = data) do
description = get_in(data, ["alt_text", "text"]) || data["name"] || data["description"]
with %Object{} = object <- Repo.get(Object, id), is_binary(description) do
new_data = Map.put(object.data, "name", description)
change = Object.change(object, %{data: new_data})
{:ok, _} = Repo.update(change)
end
conn
|> put_status(:no_content)
|> json("")
end
def upload(conn, %{"media" => media}) do
response = TwitterAPI.upload(media)

View file

@ -1253,4 +1253,24 @@ test "it returns users, ordered by similarity", %{conn: conn} do
assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end)
end
end
describe "POST /api/media/metadata/create" do
test "it updates `data[name]` of referenced Object with provided value", %{conn: conn} do
user = insert(:user)
object = insert(:note)
description = "Informative description of the image. Initial: #{object.data["name"]}}"
_conn =
conn
|> assign(:user, user)
|> post("/api/media/metadata/create.json", %{
"media_id" => object.id,
"alt_text" => %{"text" => description}
})
|> json_response(:no_content)
object = Repo.get!(Object, object.id)
assert object.data["name"] == description
end
end
end