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)
41 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id}) do
42 with %User{} = followed <- User.get_cached_by_ap_id(followed),
43 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
44 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
45 # TODO: Send an "Accept" activity.
46 User.follow(follower, followed)
53 def handle_incoming(_), do: :error
59 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
65 |> Map.put("object", object)
66 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
71 def add_mention_tags(object) do
72 mentions = object["to"]
73 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
75 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
77 tags = object["tag"] || []
80 |> Map.put("tag", tags ++ mentions)
83 def add_attributed_to(object) do
84 attributedTo = object["attributedTo"] || object["actor"]
87 |> Map.put("attributedTo", attributedTo)