Return the corresponding object id in attachment view

This commit is contained in:
Tusooa Zhu 2022-06-08 11:45:24 -04:00 committed by FloatingGhost
parent a9038093ba
commit a0681391a1
4 changed files with 48 additions and 4 deletions

View file

@ -37,7 +37,7 @@ def attachments_from_ids_no_descs([]), do: []
def attachments_from_ids_no_descs(ids) do def attachments_from_ids_no_descs(ids) do
Enum.map(ids, fn media_id -> Enum.map(ids, fn media_id ->
case Repo.get(Object, media_id) do case get_attachment(media_id) do
%Object{data: data} -> data %Object{data: data} -> data
_ -> nil _ -> nil
end end
@ -51,13 +51,17 @@ def attachments_from_ids_descs(ids, descs_str) do
{_, descs} = Jason.decode(descs_str) {_, descs} = Jason.decode(descs_str)
Enum.map(ids, fn media_id -> Enum.map(ids, fn media_id ->
with %Object{data: data} <- Repo.get(Object, media_id) do with %Object{data: data} <- get_attachment(media_id) do
Map.put(data, "name", descs[media_id]) Map.put(data, "name", descs[media_id])
end end
end) end)
|> Enum.reject(&is_nil/1) |> Enum.reject(&is_nil/1)
end end
defp get_attachment(media_id) do
Repo.get(Object, media_id)
end
@spec get_to_and_cc(ActivityDraft.t()) :: {list(String.t()), list(String.t())} @spec get_to_and_cc(ActivityDraft.t()) :: {list(String.t()), list(String.t())}
def get_to_and_cc(%{in_reply_to_conversation: %Participation{} = participation}) do def get_to_and_cc(%{in_reply_to_conversation: %Participation{} = participation}) do

View file

@ -539,10 +539,19 @@ def render("attachment.json", %{attachment: attachment}) do
true -> "unknown" true -> "unknown"
end end
attachment_id =
with {_, ap_id} when is_binary(ap_id) <- {:ap_id, attachment["id"]},
{_, %Object{data: _object_data, id: object_id}} <-
{:object, Object.get_by_ap_id(ap_id)} do
to_string(object_id)
else
_ ->
<<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href) <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
to_string(attachment["id"] || hash_id)
end
%{ %{
id: to_string(attachment["id"] || hash_id), id: attachment_id,
url: href, url: href,
remote_url: href, remote_url: href,
preview_url: href_preview, preview_url: href_preview,

View file

@ -2264,6 +2264,25 @@ test "it updates the status", %{conn: conn, user: user} do
assert response["spoiler_text"] == "lol" assert response["spoiler_text"] == "lol"
end end
test "it updates the attachments", %{conn: conn, user: user} do
attachment = insert(:attachment, user: user)
attachment_id = to_string(attachment.id)
{:ok, activity} = CommonAPI.post(user, %{status: "mew mew #abc", spoiler_text: "#def"})
response =
conn
|> put_req_header("content-type", "application/json")
|> put("/api/v1/statuses/#{activity.id}", %{
"status" => "mew mew #abc",
"spoiler_text" => "#def",
"media_ids" => [attachment_id]
})
|> json_response_and_validate_schema(200)
assert [%{"id" => ^attachment_id}] = response["media_attachments"]
end
test "it does not update visibility", %{conn: conn, user: user} do test "it does not update visibility", %{conn: conn, user: user} do
{:ok, activity} = {:ok, activity} =
CommonAPI.post(user, %{ CommonAPI.post(user, %{

View file

@ -111,6 +111,18 @@ def note_factory(attrs \\ %{}) do
} }
end end
def attachment_factory(attrs \\ %{}) do
user = attrs[:user] || insert(:user)
data =
attachment_data(user.ap_id, nil)
|> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
%Pleroma.Object{
data: merge_attributes(data, Map.get(attrs, :data, %{}))
}
end
def attachment_note_factory(attrs \\ %{}) do def attachment_note_factory(attrs \\ %{}) do
user = attrs[:user] || insert(:user) user = attrs[:user] || insert(:user)
{length, attrs} = Map.pop(attrs, :length, 1) {length, attrs} = Map.pop(attrs, :length, 1)