Quote posting (#113)
[akkoma] / lib / pleroma / web / activity_pub / object_validators / article_note_page_validator.ex
index e11335170a6fc60252bee6944f12a36415589449..a0724ca55427d9f6ca0ef7dd0c4a4dab22b1ce6c 100644 (file)
@@ -4,7 +4,7 @@
 
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
   use Ecto.Schema
-
+  alias Pleroma.User
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
   alias Pleroma.Object.Fetcher
   alias Pleroma.Web.CommonAPI.Utils
@@ -81,16 +81,53 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
 
   defp fix_replies(data), do: data
 
+  defp remote_mention_resolver(
+         %{"id" => ap_id, "tag" => tags},
+         "@" <> nickname = mention,
+         buffer,
+         opts,
+         acc
+       ) do
+    initial_host =
+      ap_id
+      |> URI.parse()
+      |> Map.get(:host)
+
+    with mention_tag <-
+           Enum.find(tags, fn t ->
+             t["type"] == "Mention" &&
+               (t["name"] == mention || mention == "#{t["name"]}@#{initial_host}")
+           end),
+         false <- is_nil(mention_tag),
+         {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(mention_tag["href"]) do
+      link = Pleroma.Formatter.mention_tag(user, nickname, opts)
+      {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
+    else
+      _ -> {buffer, acc}
+    end
+  end
+
   # https://github.com/misskey-dev/misskey/pull/8787
   defp fix_misskey_content(
          %{"source" => %{"mediaType" => "text/x.misskeymarkdown", "content" => content}} = object
        ) do
-    {linked, _, _} = Utils.format_input(content, "text/x.misskeymarkdown")
+    mention_handler = fn nick, buffer, opts, acc ->
+      remote_mention_resolver(object, nick, buffer, opts, acc)
+    end
+
+    {linked, _, _} =
+      Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
+
     Map.put(object, "content", linked)
   end
 
   defp fix_misskey_content(%{"_misskey_content" => content} = object) do
-    {linked, _, _} = Utils.format_input(content, "text/x.misskeymarkdown")
+    mention_handler = fn nick, buffer, opts, acc ->
+      remote_mention_resolver(object, nick, buffer, opts, acc)
+    end
+
+    {linked, _, _} =
+      Utils.format_input(content, "text/x.misskeymarkdown", mention_handler: mention_handler)
 
     object
     |> Map.put("source", %{
@@ -119,6 +156,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
     |> fix_replies()
     |> fix_source()
     |> fix_misskey_content()
+    |> Transmogrifier.fix_quote_url()
     |> Transmogrifier.fix_attachments()
     |> Transmogrifier.fix_emoji()
     |> Transmogrifier.fix_content_map()