Merge remote-tracking branch 'pleroma/develop' into cycles-constants
[akkoma] / lib / pleroma / web / activity_pub / object_validators / article_note_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.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.ObjectValidators.TagValidator
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14
15 import Ecto.Changeset
16
17 @primary_key false
18 @derive Jason.Encoder
19
20 embedded_schema do
21 field(:id, ObjectValidators.ObjectID, primary_key: true)
22 field(:to, ObjectValidators.Recipients, default: [])
23 field(:cc, ObjectValidators.Recipients, default: [])
24 field(:bto, ObjectValidators.Recipients, default: [])
25 field(:bcc, ObjectValidators.Recipients, default: [])
26 embeds_many(:tag, TagValidator)
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
54 field(:replies, {:array, ObjectValidators.ObjectID}, default: [])
55 end
56
57 def cast_and_apply(data) do
58 data
59 |> cast_data
60 |> apply_action(:insert)
61 end
62
63 def cast_and_validate(data) do
64 data
65 |> cast_data()
66 |> validate_data()
67 end
68
69 def cast_data(data) do
70 %__MODULE__{}
71 |> changeset(data)
72 end
73
74 defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
75 defp fix_url(%{"url" => url} = data) when is_map(url), do: Map.put(data, "url", url["href"])
76 defp fix_url(data), do: data
77
78 defp fix_tag(%{"tag" => tag} = data) when is_list(tag), do: data
79 defp fix_tag(%{"tag" => tag} = data) when is_map(tag), do: Map.put(data, "tag", [tag])
80 defp fix_tag(data), do: Map.drop(data, ["tag"])
81
82 defp fix_replies(%{"replies" => %{"first" => %{"items" => replies}}} = data)
83 when is_list(replies),
84 do: Map.put(data, "replies", replies)
85
86 defp fix_replies(%{"replies" => %{"items" => replies}} = data) when is_list(replies),
87 do: Map.put(data, "replies", replies)
88
89 defp fix_replies(%{"replies" => replies} = data) when is_bitstring(replies),
90 do: Map.drop(data, ["replies"])
91
92 defp fix_replies(data), do: data
93
94 defp fix(data) do
95 data
96 |> CommonFixes.fix_actor()
97 |> CommonFixes.fix_object_defaults()
98 |> fix_url()
99 |> fix_tag()
100 |> fix_replies()
101 |> Transmogrifier.fix_emoji()
102 |> Transmogrifier.fix_content_map()
103 end
104
105 def changeset(struct, data) do
106 data = fix(data)
107
108 struct
109 |> cast(data, __schema__(:fields) -- [:attachment, :tag])
110 |> cast_embed(:attachment)
111 |> cast_embed(:tag)
112 end
113
114 defp validate_data(data_cng) do
115 data_cng
116 |> validate_inclusion(:type, ["Article", "Note"])
117 |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
118 |> CommonValidations.validate_any_presence([:cc, :to])
119 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
120 |> CommonValidations.validate_actor_presence()
121 |> CommonValidations.validate_host_match()
122 end
123 end