Remove Twemoji stuff from MastoFE
[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.Transmogrifier
14
15 import Ecto.Changeset
16
17 @primary_key false
18 @derive Jason.Encoder
19
20 # Extends from NoteValidator
21 embedded_schema do
22 field(:id, ObjectValidators.ObjectID, primary_key: true)
23 field(:to, ObjectValidators.Recipients, default: [])
24 field(:cc, ObjectValidators.Recipients, default: [])
25 field(:bto, ObjectValidators.Recipients, default: [])
26 field(:bcc, ObjectValidators.Recipients, default: [])
27 # TODO: Write type
28 field(:tag, {:array, :map}, default: [])
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_defaults()
87 |> CommonFixes.fix_attribution()
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])
97 |> cast_embed(:attachment)
98 |> cast_embed(:anyOf)
99 |> cast_embed(:oneOf)
100 end
101
102 def validate_data(data_cng) do
103 data_cng
104 |> validate_inclusion(:type, ["Question"])
105 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
106 |> CommonValidations.validate_any_presence([:cc, :to])
107 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
108 |> CommonValidations.validate_actor_presence()
109 |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
110 |> CommonValidations.validate_host_match()
111 end
112 end