{Answer,Question}Validator: Keep both actor and attributedTo for now but sync them
[akkoma] / lib / pleroma / web / activity_pub / object_validators / common_validations.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
6 import Ecto.Changeset
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.User
11
12 def validate_any_presence(cng, fields) do
13 non_empty =
14 fields
15 |> Enum.map(fn field -> get_field(cng, field) end)
16 |> Enum.any?(fn
17 [] -> false
18 _ -> true
19 end)
20
21 if non_empty do
22 cng
23 else
24 fields
25 |> Enum.reduce(cng, fn field, cng ->
26 cng
27 |> add_error(field, "none of #{inspect(fields)} present")
28 end)
29 end
30 end
31
32 def validate_actor_presence(cng, options \\ []) do
33 field_name = Keyword.get(options, :field_name, :actor)
34
35 cng
36 |> validate_change(field_name, fn field_name, actor ->
37 if User.get_cached_by_ap_id(actor) do
38 []
39 else
40 [{field_name, "can't find user"}]
41 end
42 end)
43 end
44
45 def validate_actor_is_active(cng, options \\ []) do
46 field_name = Keyword.get(options, :field_name, :actor)
47
48 cng
49 |> validate_change(field_name, fn field_name, actor ->
50 if %User{deactivated: false} = User.get_cached_by_ap_id(actor) do
51 []
52 else
53 [{field_name, "can't find user (or deactivated)"}]
54 end
55 end)
56 end
57
58 def validate_object_presence(cng, options \\ []) do
59 field_name = Keyword.get(options, :field_name, :object)
60 allowed_types = Keyword.get(options, :allowed_types, false)
61
62 cng
63 |> validate_change(field_name, fn field_name, object_id ->
64 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
65
66 cond do
67 !object ->
68 [{field_name, "can't find object"}]
69
70 object && allowed_types && object.data["type"] not in allowed_types ->
71 [{field_name, "object not in allowed types"}]
72
73 true ->
74 []
75 end
76 end)
77 end
78
79 def validate_object_or_user_presence(cng, options \\ []) do
80 field_name = Keyword.get(options, :field_name, :object)
81 options = Keyword.put(options, :field_name, field_name)
82
83 actor_cng =
84 cng
85 |> validate_actor_presence(options)
86
87 object_cng =
88 cng
89 |> validate_object_presence(options)
90
91 if actor_cng.valid?, do: actor_cng, else: object_cng
92 end
93
94 def validate_host_match(cng, fields \\ [:id, :actor]) do
95 unique_hosts =
96 fields
97 |> Enum.map(fn field ->
98 %URI{host: host} =
99 cng
100 |> get_field(field)
101 |> URI.parse()
102
103 host
104 end)
105 |> Enum.uniq()
106 |> Enum.count()
107
108 if unique_hosts == 1 do
109 cng
110 else
111 fields
112 |> Enum.reduce(cng, fn field, cng ->
113 cng
114 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
115 end)
116 end
117 end
118
119 def validate_fields_match(cng, fields) do
120 unique_fields =
121 fields
122 |> Enum.map(fn field -> get_field(cng, field) end)
123 |> Enum.uniq()
124 |> Enum.count()
125
126 if unique_fields == 1 do
127 cng
128 else
129 fields
130 |> Enum.reduce(cng, fn field, cng ->
131 cng
132 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
133 end)
134 end
135 end
136 end