summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-08-31 09:21:02 +0000
committerlain <lain@soykaf.club>2020-08-31 09:21:02 +0000
commite0eb90f62a5c8cc14c8fd7fc82ab54342f6fe209 (patch)
tree8a0f40e1b1fb261cb1644472d79f3a65f5ced623
parent0417b2f649172ea300c124159aa86b964e404a0c (diff)
parentd91c4feebeb199f7c584f0a4292ce6f9cc331798 (diff)
Merge branch 'issue/2068' into 'develop'
[#2068] marks notifications as read after mute Closes #2068 See merge request pleroma/pleroma!2930
-rw-r--r--lib/pleroma/notification.ex12
-rw-r--r--lib/pleroma/web/common_api/common_api.ex3
-rw-r--r--test/web/common_api/common_api_test.exs65
3 files changed, 79 insertions, 1 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index c1825f810..8868a910e 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -648,4 +648,16 @@ defmodule Pleroma.Notification do
)
|> Repo.one()
end
+
+ @spec mark_context_as_read(User.t(), String.t()) :: {integer(), nil | [term()]}
+ def mark_context_as_read(%User{id: id}, context) do
+ from(
+ n in Notification,
+ join: a in assoc(n, :activity),
+ where: n.user_id == ^id,
+ where: n.seen == false,
+ where: fragment("?->>'context'", a.data) == ^context
+ )
+ |> Repo.update_all(set: [seen: true])
+ end
end
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 5ad2b91c2..4ab533658 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -452,7 +452,8 @@ defmodule Pleroma.Web.CommonAPI do
end
def add_mute(user, activity) do
- with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
+ with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]),
+ _ <- Pleroma.Notification.mark_context_as_read(user, activity.data["context"]) do
{:ok, activity}
else
{:error, _} -> {:error, dgettext("errors", "conversation is already muted")}
diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs
index 4ba6232dc..800db9a20 100644
--- a/test/web/common_api/common_api_test.exs
+++ b/test/web/common_api/common_api_test.exs
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.CommonAPITest do
alias Pleroma.Conversation.Participation
alias Pleroma.Notification
alias Pleroma.Object
+ alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier
@@ -18,6 +19,7 @@ defmodule Pleroma.Web.CommonAPITest do
import Pleroma.Factory
import Mock
+ import Ecto.Query, only: [from: 2]
require Pleroma.Constants
@@ -808,6 +810,69 @@ defmodule Pleroma.Web.CommonAPITest do
[user: user, activity: activity]
end
+ test "marks notifications as read after mute" do
+ author = insert(:user)
+ activity = insert(:note_activity, user: author)
+
+ friend1 = insert(:user)
+ friend2 = insert(:user)
+
+ {:ok, reply_activity} =
+ CommonAPI.post(
+ friend2,
+ %{
+ status: "@#{author.nickname} @#{friend1.nickname} test reply",
+ in_reply_to_status_id: activity.id
+ }
+ )
+
+ {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
+ {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
+
+ assert Repo.aggregate(
+ from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
+ :count
+ ) == 1
+
+ unread_notifications =
+ Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
+
+ assert Enum.any?(unread_notifications, fn n ->
+ n.type == "favourite" && n.activity_id == favorite_activity.id
+ end)
+
+ assert Enum.any?(unread_notifications, fn n ->
+ n.type == "reblog" && n.activity_id == repeat_activity.id
+ end)
+
+ assert Enum.any?(unread_notifications, fn n ->
+ n.type == "mention" && n.activity_id == reply_activity.id
+ end)
+
+ {:ok, _} = CommonAPI.add_mute(author, activity)
+ assert CommonAPI.thread_muted?(author, activity)
+
+ assert Repo.aggregate(
+ from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
+ :count
+ ) == 1
+
+ read_notifications =
+ Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
+
+ assert Enum.any?(read_notifications, fn n ->
+ n.type == "favourite" && n.activity_id == favorite_activity.id
+ end)
+
+ assert Enum.any?(read_notifications, fn n ->
+ n.type == "reblog" && n.activity_id == repeat_activity.id
+ end)
+
+ assert Enum.any?(read_notifications, fn n ->
+ n.type == "mention" && n.activity_id == reply_activity.id
+ end)
+ end
+
test "add mute", %{user: user, activity: activity} do
{:ok, _} = CommonAPI.add_mute(user, activity)
assert CommonAPI.thread_muted?(user, activity)