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