aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHaelwenn <contact+git.pleroma.social@hacktivis.me>2019-09-04 10:46:49 +0000
committerHaelwenn <contact+git.pleroma.social@hacktivis.me>2019-09-04 10:46:49 +0000
commite54694748bdcc27a3acd3ff3feac5c0c284d8b0d (patch)
treeefd329ebcfdd39b2fe4c6592a0d96b10034268e9 /lib
parent46ffd8b3b6359ec796733a8fff5bdb7d03a728d5 (diff)
parent328b2612cd957aa3ad810101a20037e4e9843bb0 (diff)
downloadpleroma-e54694748bdcc27a3acd3ff3feac5c0c284d8b0d.tar.gz
Merge branch 'feature/mastoapi-read-notifications' into 'develop'
Extend `/api/pleroma/notifications/read` to mark multiple notifications as read and make it respond with Mastoapi entities See merge request pleroma/pleroma!1625
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex22
-rw-r--r--lib/pleroma/web/pleroma_api/pleroma_api_controller.ex25
-rw-r--r--lib/pleroma/web/router.ex7
3 files changed, 46 insertions, 8 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index 5d29af853..b7c880c51 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -102,15 +102,33 @@ 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, [])
+
+ Notification
+ |> where([n], 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