merge
[akkoma] / lib / pleroma / web / activity_pub / object_validators / question_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.CommonFixes
11 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
12 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator
13 alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
14 alias Pleroma.Web.ActivityPub.Transmogrifier
15
16 import Ecto.Changeset
17
18 @primary_key false
19 @derive Jason.Encoder
20
21 # Extends from NoteValidator
22 embedded_schema do
23 field(:id, ObjectValidators.ObjectID, primary_key: true)
24 field(:to, ObjectValidators.Recipients, default: [])
25 field(:cc, ObjectValidators.Recipients, default: [])
26 field(:bto, ObjectValidators.Recipients, default: [])
27 field(:bcc, ObjectValidators.Recipients, default: [])
28 embeds_many(:tag, TagValidator)
29 field(:type, :string)
30 field(:content, :string)
31 field(:context, :string)
32
33 # TODO: Remove actor on objects
34 field(:actor, ObjectValidators.ObjectID)
35
36 field(:attributedTo, ObjectValidators.ObjectID)
37 field(:summary, :string)
38 field(:published, ObjectValidators.DateTime)
39 field(:emoji, ObjectValidators.Emoji, 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, ObjectValidators.ObjectID)
46 field(:url, ObjectValidators.Uri)
47 # short identifier for PleromaFE to group statuses by context
48 field(:context_id, :integer)
49
50 field(:likes, {:array, ObjectValidators.ObjectID}, default: [])
51 field(:announcements, {:array, ObjectValidators.ObjectID}, 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 defp fix(data) do
85 data
86 |> CommonFixes.fix_actor()
87 |> CommonFixes.fix_object_defaults()
88 |> Transmogrifier.fix_emoji()
89 |> fix_closed()
90 end
91
92 def changeset(struct, data) do
93 data = fix(data)
94
95 struct
96 |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment, :tag])
97 |> cast_embed(:attachment)
98 |> cast_embed(:anyOf)
99 |> cast_embed(:oneOf)
100 |> cast_embed(:tag)
101 end
102
103 defp validate_data(data_cng) do
104 data_cng
105 |> validate_inclusion(:type, ["Question"])
106 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
107 |> CommonValidations.validate_any_presence([:cc, :to])
108 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
109 |> CommonValidations.validate_actor_presence()
110 |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
111 |> CommonValidations.validate_host_match()
112 end
113 end