diff options
author | Alibek Omarov <a1ba.omarov@gmail.com> | 2020-09-07 22:14:40 +0200 |
---|---|---|
committer | Gitea <gitea@fake.local> | 2020-09-07 22:15:42 +0200 |
commit | 699224a900d54b6d32e0bd3f2abd9eccc523df11 (patch) | |
tree | 47511a336b0a8dd1c72fd8a24929201348a6d5b9 | |
parent | c5434dbefc5e6a25ffb72debe8ed9e09d77bf885 (diff) | |
download | pleroma-699224a900d54b6d32e0bd3f2abd9eccc523df11.tar.gz |
ForceBotUnlistedPolicy: initial add, tiny clean up from my previous version
-rw-r--r-- | lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex new file mode 100644 index 000000000..31fd90586 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex @@ -0,0 +1,61 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do + alias Pleroma.User + @behaviour Pleroma.Web.ActivityPub.MRF + @moduledoc "Remove bot posts from federated timeline" + + require Pleroma.Constants + + defp check_by_actor_type(user) do + if user.actor_type in ["Application", "Service"], do: 1.0, else: 0.0 + end + + defp check_by_nickname(user) do + if Regex.match?(~r/bot@|ebooks@/i, user.nickname), do: 1.0, else: 0.0 + end + + defp botness_score(user), do: check_by_actor_type(user) + check_by_nickname(user) + + @impl true + def filter( + %{ + "type" => "Create", + "to" => to, + "cc" => cc, + "actor" => actor, + "object" => object + } = message + ) do + user = User.get_cached_by_ap_id(actor) + isbot = 0.8 < botness_score(user) + + if isbot and Enum.member?(to, Pleroma.Constants.as_public()) do + to = List.delete(to, Pleroma.Constants.as_public()) ++ [user.follower_address] + cc = List.delete(cc, user.follower_address) ++ [Pleroma.Constants.as_public()] + + object = + object + |> Map.put("to", to) + |> Map.put("cc", cc) + + message = + message + |> Map.put("to", to) + |> Map.put("cc", cc) + |> Map.put("object", object) + + {:ok, message} + else + {:ok, message} + end + end + + @impl true + def filter(message), do: {:ok, message} + + @impl true + def describe, do: {:ok, %{}} +end |