Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/web/activity_pub/mrf/block_notification_policy.ex b/lib/pleroma/web/activity_pub/mrf/block_notification_policy.ex
new file mode 100644
index 000000000..21854e824
--- /dev/null
+++ b/lib/pleroma/web/activity_pub/mrf/block_notification_policy.ex
@@ -0,0 +1,104 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.MRF.BlockNotificationPolicy do
+ @moduledoc "Notify local users upon remote block and unblock."
+ @behaviour Pleroma.Web.ActivityPub.MRF.Policy
+
+ alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+ alias Pleroma.Config
+
+ require Logger
+
+ defp is_block_or_unblock(%{"type" => "Block", "object" => object}),
+ do: {true, "blocked", object}
+
+ defp is_block_or_unblock(%{
+ "type" => "Undo",
+ "object" => %{"type" => "Block", "object" => object}
+ }),
+ do: {true, "unblocked", object}
+
+ defp is_block_or_unblock(_), do: {false, nil, nil}
+
+ defp is_local(actor, recipient) do
+ cond do
+ actor.local -> true
+ recipient.local -> true
+ true -> false
+ end
+ end
+
+ @impl true
+ def filter(message) do
+
+ with {true, action, object} <- is_block_or_unblock(message),
+ %User{} = actor <- User.get_cached_by_ap_id(message["actor"]),
+ %User{} = recipient <- User.get_cached_by_ap_id(object),
+ true <- is_local(actor, recipient),
+ false <- User.blocks_user?(recipient, actor) do
+
+ bot_user = Pleroma.Config.get([:mrf_block_notification_policy, :user])
+
+ replacements = %{
+ "actor" => actor.nickname,
+ "target" => recipient.nickname,
+ "action" => action
+ }
+
+ msg = Regex.replace(~r/{([a-z]+)?}/, Pleroma.Config.get([:mrf_block_notification_policy, :message]), fn _, match ->
+ replacements[match]
+ end)
+
+ _reply =
+ CommonAPI.post(User.get_by_nickname(bot_user), %{
+ status: msg,
+ visibility: Pleroma.Config.get([:mrf_block_notification_policy, :visibility])
+ })
+ end
+
+ {:ok, message}
+ end
+
+ @impl true
+ def describe do
+ mrf_block_notification_policy =
+ Config.get(:mrf_block_notification_policy)
+
+ {:ok, %{mrf_block_notification_policy: mrf_block_notification_policy}}
+ end
+
+ @impl true
+ def config_description do
+ %{
+ key: :mrf_block_notification_policy,
+ related_policy: "Pleroma.Web.ActivityPub.MRF.BlockNotificationPolicy",
+ description: "Notify local users upon remote block.",
+ children: [
+ %{
+ key: :message,
+ type: :string,
+ label: "Message",
+ description: "The message to send when someone is blocked or unblocked; use {actor}, {target}, and {action} variables",
+ suggestions: ["@{actor} {action} @{recipient}"]
+ },
+ %{
+ key: :user,
+ type: :string,
+ label: "Block User",
+ description: "The user account that announces a block"
+ },
+ %{
+ key: :visibility,
+ type: :string,
+ label: "Visibility",
+ description: "The visibility of block messages",
+ suggestions: ["public", "unlisted", "private", "direct"]
+ }
+ ]
+ }
+ end
+end
+

File Metadata

Mime Type
text/x-diff
Expires
Sat, Sep 19, 5:22 AM (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1768950
Default Alt Text
(3 KB)

Event Timeline