aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2019-09-04 11:33:08 +0300
committerrinpatch <rinpatch@sdf.org>2019-09-04 12:38:27 +0300
commitc2b6c1b089a813cf8c7cbd54c0f80bee4985522c (patch)
tree0d9ee1d4bd3a6279aa3b04eb51d93d07094ebd0f /lib
parent46ffd8b3b6359ec796733a8fff5bdb7d03a728d5 (diff)
downloadpleroma-c2b6c1b089a813cf8c7cbd54c0f80bee4985522c.tar.gz
Extend `/api/pleroma/notifications/read` to mark multiple notifications
as read and make it respond with Mastoapi entities
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex21
-rw-r--r--lib/pleroma/web/pleroma_api/pleroma_api_controller.ex25
-rw-r--r--lib/pleroma/web/router.ex7
3 files changed, 45 insertions, 8 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index 5d29af853..d7e232992 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -102,15 +102,32 @@ defmodule Pleroma.Notification do
n in Notification,
where: n.user_id == ^user_id,
where: n.id <= ^id,
+ where: n.seen == false,
update: [
set: [
seen: true,
updated_at: ^NaiveDateTime.utc_now()
]
- ]
+ ],
+ # Ideally we would preload object and activities here
+ # but Ecto does not support preloads in update_all
+ select: n.id
)
- Repo.update_all(query, [])
+ {_, notification_ids} = Repo.update_all(query, [])
+
+ from(n in Notification, where: n.id in ^notification_ids)
+ |> join(:inner, [n], activity in assoc(n, :activity))
+ |> join(:left, [n, a], object in Object,
+ on:
+ fragment(
+ "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
+ object.data,
+ a.data
+ )
+ )
+ |> preload([n, a, o], activity: {a, object: o})
+ |> Repo.all()
end
def read_one(%User{} = user, notification_id) do
diff --git a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
index b6d2bf86b..f4df3b024 100644
--- a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
+++ b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
@@ -8,8 +8,10 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
alias Pleroma.Conversation.Participation
+ alias Pleroma.Notification
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.MastodonAPI.ConversationView
+ alias Pleroma.Web.MastodonAPI.NotificationView
alias Pleroma.Web.MastodonAPI.StatusView
def conversation(%{assigns: %{user: user}} = conn, %{"id" => participation_id}) do
@@ -70,4 +72,27 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|> render("participation.json", %{participation: participation, for: user})
end
end
+
+ def read_notification(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
+ with {:ok, notification} <- Notification.read_one(user, notification_id) do
+ conn
+ |> put_view(NotificationView)
+ |> render("show.json", %{notification: notification, for: user})
+ else
+ {:error, message} ->
+ conn
+ |> put_status(:bad_request)
+ |> json(%{"error" => message})
+ end
+ end
+
+ def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do
+ with notifications <- Notification.set_read_up_to(user, max_id) do
+ notifications = Enum.take(notifications, 80)
+
+ conn
+ |> put_view(NotificationView)
+ |> render("index.json", %{notifications: notifications, for: user})
+ end
+ end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 969dc66fd..44a4279f7 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -236,12 +236,6 @@ defmodule Pleroma.Web.Router do
post("/blocks_import", UtilController, :blocks_import)
post("/follow_import", UtilController, :follow_import)
end
-
- scope [] do
- pipe_through(:oauth_read)
-
- post("/notifications/read", UtilController, :notifications_read)
- end
end
scope "/oauth", Pleroma.Web.OAuth do
@@ -277,6 +271,7 @@ defmodule Pleroma.Web.Router do
scope [] do
pipe_through(:oauth_write)
patch("/conversations/:id", PleromaAPIController, :update_conversation)
+ post("/notifications/read", PleromaAPIController, :read_notification)
end
end