0b51eccfa80131e1977acb34c3029d3d88fbe31b
[akkoma] / lib / pleroma / web / activity_pub / object_validators / answer_validator.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.AnswerValidator do
6 use Ecto.Schema
7
8 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
9 alias Pleroma.Web.ActivityPub.ObjectValidators.Types
10
11 import Ecto.Changeset
12
13 @primary_key false
14 @derive Jason.Encoder
15
16 # Extends from NoteValidator
17 embedded_schema do
18 field(:id, Types.ObjectID, primary_key: true)
19 field(:to, {:array, :string}, default: [])
20 field(:cc, {:array, :string}, default: [])
21 field(:bto, {:array, :string}, default: [])
22 field(:bcc, {:array, :string}, default: [])
23 field(:type, :string)
24 field(:name, :string)
25 field(:inReplyTo, :string)
26 field(:attributedTo, Types.ObjectID)
27 end
28
29 def cast_and_apply(data) do
30 data
31 |> cast_data
32 |> apply_action(:insert)
33 end
34
35 def cast_and_validate(data) do
36 data
37 |> cast_data()
38 |> validate_data()
39 end
40
41 def cast_data(data) do
42 %__MODULE__{}
43 |> changeset(data)
44 end
45
46 def changeset(struct, data) do
47 struct
48 |> cast(data, __schema__(:fields))
49 end
50
51 def validate_data(data_cng) do
52 data_cng
53 |> validate_inclusion(:type, ["Answer"])
54 |> validate_required([:id, :inReplyTo, :name])
55 |> CommonValidations.validate_any_presence([:cc, :to])
56 |> CommonValidations.validate_actor_presence()
57 end
58 end