Add new Emoji Ecto.Type and fix emoji in Question
[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 alias Pleroma.Web.ActivityPub.Transmogrifier
10
11 import Ecto.Changeset
12
13 @primary_key false
14
15 embedded_schema do
16 field(:id, ObjectValidators.ObjectID, primary_key: true)
17 field(:to, ObjectValidators.Recipients, default: [])
18 field(:cc, ObjectValidators.Recipients, default: [])
19 field(:bto, ObjectValidators.Recipients, default: [])
20 field(:bcc, ObjectValidators.Recipients, default: [])
21 # TODO: Write type
22 field(:tag, {:array, :map}, default: [])
23 field(:type, :string)
24
25 field(:name, :string)
26 field(:summary, :string)
27 field(:content, :string)
28
29 field(:context, :string)
30 # short identifier for PleromaFE to group statuses by context
31 field(:context_id, :integer)
32
33 field(:actor, ObjectValidators.ObjectID)
34 field(:attributedTo, ObjectValidators.ObjectID)
35 field(:published, ObjectValidators.DateTime)
36 field(:emoji, ObjectValidators.Emoji, default: %{})
37 field(:sensitive, :boolean, default: false)
38 # TODO: Write type
39 field(:attachment, {:array, :map}, default: [])
40 field(:replies_count, :integer, default: 0)
41 field(:like_count, :integer, default: 0)
42 field(:announcement_count, :integer, default: 0)
43 field(:inReplyTo, ObjectValidators.ObjectID)
44 field(:url, ObjectValidators.Uri)
45
46 field(:likes, {:array, :string}, default: [])
47 field(:announcements, {:array, :string}, default: [])
48 end
49
50 def cast_and_validate(data) do
51 data
52 |> cast_data()
53 |> validate_data()
54 end
55
56 defp fix(data) do
57 data
58 |> Transmogrifier.fix_emoji()
59 end
60
61 def cast_data(data) do
62 data = fix(data)
63
64 %__MODULE__{}
65 |> cast(data, __schema__(:fields))
66 end
67
68 def validate_data(data_cng) do
69 data_cng
70 |> validate_inclusion(:type, ["Note"])
71 |> validate_required([:id, :actor, :to, :cc, :type, :content, :context])
72 end
73 end