8d4c925206711a108a3c9fd054c1a415d1e5ed65
[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 embedded_schema do
17 field(:id, Types.ObjectID, primary_key: true)
18 field(:to, {:array, :string}, default: [])
19 field(:cc, {:array, :string}, default: [])
20
21 # is this actually needed?
22 field(:bto, {:array, :string}, default: [])
23 field(:bcc, {:array, :string}, default: [])
24
25 field(:type, :string)
26 field(:name, :string)
27 field(:inReplyTo, :string)
28 field(:attributedTo, Types.ObjectID)
29 field(:actor, Types.ObjectID)
30 end
31
32 def cast_and_apply(data) do
33 data
34 |> cast_data()
35 |> apply_action(:insert)
36 end
37
38 def cast_and_validate(data) do
39 data
40 |> cast_data()
41 |> validate_data()
42 end
43
44 def cast_data(data) do
45 %__MODULE__{}
46 |> changeset(data)
47 end
48
49 def changeset(struct, data) do
50 struct
51 |> cast(data, __schema__(:fields))
52 end
53
54 def validate_data(data_cng) do
55 data_cng
56 |> validate_inclusion(:type, ["Answer"])
57 |> validate_required([:id, :inReplyTo, :name])
58 |> CommonValidations.validate_any_presence([:cc, :to])
59 |> CommonValidations.validate_actor_presence()
60 end
61 end