f47acf606aabd89ddc756f83176db4f4db48e628
[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} =
87 Utils.create_context(data["context"] || data["conversation"])
88
89 data
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_attribution(data) do
96 data
97 |> Map.put_new("actor", data["attributedTo"])
98 end
99
100 defp fix(data) do
101 data
102 |> fix_attribution()
103 |> fix_closed()
104 |> fix_defaults()
105 end
106
107 def changeset(struct, data) do
108 data = fix(data)
109
110 struct
111 |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment])
112 |> cast_embed(:attachment)
113 |> cast_embed(:anyOf)
114 |> cast_embed(:oneOf)
115 end
116
117 def validate_data(data_cng) do
118 data_cng
119 |> validate_inclusion(:type, ["Question"])
120 |> validate_required([:id, :actor, :attributedTo, :type, :context])
121 |> CommonValidations.validate_any_presence([:cc, :to])
122 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
123 |> CommonValidations.validate_actor_presence()
124 |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
125 |> CommonValidations.validate_host_match()
126 end
127 end