forked from AkkomaGang/akkoma
Make HashtagPolicy history-aware
This commit is contained in:
parent
6283fd9fa7
commit
0d1f3212d9
2 changed files with 107 additions and 10 deletions
|
@ -16,6 +16,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.HashtagPolicy do
|
||||||
|
|
||||||
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def history_awareness, do: :manual
|
||||||
|
|
||||||
defp check_reject(message, hashtags) do
|
defp check_reject(message, hashtags) do
|
||||||
if Enum.any?(Config.get([:mrf_hashtag, :reject]), fn match -> match in hashtags end) do
|
if Enum.any?(Config.get([:mrf_hashtag, :reject]), fn match -> match in hashtags end) do
|
||||||
{:reject, "[HashtagPolicy] Matches with rejected keyword"}
|
{:reject, "[HashtagPolicy] Matches with rejected keyword"}
|
||||||
|
@ -47,22 +50,46 @@ defp check_ftl_removal(%{"to" => to} = message, hashtags) do
|
||||||
|
|
||||||
defp check_ftl_removal(message, _hashtags), do: {:ok, message}
|
defp check_ftl_removal(message, _hashtags), do: {:ok, message}
|
||||||
|
|
||||||
defp check_sensitive(message, hashtags) do
|
defp check_sensitive(message) do
|
||||||
if Enum.any?(Config.get([:mrf_hashtag, :sensitive]), fn match -> match in hashtags end) do
|
{:ok, new_object} =
|
||||||
{:ok, Kernel.put_in(message, ["object", "sensitive"], true)}
|
Object.Updater.do_with_history(message["object"], fn object ->
|
||||||
else
|
hashtags = Object.hashtags(%Object{data: object})
|
||||||
{:ok, message}
|
|
||||||
end
|
if Enum.any?(Config.get([:mrf_hashtag, :sensitive]), fn match -> match in hashtags end) do
|
||||||
|
{:ok, Map.put(object, "sensitive", true)}
|
||||||
|
else
|
||||||
|
{:ok, object}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:ok, Map.put(message, "object", new_object)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def filter(%{"type" => "Create", "object" => object} = message) do
|
def filter(%{"type" => type, "object" => object} = message) when type in ["Create", "Update"] do
|
||||||
hashtags = Object.hashtags(%Object{data: object})
|
history_items =
|
||||||
|
with %{"formerRepresentations" => %{"orderedItems" => items}} <- object do
|
||||||
|
items
|
||||||
|
else
|
||||||
|
_ -> []
|
||||||
|
end
|
||||||
|
|
||||||
|
historical_hashtags =
|
||||||
|
Enum.reduce(history_items, [], fn item, acc ->
|
||||||
|
acc ++ Object.hashtags(%Object{data: item})
|
||||||
|
end)
|
||||||
|
|
||||||
|
hashtags = Object.hashtags(%Object{data: object}) ++ historical_hashtags
|
||||||
|
|
||||||
if hashtags != [] do
|
if hashtags != [] do
|
||||||
with {:ok, message} <- check_reject(message, hashtags),
|
with {:ok, message} <- check_reject(message, hashtags),
|
||||||
{:ok, message} <- check_ftl_removal(message, hashtags),
|
{:ok, message} <-
|
||||||
{:ok, message} <- check_sensitive(message, hashtags) do
|
(if "type" == "Create" do
|
||||||
|
check_ftl_removal(message, hashtags)
|
||||||
|
else
|
||||||
|
{:ok, message}
|
||||||
|
end),
|
||||||
|
{:ok, message} <- check_sensitive(message) do
|
||||||
{:ok, message}
|
{:ok, message}
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
|
@ -20,6 +20,76 @@ test "it sets the sensitive property with relevant hashtags" do
|
||||||
assert modified["object"]["sensitive"]
|
assert modified["object"]["sensitive"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it is history-aware" do
|
||||||
|
activity = %{
|
||||||
|
"type" => "Create",
|
||||||
|
"object" => %{
|
||||||
|
"content" => "hey",
|
||||||
|
"tag" => []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
activity_data =
|
||||||
|
activity
|
||||||
|
|> put_in(
|
||||||
|
["object", "formerRepresentations"],
|
||||||
|
%{
|
||||||
|
"type" => "OrderedCollection",
|
||||||
|
"orderedItems" => [
|
||||||
|
Map.put(
|
||||||
|
activity["object"],
|
||||||
|
"tag",
|
||||||
|
[%{"type" => "Hashtag", "name" => "#nsfw"}]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
{:ok, modified} =
|
||||||
|
Pleroma.Web.ActivityPub.MRF.filter_one(
|
||||||
|
Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
|
||||||
|
activity_data
|
||||||
|
)
|
||||||
|
|
||||||
|
refute modified["object"]["sensitive"]
|
||||||
|
assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it works with Update" do
|
||||||
|
activity = %{
|
||||||
|
"type" => "Update",
|
||||||
|
"object" => %{
|
||||||
|
"content" => "hey",
|
||||||
|
"tag" => []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
activity_data =
|
||||||
|
activity
|
||||||
|
|> put_in(
|
||||||
|
["object", "formerRepresentations"],
|
||||||
|
%{
|
||||||
|
"type" => "OrderedCollection",
|
||||||
|
"orderedItems" => [
|
||||||
|
Map.put(
|
||||||
|
activity["object"],
|
||||||
|
"tag",
|
||||||
|
[%{"type" => "Hashtag", "name" => "#nsfw"}]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
{:ok, modified} =
|
||||||
|
Pleroma.Web.ActivityPub.MRF.filter_one(
|
||||||
|
Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
|
||||||
|
activity_data
|
||||||
|
)
|
||||||
|
|
||||||
|
refute modified["object"]["sensitive"]
|
||||||
|
assert Enum.at(modified["object"]["formerRepresentations"]["orderedItems"], 0)["sensitive"]
|
||||||
|
end
|
||||||
|
|
||||||
test "it doesn't sets the sensitive property with irrelevant hashtags" do
|
test "it doesn't sets the sensitive property with irrelevant hashtags" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue