Do not include notifications from blocked users when with_muted is set

This is not what with_muted is for per documentation and it was agreed
on irc that this behavior doesn't make sense.
This commit is contained in:
rinpatch 2019-10-21 12:38:35 +03:00
parent e91d5a23f4
commit ca1871fbb4
2 changed files with 46 additions and 38 deletions

View file

@ -34,41 +34,49 @@ def changeset(%Notification{} = notification, attrs) do
end end
def for_user_query(user, opts \\ []) do def for_user_query(user, opts \\ []) do
query = Notification
Notification |> where(user_id: ^user.id)
|> where(user_id: ^user.id) |> where(
|> where( [n, a],
[n, a], fragment(
"? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
a.actor
)
)
|> join(:inner, [n], activity in assoc(n, :activity))
|> join(:left, [n, a], object in Object,
on:
fragment( fragment(
"? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')", "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
a.actor object.data,
a.data
) )
) )
|> join(:inner, [n], activity in assoc(n, :activity)) |> preload([n, a, o], activity: {a, object: o})
|> join(:left, [n, a], object in Object, |> exclude_muted(user, opts)
on: |> exclude_blocked(user)
fragment( end
"(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
object.data,
a.data
)
)
|> preload([n, a, o], activity: {a, object: o})
if opts[:with_muted] do defp exclude_blocked(query, user) do
query query
else |> where([n, a], a.actor not in ^user.info.blocks)
where(query, [n, a], a.actor not in ^user.info.muted_notifications) |> where(
|> where([n, a], a.actor not in ^user.info.blocks) [n, a],
|> where( fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.domain_blocks
[n, a], )
fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.domain_blocks end
)
|> join(:left, [n, a], tm in Pleroma.ThreadMute, defp exclude_muted(query, _, %{with_muted: true}) do
on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data) query
) end
|> where([n, a, o, tm], is_nil(tm.user_id))
end defp exclude_muted(query, user, _opts) do
query
|> where([n, a], a.actor not in ^user.info.muted_notifications)
|> join(:left, [n, a], tm in Pleroma.ThreadMute,
on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data)
)
|> where([n, a, o, tm], is_nil(tm.user_id))
end end
def for_user(user, opts \\ %{}) do def for_user(user, opts \\ %{}) do

View file

@ -680,7 +680,7 @@ test "it doesn't return notifications for muted thread" do
assert Notification.for_user(user) == [] assert Notification.for_user(user) == []
end end
test "it returns notifications for muted user with notifications and with_muted parameter" do test "it returns notifications from a muted user when with_muted is set" do
user = insert(:user) user = insert(:user)
muted = insert(:user) muted = insert(:user)
{:ok, user} = User.mute(user, muted) {:ok, user} = User.mute(user, muted)
@ -690,27 +690,27 @@ test "it returns notifications for muted user with notifications and with_muted
assert length(Notification.for_user(user, %{with_muted: true})) == 1 assert length(Notification.for_user(user, %{with_muted: true})) == 1
end end
test "it returns notifications for blocked user and with_muted parameter" do test "it doesn't return notifications from a blocked user when with_muted is set" do
user = insert(:user) user = insert(:user)
blocked = insert(:user) blocked = insert(:user)
{:ok, user} = User.block(user, blocked) {:ok, user} = User.block(user, blocked)
{:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
assert length(Notification.for_user(user, %{with_muted: true})) == 1 assert length(Notification.for_user(user, %{with_muted: true})) == 0
end end
test "it returns notificatitons for blocked domain and with_muted parameter" do test "it doesn't return notifications from a domain-blocked user when with_muted is set" do
user = insert(:user) user = insert(:user)
blocked = insert(:user, ap_id: "http://some-domain.com") blocked = insert(:user, ap_id: "http://some-domain.com")
{:ok, user} = User.block_domain(user, "some-domain.com") {:ok, user} = User.block_domain(user, "some-domain.com")
{:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"})
assert length(Notification.for_user(user, %{with_muted: true})) == 1 assert length(Notification.for_user(user, %{with_muted: true})) == 0
end end
test "it returns notifications for muted thread with_muted parameter" do test "it returns notifications from muted threads when with_muted is set" do
user = insert(:user) user = insert(:user)
another_user = insert(:user) another_user = insert(:user)