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
16 Modifies an incoming AP object (mastodon format) to our internal format.
18 def fix_object(object) do
20 |> Map.put("actor", object["attributedTo"])
25 def fix_context(object) do
27 |> Map.put("context", object["conversation"])
30 def fix_attachments(object) do
31 attachments = (object["attachment"] || [])
32 |> Enum.map(fn (data) ->
33 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
34 Map.put(data, "url", url)
38 |> Map.put("attachment", attachments)
41 # TODO: validate those with a Ecto scheme
44 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
45 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
46 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
47 object = fix_object(data["object"])
49 replied_to_id = if object["inReplyTo"] do
50 case ActivityPub.fetch_object_from_id(object["inReplyTo"]) do
51 {:ok, object} -> object.data["id"]
53 Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
60 object = Map.put(object, "inReplyTo", replied_to_id || object["inReplyTo"])
66 context: data["object"]["conversation"],
68 published: data["published"],
69 additional: Map.take(data, [
76 ActivityPub.create(params)
78 %Activity{} = activity -> {:ok, activity}
83 def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data) do
84 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
85 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
86 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
87 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
88 User.follow(follower, followed)
95 def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
96 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
97 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
98 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
105 def handle_incoming(%{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data) do
106 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
107 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
108 {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
119 def handle_incoming(_), do: :error
121 def get_obj_helper(id) do
122 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
125 def prepare_object(object) do
131 |> prepare_attachments
139 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
144 |> Map.put("object", object)
145 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
150 def prepare_outgoing(%{"type" => type} = data) do
152 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
157 def add_hashtags(object) do
158 tags = (object["tag"] || [])
159 |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
162 |> Map.put("tag", tags)
165 def add_mention_tags(object) do
166 recipients = object["to"] ++ (object["cc"] || [])
167 mentions = recipients
168 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
169 |> Enum.filter(&(&1))
170 |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
172 tags = object["tag"] || []
175 |> Map.put("tag", tags ++ mentions)
178 def set_conversation(object) do
179 Map.put(object, "conversation", object["context"])
182 def set_sensitive(object) do
183 tags = object["tag"] || []
184 Map.put(object, "sensitive", "nsfw" in tags)
187 def add_attributed_to(object) do
188 attributedTo = object["attributedTo"] || object["actor"]
191 |> Map.put("attributedTo", attributedTo)
194 def prepare_attachments(object) do
195 attachments = (object["attachment"] || [])
196 |> Enum.map(fn (data) ->
197 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
198 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
202 |> Map.put("attachment", attachments)
205 defp user_upgrade_task(user) do
206 old_follower_address = User.ap_followers(user)
208 where: ^old_follower_address in u.following,
209 update: [set: [following: fragment("array_replace(?,?,?)", u.following, ^old_follower_address, ^user.follower_address)]]
210 Repo.update_all(q, [])
212 maybe_retire_websub(user.ap_id)
214 # Only do this for recent activties, don't go through the whole db.
215 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
216 q = from a in Activity,
217 where: ^old_follower_address in a.recipients,
218 where: a.id > ^since,
219 update: [set: [recipients: fragment("array_replace(?,?,?)", a.recipients, ^old_follower_address, ^user.follower_address)]]
220 Repo.update_all(q, [])
223 def upgrade_user_from_ap_id(ap_id, async \\ true) do
224 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
225 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
227 |> Map.put(:info, Map.merge(user.info, data[:info]))
229 {:ok, user} = User.upgrade_changeset(user, data)
232 # This could potentially take a long time, do it in the background
235 user_upgrade_task(user)
238 user_upgrade_task(user)
247 def maybe_retire_websub(ap_id) do
249 if is_binary(ap_id) && (String.length(ap_id) > 8) do
250 q = from ws in Pleroma.Web.Websub.WebsubClientSubscription,
251 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")