Merge remote-tracking branch 'pleroma/develop' into admin-api-users-sort
[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.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.TagValidator
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 embeds_many(:tag, TagValidator)
28 field(:type, :string)
29
30 field(:name, :string)
31 field(:summary, :string)
32 field(:content, :string)
33
34 field(:context, :string)
35 # short identifier for PleromaFE to group statuses by context
36 field(:context_id, :integer)
37
38 # TODO: Remove actor on objects
39 field(:actor, ObjectValidators.ObjectID)
40
41 field(:attributedTo, ObjectValidators.ObjectID)
42 field(:published, ObjectValidators.DateTime)
43 field(:emoji, ObjectValidators.Emoji, default: %{})
44 field(:sensitive, :boolean, default: false)
45 embeds_many(:attachment, AttachmentValidator)
46 field(:replies_count, :integer, default: 0)
47 field(:like_count, :integer, default: 0)
48 field(:announcement_count, :integer, default: 0)
49 field(:inReplyTo, ObjectValidators.ObjectID)
50 field(:url, ObjectValidators.Uri)
51
52 field(:likes, {:array, ObjectValidators.ObjectID}, default: [])
53 field(:announcements, {:array, ObjectValidators.ObjectID}, default: [])
54 end
55
56 def cast_and_apply(data) do
57 data
58 |> cast_data
59 |> apply_action(:insert)
60 end
61
62 def cast_and_validate(data) do
63 data
64 |> cast_data()
65 |> validate_data()
66 end
67
68 def cast_data(data) do
69 %__MODULE__{}
70 |> changeset(data)
71 end
72
73 defp fix(data) do
74 data
75 |> CommonFixes.fix_actor()
76 |> CommonFixes.fix_object_defaults()
77 |> Transmogrifier.fix_emoji()
78 end
79
80 def changeset(struct, data) do
81 data = fix(data)
82
83 struct
84 |> cast(data, __schema__(:fields) -- [:attachment, :tag])
85 |> cast_embed(:attachment)
86 |> cast_embed(:tag)
87 end
88
89 defp validate_data(data_cng) do
90 data_cng
91 |> validate_inclusion(:type, ["Event"])
92 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
93 |> CommonValidations.validate_any_presence([:cc, :to])
94 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
95 |> CommonValidations.validate_actor_presence()
96 |> CommonValidations.validate_host_match()
97 end
98 end