added tests /activity_pub/transmogrifier.ex
This commit is contained in:
parent
ceb2e09126
commit
a31af93e1d
|
@ -41,8 +41,7 @@ def fix_object(object, options \\ []) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_summary(%{"summary" => nil} = object) do
|
def fix_summary(%{"summary" => nil} = object) do
|
||||||
object
|
Map.put(object, "summary", "")
|
||||||
|> Map.put("summary", "")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_summary(%{"summary" => _} = object) do
|
def fix_summary(%{"summary" => _} = object) do
|
||||||
|
@ -50,10 +49,7 @@ def fix_summary(%{"summary" => _} = object) do
|
||||||
object
|
object
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_summary(object) do
|
def fix_summary(object), do: Map.put(object, "summary", "")
|
||||||
object
|
|
||||||
|> Map.put("summary", "")
|
|
||||||
end
|
|
||||||
|
|
||||||
def fix_addressing_list(map, field) do
|
def fix_addressing_list(map, field) do
|
||||||
cond do
|
cond do
|
||||||
|
@ -73,13 +69,9 @@ def fix_explicit_addressing(
|
||||||
explicit_mentions,
|
explicit_mentions,
|
||||||
follower_collection
|
follower_collection
|
||||||
) do
|
) do
|
||||||
explicit_to =
|
explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
|
||||||
to
|
|
||||||
|> Enum.filter(fn x -> x in explicit_mentions end)
|
|
||||||
|
|
||||||
explicit_cc =
|
explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
|
||||||
to
|
|
||||||
|> Enum.filter(fn x -> x not in explicit_mentions end)
|
|
||||||
|
|
||||||
final_cc =
|
final_cc =
|
||||||
(cc ++ explicit_cc)
|
(cc ++ explicit_cc)
|
||||||
|
@ -97,13 +89,19 @@ def fix_explicit_addressing(object, _explicit_mentions, _followers_collection),
|
||||||
def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
|
def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
|
||||||
|
|
||||||
def fix_explicit_addressing(object) do
|
def fix_explicit_addressing(object) do
|
||||||
explicit_mentions =
|
explicit_mentions = Utils.determine_explicit_mentions(object)
|
||||||
|
|
||||||
|
%User{follower_address: follower_collection} =
|
||||||
object
|
object
|
||||||
|> Utils.determine_explicit_mentions()
|
|> Containment.get_actor()
|
||||||
|
|> User.get_cached_by_ap_id()
|
||||||
|
|
||||||
follower_collection = User.get_cached_by_ap_id(Containment.get_actor(object)).follower_address
|
explicit_mentions =
|
||||||
|
explicit_mentions ++
|
||||||
explicit_mentions = explicit_mentions ++ [Pleroma.Constants.as_public(), follower_collection]
|
[
|
||||||
|
Pleroma.Constants.as_public(),
|
||||||
|
follower_collection
|
||||||
|
]
|
||||||
|
|
||||||
fix_explicit_addressing(object, explicit_mentions, follower_collection)
|
fix_explicit_addressing(object, explicit_mentions, follower_collection)
|
||||||
end
|
end
|
||||||
|
@ -147,48 +145,25 @@ def fix_addressing(object) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_actor(%{"attributedTo" => actor} = object) do
|
def fix_actor(%{"attributedTo" => actor} = object) do
|
||||||
object
|
Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
|
||||||
|> Map.put("actor", Containment.get_actor(%{"actor" => actor}))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_in_reply_to(object, options \\ [])
|
def fix_in_reply_to(object, options \\ [])
|
||||||
|
|
||||||
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
|
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
|
||||||
when not is_nil(in_reply_to) do
|
when not is_nil(in_reply_to) do
|
||||||
in_reply_to_id =
|
in_reply_to_id = prepare_in_reply_to(in_reply_to)
|
||||||
cond do
|
|
||||||
is_bitstring(in_reply_to) ->
|
|
||||||
in_reply_to
|
|
||||||
|
|
||||||
is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
|
|
||||||
in_reply_to["id"]
|
|
||||||
|
|
||||||
is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
|
|
||||||
Enum.at(in_reply_to, 0)
|
|
||||||
|
|
||||||
# Maybe I should output an error too?
|
|
||||||
true ->
|
|
||||||
""
|
|
||||||
end
|
|
||||||
|
|
||||||
object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
|
object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
|
||||||
|
|
||||||
if Federator.allowed_incoming_reply_depth?(options[:depth]) do
|
if Federator.allowed_incoming_reply_depth?(options[:depth]) do
|
||||||
case get_obj_helper(in_reply_to_id, options) do
|
with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
|
||||||
{:ok, replied_object} ->
|
%Activity{} = _ <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
|
||||||
with %Activity{} = _activity <-
|
object
|
||||||
Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
|
|> Map.put("inReplyTo", replied_object.data["id"])
|
||||||
object
|
|> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
|
||||||
|> Map.put("inReplyTo", replied_object.data["id"])
|
|> Map.put("conversation", replied_object.data["context"] || object["conversation"])
|
||||||
|> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
|
|> Map.put("context", replied_object.data["context"] || object["conversation"])
|
||||||
|> Map.put("conversation", replied_object.data["context"] || object["conversation"])
|
else
|
||||||
|> Map.put("context", replied_object.data["context"] || object["conversation"])
|
|
||||||
else
|
|
||||||
e ->
|
|
||||||
Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
|
|
||||||
object
|
|
||||||
end
|
|
||||||
|
|
||||||
e ->
|
e ->
|
||||||
Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
|
Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
|
||||||
object
|
object
|
||||||
|
@ -200,6 +175,22 @@ def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
|
||||||
|
|
||||||
def fix_in_reply_to(object, _options), do: object
|
def fix_in_reply_to(object, _options), do: object
|
||||||
|
|
||||||
|
defp prepare_in_reply_to(in_reply_to) do
|
||||||
|
cond do
|
||||||
|
is_bitstring(in_reply_to) ->
|
||||||
|
in_reply_to
|
||||||
|
|
||||||
|
is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
|
||||||
|
in_reply_to["id"]
|
||||||
|
|
||||||
|
is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
|
||||||
|
Enum.at(in_reply_to, 0)
|
||||||
|
|
||||||
|
true ->
|
||||||
|
""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def fix_context(object) do
|
def fix_context(object) do
|
||||||
context = object["context"] || object["conversation"] || Utils.generate_context_id()
|
context = object["context"] || object["conversation"] || Utils.generate_context_id()
|
||||||
|
|
||||||
|
@ -210,8 +201,7 @@ def fix_context(object) do
|
||||||
|
|
||||||
def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
|
def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
|
||||||
attachments =
|
attachments =
|
||||||
attachment
|
Enum.map(attachment, fn data ->
|
||||||
|> Enum.map(fn data ->
|
|
||||||
media_type = data["mediaType"] || data["mimeType"]
|
media_type = data["mediaType"] || data["mimeType"]
|
||||||
href = data["url"] || data["href"]
|
href = data["url"] || data["href"]
|
||||||
|
|
||||||
|
@ -222,30 +212,23 @@ def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachm
|
||||||
|> Map.put("url", url)
|
|> Map.put("url", url)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
object
|
Map.put(object, "attachment", attachments)
|
||||||
|> Map.put("attachment", attachments)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
|
def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
|
||||||
Map.put(object, "attachment", [attachment])
|
fix_attachments(Map.put(object, "attachment", [attachment]))
|
||||||
|> fix_attachments()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_attachments(object), do: object
|
def fix_attachments(object), do: object
|
||||||
|
|
||||||
def fix_url(%{"url" => url} = object) when is_map(url) do
|
def fix_url(%{"url" => url} = object) when is_map(url) do
|
||||||
object
|
Map.put(object, "url", url["href"])
|
||||||
|> Map.put("url", url["href"])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
|
def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
|
||||||
first_element = Enum.at(url, 0)
|
first_element = Enum.at(url, 0)
|
||||||
|
|
||||||
link_element =
|
link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
|
||||||
url
|
|
||||||
|> Enum.filter(fn x -> is_map(x) end)
|
|
||||||
|> Enum.filter(fn x -> x["mimeType"] == "text/html" end)
|
|
||||||
|> Enum.at(0)
|
|
||||||
|
|
||||||
object
|
object
|
||||||
|> Map.put("attachment", [first_element])
|
|> Map.put("attachment", [first_element])
|
||||||
|
@ -263,36 +246,32 @@ def fix_url(%{"type" => object_type, "url" => url} = object)
|
||||||
true -> ""
|
true -> ""
|
||||||
end
|
end
|
||||||
|
|
||||||
object
|
Map.put(object, "url", url_string)
|
||||||
|> Map.put("url", url_string)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_url(object), do: object
|
def fix_url(object), do: object
|
||||||
|
|
||||||
def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
|
def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
|
||||||
emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
|
|
||||||
|
|
||||||
emoji =
|
emoji =
|
||||||
emoji
|
tags
|
||||||
|
|> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
|
||||||
|> Enum.reduce(%{}, fn data, mapping ->
|
|> Enum.reduce(%{}, fn data, mapping ->
|
||||||
name = String.trim(data["name"], ":")
|
name = String.trim(data["name"], ":")
|
||||||
|
|
||||||
mapping |> Map.put(name, data["icon"]["url"])
|
Map.put(mapping, name, data["icon"]["url"])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
# we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
|
# we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
|
||||||
emoji = Map.merge(object["emoji"] || %{}, emoji)
|
emoji = Map.merge(object["emoji"] || %{}, emoji)
|
||||||
|
|
||||||
object
|
Map.put(object, "emoji", emoji)
|
||||||
|> Map.put("emoji", emoji)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
|
def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
|
||||||
name = String.trim(tag["name"], ":")
|
name = String.trim(tag["name"], ":")
|
||||||
emoji = %{name => tag["icon"]["url"]}
|
emoji = %{name => tag["icon"]["url"]}
|
||||||
|
|
||||||
object
|
Map.put(object, "emoji", emoji)
|
||||||
|> Map.put("emoji", emoji)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_emoji(object), do: object
|
def fix_emoji(object), do: object
|
||||||
|
@ -303,17 +282,13 @@ def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
|
||||||
|> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
|
|> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
|
||||||
|> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
|
|> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
|
||||||
|
|
||||||
combined = tag ++ tags
|
Map.put(object, "tag", tag ++ tags)
|
||||||
|
|
||||||
object
|
|
||||||
|> Map.put("tag", combined)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
|
def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
|
||||||
combined = [tag, String.slice(hashtag, 1..-1)]
|
combined = [tag, String.slice(hashtag, 1..-1)]
|
||||||
|
|
||||||
object
|
Map.put(object, "tag", combined)
|
||||||
|> Map.put("tag", combined)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
|
def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
|
||||||
|
@ -325,8 +300,7 @@ def fix_content_map(%{"contentMap" => content_map} = object) do
|
||||||
content_groups = Map.to_list(content_map)
|
content_groups = Map.to_list(content_map)
|
||||||
{_, content} = Enum.at(content_groups, 0)
|
{_, content} = Enum.at(content_groups, 0)
|
||||||
|
|
||||||
object
|
Map.put(object, "content", content)
|
||||||
|> Map.put("content", content)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_content_map(object), do: object
|
def fix_content_map(object), do: object
|
||||||
|
@ -335,16 +309,11 @@ def fix_type(object, options \\ [])
|
||||||
|
|
||||||
def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
|
def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
|
||||||
when is_binary(reply_id) do
|
when is_binary(reply_id) do
|
||||||
reply =
|
with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
|
||||||
with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
|
{:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
|
||||||
{:ok, object} <- get_obj_helper(reply_id, options) do
|
|
||||||
object
|
|
||||||
end
|
|
||||||
|
|
||||||
if reply && reply.data["type"] == "Question" do
|
|
||||||
Map.put(object, "type", "Answer")
|
Map.put(object, "type", "Answer")
|
||||||
else
|
else
|
||||||
object
|
_ -> object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -376,6 +345,17 @@ defp get_follow_activity(follow_object, followed) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Reduce the object list to find the reported user.
|
||||||
|
defp get_reported(objects) do
|
||||||
|
Enum.reduce_while(objects, nil, fn ap_id, _ ->
|
||||||
|
with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
|
||||||
|
{:halt, user}
|
||||||
|
else
|
||||||
|
_ -> {:cont, nil}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
def handle_incoming(data, options \\ [])
|
def handle_incoming(data, options \\ [])
|
||||||
|
|
||||||
# Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
|
# Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
|
||||||
|
@ -384,31 +364,19 @@ def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} =
|
||||||
with context <- data["context"] || Utils.generate_context_id(),
|
with context <- data["context"] || Utils.generate_context_id(),
|
||||||
content <- data["content"] || "",
|
content <- data["content"] || "",
|
||||||
%User{} = actor <- User.get_cached_by_ap_id(actor),
|
%User{} = actor <- User.get_cached_by_ap_id(actor),
|
||||||
|
|
||||||
# Reduce the object list to find the reported user.
|
# Reduce the object list to find the reported user.
|
||||||
%User{} = account <-
|
%User{} = account <- get_reported(objects),
|
||||||
Enum.reduce_while(objects, nil, fn ap_id, _ ->
|
|
||||||
with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
|
|
||||||
{:halt, user}
|
|
||||||
else
|
|
||||||
_ -> {:cont, nil}
|
|
||||||
end
|
|
||||||
end),
|
|
||||||
|
|
||||||
# Remove the reported user from the object list.
|
# Remove the reported user from the object list.
|
||||||
statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
|
statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
|
||||||
params = %{
|
%{
|
||||||
actor: actor,
|
actor: actor,
|
||||||
context: context,
|
context: context,
|
||||||
account: account,
|
account: account,
|
||||||
statuses: statuses,
|
statuses: statuses,
|
||||||
content: content,
|
content: content,
|
||||||
additional: %{
|
additional: %{"cc" => [account.ap_id]}
|
||||||
"cc" => [account.ap_id]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|> ActivityPub.flag()
|
||||||
ActivityPub.flag(params)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -755,8 +723,13 @@ def handle_incoming(
|
||||||
|
|
||||||
def handle_incoming(_, _), do: :error
|
def handle_incoming(_, _), do: :error
|
||||||
|
|
||||||
|
@spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
|
||||||
def get_obj_helper(id, options \\ []) do
|
def get_obj_helper(id, options \\ []) do
|
||||||
if object = Object.normalize(id, true, options), do: {:ok, object}, else: nil
|
if object = Object.normalize(id, true, options) do
|
||||||
|
{:ok, object}
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
|
def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
|
||||||
|
@ -855,27 +828,24 @@ def prepare_outgoing(%{"type" => _type} = data) do
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_fix_object_url(data) do
|
def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
|
||||||
if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
|
with false <- String.starts_with?(object, "http"),
|
||||||
case get_obj_helper(data["object"]) do
|
{:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
|
||||||
{:ok, relative_object} ->
|
%{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
|
||||||
if relative_object.data["external_url"] do
|
relative_object do
|
||||||
_data =
|
Map.put(data, "object", external_url)
|
||||||
data
|
|
||||||
|> Map.put("object", relative_object.data["external_url"])
|
|
||||||
else
|
|
||||||
data
|
|
||||||
end
|
|
||||||
|
|
||||||
e ->
|
|
||||||
Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
|
|
||||||
data
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
data
|
{:fetch, e} ->
|
||||||
|
Logger.error("Couldn't fetch #{object} #{inspect(e)}")
|
||||||
|
data
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def maybe_fix_object_url(data), do: data
|
||||||
|
|
||||||
def add_hashtags(object) do
|
def add_hashtags(object) do
|
||||||
tags =
|
tags =
|
||||||
(object["tag"] || [])
|
(object["tag"] || [])
|
||||||
|
@ -893,8 +863,7 @@ def add_hashtags(object) do
|
||||||
tag
|
tag
|
||||||
end)
|
end)
|
||||||
|
|
||||||
object
|
Map.put(object, "tag", tags)
|
||||||
|> Map.put("tag", tags)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_mention_tags(object) do
|
def add_mention_tags(object) do
|
||||||
|
@ -907,15 +876,13 @@ def add_mention_tags(object) do
|
||||||
|
|
||||||
tags = object["tag"] || []
|
tags = object["tag"] || []
|
||||||
|
|
||||||
object
|
Map.put(object, "tag", tags ++ mentions)
|
||||||
|> Map.put("tag", tags ++ mentions)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_emoji_tags(%User{info: %{"emoji" => _emoji} = user_info} = object) do
|
def add_emoji_tags(%User{info: %{"emoji" => _emoji} = user_info} = object) do
|
||||||
user_info = add_emoji_tags(user_info)
|
user_info = add_emoji_tags(user_info)
|
||||||
|
|
||||||
object
|
Map.put(object, :info, user_info)
|
||||||
|> Map.put(:info, user_info)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: we should probably send mtime instead of unix epoch time for updated
|
# TODO: we should probably send mtime instead of unix epoch time for updated
|
||||||
|
@ -923,8 +890,7 @@ def add_emoji_tags(%{"emoji" => emoji} = object) do
|
||||||
tags = object["tag"] || []
|
tags = object["tag"] || []
|
||||||
|
|
||||||
out =
|
out =
|
||||||
emoji
|
Enum.map(emoji, fn {name, url} ->
|
||||||
|> Enum.map(fn {name, url} ->
|
|
||||||
%{
|
%{
|
||||||
"icon" => %{"url" => url, "type" => "Image"},
|
"icon" => %{"url" => url, "type" => "Image"},
|
||||||
"name" => ":" <> name <> ":",
|
"name" => ":" <> name <> ":",
|
||||||
|
@ -934,13 +900,10 @@ def add_emoji_tags(%{"emoji" => emoji} = object) do
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
object
|
Map.put(object, "tag", tags ++ out)
|
||||||
|> Map.put("tag", tags ++ out)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_emoji_tags(object) do
|
def add_emoji_tags(object), do: object
|
||||||
object
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_conversation(object) do
|
def set_conversation(object) do
|
||||||
Map.put(object, "conversation", object["context"])
|
Map.put(object, "conversation", object["context"])
|
||||||
|
@ -959,9 +922,7 @@ def set_type(object), do: object
|
||||||
|
|
||||||
def add_attributed_to(object) do
|
def add_attributed_to(object) do
|
||||||
attributed_to = object["attributedTo"] || object["actor"]
|
attributed_to = object["attributedTo"] || object["actor"]
|
||||||
|
Map.put(object, "attributedTo", attributed_to)
|
||||||
object
|
|
||||||
|> Map.put("attributedTo", attributed_to)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare_attachments(object) do
|
def prepare_attachments(object) do
|
||||||
|
@ -972,8 +933,7 @@ def prepare_attachments(object) do
|
||||||
%{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
|
%{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
object
|
Map.put(object, "attachment", attachments)
|
||||||
|> Map.put("attachment", attachments)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp strip_internal_fields(object) do
|
defp strip_internal_fields(object) do
|
||||||
|
@ -990,12 +950,9 @@ defp strip_internal_fields(object) do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp strip_internal_tags(%{"tag" => tags} = object) do
|
defp strip_internal_tags(%{"tag" => tags} = object) do
|
||||||
tags =
|
tags = Enum.filter(tags, fn x -> is_map(x) end)
|
||||||
tags
|
|
||||||
|> Enum.filter(fn x -> is_map(x) end)
|
|
||||||
|
|
||||||
object
|
Map.put(object, "tag", tags)
|
||||||
|> Map.put("tag", tags)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp strip_internal_tags(object), do: object
|
defp strip_internal_tags(object), do: object
|
||||||
|
@ -1074,16 +1031,11 @@ def maybe_retire_websub(ap_id) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_fix_user_url(data) do
|
def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
|
||||||
if is_map(data["url"]) do
|
Map.put(data, "url", url["href"])
|
||||||
Map.put(data, "url", data["url"]["href"])
|
|
||||||
else
|
|
||||||
data
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_fix_user_object(data) do
|
def maybe_fix_user_url(data), do: data
|
||||||
data
|
|
||||||
|> maybe_fix_user_url
|
def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1451,4 +1451,166 @@ test "removes recipient's follower collection from cc", %{user: user} do
|
||||||
refute recipient.follower_address in fixed_object["to"]
|
refute recipient.follower_address in fixed_object["to"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "fix_summary/1" do
|
||||||
|
test "returns fixed object" do
|
||||||
|
assert Transmogrifier.fix_summary(%{"summary" => nil}) == %{"summary" => ""}
|
||||||
|
assert Transmogrifier.fix_summary(%{"summary" => "ok"}) == %{"summary" => "ok"}
|
||||||
|
assert Transmogrifier.fix_summary(%{}) == %{"summary" => ""}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "fix_in_reply_to/2" do
|
||||||
|
clear_config([:instance, :federation_incoming_replies_max_depth])
|
||||||
|
|
||||||
|
setup do
|
||||||
|
data = Poison.decode!(File.read!("test/fixtures/mastodon-post-activity.json"))
|
||||||
|
[data: data]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns not modified object when hasn't containts inReplyTo field", %{data: data} do
|
||||||
|
assert Transmogrifier.fix_in_reply_to(data) == data
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns object with inReplyToAtomUri when denied incoming reply", %{data: data} do
|
||||||
|
Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
|
||||||
|
|
||||||
|
object_with_reply =
|
||||||
|
Map.put(data["object"], "inReplyTo", "https://shitposter.club/notice/2827873")
|
||||||
|
|
||||||
|
modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
|
||||||
|
assert modified_object["inReplyTo"] == "https://shitposter.club/notice/2827873"
|
||||||
|
assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
|
||||||
|
|
||||||
|
object_with_reply =
|
||||||
|
Map.put(data["object"], "inReplyTo", %{"id" => "https://shitposter.club/notice/2827873"})
|
||||||
|
|
||||||
|
modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
|
||||||
|
assert modified_object["inReplyTo"] == %{"id" => "https://shitposter.club/notice/2827873"}
|
||||||
|
assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
|
||||||
|
|
||||||
|
object_with_reply =
|
||||||
|
Map.put(data["object"], "inReplyTo", ["https://shitposter.club/notice/2827873"])
|
||||||
|
|
||||||
|
modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
|
||||||
|
assert modified_object["inReplyTo"] == ["https://shitposter.club/notice/2827873"]
|
||||||
|
assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
|
||||||
|
|
||||||
|
object_with_reply = Map.put(data["object"], "inReplyTo", [])
|
||||||
|
modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
|
||||||
|
assert modified_object["inReplyTo"] == []
|
||||||
|
assert modified_object["inReplyToAtomUri"] == ""
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns modified object when allowed incoming reply", %{data: data} do
|
||||||
|
object_with_reply =
|
||||||
|
Map.put(
|
||||||
|
data["object"],
|
||||||
|
"inReplyTo",
|
||||||
|
"https://shitposter.club/notice/2827873"
|
||||||
|
)
|
||||||
|
|
||||||
|
Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 5)
|
||||||
|
modified_object = Transmogrifier.fix_in_reply_to(object_with_reply)
|
||||||
|
|
||||||
|
assert modified_object["inReplyTo"] ==
|
||||||
|
"tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
|
||||||
|
|
||||||
|
assert modified_object["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873"
|
||||||
|
|
||||||
|
assert modified_object["conversation"] ==
|
||||||
|
"tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
|
||||||
|
|
||||||
|
assert modified_object["context"] ==
|
||||||
|
"tag:shitposter.club,2017-05-05:objectType=thread:nonce=3c16e9c2681f6d26"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "fix_url/1" do
|
||||||
|
test "fixes data for object when url is map" do
|
||||||
|
object = %{
|
||||||
|
"url" => %{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "video/mp4",
|
||||||
|
"href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert Transmogrifier.fix_url(object) == %{
|
||||||
|
"url" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fixes data for video object" do
|
||||||
|
object = %{
|
||||||
|
"type" => "Video",
|
||||||
|
"url" => [
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "video/mp4",
|
||||||
|
"href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4"
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "video/mp4",
|
||||||
|
"href" => "https://peertube46fb-ad81-2d4c2d1630e3-240.mp4"
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "text/html",
|
||||||
|
"href" => "https://peertube.-2d4c2d1630e3"
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "text/html",
|
||||||
|
"href" => "https://peertube.-2d4c2d16377-42"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert Transmogrifier.fix_url(object) == %{
|
||||||
|
"attachment" => [
|
||||||
|
%{
|
||||||
|
"href" => "https://peede8d-46fb-ad81-2d4c2d1630e3-480.mp4",
|
||||||
|
"mimeType" => "video/mp4",
|
||||||
|
"type" => "Link"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type" => "Video",
|
||||||
|
"url" => "https://peertube.-2d4c2d1630e3"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fixes url for not Video object" do
|
||||||
|
object = %{
|
||||||
|
"type" => "Text",
|
||||||
|
"url" => [
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "text/html",
|
||||||
|
"href" => "https://peertube.-2d4c2d1630e3"
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mimeType" => "text/html",
|
||||||
|
"href" => "https://peertube.-2d4c2d16377-42"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert Transmogrifier.fix_url(object) == %{
|
||||||
|
"type" => "Text",
|
||||||
|
"url" => "https://peertube.-2d4c2d1630e3"
|
||||||
|
}
|
||||||
|
|
||||||
|
assert Transmogrifier.fix_url(%{"type" => "Text", "url" => []}) == %{
|
||||||
|
"type" => "Text",
|
||||||
|
"url" => ""
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "retunrs not modified object" do
|
||||||
|
assert Transmogrifier.fix_url(%{"type" => "Text"}) == %{"type" => "Text"}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue