1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
3 A module to handle coding from internal to wire ActivityPub and back.
6 alias Pleroma.Web.ActivityPub.ActivityPub
9 Modifies an incoming AP object (mastodon format) to our internal format.
11 def fix_object(object) do
13 |> Map.put("actor", object["attributedTo"])
16 # TODO: validate those with a Ecto scheme
19 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
20 with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
21 object = fix_object(data["object"])
26 context: data["object"]["conversation"],
28 published: data["published"],
29 additional: Map.take(data, [
35 ActivityPub.create(params)
45 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
51 |> Map.put("object", object)
52 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
57 def add_mention_tags(object) do
58 mentions = object["to"]
59 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
61 |> Enum.map(fn(user) -> %{"type" => "mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
63 tags = object["tags"] || []
66 |> Map.put("tags", tags ++ mentions)
69 def add_attributed_to(object) do
70 attributedTo = object["attributedTo"] || object["actor"]
73 |> Map.put("attributedTo", attributedTo)