|> fix_context
|> fix_in_reply_to
|> fix_emoji
+ |> fix_tag
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object) when not is_nil(in_reply_to_id) do
|> Map.put("emoji", emoji)
end
+ def fix_tag(object) do
+ tags = (object["tag"] || [])
+ |> Enum.filter(fn (data) -> data["type"] == "Hashtag" and data["name"] end)
+ |> Enum.map(fn (data) -> String.slice(data["name"], 1..-1) end)
+
+ combined = (object["tag"] || []) ++ tags
+
+ object
+ |> Map.put("tag", combined)
+ end
+
# TODO: validate those with a Ecto scheme
# - tags
# - emoji
if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
end
+ def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
+ with false <- String.starts_with?(inReplyTo, "http"),
+ {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
+ Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
+ else
+ _e -> object
+ end
+ end
+ def set_reply_to_uri(obj), do: obj
+
+ # Prepares the object of an outgoing create activity.
def prepare_object(object) do
object
|> set_sensitive
|> add_attributed_to
|> prepare_attachments
|> set_conversation
+ |> set_reply_to_uri
end
@doc
def prepare_outgoing(%{"type" => type} = data) do
data = data
+ |> maybe_fix_object_url
|> Map.put("@context", "https://www.w3.org/ns/activitystreams")
{:ok, data}
end
+ def maybe_fix_object_url(data) do
+ if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
+ case ActivityPub.fetch_object_from_id(data["object"]) do
+ {:ok, relative_object} ->
+ if relative_object.data["external_url"] do
+ data = 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
+ data
+ end
+ end
+
def add_hashtags(object) do
tags = (object["tag"] || [])
|> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end