mix format
[akkoma] / lib / pleroma / web / activity_pub / object_validators / common_fixes.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
6 alias Pleroma.EctoType.ActivityPub.ObjectValidators
7 alias Pleroma.Object
8 alias Pleroma.Object.Containment
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12
13 def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do
14 {:ok, data} = ObjectValidators.Recipients.cast(message[field] || field_fallback)
15
16 data =
17 Enum.reject(data, fn x ->
18 String.ends_with?(x, "/followers") and x != follower_collection
19 end)
20
21 Map.put(message, field, data)
22 end
23
24 def fix_object_defaults(data) do
25 %{data: %{"id" => context}, id: context_id} =
26 Utils.create_context(data["context"] || data["conversation"])
27
28 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
29
30 data
31 |> Map.put("context", context)
32 |> Map.put("context_id", context_id)
33 |> cast_and_filter_recipients("to", follower_collection)
34 |> cast_and_filter_recipients("cc", follower_collection)
35 |> cast_and_filter_recipients("bto", follower_collection)
36 |> cast_and_filter_recipients("bcc", follower_collection)
37 |> Transmogrifier.fix_implicit_addressing(follower_collection)
38 end
39
40 def fix_activity_addressing(activity) do
41 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(activity["actor"])
42
43 activity
44 |> cast_and_filter_recipients("to", follower_collection)
45 |> cast_and_filter_recipients("cc", follower_collection)
46 |> cast_and_filter_recipients("bto", follower_collection)
47 |> cast_and_filter_recipients("bcc", follower_collection)
48 |> Transmogrifier.fix_implicit_addressing(follower_collection)
49 end
50
51 def fix_actor(data) do
52 actor =
53 data
54 |> Map.put_new("actor", data["attributedTo"])
55 |> Containment.get_actor()
56
57 data
58 |> Map.put("actor", actor)
59 |> Map.put("attributedTo", actor)
60 end
61
62 def fix_activity_context(data, %Object{data: %{"context" => object_context}}) do
63 data
64 |> Map.put("context", object_context)
65 end
66
67 def fix_object_action_recipients(%{"actor" => actor} = data, %Object{data: %{"actor" => actor}}) do
68 to = ((data["to"] || []) -- [actor]) |> Enum.uniq()
69
70 Map.put(data, "to", to)
71 end
72
73 def fix_object_action_recipients(data, %Object{data: %{"actor" => actor}}) do
74 to = ((data["to"] || []) ++ [actor]) |> Enum.uniq()
75
76 Map.put(data, "to", to)
77 end
78 end