WIP: post editing #103
4 changed files with 59 additions and 2 deletions
|
@ -124,6 +124,22 @@ def validate(%{"type" => type} = object, meta)
|
|||
end
|
||||
end
|
||||
|
||||
def validate(
|
||||
%{"type" => "Update", "object" => %{"type" => objtype} = object} = update_activity,
|
||||
meta
|
||||
)
|
||||
when objtype in ~w[Question Answer Audio Video Event Article Note Page] do
|
||||
with {:ok, object_data} <- cast_and_apply(object),
|
||||
meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
|
||||
{:ok, update_activity} <-
|
||||
update_activity
|
||||
|> UpdateValidator.cast_and_validate()
|
||||
|> Ecto.Changeset.apply_action(:insert) do
|
||||
update_activity = stringify_keys(update_activity)
|
||||
{:ok, update_activity, meta}
|
||||
end
|
||||
end
|
||||
|
||||
def validate(%{"type" => type} = object, meta)
|
||||
when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
|
||||
Answer] do
|
||||
|
|
|
@ -54,7 +54,10 @@ defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
|
|||
defp fix_url(%{"url" => url} = data) when is_map(url), do: Map.put(data, "url", url["href"])
|
||||
defp fix_url(data), do: data
|
||||
|
||||
defp fix_tag(%{"tag" => tag} = data) when is_list(tag), do: data
|
||||
defp fix_tag(%{"tag" => tag} = data) when is_list(tag) do
|
||||
Map.put(data, "tag", Enum.filter(tag, &is_map/1))
|
||||
end
|
||||
|
||||
defp fix_tag(%{"tag" => tag} = data) when is_map(tag), do: Map.put(data, "tag", [tag])
|
||||
defp fix_tag(data), do: Map.drop(data, ["tag"])
|
||||
|
||||
|
|
|
@ -38,6 +38,11 @@ test "a basic note validates", %{note: note} do
|
|||
%{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
|
||||
end
|
||||
|
||||
test "a note from factory validates" do
|
||||
note = insert(:note)
|
||||
%{valid?: true} = ArticleNotePageValidator.cast_and_validate(note.data)
|
||||
end
|
||||
|
||||
test "a note with a remote replies collection should validate", _ do
|
||||
insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
|
||||
collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
|
||||
|
|
|
@ -60,7 +60,40 @@ test "validates if the object is not of an Actor type" do
|
|||
|
||||
{:ok, update, _} = Builder.update(other_user, updated_note)
|
||||
|
||||
assert {:ok, _update, []} = ObjectValidator.validate(update, [])
|
||||
assert {:ok, _update, _} = ObjectValidator.validate(update, [])
|
||||
end
|
||||
end
|
||||
|
||||
describe "update note" do
|
||||
test "converts object into Pleroma's format" do
|
||||
mastodon_tags = [
|
||||
%{
|
||||
"icon" => %{
|
||||
"mediaType" => "image/png",
|
||||
"type" => "Image",
|
||||
"url" => "https://somewhere.org/emoji/url/1.png"
|
||||
},
|
||||
"id" => "https://somewhere.org/emoji/1",
|
||||
"name" => ":some_emoji:",
|
||||
"type" => "Emoji",
|
||||
"updated" => "2021-04-07T11:00:00Z"
|
||||
}
|
||||
]
|
||||
|
||||
user = insert(:user)
|
||||
note = insert(:note, user: user)
|
||||
|
||||
updated_note =
|
||||
note.data
|
||||
|> Map.put("content", "edited content")
|
||||
|> Map.put("tag", mastodon_tags)
|
||||
|
||||
{:ok, update, _} = Builder.update(user, updated_note)
|
||||
|
||||
assert {:ok, _update, meta} = ObjectValidator.validate(update, [])
|
||||
|
||||
assert %{"emoji" => %{"some_emoji" => "https://somewhere.org/emoji/url/1.png"}} =
|
||||
meta[:object_data]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue