Merge remote-tracking branch 'origin/develop' into fix-attachment-dimensions
[akkoma] / lib / pleroma / web / activity_pub / object_validators / answer_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 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
10 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
11
12 import Ecto.Changeset
13
14 @primary_key false
15 @derive Jason.Encoder
16
17 embedded_schema do
18 quote do
19 unquote do
20 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
21 message_fields()
22 end
23 end
24
25 field(:name, :string)
26 field(:inReplyTo, ObjectValidators.ObjectID)
27 field(:attributedTo, ObjectValidators.ObjectID)
28 field(:context, :string)
29
30 # TODO: Remove actor on objects
31 field(:actor, ObjectValidators.ObjectID)
32 end
33
34 def cast_and_apply(data) do
35 data
36 |> cast_data()
37 |> apply_action(:insert)
38 end
39
40 def cast_and_validate(data) do
41 data
42 |> cast_data()
43 |> validate_data()
44 end
45
46 def cast_data(data) do
47 %__MODULE__{}
48 |> changeset(data)
49 end
50
51 def changeset(struct, data) do
52 data =
53 data
54 |> CommonFixes.fix_actor()
55 |> CommonFixes.fix_object_defaults()
56
57 struct
58 |> cast(data, __schema__(:fields))
59 end
60
61 defp validate_data(data_cng) do
62 data_cng
63 |> validate_inclusion(:type, ["Answer"])
64 |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
65 |> CommonValidations.validate_any_presence([:cc, :to])
66 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
67 |> CommonValidations.validate_actor_presence()
68 |> CommonValidations.validate_host_match()
69 end
70 end