1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.AttachmentValidator
10 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
11 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator
12 alias Pleroma.Web.ActivityPub.Utils
19 # Extends from NoteValidator
21 field(:id, ObjectValidators.ObjectID, primary_key: true)
22 field(:to, {:array, :string}, default: [])
23 field(:cc, {:array, :string}, default: [])
24 field(:bto, {:array, :string}, default: [])
25 field(:bcc, {:array, :string}, default: [])
27 field(:tag, {:array, :map}, default: [])
29 field(:content, :string)
30 field(:context, :string)
32 # TODO: Remove actor on objects
33 field(:actor, ObjectValidators.ObjectID)
35 field(:attributedTo, ObjectValidators.ObjectID)
36 field(:summary, :string)
37 field(:published, ObjectValidators.DateTime)
39 field(:emoji, :map, default: %{})
40 field(:sensitive, :boolean, default: false)
41 embeds_many(:attachment, AttachmentValidator)
42 field(:replies_count, :integer, default: 0)
43 field(:like_count, :integer, default: 0)
44 field(:announcement_count, :integer, default: 0)
45 field(:inReplyTo, :string)
46 field(:uri, ObjectValidators.Uri)
47 # short identifier for PleromaFE to group statuses by context
48 field(:context_id, :integer)
50 field(:likes, {:array, :string}, default: [])
51 field(:announcements, {:array, :string}, default: [])
53 field(:closed, ObjectValidators.DateTime)
54 field(:voters, {:array, ObjectValidators.ObjectID}, default: [])
55 embeds_many(:anyOf, QuestionOptionsValidator)
56 embeds_many(:oneOf, QuestionOptionsValidator)
59 def cast_and_apply(data) do
62 |> apply_action(:insert)
65 def cast_and_validate(data) do
71 def cast_data(data) do
76 defp fix_closed(data) do
78 is_binary(data["closed"]) -> data
79 is_binary(data["endTime"]) -> Map.put(data, "closed", data["endTime"])
80 true -> Map.drop(data, ["closed"])
84 # based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
85 defp fix_defaults(data) do
86 %{data: %{"id" => context}, id: context_id} = Utils.create_context(data["context"])
89 |> Map.put_new_lazy("id", &Utils.generate_object_id/0)
90 |> Map.put_new_lazy("published", &Utils.make_date/0)
91 |> Map.put_new("context", context)
92 |> Map.put_new("context_id", context_id)
101 def changeset(struct, data) do
105 |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment])
106 |> cast_embed(:attachment)
107 |> cast_embed(:anyOf)
108 |> cast_embed(:oneOf)
111 def validate_data(data_cng) do
113 |> validate_inclusion(:type, ["Question"])
114 |> validate_required([:id, :actor, :attributedTo, :type, :context])
115 |> CommonValidations.validate_any_presence([:cc, :to])
116 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
117 |> CommonValidations.validate_actor_is_active()
118 |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
119 |> CommonValidations.validate_host_match()