ObjectValidator.CommonFixes: Introduce fix_objects_defaults and fix_activity_defaults
[akkoma] / lib / pleroma / web / activity_pub / object_validators / block_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.BlockValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9
10 import Ecto.Changeset
11 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
12
13 @primary_key false
14
15 embedded_schema do
16 field(:id, ObjectValidators.ObjectID, primary_key: true)
17 field(:type, :string)
18 field(:actor, ObjectValidators.ObjectID)
19 field(:to, ObjectValidators.Recipients, default: [])
20 field(:cc, ObjectValidators.Recipients, default: [])
21 field(:object, ObjectValidators.ObjectID)
22 end
23
24 def cast_data(data) do
25 %__MODULE__{}
26 |> cast(data, __schema__(:fields))
27 end
28
29 defp validate_data(cng) do
30 cng
31 |> validate_required([:id, :type, :actor, :to, :cc, :object])
32 |> validate_inclusion(:type, ["Block"])
33 |> validate_actor_presence()
34 |> validate_actor_presence(field_name: :object)
35 end
36
37 def cast_and_validate(data) do
38 data
39 |> cast_data
40 |> validate_data
41 end
42 end