First
[anni] / lib / pleroma / web / activity_pub / object_validators / question_options_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
6   use Ecto.Schema
7
8   import Ecto.Changeset
9
10   @primary_key false
11
12   embedded_schema do
13     field(:name, :string)
14
15     embeds_one :replies, Replies, primary_key: false do
16       field(:totalItems, :integer)
17       field(:type, :string)
18     end
19
20     field(:type, :string)
21   end
22
23   def changeset(struct, data) do
24     struct
25     |> cast(data, [:name, :type])
26     |> cast_embed(:replies, with: &replies_changeset/2)
27     |> validate_inclusion(:type, ["Note"])
28     |> validate_required([:name, :type])
29   end
30
31   def replies_changeset(struct, data) do
32     struct
33     |> cast(data, [:totalItems, :type])
34     |> validate_inclusion(:type, ["Collection"])
35     |> validate_required([:type])
36   end
37 end