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