GTS: cherry-picks and collection usage (#186)
[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.Utils
11 require Pleroma.Constants
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 context = Utils.maybe_create_context(data["context"] || data["conversation"])
26
27 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
28
29 data
30 |> Map.put("context", context)
31 |> cast_and_filter_recipients("to", follower_collection)
32 |> cast_and_filter_recipients("cc", follower_collection)
33 |> cast_and_filter_recipients("bto", follower_collection)
34 |> cast_and_filter_recipients("bcc", follower_collection)
35 |> fix_implicit_addressing(follower_collection)
36 end
37
38 def fix_activity_addressing(activity) do
39 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(activity["actor"])
40
41 activity
42 |> cast_and_filter_recipients("to", follower_collection)
43 |> cast_and_filter_recipients("cc", follower_collection)
44 |> cast_and_filter_recipients("bto", follower_collection)
45 |> cast_and_filter_recipients("bcc", follower_collection)
46 |> fix_implicit_addressing(follower_collection)
47 end
48
49 def fix_actor(data) do
50 actor =
51 data
52 |> Map.put_new("actor", data["attributedTo"])
53 |> Containment.get_actor()
54
55 data
56 |> Map.put("actor", actor)
57 |> Map.put("attributedTo", actor)
58 end
59
60 def fix_activity_context(data, %Object{data: %{"context" => object_context}}) do
61 data
62 |> Map.put("context", object_context)
63 end
64
65 def fix_object_action_recipients(%{"actor" => actor} = data, %Object{data: %{"actor" => actor}}) do
66 to = ((data["to"] || []) -- [actor]) |> Enum.uniq()
67
68 Map.put(data, "to", to)
69 end
70
71 def fix_object_action_recipients(data, %Object{data: %{"actor" => actor}}) do
72 to = ((data["to"] || []) ++ [actor]) |> Enum.uniq()
73
74 Map.put(data, "to", to)
75 end
76
77 # if as:Public is addressed, then make sure the followers collection is also addressed
78 # so that the activities will be delivered to local users.
79 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
80 recipients = to ++ cc
81
82 if followers_collection not in recipients do
83 cond do
84 Pleroma.Constants.as_public() in cc ->
85 to = to ++ [followers_collection]
86 Map.put(object, "to", to)
87
88 Pleroma.Constants.as_public() in to ->
89 cc = cc ++ [followers_collection]
90 Map.put(object, "cc", cc)
91
92 true ->
93 object
94 end
95 else
96 object
97 end
98 end
99 end