Merge remote-tracking branch 'pleroma/develop' into cycles-router-api-routes
[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.CommonValidations
10
11 import Ecto.Changeset
12
13 @primary_key false
14 @derive Jason.Encoder
15
16 embedded_schema do
17 field(:id, ObjectValidators.ObjectID, primary_key: true)
18 field(:to, ObjectValidators.Recipients, default: [])
19 field(:cc, ObjectValidators.Recipients, default: [])
20 field(:bto, ObjectValidators.Recipients, default: [])
21 field(:bcc, ObjectValidators.Recipients, default: [])
22 field(:type, :string)
23 field(:name, :string)
24 field(:inReplyTo, ObjectValidators.ObjectID)
25 field(:attributedTo, ObjectValidators.ObjectID)
26
27 # TODO: Remove actor on objects
28 field(:actor, ObjectValidators.ObjectID)
29 end
30
31 def cast_and_apply(data) do
32 data
33 |> cast_data()
34 |> apply_action(:insert)
35 end
36
37 def cast_and_validate(data) do
38 data
39 |> cast_data()
40 |> validate_data()
41 end
42
43 def cast_data(data) do
44 %__MODULE__{}
45 |> changeset(data)
46 end
47
48 def changeset(struct, data) do
49 struct
50 |> cast(data, __schema__(:fields))
51 end
52
53 defp validate_data(data_cng) do
54 data_cng
55 |> validate_inclusion(:type, ["Answer"])
56 |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
57 |> CommonValidations.validate_any_presence([:cc, :to])
58 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
59 |> CommonValidations.validate_actor_presence()
60 |> CommonValidations.validate_host_match()
61 end
62 end