Merge branch 'feature/2071-actor-type' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / object_validators / note_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.NoteValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9
10 import Ecto.Changeset
11
12 @primary_key false
13
14 embedded_schema do
15 field(:id, ObjectValidators.ObjectID, primary_key: true)
16 field(:to, ObjectValidators.Recipients, default: [])
17 field(:cc, ObjectValidators.Recipients, default: [])
18 field(:bto, ObjectValidators.Recipients, default: [])
19 field(:bcc, ObjectValidators.Recipients, default: [])
20 # TODO: Write type
21 field(:tag, {:array, :map}, default: [])
22 field(:type, :string)
23 field(:content, :string)
24 field(:context, :string)
25 field(:actor, ObjectValidators.ObjectID)
26 field(:attributedTo, ObjectValidators.ObjectID)
27 field(:summary, :string)
28 field(:published, ObjectValidators.DateTime)
29 # TODO: Write type
30 field(:emoji, :map, default: %{})
31 field(:sensitive, :boolean, default: false)
32 # TODO: Write type
33 field(:attachment, {:array, :map}, default: [])
34 field(:replies_count, :integer, default: 0)
35 field(:like_count, :integer, default: 0)
36 field(:announcement_count, :integer, default: 0)
37 field(:inReplyTo, ObjectValidators.ObjectID)
38 field(:url, ObjectValidators.Uri)
39
40 field(:likes, {:array, :string}, default: [])
41 field(:announcements, {:array, :string}, default: [])
42
43 # see if needed
44 field(:context_id, :string)
45 end
46
47 def cast_and_validate(data) do
48 data
49 |> cast_data()
50 |> validate_data()
51 end
52
53 def cast_data(data) do
54 %__MODULE__{}
55 |> cast(data, __schema__(:fields))
56 end
57
58 def validate_data(data_cng) do
59 data_cng
60 |> validate_inclusion(:type, ["Note"])
61 |> validate_required([:id, :actor, :to, :cc, :type, :content, :context])
62 end
63 end