fix formatting
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
6 @moduledoc """
7 A module to handle coding from internal to wire ActivityPub and back.
8 """
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.Object.Containment
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Utils
16 alias Pleroma.Web.ActivityPub.Visibility
17 alias Pleroma.Web.Federator
18
19 import Ecto.Query
20
21 require Logger
22 require Pleroma.Constants
23
24 @doc """
25 Modifies an incoming AP object (mastodon format) to our internal format.
26 """
27 def fix_object(object, options \\ []) do
28 object
29 |> strip_internal_fields
30 |> fix_actor
31 |> fix_url
32 |> fix_attachments
33 |> fix_context
34 |> fix_in_reply_to(options)
35 |> fix_emoji
36 |> fix_tag
37 |> fix_content_map
38 |> fix_addressing
39 |> fix_summary
40 |> fix_type(options)
41 end
42
43 def fix_summary(%{"summary" => nil} = object) do
44 object
45 |> Map.put("summary", "")
46 end
47
48 def fix_summary(%{"summary" => _} = object) do
49 # summary is present, nothing to do
50 object
51 end
52
53 def fix_summary(object) do
54 object
55 |> Map.put("summary", "")
56 end
57
58 def fix_addressing_list(map, field) do
59 cond do
60 is_binary(map[field]) ->
61 Map.put(map, field, [map[field]])
62
63 is_nil(map[field]) ->
64 Map.put(map, field, [])
65
66 true ->
67 map
68 end
69 end
70
71 def fix_explicit_addressing(
72 %{"to" => to, "cc" => cc} = object,
73 explicit_mentions,
74 follower_collection
75 ) do
76 explicit_to =
77 to
78 |> Enum.filter(fn x -> x in explicit_mentions end)
79
80 explicit_cc =
81 to
82 |> Enum.filter(fn x -> x not in explicit_mentions end)
83
84 final_cc =
85 (cc ++ explicit_cc)
86 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
87 |> Enum.uniq()
88
89 object
90 |> Map.put("to", explicit_to)
91 |> Map.put("cc", final_cc)
92 end
93
94 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
95
96 # if directMessage flag is set to true, leave the addressing alone
97 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
98
99 def fix_explicit_addressing(object) do
100 explicit_mentions =
101 object
102 |> Utils.determine_explicit_mentions()
103
104 follower_collection = User.get_cached_by_ap_id(Containment.get_actor(object)).follower_address
105
106 explicit_mentions = explicit_mentions ++ [Pleroma.Constants.as_public(), follower_collection]
107
108 fix_explicit_addressing(object, explicit_mentions, follower_collection)
109 end
110
111 # if as:Public is addressed, then make sure the followers collection is also addressed
112 # so that the activities will be delivered to local users.
113 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
114 recipients = to ++ cc
115
116 if followers_collection not in recipients do
117 cond do
118 Pleroma.Constants.as_public() in cc ->
119 to = to ++ [followers_collection]
120 Map.put(object, "to", to)
121
122 Pleroma.Constants.as_public() in to ->
123 cc = cc ++ [followers_collection]
124 Map.put(object, "cc", cc)
125
126 true ->
127 object
128 end
129 else
130 object
131 end
132 end
133
134 def fix_implicit_addressing(object, _), do: object
135
136 def fix_addressing(object) do
137 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
138 followers_collection = User.ap_followers(user)
139
140 object
141 |> fix_addressing_list("to")
142 |> fix_addressing_list("cc")
143 |> fix_addressing_list("bto")
144 |> fix_addressing_list("bcc")
145 |> fix_explicit_addressing()
146 |> fix_implicit_addressing(followers_collection)
147 end
148
149 def fix_actor(%{"attributedTo" => actor} = object) do
150 object
151 |> Map.put("actor", Containment.get_actor(%{"actor" => actor}))
152 end
153
154 def fix_in_reply_to(object, options \\ [])
155
156 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
157 when not is_nil(in_reply_to) do
158 in_reply_to_id =
159 cond do
160 is_bitstring(in_reply_to) ->
161 in_reply_to
162
163 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
164 in_reply_to["id"]
165
166 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
167 Enum.at(in_reply_to, 0)
168
169 # Maybe I should output an error too?
170 true ->
171 ""
172 end
173
174 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
175
176 if Federator.allowed_incoming_reply_depth?(options[:depth]) do
177 case get_obj_helper(in_reply_to_id, options) do
178 {:ok, replied_object} ->
179 with %Activity{} = _activity <-
180 Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
181 object
182 |> Map.put("inReplyTo", replied_object.data["id"])
183 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
184 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
185 |> Map.put("context", replied_object.data["context"] || object["conversation"])
186 else
187 e ->
188 Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
189 object
190 end
191
192 e ->
193 Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
194 object
195 end
196 else
197 object
198 end
199 end
200
201 def fix_in_reply_to(object, _options), do: object
202
203 def fix_context(object) do
204 context = object["context"] || object["conversation"] || Utils.generate_context_id()
205
206 object
207 |> Map.put("context", context)
208 |> Map.put("conversation", context)
209 end
210
211 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
212 attachments =
213 attachment
214 |> Enum.map(fn data ->
215 media_type = data["mediaType"] || data["mimeType"]
216 href = data["url"] || data["href"]
217
218 url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]
219
220 data
221 |> Map.put("mediaType", media_type)
222 |> Map.put("url", url)
223 end)
224
225 object
226 |> Map.put("attachment", attachments)
227 end
228
229 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
230 Map.put(object, "attachment", [attachment])
231 |> fix_attachments()
232 end
233
234 def fix_attachments(object), do: object
235
236 def fix_url(%{"url" => url} = object) when is_map(url) do
237 object
238 |> Map.put("url", url["href"])
239 end
240
241 def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
242 first_element = Enum.at(url, 0)
243
244 link_element =
245 url
246 |> Enum.filter(fn x -> is_map(x) end)
247 |> Enum.filter(fn x -> x["mimeType"] == "text/html" end)
248 |> Enum.at(0)
249
250 object
251 |> Map.put("attachment", [first_element])
252 |> Map.put("url", link_element["href"])
253 end
254
255 def fix_url(%{"type" => object_type, "url" => url} = object)
256 when object_type != "Video" and is_list(url) do
257 first_element = Enum.at(url, 0)
258
259 url_string =
260 cond do
261 is_bitstring(first_element) -> first_element
262 is_map(first_element) -> first_element["href"] || ""
263 true -> ""
264 end
265
266 object
267 |> Map.put("url", url_string)
268 end
269
270 def fix_url(object), do: object
271
272 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
273 emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
274
275 emoji =
276 emoji
277 |> Enum.reduce(%{}, fn data, mapping ->
278 name = String.trim(data["name"], ":")
279
280 mapping |> Map.put(name, data["icon"]["url"])
281 end)
282
283 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
284 emoji = Map.merge(object["emoji"] || %{}, emoji)
285
286 object
287 |> Map.put("emoji", emoji)
288 end
289
290 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
291 name = String.trim(tag["name"], ":")
292 emoji = %{name => tag["icon"]["url"]}
293
294 object
295 |> Map.put("emoji", emoji)
296 end
297
298 def fix_emoji(object), do: object
299
300 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
301 tags =
302 tag
303 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
304 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
305
306 combined = tag ++ tags
307
308 object
309 |> Map.put("tag", combined)
310 end
311
312 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
313 combined = [tag, String.slice(hashtag, 1..-1)]
314
315 object
316 |> Map.put("tag", combined)
317 end
318
319 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
320
321 def fix_tag(object), do: object
322
323 # content map usually only has one language so this will do for now.
324 def fix_content_map(%{"contentMap" => content_map} = object) do
325 content_groups = Map.to_list(content_map)
326 {_, content} = Enum.at(content_groups, 0)
327
328 object
329 |> Map.put("content", content)
330 end
331
332 def fix_content_map(object), do: object
333
334 def fix_type(object, options \\ [])
335
336 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
337 when is_binary(reply_id) do
338 reply =
339 with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
340 {:ok, object} <- get_obj_helper(reply_id, options) do
341 object
342 end
343
344 if reply && reply.data["type"] == "Question" do
345 Map.put(object, "type", "Answer")
346 else
347 object
348 end
349 end
350
351 def fix_type(object, _), do: object
352
353 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
354 with true <- id =~ "follows",
355 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
356 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
357 {:ok, activity}
358 else
359 _ -> {:error, nil}
360 end
361 end
362
363 defp mastodon_follow_hack(_, _), do: {:error, nil}
364
365 defp get_follow_activity(follow_object, followed) do
366 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
367 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
368 {:ok, activity}
369 else
370 # Can't find the activity. This might a Mastodon 2.3 "Accept"
371 {:activity, nil} ->
372 mastodon_follow_hack(follow_object, followed)
373
374 _ ->
375 {:error, nil}
376 end
377 end
378
379 def handle_incoming(data, options \\ [])
380
381 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
382 # with nil ID.
383 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
384 with context <- data["context"] || Utils.generate_context_id(),
385 content <- data["content"] || "",
386 %User{} = actor <- User.get_cached_by_ap_id(actor),
387
388 # Reduce the object list to find the reported user.
389 %User{} = account <-
390 Enum.reduce_while(objects, nil, fn ap_id, _ ->
391 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
392 {:halt, user}
393 else
394 _ -> {:cont, nil}
395 end
396 end),
397
398 # Remove the reported user from the object list.
399 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
400 params = %{
401 actor: actor,
402 context: context,
403 account: account,
404 statuses: statuses,
405 content: content,
406 additional: %{
407 "cc" => [account.ap_id]
408 }
409 }
410
411 ActivityPub.flag(params)
412 end
413 end
414
415 # disallow objects with bogus IDs
416 def handle_incoming(%{"id" => nil}, _options), do: :error
417 def handle_incoming(%{"id" => ""}, _options), do: :error
418 # length of https:// = 8, should validate better, but good enough for now.
419 def handle_incoming(%{"id" => id}, _options) when not (is_binary(id) and length(id) > 8),
420 do: :error
421
422 # TODO: validate those with a Ecto scheme
423 # - tags
424 # - emoji
425 def handle_incoming(
426 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
427 options
428 )
429 when objtype in ["Article", "Note", "Video", "Page", "Question", "Answer"] do
430 actor = Containment.get_actor(data)
431
432 data =
433 Map.put(data, "actor", actor)
434 |> fix_addressing
435
436 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
437 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
438 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
439 object = fix_object(data["object"], options)
440
441 params = %{
442 to: data["to"],
443 object: object,
444 actor: user,
445 context: object["conversation"],
446 local: false,
447 published: data["published"],
448 additional:
449 Map.take(data, [
450 "cc",
451 "directMessage",
452 "id"
453 ])
454 }
455
456 ActivityPub.create(params)
457 else
458 %Activity{} = activity -> {:ok, activity}
459 _e -> :error
460 end
461 end
462
463 def handle_incoming(
464 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
465 _options
466 ) do
467 with %User{local: true} = followed <-
468 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
469 {:ok, %User{} = follower} <-
470 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
471 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
472 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
473 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
474 {_, false} <- {:user_locked, User.locked?(followed)},
475 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
476 {_, {:ok, _}} <-
477 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")} do
478 ActivityPub.accept(%{
479 to: [follower.ap_id],
480 actor: followed,
481 object: data,
482 local: true
483 })
484 else
485 {:user_blocked, true} ->
486 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
487
488 ActivityPub.reject(%{
489 to: [follower.ap_id],
490 actor: followed,
491 object: data,
492 local: true
493 })
494
495 {:follow, {:error, _}} ->
496 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
497
498 ActivityPub.reject(%{
499 to: [follower.ap_id],
500 actor: followed,
501 object: data,
502 local: true
503 })
504
505 {:user_locked, true} ->
506 :noop
507 end
508
509 {:ok, activity}
510 else
511 _e ->
512 :error
513 end
514 end
515
516 def handle_incoming(
517 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
518 _options
519 ) do
520 with actor <- Containment.get_actor(data),
521 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
522 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
523 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
524 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
525 {:ok, _follower} = User.follow(follower, followed) do
526 ActivityPub.accept(%{
527 to: follow_activity.data["to"],
528 type: "Accept",
529 actor: followed,
530 object: follow_activity.data["id"],
531 local: false
532 })
533 else
534 _e -> :error
535 end
536 end
537
538 def handle_incoming(
539 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
540 _options
541 ) do
542 with actor <- Containment.get_actor(data),
543 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
544 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
545 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
546 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
547 {:ok, activity} <-
548 ActivityPub.reject(%{
549 to: follow_activity.data["to"],
550 type: "Reject",
551 actor: followed,
552 object: follow_activity.data["id"],
553 local: false
554 }) do
555 User.unfollow(follower, followed)
556
557 {:ok, activity}
558 else
559 _e -> :error
560 end
561 end
562
563 def handle_incoming(
564 %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data,
565 _options
566 ) do
567 with actor <- Containment.get_actor(data),
568 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
569 {:ok, object} <- get_obj_helper(object_id),
570 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
571 {:ok, activity}
572 else
573 _e -> :error
574 end
575 end
576
577 def handle_incoming(
578 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
579 _options
580 ) do
581 with actor <- Containment.get_actor(data),
582 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
583 {:ok, object} <- get_obj_helper(object_id),
584 public <- Visibility.is_public?(data),
585 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
586 {:ok, activity}
587 else
588 _e -> :error
589 end
590 end
591
592 def handle_incoming(
593 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
594 data,
595 _options
596 )
597 when object_type in ["Person", "Application", "Service", "Organization"] do
598 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
599 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
600
601 banner = new_user_data[:info][:banner]
602 locked = new_user_data[:info][:locked] || false
603 attachment = get_in(new_user_data, [:info, :source_data, "attachment"]) || []
604
605 fields =
606 attachment
607 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
608 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
609
610 update_data =
611 new_user_data
612 |> Map.take([:name, :bio, :avatar])
613 |> Map.put(:info, %{banner: banner, locked: locked, fields: fields})
614
615 actor
616 |> User.upgrade_changeset(update_data, true)
617 |> User.update_and_set_cache()
618
619 ActivityPub.update(%{
620 local: false,
621 to: data["to"] || [],
622 cc: data["cc"] || [],
623 object: object,
624 actor: actor_id
625 })
626 else
627 e ->
628 Logger.error(e)
629 :error
630 end
631 end
632
633 # TODO: We presently assume that any actor on the same origin domain as the object being
634 # deleted has the rights to delete that object. A better way to validate whether or not
635 # the object should be deleted is to refetch the object URI, which should return either
636 # an error or a tombstone. This would allow us to verify that a deletion actually took
637 # place.
638 def handle_incoming(
639 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = data,
640 _options
641 ) do
642 object_id = Utils.get_ap_id(object_id)
643
644 with actor <- Containment.get_actor(data),
645 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
646 {:ok, object} <- get_obj_helper(object_id),
647 :ok <- Containment.contain_origin(actor.ap_id, object.data),
648 {:ok, activity} <- ActivityPub.delete(object, false) do
649 {:ok, activity}
650 else
651 nil ->
652 case User.get_cached_by_ap_id(object_id) do
653 %User{ap_id: ^actor} = user ->
654 User.delete(user)
655
656 nil ->
657 :error
658 end
659
660 _e ->
661 :error
662 end
663 end
664
665 def handle_incoming(
666 %{
667 "type" => "Undo",
668 "object" => %{"type" => "Announce", "object" => object_id},
669 "actor" => _actor,
670 "id" => id
671 } = data,
672 _options
673 ) do
674 with actor <- Containment.get_actor(data),
675 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
676 {:ok, object} <- get_obj_helper(object_id),
677 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
678 {:ok, activity}
679 else
680 _e -> :error
681 end
682 end
683
684 def handle_incoming(
685 %{
686 "type" => "Undo",
687 "object" => %{"type" => "Follow", "object" => followed},
688 "actor" => follower,
689 "id" => id
690 } = _data,
691 _options
692 ) do
693 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
694 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
695 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
696 User.unfollow(follower, followed)
697 {:ok, activity}
698 else
699 _e -> :error
700 end
701 end
702
703 def handle_incoming(
704 %{
705 "type" => "Undo",
706 "object" => %{"type" => "Block", "object" => blocked},
707 "actor" => blocker,
708 "id" => id
709 } = _data,
710 _options
711 ) do
712 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
713 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
714 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
715 User.unblock(blocker, blocked)
716 {:ok, activity}
717 else
718 _e -> :error
719 end
720 end
721
722 def handle_incoming(
723 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
724 _options
725 ) do
726 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
727 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
728 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
729 User.unfollow(blocker, blocked)
730 User.block(blocker, blocked)
731 {:ok, activity}
732 else
733 _e -> :error
734 end
735 end
736
737 def handle_incoming(
738 %{
739 "type" => "Undo",
740 "object" => %{"type" => "Like", "object" => object_id},
741 "actor" => _actor,
742 "id" => id
743 } = data,
744 _options
745 ) do
746 with actor <- Containment.get_actor(data),
747 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
748 {:ok, object} <- get_obj_helper(object_id),
749 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
750 {:ok, activity}
751 else
752 _e -> :error
753 end
754 end
755
756 def handle_incoming(_, _), do: :error
757
758 def get_obj_helper(id, options \\ []) do
759 if object = Object.normalize(id, true, options), do: {:ok, object}, else: nil
760 end
761
762 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
763 with false <- String.starts_with?(in_reply_to, "http"),
764 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
765 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
766 else
767 _e -> object
768 end
769 end
770
771 def set_reply_to_uri(obj), do: obj
772
773 # Prepares the object of an outgoing create activity.
774 def prepare_object(object) do
775 object
776 |> set_sensitive
777 |> add_hashtags
778 |> add_mention_tags
779 |> add_emoji_tags
780 |> add_attributed_to
781 |> prepare_attachments
782 |> set_conversation
783 |> set_reply_to_uri
784 |> strip_internal_fields
785 |> strip_internal_tags
786 |> set_type
787 end
788
789 # @doc
790 # """
791 # internal -> Mastodon
792 # """
793
794 def prepare_outgoing(%{"type" => "Create", "object" => object_id} = data) do
795 object =
796 object_id
797 |> Object.normalize()
798 |> Map.get(:data)
799 |> prepare_object
800
801 data =
802 data
803 |> Map.put("object", object)
804 |> Map.merge(Utils.make_json_ld_header())
805 |> Map.delete("bcc")
806
807 {:ok, data}
808 end
809
810 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
811 # because of course it does.
812 def prepare_outgoing(%{"type" => "Accept"} = data) do
813 with follow_activity <- Activity.normalize(data["object"]) do
814 object = %{
815 "actor" => follow_activity.actor,
816 "object" => follow_activity.data["object"],
817 "id" => follow_activity.data["id"],
818 "type" => "Follow"
819 }
820
821 data =
822 data
823 |> Map.put("object", object)
824 |> Map.merge(Utils.make_json_ld_header())
825
826 {:ok, data}
827 end
828 end
829
830 def prepare_outgoing(%{"type" => "Reject"} = data) do
831 with follow_activity <- Activity.normalize(data["object"]) do
832 object = %{
833 "actor" => follow_activity.actor,
834 "object" => follow_activity.data["object"],
835 "id" => follow_activity.data["id"],
836 "type" => "Follow"
837 }
838
839 data =
840 data
841 |> Map.put("object", object)
842 |> Map.merge(Utils.make_json_ld_header())
843
844 {:ok, data}
845 end
846 end
847
848 def prepare_outgoing(%{"type" => _type} = data) do
849 data =
850 data
851 |> strip_internal_fields
852 |> maybe_fix_object_url
853 |> Map.merge(Utils.make_json_ld_header())
854
855 {:ok, data}
856 end
857
858 def maybe_fix_object_url(data) do
859 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
860 case get_obj_helper(data["object"]) do
861 {:ok, relative_object} ->
862 if relative_object.data["external_url"] do
863 _data =
864 data
865 |> Map.put("object", relative_object.data["external_url"])
866 else
867 data
868 end
869
870 e ->
871 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
872 data
873 end
874 else
875 data
876 end
877 end
878
879 def add_hashtags(object) do
880 tags =
881 (object["tag"] || [])
882 |> Enum.map(fn
883 # Expand internal representation tags into AS2 tags.
884 tag when is_binary(tag) ->
885 %{
886 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
887 "name" => "##{tag}",
888 "type" => "Hashtag"
889 }
890
891 # Do not process tags which are already AS2 tag objects.
892 tag when is_map(tag) ->
893 tag
894 end)
895
896 object
897 |> Map.put("tag", tags)
898 end
899
900 def add_mention_tags(object) do
901 mentions =
902 object
903 |> Utils.get_notified_from_object()
904 |> Enum.map(fn user ->
905 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
906 end)
907
908 tags = object["tag"] || []
909
910 object
911 |> Map.put("tag", tags ++ mentions)
912 end
913
914 def add_emoji_tags(%User{info: %{"emoji" => _emoji} = user_info} = object) do
915 user_info = add_emoji_tags(user_info)
916
917 object
918 |> Map.put(:info, user_info)
919 end
920
921 # TODO: we should probably send mtime instead of unix epoch time for updated
922 def add_emoji_tags(%{"emoji" => emoji} = object) do
923 tags = object["tag"] || []
924
925 out =
926 emoji
927 |> Enum.map(fn {name, url} ->
928 %{
929 "icon" => %{"url" => url, "type" => "Image"},
930 "name" => ":" <> name <> ":",
931 "type" => "Emoji",
932 "updated" => "1970-01-01T00:00:00Z",
933 "id" => url
934 }
935 end)
936
937 object
938 |> Map.put("tag", tags ++ out)
939 end
940
941 def add_emoji_tags(object) do
942 object
943 end
944
945 def set_conversation(object) do
946 Map.put(object, "conversation", object["context"])
947 end
948
949 def set_sensitive(object) do
950 tags = object["tag"] || []
951 Map.put(object, "sensitive", "nsfw" in tags)
952 end
953
954 def set_type(%{"type" => "Answer"} = object) do
955 Map.put(object, "type", "Note")
956 end
957
958 def set_type(object), do: object
959
960 def add_attributed_to(object) do
961 attributed_to = object["attributedTo"] || object["actor"]
962
963 object
964 |> Map.put("attributedTo", attributed_to)
965 end
966
967 def prepare_attachments(object) do
968 attachments =
969 (object["attachment"] || [])
970 |> Enum.map(fn data ->
971 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
972 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
973 end)
974
975 object
976 |> Map.put("attachment", attachments)
977 end
978
979 defp strip_internal_fields(object) do
980 object
981 |> Map.drop([
982 "likes",
983 "like_count",
984 "announcements",
985 "announcement_count",
986 "emoji",
987 "context_id",
988 "deleted_activity_id"
989 ])
990 end
991
992 defp strip_internal_tags(%{"tag" => tags} = object) do
993 tags =
994 tags
995 |> Enum.filter(fn x -> is_map(x) end)
996
997 object
998 |> Map.put("tag", tags)
999 end
1000
1001 defp strip_internal_tags(object), do: object
1002
1003 def perform(:user_upgrade, user) do
1004 # we pass a fake user so that the followers collection is stripped away
1005 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1006
1007 q =
1008 from(
1009 u in User,
1010 where: ^old_follower_address in u.following,
1011 update: [
1012 set: [
1013 following:
1014 fragment(
1015 "array_replace(?,?,?)",
1016 u.following,
1017 ^old_follower_address,
1018 ^user.follower_address
1019 )
1020 ]
1021 ]
1022 )
1023
1024 Repo.update_all(q, [])
1025
1026 maybe_retire_websub(user.ap_id)
1027
1028 q =
1029 from(
1030 a in Activity,
1031 where: ^old_follower_address in a.recipients,
1032 update: [
1033 set: [
1034 recipients:
1035 fragment(
1036 "array_replace(?,?,?)",
1037 a.recipients,
1038 ^old_follower_address,
1039 ^user.follower_address
1040 )
1041 ]
1042 ]
1043 )
1044
1045 Repo.update_all(q, [])
1046 end
1047
1048 def upgrade_user_from_ap_id(ap_id) do
1049 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1050 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1051 already_ap <- User.ap_enabled?(user),
1052 {:ok, user} <- user |> User.upgrade_changeset(data) |> User.update_and_set_cache() do
1053 unless already_ap do
1054 PleromaJobQueue.enqueue(:transmogrifier, __MODULE__, [:user_upgrade, user])
1055 end
1056
1057 {:ok, user}
1058 else
1059 %User{} = user -> {:ok, user}
1060 e -> e
1061 end
1062 end
1063
1064 def maybe_retire_websub(ap_id) do
1065 # some sanity checks
1066 if is_binary(ap_id) && String.length(ap_id) > 8 do
1067 q =
1068 from(
1069 ws in Pleroma.Web.Websub.WebsubClientSubscription,
1070 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")
1071 )
1072
1073 Repo.delete_all(q)
1074 end
1075 end
1076
1077 def maybe_fix_user_url(data) do
1078 if is_map(data["url"]) do
1079 Map.put(data, "url", data["url"]["href"])
1080 else
1081 data
1082 end
1083 end
1084
1085 def maybe_fix_user_object(data) do
1086 data
1087 |> maybe_fix_user_url
1088 end
1089 end