57d4456aa202659b31490c5604c05b029f98876f
[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 case User.get_cached_by_ap_id(actor) do
38 %User{deactivated: true} ->
39 [{field_name, "user is deactivated"}]
40
41 %User{} ->
42 []
43
44 _ ->
45 [{field_name, "can't find user"}]
46 end
47 end)
48 end
49
50 def validate_actor_is_active(cng, options \\ []) do
51 field_name = Keyword.get(options, :field_name, :actor)
52
53 cng
54 |> validate_change(field_name, fn field_name, actor ->
55 if %User{deactivated: false} = User.get_cached_by_ap_id(actor) do
56 []
57 else
58 [{field_name, "can't find user (or deactivated)"}]
59 end
60 end)
61 end
62
63 def validate_object_presence(cng, options \\ []) do
64 field_name = Keyword.get(options, :field_name, :object)
65 allowed_types = Keyword.get(options, :allowed_types, false)
66
67 cng
68 |> validate_change(field_name, fn field_name, object_id ->
69 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
70
71 cond do
72 !object ->
73 [{field_name, "can't find object"}]
74
75 object && allowed_types && object.data["type"] not in allowed_types ->
76 [{field_name, "object not in allowed types"}]
77
78 true ->
79 []
80 end
81 end)
82 end
83
84 def validate_object_or_user_presence(cng, options \\ []) do
85 field_name = Keyword.get(options, :field_name, :object)
86 options = Keyword.put(options, :field_name, field_name)
87
88 actor_cng =
89 cng
90 |> validate_actor_presence(options)
91
92 object_cng =
93 cng
94 |> validate_object_presence(options)
95
96 if actor_cng.valid?, do: actor_cng, else: object_cng
97 end
98
99 def validate_host_match(cng, fields \\ [:id, :actor]) do
100 unique_hosts =
101 fields
102 |> Enum.map(fn field ->
103 %URI{host: host} =
104 cng
105 |> get_field(field)
106 |> URI.parse()
107
108 host
109 end)
110 |> Enum.uniq()
111 |> Enum.count()
112
113 if unique_hosts == 1 do
114 cng
115 else
116 fields
117 |> Enum.reduce(cng, fn field, cng ->
118 cng
119 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
120 end)
121 end
122 end
123
124 def validate_fields_match(cng, fields) do
125 unique_fields =
126 fields
127 |> Enum.map(fn field -> get_field(cng, field) end)
128 |> Enum.uniq()
129 |> Enum.count()
130
131 if unique_fields == 1 do
132 cng
133 else
134 fields
135 |> Enum.reduce(cng, fn field, cng ->
136 cng
137 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
138 end)
139 end
140 end
141 end