GTS: cherry-picks and collection usage (#186)
[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: data
57 defp fix_tag(%{"tag" => tag} = data) when is_map(tag), do: Map.put(data, "tag", [tag])
58 defp fix_tag(data), do: Map.drop(data, ["tag"])
59
60 defp fix_replies(%{"replies" => replies} = data) when is_list(replies), do: data
61
62 defp fix_replies(%{"replies" => %{"first" => first}} = data) do
63 with {:ok, replies} <- Akkoma.Collections.Fetcher.fetch_collection(first) do
64 Map.put(data, "replies", replies)
65 else
66 {:error, _} ->
67 Logger.error("Could not fetch replies for #{first}")
68 Map.put(data, "replies", [])
69 end
70 end
71
72 defp fix_replies(%{"replies" => %{"items" => replies}} = data) when is_list(replies),
73 do: Map.put(data, "replies", replies)
74
75 defp fix_replies(data), do: Map.delete(data, "replies")
76
77 defp remote_mention_resolver(
78 %{"id" => ap_id, "tag" => tags},
79 "@" <> nickname = mention,
80 buffer,
81 opts,
82 acc
83 ) do
84 initial_host =
85 ap_id
86 |> URI.parse()
87 |> Map.get(:host)
88
89 with mention_tag <-
90 Enum.find(tags, fn t ->
91 t["type"] == "Mention" &&
92 (t["name"] == mention || mention == "#{t["name"]}@#{initial_host}")
93 end),
94 false <- is_nil(mention_tag),
95 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(mention_tag["href"]) do
96 link = Pleroma.Formatter.mention_tag(user, nickname, opts)
97 {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
98 else
99 _ -> {buffer, acc}
100 end
101 end
102
103 # https://github.com/misskey-dev/misskey/pull/8787
104 # Misskey has an awful tendency to drop all custom formatting when it sends remotely
105 # So this basically reprocesses their MFM source
106 defp fix_misskey_content(
107 %{"source" => %{"mediaType" => "text/x.misskeymarkdown", "content" => content}} = object
108 )
109 when is_binary(content) do
110 mention_handler = fn nick, buffer, opts, acc ->
111 remote_mention_resolver(object, nick, buffer, opts, acc)
112 end
113
114 {linked, _, _} =
115 Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
116
117 Map.put(object, "content", linked)
118 end
119
120 defp fix_misskey_content(%{"_misskey_content" => content} = object) when is_binary(content) do
121 mention_handler = fn nick, buffer, opts, acc ->
122 remote_mention_resolver(object, nick, buffer, opts, acc)
123 end
124
125 {linked, _, _} =
126 Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
127
128 object
129 |> Map.put("source", %{
130 "content" => content,
131 "mediaType" => "text/x.misskeymarkdown"
132 })
133 |> Map.put("content", linked)
134 |> Map.delete("_misskey_content")
135 end
136
137 defp fix_misskey_content(data), do: data
138
139 defp fix_source(%{"source" => source} = object) when is_binary(source) do
140 object
141 |> Map.put("source", %{"content" => source})
142 end
143
144 defp fix_source(object), do: object
145
146 defp fix(data) do
147 data
148 |> CommonFixes.fix_actor()
149 |> CommonFixes.fix_object_defaults()
150 |> fix_url()
151 |> fix_tag()
152 |> fix_replies()
153 |> fix_source()
154 |> fix_misskey_content()
155 |> Transmogrifier.fix_attachments()
156 |> Transmogrifier.fix_emoji()
157 |> Transmogrifier.fix_content_map()
158 end
159
160 def changeset(struct, data) do
161 data = fix(data)
162
163 struct
164 |> cast(data, __schema__(:fields) -- [:attachment, :tag])
165 |> cast_embed(:attachment)
166 |> cast_embed(:tag)
167 end
168
169 defp validate_data(data_cng) do
170 data_cng
171 |> validate_inclusion(:type, ["Article", "Note", "Page"])
172 |> validate_required([:id, :actor, :attributedTo, :type, :context])
173 |> CommonValidations.validate_any_presence([:cc, :to])
174 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
175 |> CommonValidations.validate_actor_presence()
176 |> CommonValidations.validate_host_match()
177 end
178 end