Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / activity_pub / object_validators / article_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.ArticleNoteValidator 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.Transmogrifier
13
14 import Ecto.Changeset
15
16 @primary_key false
17 @derive Jason.Encoder
18
19 embedded_schema do
20 field(:id, ObjectValidators.ObjectID, primary_key: true)
21 field(:to, ObjectValidators.Recipients, default: [])
22 field(:cc, ObjectValidators.Recipients, default: [])
23 field(:bto, ObjectValidators.Recipients, default: [])
24 field(:bcc, ObjectValidators.Recipients, default: [])
25 # TODO: Write type
26 field(:tag, {:array, :map}, default: [])
27 field(:type, :string)
28
29 field(:name, :string)
30 field(:summary, :string)
31 field(:content, :string)
32
33 field(:context, :string)
34 # short identifier for PleromaFE to group statuses by context
35 field(:context_id, :integer)
36
37 # TODO: Remove actor on objects
38 field(:actor, ObjectValidators.ObjectID)
39
40 field(:attributedTo, ObjectValidators.ObjectID)
41 field(:published, ObjectValidators.DateTime)
42 field(:emoji, ObjectValidators.Emoji, default: %{})
43 field(:sensitive, :boolean, default: false)
44 embeds_many(:attachment, AttachmentValidator)
45 field(:replies_count, :integer, default: 0)
46 field(:like_count, :integer, default: 0)
47 field(:announcement_count, :integer, default: 0)
48 field(:inReplyTo, ObjectValidators.ObjectID)
49 field(:url, ObjectValidators.Uri)
50
51 field(:likes, {:array, ObjectValidators.ObjectID}, default: [])
52 field(:announcements, {:array, ObjectValidators.ObjectID}, default: [])
53 end
54
55 def cast_and_apply(data) do
56 data
57 |> cast_data
58 |> apply_action(:insert)
59 end
60
61 def cast_and_validate(data) do
62 data
63 |> cast_data()
64 |> validate_data()
65 end
66
67 def cast_data(data) do
68 data = fix(data)
69
70 %__MODULE__{}
71 |> changeset(data)
72 end
73
74 defp fix_url(%{"url" => url} = data) when is_map(url) do
75 Map.put(data, "url", url["href"])
76 end
77
78 defp fix_url(data), do: data
79
80 defp fix(data) do
81 data
82 |> CommonFixes.fix_defaults()
83 |> CommonFixes.fix_attribution()
84 |> CommonFixes.fix_actor()
85 |> fix_url()
86 |> Transmogrifier.fix_emoji()
87 end
88
89 def changeset(struct, data) do
90 data = fix(data)
91
92 struct
93 |> cast(data, __schema__(:fields) -- [:attachment])
94 |> cast_embed(:attachment)
95 end
96
97 def validate_data(data_cng) do
98 data_cng
99 |> validate_inclusion(:type, ["Article", "Note"])
100 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
101 |> CommonValidations.validate_any_presence([:cc, :to])
102 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
103 |> CommonValidations.validate_actor_presence()
104 |> CommonValidations.validate_host_match()
105 end
106 end