From 1bbc701a3ababc4c6e856ad24f8826da0a0ba1a3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:14 -0600 Subject: [PATCH 1/4] ForceMentionsInContent: use `to` instead of `tag` --- .../web/activity_pub/mrf/force_mentions_in_content.ex | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 522ae2f600..8d3885d7b1 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -35,15 +35,12 @@ defp extract_mention_uris_from_content(content) do end @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}} = object) do + def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["content"] || "" mention_users = - tag - |> Enum.filter(fn tag -> tag["type"] == "Mention" end) - |> Enum.map(& &1["href"]) - |> Enum.reject(&is_nil/1) + to |> Enum.map(fn ap_id_or_uri -> case User.get_or_fetch_by_ap_id(ap_id_or_uri) do {:ok, user} -> {ap_id_or_uri, user} From d5644a52aa4f031c69a2938b333636660156c703 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:45 -0600 Subject: [PATCH 2/4] ForceMentionsInContent: wrap inline mentions with span tag --- lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex | 2 +- priv/scrubbers/default.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 8d3885d7b1..97ea67002e 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -63,7 +63,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = content = if added_mentions != "", - do: added_mentions <> " " <> content, + do: "#{added_mentions} " <> content, else: content {:ok, put_in(object["object"]["content"], content)} diff --git a/priv/scrubbers/default.ex b/priv/scrubbers/default.ex index 4694a92a53..f33a721a8b 100644 --- a/priv/scrubbers/default.ex +++ b/priv/scrubbers/default.ex @@ -56,7 +56,7 @@ defmodule Pleroma.HTML.Scrubber.Default do Meta.allow_tag_with_these_attributes(:u, []) Meta.allow_tag_with_these_attributes(:ul, []) - Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card"]) + Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card", "recipients-inline"]) Meta.allow_tag_with_these_attributes(:span, []) Meta.allow_tag_with_this_attribute_values(:code, "class", ["inline"]) From c5a20c80c4ed8fdd4e7de18caf85efb56be1db6e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:44:44 -0600 Subject: [PATCH 3/4] ForceMentionsInContent: simplify finding users --- .../web/activity_pub/mrf/force_mentions_in_content.ex | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 97ea67002e..a458bed310 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -41,13 +41,8 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to - |> Enum.map(fn ap_id_or_uri -> - case User.get_or_fetch_by_ap_id(ap_id_or_uri) do - {:ok, user} -> {ap_id_or_uri, user} - _ -> {ap_id_or_uri, User.get_by_uri(ap_id_or_uri)} - end - end) - |> Enum.reject(fn {_, user} -> user == nil end) + |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) explicitly_mentioned_uris = extract_mention_uris_from_content(content) From 267184b70e587a3e6301d3a01bd16fd465bc0f68 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 20:03:43 -0600 Subject: [PATCH 4/4] ForceMentionsInContentTest: return mentions in a not terrible format --- .../mrf/force_mentions_in_content.ex | 6 +-- .../mrf/force_mentions_in_content_test.exs | 38 +++++++++++++------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index a458bed310..1d750d4c23 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -41,7 +41,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to - |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.map(&{&1, User.get_cached_by_ap_id(&1)}) |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) @@ -50,7 +50,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = added_mentions = Enum.reduce(mention_users, "", fn {uri, user}, acc -> unless uri in explicitly_mentioned_uris do - acc <> Formatter.mention_from_user(user) + acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " " else acc end @@ -58,7 +58,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = content = if added_mentions != "", - do: "#{added_mentions} " <> content, + do: "#{added_mentions}" <> content, else: content {:ok, put_in(object["object"]["content"], content)} diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 090bdc35ed..3c40887c49 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -8,17 +8,29 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do use Pleroma.DataCase test "adds mentions to post content" do - users = %{ - "lain@lain.com" => "https://lain.com/users/lain", - "coolboymew@shitposter.club" => "https://shitposter.club/users/coolboymew", - "dielan@shitposter.club" => "https://shitposter.club/users/dielan", - "hakui@tuusin.misono-ya.info" => "https://tuusin.misono-ya.info/users/hakui", - "fence@xyzzy.link" => "https://xyzzy.link/users/fence" - } - - Enum.each(users, fn {nickname, ap_id} -> - insert(:user, ap_id: ap_id, nickname: nickname, local: false) - end) + [lain, coolboymew, dielan, hakui, fence] = [ + insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain@lain.com", local: false), + insert(:user, + ap_id: "https://shitposter.club/users/coolboymew", + nickname: "coolboymew@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://shitposter.club/users/dielan", + nickname: "dielan@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://tuusin.misono-ya.info/users/hakui", + nickname: "hakui@tuusin.misono-ya.info", + local: false + ), + insert(:user, + ap_id: "https://xyzzy.link/users/fence", + nickname: "fence@xyzzy.link", + local: false + ) + ] object = File.read!("test/fixtures/soapbox_no_mentions_in_content.json") |> Jason.decode!() @@ -29,6 +41,8 @@ test "adds mentions to post content" do } {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) - Enum.each(users, fn {nickname, _} -> assert filtered =~ nickname end) + + assert filtered == + "@lain @coolboymew @dielan @hakui @fence

Haha yeah, you can control who you reply to.

" end end