Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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.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, {: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, ObjectValidators.ObjectID)
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 struct
53 |> cast(data, __schema__(:fields))
54 end
55
56 def validate_data(data_cng) do
57 data_cng
58 |> validate_inclusion(:type, ["Answer"])
59 |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
60 |> CommonValidations.validate_any_presence([:cc, :to])
61 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
62 |> CommonValidations.validate_actor_presence()
63 |> CommonValidations.validate_host_match()
64 end
65 end