report notifications for privileged users

Instead of `Pleroma.User.all_superusers()` we now use `Pleroma.User.all_superusers(:report_handle)`

I also changed it for sending emails, but there were no tests.
This commit is contained in:
Ilja 2022-06-19 16:26:56 +02:00
parent 34adea8d28
commit e21ef5aef3
4 changed files with 21 additions and 12 deletions

View file

@ -542,7 +542,8 @@ defmodule Pleroma.Notification do
end end
def get_potential_receiver_ap_ids(%{data: %{"type" => "Flag", "actor" => actor}}) do def get_potential_receiver_ap_ids(%{data: %{"type" => "Flag", "actor" => actor}}) do
(User.all_superusers() |> Enum.map(fn user -> user.ap_id end)) -- [actor] (User.all_users_with_privilege(:report_handle) |> Enum.map(fn user -> user.ap_id end)) --
[actor]
end end
def get_potential_receiver_ap_ids(activity) do def get_potential_receiver_ap_ids(activity) do

View file

@ -392,11 +392,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- :ok <-
maybe_federate(stripped_activity) do maybe_federate(stripped_activity) do
User.all_superusers() User.all_users_with_privilege(:report_handle)
|> Enum.filter(fn user -> user.ap_id != actor end) |> Enum.filter(fn user -> user.ap_id != actor end)
|> Enum.filter(fn user -> not is_nil(user.email) end) |> Enum.filter(fn user -> not is_nil(user.email) end)
|> Enum.each(fn superuser -> |> Enum.each(fn privileged_user ->
superuser privileged_user
|> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content) |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
|> Pleroma.Emails.Mailer.deliver_async() |> Pleroma.Emails.Mailer.deliver_async()
end) end)

View file

@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.NotificationTest do defmodule Pleroma.NotificationTest do
use Pleroma.DataCase use Pleroma.DataCase, async: false
import Pleroma.Factory import Pleroma.Factory
import Mock import Mock
@ -32,20 +32,26 @@ defmodule Pleroma.NotificationTest do
refute {:ok, [nil]} == Notification.create_notifications(activity) refute {:ok, [nil]} == Notification.create_notifications(activity)
end end
test "creates a notification for a report" do test "creates a report notification only for privileged users" do
reporting_user = insert(:user) reporting_user = insert(:user)
reported_user = insert(:user) reported_user = insert(:user)
{:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true}) moderator_user = insert(:user, is_moderator: true)
{:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id}) clear_config([:instance, :moderator_privileges], [])
{:ok, activity1} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
{:ok, []} = Notification.create_notifications(activity1)
{:ok, [notification]} = Notification.create_notifications(activity) clear_config([:instance, :moderator_privileges], [:report_handle])
{:ok, activity2} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
{:ok, [notification]} = Notification.create_notifications(activity2)
assert notification.user_id == moderator_user.id assert notification.user_id == moderator_user.id
assert notification.type == "pleroma:report" assert notification.type == "pleroma:report"
end end
test "suppresses notification to reporter if reporter is an admin" do test "suppresses notifications for own reports" do
clear_config([:instance, :admin_privileges], [:report_handle])
reporting_admin = insert(:user, is_admin: true) reporting_admin = insert(:user, is_admin: true)
reported_user = insert(:user) reported_user = insert(:user)
other_admin = insert(:user, is_admin: true) other_admin = insert(:user, is_admin: true)

View file

@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
use Pleroma.DataCase use Pleroma.DataCase, async: false
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Chat alias Pleroma.Chat
@ -218,9 +218,11 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
end end
test "Report notification" do test "Report notification" do
clear_config([:instance, :moderator_privileges], [:report_handle])
reporting_user = insert(:user) reporting_user = insert(:user)
reported_user = insert(:user) reported_user = insert(:user)
{:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true}) moderator_user = insert(:user, is_moderator: true)
{:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id}) {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
{:ok, [notification]} = Notification.create_notifications(activity) {:ok, [notification]} = Notification.create_notifications(activity)