d248c6aec2fb54c4a4c856b2ea2b97c5dddc67a4
[akkoma] / lib / pleroma / web / activity_pub / object_validators / question_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.QuestionValidator do
6 use Ecto.Schema
7
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
13
14 import Ecto.Changeset
15
16 @primary_key false
17 @derive Jason.Encoder
18
19 # Extends from NoteValidator
20 embedded_schema do
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: [])
26 # TODO: Write type
27 field(:tag, {:array, :map}, default: [])
28 field(:type, :string)
29 field(:content, :string)
30 field(:context, :string)
31
32 # TODO: Remove actor on objects
33 field(:actor, ObjectValidators.ObjectID)
34
35 field(:attributedTo, ObjectValidators.ObjectID)
36 field(:summary, :string)
37 field(:published, ObjectValidators.DateTime)
38 # TODO: Write type
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)
49
50 field(:likes, {:array, :string}, default: [])
51 field(:announcements, {:array, :string}, default: [])
52
53 field(:closed, ObjectValidators.DateTime)
54 field(:voters, {:array, ObjectValidators.ObjectID}, default: [])
55 embeds_many(:anyOf, QuestionOptionsValidator)
56 embeds_many(:oneOf, QuestionOptionsValidator)
57 end
58
59 def cast_and_apply(data) do
60 data
61 |> cast_data
62 |> apply_action(:insert)
63 end
64
65 def cast_and_validate(data) do
66 data
67 |> cast_data()
68 |> validate_data()
69 end
70
71 def cast_data(data) do
72 %__MODULE__{}
73 |> changeset(data)
74 end
75
76 defp fix_closed(data) do
77 cond do
78 is_binary(data["closed"]) -> data
79 is_binary(data["endTime"]) -> Map.put(data, "closed", data["endTime"])
80 true -> Map.drop(data, ["closed"])
81 end
82 end
83
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"])
87
88 data
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)
93 end
94
95 defp fix(data) do
96 data
97 |> fix_closed()
98 |> fix_defaults()
99 end
100
101 def changeset(struct, data) do
102 data = fix(data)
103
104 struct
105 |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment])
106 |> cast_embed(:attachment)
107 |> cast_embed(:anyOf)
108 |> cast_embed(:oneOf)
109 end
110
111 def validate_data(data_cng) do
112 data_cng
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()
120 end
121 end