1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
3 A module to handle coding from internal to wire ActivityPub and back.
9 alias Pleroma.Web.ActivityPub.ActivityPub
14 Modifies an incoming AP object (mastodon format) to our internal format.
16 def fix_object(object) do
18 |> Map.put("actor", object["attributedTo"])
23 def fix_context(object) do
25 |> Map.put("context", object["conversation"])
28 def fix_attachments(object) do
29 attachments = (object["attachment"] || [])
30 |> Enum.map(fn (data) ->
31 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
32 Map.put(data, "url", url)
36 |> Map.put("attachment", attachments)
39 # TODO: validate those with a Ecto scheme
42 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
43 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
44 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
45 object = fix_object(data["object"])
50 context: data["object"]["conversation"],
52 published: data["published"],
53 additional: Map.take(data, [
59 if object["inReplyTo"] do
60 {:ok, object} = ActivityPub.fetch_object_from_id(object["inReplyTo"])
63 ActivityPub.create(params)
65 %Activity{} = activity -> {:ok, activity}
70 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
71 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
72 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
73 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
74 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
75 User.follow(follower, followed)
82 def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
83 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
84 %Object{} = object <- Object.get_by_ap_id(object_id),
85 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
92 def handle_incoming(%{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data) do
93 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
94 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
95 {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
106 def handle_incoming(_), do: :error
108 def get_obj_helper(id) do
109 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
116 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
122 |> prepare_attachments
126 |> Map.put("object", object)
127 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
132 def prepare_outgoing(%{"type" => type} = data) do
134 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
139 def add_hashtags(object) do
140 tags = (object["tag"] || [])
141 |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
144 |> Map.put("tag", tags)
147 def add_mention_tags(object) do
148 recipients = object["to"] ++ (object["cc"] || [])
149 mentions = recipients
150 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
151 |> Enum.filter(&(&1))
152 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
154 tags = object["tag"] || []
157 |> Map.put("tag", tags ++ mentions)
160 def set_conversation(object) do
161 Map.put(object, "conversation", object["context"])
164 def set_sensitive(object) do
165 tags = object["tag"] || []
166 Map.put(object, "sensitive", "nsfw" in tags)
169 def add_attributed_to(object) do
170 attributedTo = object["attributedTo"] || object["actor"]
173 |> Map.put("attributedTo", attributedTo)
176 def prepare_attachments(object) do
177 attachments = (object["attachment"] || [])
178 |> Enum.map(fn (data) ->
179 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
180 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
184 |> Map.put("attachment", attachments)
187 def upgrade_user_from_ap_id(ap_id) do
188 with %User{} = user <- User.get_by_ap_id(ap_id),
189 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
191 |> Map.put(:info, Map.merge(user.info, data[:info]))
193 old_follower_address = user.follower_address
194 {:ok, user} = User.upgrade_changeset(user, data)
197 # This could potentially take a long time, do it in the background
199 q = from a in Activity,
200 where: ^old_follower_address in a.recipients,
201 update: [set: [recipients: fragment("array_replace(?,?,?)", a.recipients, ^old_follower_address, ^user.follower_address)]]
202 Repo.update_all(q, [])
205 where: ^old_follower_address in u.following,
206 update: [set: [following: fragment("array_replace(?,?,?)", u.following, ^old_follower_address, ^user.follower_address)]]
207 Repo.update_all(q, [])