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"])
17 def fix_attachments(object) do
18 attachments = object["attachment"] || []
19 |> Enum.map(fn (data) ->
20 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "url" => data["url"]}]
21 Map.put(data, "url", url)
25 |> Map.put("attachment", attachments)
28 # TODO: validate those with a Ecto scheme
31 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
32 with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
33 object = fix_object(data["object"])
38 context: data["object"]["conversation"],
40 published: data["published"],
41 additional: Map.take(data, [
47 ActivityPub.create(params)
53 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
54 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
55 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
56 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
57 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
58 User.follow(follower, followed)
65 def handle_incoming(_), do: :error
71 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
75 |> prepare_attachments
78 |> Map.put("object", object)
79 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
84 def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept"] do
86 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
91 def add_mention_tags(object) do
92 mentions = object["to"]
93 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
95 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
97 tags = object["tag"] || []
100 |> Map.put("tag", tags ++ mentions)
103 def add_attributed_to(object) do
104 attributedTo = object["attributedTo"] || object["actor"]
107 |> Map.put("attributedTo", attributedTo)
110 def prepare_attachments(object) do
111 attachments = (object["attachment"] || [])
112 |> Enum.map(fn (data) ->
113 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
114 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
118 |> Map.put("attachment", attachments)