1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
10 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
11 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator
12 alias Pleroma.Web.ActivityPub.Transmogrifier
19 # Extends from NoteValidator
23 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
26 status_object_fields()
30 field(:closed, ObjectValidators.DateTime)
31 field(:voters, {:array, ObjectValidators.ObjectID}, default: [])
32 embeds_many(:anyOf, QuestionOptionsValidator)
33 embeds_many(:oneOf, QuestionOptionsValidator)
36 def cast_and_apply(data) do
39 |> apply_action(:insert)
42 def cast_and_validate(data) do
48 def cast_data(data) do
53 defp fix_closed(data) do
55 is_binary(data["closed"]) -> data
56 is_binary(data["endTime"]) -> Map.put(data, "closed", data["endTime"])
57 true -> Map.drop(data, ["closed"])
63 |> CommonFixes.fix_actor()
64 |> CommonFixes.fix_object_defaults()
65 |> Transmogrifier.fix_emoji()
69 def changeset(struct, data) do
73 |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment, :tag])
74 |> cast_embed(:attachment)
80 defp validate_data(data_cng) do
82 |> validate_inclusion(:type, ["Question"])
83 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
84 |> CommonValidations.validate_any_presence([:cc, :to])
85 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
86 |> CommonValidations.validate_actor_presence()
87 |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
88 |> CommonValidations.validate_host_match()