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)
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
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)
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