mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-24 18:15:19 +00:00
[#210] TwitterAPI: implemented /api/media/metadata/create
to allow uploads description (alt text) setting.
This commit is contained in:
parent
88b05aeabb
commit
826fc446d5
|
@ -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)
|
||||
|
|
|
@ -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 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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)
|
||||
|
||||
|
|
|
@ -1253,4 +1253,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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
|
||||
|
|
Loading…
Reference in a new issue