Merge remote-tracking branch 'pleroma/recipients-inline' into recipients-inline
This commit is contained in:
commit
6bc109b31d
3 changed files with 38 additions and 27 deletions
|
@ -35,22 +35,14 @@ 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)
|
||||
|> 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)
|
||||
to
|
||||
|> 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)
|
||||
|
@ -58,7 +50,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}}
|
|||
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
|
||||
|
@ -66,7 +58,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}}
|
|||
|
||||
content =
|
||||
if added_mentions != "",
|
||||
do: added_mentions <> " " <> content,
|
||||
do: "<span class=\"recipients-inline\">#{added_mentions}</span>" <> content,
|
||||
else: content
|
||||
|
||||
{:ok, put_in(object["object"]["content"], content)}
|
||||
|
|
|
@ -56,7 +56,12 @@ 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", "quote-inline"])
|
||||
Meta.allow_tag_with_this_attribute_values(:span, "class", [
|
||||
"h-card",
|
||||
"quote-inline",
|
||||
"recipients-inline"
|
||||
])
|
||||
|
||||
Meta.allow_tag_with_these_attributes(:span, [])
|
||||
|
||||
Meta.allow_tag_with_this_attribute_values(:code, "class", ["inline"])
|
||||
|
|
|
@ -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 ==
|
||||
"<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{lain.id}\" href=\"https://lain.com/users/lain\" rel=\"ugc\">@<span>lain</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{coolboymew.id}\" href=\"https://shitposter.club/users/coolboymew\" rel=\"ugc\">@<span>coolboymew</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{dielan.id}\" href=\"https://shitposter.club/users/dielan\" rel=\"ugc\">@<span>dielan</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{hakui.id}\" href=\"https://tuusin.misono-ya.info/users/hakui\" rel=\"ugc\">@<span>hakui</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{fence.id}\" href=\"https://xyzzy.link/users/fence\" rel=\"ugc\">@<span>fence</span></a></span> </span><p>Haha yeah, you can control who you reply to.</p>"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue