ObjectValidator.CommonFixes: Introduce fix_objects_defaults and fix_activity_defaults
[akkoma] / lib / pleroma / web / activity_pub / object_validators / create_generic_validator.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 # Code based on CreateChatMessageValidator
6 # NOTES
7 # - doesn't embed, will only get the object id
8 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
9 use Ecto.Schema
10
11 alias Pleroma.EctoType.ActivityPub.ObjectValidators
12 alias Pleroma.Object
13 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
14 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
15
16 import Ecto.Changeset
17
18 @primary_key false
19
20 embedded_schema do
21 field(:id, ObjectValidators.ObjectID, primary_key: true)
22 field(:actor, ObjectValidators.ObjectID)
23 field(:type, :string)
24 field(:to, ObjectValidators.Recipients, default: [])
25 field(:cc, ObjectValidators.Recipients, default: [])
26 field(:object, ObjectValidators.ObjectID)
27 field(:expires_at, ObjectValidators.DateTime)
28
29 # Should be moved to object, done for CommonAPI.Utils.make_context
30 field(:context, :string)
31 end
32
33 def cast_data(data, meta \\ []) do
34 data = fix(data, meta)
35
36 %__MODULE__{}
37 |> changeset(data)
38 end
39
40 def cast_and_apply(data) do
41 data
42 |> cast_data
43 |> apply_action(:insert)
44 end
45
46 def cast_and_validate(data, meta \\ []) do
47 data
48 |> cast_data(meta)
49 |> validate_data(meta)
50 end
51
52 def changeset(struct, data) do
53 struct
54 |> cast(data, __schema__(:fields))
55 end
56
57 defp fix_context(data, meta) do
58 if object = meta[:object_data] do
59 Map.put_new(data, "context", object["context"])
60 else
61 data
62 end
63 end
64
65 defp fix(data, meta) do
66 data
67 |> fix_context(meta)
68 |> CommonFixes.fix_actor()
69 |> CommonFixes.fix_activity_defaults(meta)
70 end
71
72 defp validate_data(cng, meta) do
73 cng
74 |> validate_required([:actor, :type, :object])
75 |> validate_inclusion(:type, ["Create"])
76 |> CommonValidations.validate_actor_presence()
77 |> CommonValidations.validate_any_presence([:to, :cc])
78 |> validate_actors_match(meta)
79 |> validate_context_match(meta)
80 |> validate_object_nonexistence()
81 |> validate_object_containment()
82 end
83
84 def validate_object_containment(cng) do
85 actor = get_field(cng, :actor)
86
87 cng
88 |> validate_change(:object, fn :object, object_id ->
89 %URI{host: object_id_host} = URI.parse(object_id)
90 %URI{host: actor_host} = URI.parse(actor)
91
92 if object_id_host == actor_host do
93 []
94 else
95 [{:object, "The host of the object id doesn't match with the host of the actor"}]
96 end
97 end)
98 end
99
100 def validate_object_nonexistence(cng) do
101 cng
102 |> validate_change(:object, fn :object, object_id ->
103 if Object.get_cached_by_ap_id(object_id) do
104 [{:object, "The object to create already exists"}]
105 else
106 []
107 end
108 end)
109 end
110
111 def validate_actors_match(cng, meta) do
112 attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
113
114 cng
115 |> validate_change(:actor, fn :actor, actor ->
116 if actor == attributed_to do
117 []
118 else
119 [{:actor, "Actor doesn't match with object attributedTo"}]
120 end
121 end)
122 end
123
124 def validate_context_match(cng, %{object_data: %{"context" => object_context}}) do
125 cng
126 |> validate_change(:context, fn :context, context ->
127 if context == object_context do
128 []
129 else
130 [{:context, "context field not matching between Create and object (#{object_context})"}]
131 end
132 end)
133 end
134
135 def validate_context_match(cng, _), do: cng
136 end