1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
3 A module to handle coding from internal to wire ActivityPub and back.
7 alias Pleroma.Web.ActivityPub.ActivityPub
10 Modifies an incoming AP object (mastodon format) to our internal format.
12 def fix_object(object) do
14 |> Map.put("actor", object["attributedTo"])
18 def fix_attachments(object) do
19 attachments = object["attachment"] || []
20 |> Enum.map(fn (data) ->
21 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "url" => data["url"]}]
22 Map.put(data, "url", url)
26 |> Map.put("attachment", attachments)
29 # TODO: validate those with a Ecto scheme
32 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
33 with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
34 object = fix_object(data["object"])
39 context: data["object"]["conversation"],
41 published: data["published"],
42 additional: Map.take(data, [
48 ActivityPub.create(params)
54 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
55 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
56 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
57 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
58 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
59 User.follow(follower, followed)
66 def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
67 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
68 %Object{} = object <- Object.get_by_ap_id(object_id),
69 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
80 def handle_incoming(_), do: :error
86 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
90 |> prepare_attachments
93 |> Map.put("object", object)
94 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
99 def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept", "Like", "Announce"] do
101 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
106 def add_mention_tags(object) do
107 mentions = object["to"]
108 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
109 |> Enum.filter(&(&1))
110 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
112 tags = object["tag"] || []
115 |> Map.put("tag", tags ++ mentions)
118 def add_attributed_to(object) do
119 attributedTo = object["attributedTo"] || object["actor"]
122 |> Map.put("attributedTo", attributedTo)
125 def prepare_attachments(object) do
126 attachments = (object["attachment"] || [])
127 |> Enum.map(fn (data) ->
128 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
129 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
133 |> Map.put("attachment", attachments)