Merge branch 'mark-read' into 'develop'
PleromaAPI: Simplify marking notifications as read See merge request pleroma/pleroma!4111
This commit is contained in:
commit
401aca2548
1
changelog.d/mark-read.fix
Normal file
1
changelog.d/mark-read.fix
Normal file
@ -0,0 +1 @@
|
|||||||
|
The query for marking notifications as read has been simplified
|
@ -280,15 +280,10 @@ defmodule Pleroma.Notification do
|
|||||||
select: n.id
|
select: n.id
|
||||||
)
|
)
|
||||||
|
|
||||||
{:ok, %{ids: {_, notification_ids}}} =
|
Multi.new()
|
||||||
Multi.new()
|
|> Multi.update_all(:ids, query, set: [seen: true, updated_at: NaiveDateTime.utc_now()])
|
||||||
|> Multi.update_all(:ids, query, set: [seen: true, updated_at: NaiveDateTime.utc_now()])
|
|> Marker.multi_set_last_read_id(user, "notifications")
|
||||||
|> Marker.multi_set_last_read_id(user, "notifications")
|
|> Repo.transaction()
|
||||||
|> Repo.transaction()
|
|
||||||
|
|
||||||
for_user_query(user)
|
|
||||||
|> where([n], n.id in ^notification_ids)
|
|
||||||
|> Repo.all()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec read_one(User.t(), String.t()) ::
|
@spec read_one(User.t(), String.t()) ::
|
||||||
@ -299,10 +294,6 @@ defmodule Pleroma.Notification do
|
|||||||
|> Multi.update(:update, changeset(notification, %{seen: true}))
|
|> Multi.update(:update, changeset(notification, %{seen: true}))
|
||||||
|> Marker.multi_set_last_read_id(user, "notifications")
|
|> Marker.multi_set_last_read_id(user, "notifications")
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|
||||||
{:ok, %{update: notification}} -> {:ok, notification}
|
|
||||||
{:error, :update, changeset, _} -> {:error, changeset}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
|
defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
|
||||||
alias OpenApiSpex.Operation
|
alias OpenApiSpex.Operation
|
||||||
alias OpenApiSpex.Schema
|
alias OpenApiSpex.Schema
|
||||||
alias Pleroma.Web.ApiSpec.NotificationOperation
|
|
||||||
alias Pleroma.Web.ApiSpec.Schemas.ApiError
|
alias Pleroma.Web.ApiSpec.Schemas.ApiError
|
||||||
|
|
||||||
import Pleroma.Web.ApiSpec.Helpers
|
import Pleroma.Web.ApiSpec.Helpers
|
||||||
@ -35,12 +34,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaNotificationOperation do
|
|||||||
Operation.response(
|
Operation.response(
|
||||||
"A Notification or array of Notifications",
|
"A Notification or array of Notifications",
|
||||||
"application/json",
|
"application/json",
|
||||||
%Schema{
|
%Schema{type: :string}
|
||||||
anyOf: [
|
|
||||||
%Schema{type: :array, items: NotificationOperation.notification()},
|
|
||||||
NotificationOperation.notification()
|
|
||||||
]
|
|
||||||
}
|
|
||||||
),
|
),
|
||||||
400 => Operation.response("Bad Request", "application/json", ApiError)
|
400 => Operation.response("Bad Request", "application/json", ApiError)
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,9 @@ defmodule Pleroma.Web.PleromaAPI.NotificationController do
|
|||||||
} = conn,
|
} = conn,
|
||||||
_
|
_
|
||||||
) do
|
) do
|
||||||
with {:ok, notification} <- Notification.read_one(user, notification_id) do
|
with {:ok, _} <- Notification.read_one(user, notification_id) do
|
||||||
render(conn, "show.json", notification: notification, for: user)
|
conn
|
||||||
|
|> json("ok")
|
||||||
else
|
else
|
||||||
{:error, message} ->
|
{:error, message} ->
|
||||||
conn
|
conn
|
||||||
@ -38,11 +39,14 @@ defmodule Pleroma.Web.PleromaAPI.NotificationController do
|
|||||||
conn,
|
conn,
|
||||||
_
|
_
|
||||||
) do
|
) do
|
||||||
notifications =
|
with {:ok, _} <- Notification.set_read_up_to(user, max_id) do
|
||||||
user
|
conn
|
||||||
|> Notification.set_read_up_to(max_id)
|
|> json("ok")
|
||||||
|> Enum.take(80)
|
else
|
||||||
|
{:error, message} ->
|
||||||
render(conn, "index.json", notifications: notifications, for: user)
|
conn
|
||||||
|
|> put_status(:bad_request)
|
||||||
|
|> json(%{"error" => message})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -449,9 +449,7 @@ defmodule Pleroma.NotificationTest do
|
|||||||
status: "hey yet again @#{other_user.nickname}!"
|
status: "hey yet again @#{other_user.nickname}!"
|
||||||
})
|
})
|
||||||
|
|
||||||
[_, read_notification] = Notification.set_read_up_to(other_user, n2.id)
|
Notification.set_read_up_to(other_user, n2.id)
|
||||||
|
|
||||||
assert read_notification.activity.object
|
|
||||||
|
|
||||||
[n3, n2, n1] = Notification.for_user(other_user)
|
[n3, n2, n1] = Notification.for_user(other_user)
|
||||||
|
|
||||||
|
@ -21,13 +21,11 @@ defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
|
|||||||
{:ok, [notification1]} = Notification.create_notifications(activity1)
|
{:ok, [notification1]} = Notification.create_notifications(activity1)
|
||||||
{:ok, [notification2]} = Notification.create_notifications(activity2)
|
{:ok, [notification2]} = Notification.create_notifications(activity2)
|
||||||
|
|
||||||
response =
|
conn
|
||||||
conn
|
|> put_req_header("content-type", "application/json")
|
||||||
|> put_req_header("content-type", "application/json")
|
|> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
|
||||||
|> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
|
|> json_response_and_validate_schema(:ok)
|
||||||
|> json_response_and_validate_schema(:ok)
|
|
||||||
|
|
||||||
assert %{"pleroma" => %{"is_seen" => true}} = response
|
|
||||||
assert Repo.get(Notification, notification1.id).seen
|
assert Repo.get(Notification, notification1.id).seen
|
||||||
refute Repo.get(Notification, notification2.id).seen
|
refute Repo.get(Notification, notification2.id).seen
|
||||||
end
|
end
|
||||||
@ -40,14 +38,17 @@ defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
|
|||||||
|
|
||||||
[notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
|
[notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
|
||||||
|
|
||||||
[response1, response2] =
|
refute Repo.get(Notification, notification1.id).seen
|
||||||
conn
|
refute Repo.get(Notification, notification2.id).seen
|
||||||
|> put_req_header("content-type", "application/json")
|
refute Repo.get(Notification, notification3.id).seen
|
||||||
|> post("/api/v1/pleroma/notifications/read", %{max_id: notification2.id})
|
|
||||||
|> json_response_and_validate_schema(:ok)
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> post("/api/v1/pleroma/notifications/read", %{max_id: notification2.id})
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
[notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
|
||||||
|
|
||||||
assert %{"pleroma" => %{"is_seen" => true}} = response1
|
|
||||||
assert %{"pleroma" => %{"is_seen" => true}} = response2
|
|
||||||
assert Repo.get(Notification, notification1.id).seen
|
assert Repo.get(Notification, notification1.id).seen
|
||||||
assert Repo.get(Notification, notification2.id).seen
|
assert Repo.get(Notification, notification2.id).seen
|
||||||
refute Repo.get(Notification, notification3.id).seen
|
refute Repo.get(Notification, notification3.id).seen
|
||||||
|
Loading…
Reference in New Issue
Block a user