mandate published on notes
[akkoma] / lib / pleroma / web / activity_pub / object_validators / article_note_page_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.ArticleNotePageValidator do
6 use Ecto.Schema
7 alias Pleroma.User
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9 alias Pleroma.Web.CommonAPI.Utils
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 require Logger
17
18 @primary_key false
19 @derive Jason.Encoder
20
21 embedded_schema do
22 quote do
23 unquote do
24 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
25 message_fields()
26 object_fields()
27 status_object_fields()
28 end
29 end
30
31 field(:replies, {:array, ObjectValidators.ObjectID}, default: [])
32 field(:source, :map)
33 end
34
35 def cast_and_apply(data) do
36 data
37 |> cast_data
38 |> apply_action(:insert)
39 end
40
41 def cast_and_validate(data) do
42 data
43 |> cast_data()
44 |> validate_data()
45 end
46
47 def cast_data(data) do
48 %__MODULE__{}
49 |> changeset(data)
50 end
51
52 defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
53 defp fix_url(%{"url" => url} = data) when is_map(url), do: Map.put(data, "url", url["href"])
54 defp fix_url(data), do: data
55
56 defp fix_tag(%{"tag" => tag} = data) when is_list(tag) do
57 Map.put(data, "tag", Enum.filter(tag, &is_map/1))
58 end
59
60 defp fix_tag(%{"tag" => tag} = data) when is_map(tag), do: Map.put(data, "tag", [tag])
61 defp fix_tag(data), do: Map.drop(data, ["tag"])
62
63 defp fix_replies(%{"replies" => replies} = data) when is_list(replies), do: data
64
65 defp fix_replies(%{"replies" => %{"first" => first}} = data) do
66 with {:ok, replies} <- Akkoma.Collections.Fetcher.fetch_collection(first) do
67 Map.put(data, "replies", replies)
68 else
69 {:error, _} ->
70 Logger.error("Could not fetch replies for #{first}")
71 Map.put(data, "replies", [])
72 end
73 end
74
75 defp fix_replies(%{"replies" => %{"items" => replies}} = data) when is_list(replies),
76 do: Map.put(data, "replies", replies)
77
78 defp fix_replies(data), do: Map.delete(data, "replies")
79
80 defp remote_mention_resolver(
81 %{"id" => ap_id, "tag" => tags},
82 "@" <> nickname = mention,
83 buffer,
84 opts,
85 acc
86 ) do
87 initial_host =
88 ap_id
89 |> URI.parse()
90 |> Map.get(:host)
91
92 with mention_tag <-
93 Enum.find(tags, fn t ->
94 t["type"] == "Mention" &&
95 (t["name"] == mention || mention == "#{t["name"]}@#{initial_host}")
96 end),
97 false <- is_nil(mention_tag),
98 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(mention_tag["href"]) do
99 link = Pleroma.Formatter.mention_tag(user, nickname, opts)
100 {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
101 else
102 _ -> {buffer, acc}
103 end
104 end
105
106 # https://github.com/misskey-dev/misskey/pull/8787
107 # Misskey has an awful tendency to drop all custom formatting when it sends remotely
108 # So this basically reprocesses their MFM source
109 defp fix_misskey_content(
110 %{"source" => %{"mediaType" => "text/x.misskeymarkdown", "content" => content}} = object
111 )
112 when is_binary(content) do
113 mention_handler = fn nick, buffer, opts, acc ->
114 remote_mention_resolver(object, nick, buffer, opts, acc)
115 end
116
117 {linked, _, _} =
118 Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
119
120 Map.put(object, "content", linked)
121 end
122
123 defp fix_misskey_content(%{"_misskey_content" => content} = object) when is_binary(content) do
124 mention_handler = fn nick, buffer, opts, acc ->
125 remote_mention_resolver(object, nick, buffer, opts, acc)
126 end
127
128 {linked, _, _} =
129 Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
130
131 object
132 |> Map.put("source", %{
133 "content" => content,
134 "mediaType" => "text/x.misskeymarkdown"
135 })
136 |> Map.put("content", linked)
137 |> Map.delete("_misskey_content")
138 end
139
140 defp fix_misskey_content(data), do: data
141
142 defp fix_source(%{"source" => source} = object) when is_binary(source) do
143 object
144 |> Map.put("source", %{"content" => source})
145 end
146
147 defp fix_source(object), do: object
148
149 defp fix(data) do
150 data
151 |> CommonFixes.fix_actor()
152 |> CommonFixes.fix_object_defaults()
153 |> fix_url()
154 |> fix_tag()
155 |> fix_replies()
156 |> fix_source()
157 |> fix_misskey_content()
158 |> Transmogrifier.fix_attachments()
159 |> Transmogrifier.fix_emoji()
160 |> Transmogrifier.fix_content_map()
161 end
162
163 def changeset(struct, data) do
164 data = fix(data)
165
166 struct
167 |> cast(data, __schema__(:fields) -- [:attachment, :tag])
168 |> cast_embed(:attachment)
169 |> cast_embed(:tag)
170 end
171
172 defp validate_data(data_cng) do
173 data_cng
174 |> validate_inclusion(:type, ["Article", "Note", "Page"])
175 |> validate_required([:id, :actor, :attributedTo, :type, :context, :published])
176 |> CommonValidations.validate_any_presence([:cc, :to])
177 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
178 |> CommonValidations.validate_actor_presence()
179 |> CommonValidations.validate_host_match()
180 end
181 end