Merge pull request 'Allow reacting with emojis containing a number' (#6) from sn0w...
[akkoma] / lib / pleroma / web / activity_pub / object_validators / event_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.EventValidator do
6 use Ecto.Schema
7
8 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
9 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11
12 import Ecto.Changeset
13
14 @primary_key false
15 @derive Jason.Encoder
16
17 # Extends from NoteValidator
18 embedded_schema do
19 quote do
20 unquote do
21 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
22 message_fields()
23 object_fields()
24 status_object_fields()
25 end
26 end
27 end
28
29 def cast_and_apply(data) do
30 data
31 |> cast_data
32 |> apply_action(:insert)
33 end
34
35 def cast_and_validate(data) do
36 data
37 |> cast_data()
38 |> validate_data()
39 end
40
41 def cast_data(data) do
42 %__MODULE__{}
43 |> changeset(data)
44 end
45
46 defp fix(data) do
47 data
48 |> CommonFixes.fix_actor()
49 |> CommonFixes.fix_object_defaults()
50 |> Transmogrifier.fix_emoji()
51 end
52
53 def changeset(struct, data) do
54 data = fix(data)
55
56 struct
57 |> cast(data, __schema__(:fields) -- [:attachment, :tag])
58 |> cast_embed(:attachment)
59 |> cast_embed(:tag)
60 end
61
62 defp validate_data(data_cng) do
63 data_cng
64 |> validate_inclusion(:type, ["Event"])
65 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
66 |> CommonValidations.validate_any_presence([:cc, :to])
67 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
68 |> CommonValidations.validate_actor_presence()
69 |> CommonValidations.validate_host_match()
70 end
71 end