diff options
author | eugenijm <eugenijm@protonmail.com> | 2019-10-11 06:40:58 +0300 |
---|---|---|
committer | eugenijm <eugenijm@protonmail.com> | 2019-10-19 15:24:06 +0300 |
commit | 52ed2f8f2d962154ba61b31a3f5aab13dc7217fc (patch) | |
tree | 5f8bb7897cbab18831388a1924e63456fc7fa442 /lib | |
parent | fd6d05dc45e2fdff56e777431b303fc24ef7912f (diff) | |
download | pleroma-52ed2f8f2d962154ba61b31a3f5aab13dc7217fc.tar.gz |
Pleroma API: `POST /api/v1/pleroma/conversations/read` to mark all user's conversations as read
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/conversation/participation.ex | 13 | ||||
-rw-r--r-- | lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 1 |
3 files changed, 23 insertions, 0 deletions
diff --git a/lib/pleroma/conversation/participation.ex b/lib/pleroma/conversation/participation.ex index e17f49e58..41918fa78 100644 --- a/lib/pleroma/conversation/participation.ex +++ b/lib/pleroma/conversation/participation.ex @@ -69,6 +69,19 @@ defmodule Pleroma.Conversation.Participation do end end + def mark_all_as_read(user) do + {_, participations} = + __MODULE__ + |> where([p], p.user_id == ^user.id) + |> where([p], not p.read) + |> update([p], set: [read: true]) + |> select([p], p) + |> Repo.update_all([]) + + User.set_unread_conversation_count(user) + {:ok, participations} + end + def mark_as_unread(participation) do participation |> read_cng(%{read: false}) diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex index 9d50a7ca9..fc39abf05 100644 --- a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex @@ -79,6 +79,15 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do end end + def read_conversations(%{assigns: %{user: user}} = conn, _params) do + with {:ok, participations} <- Participation.mark_all_as_read(user) do + conn + |> add_link_headers(participations) + |> put_view(ConversationView) + |> render("participations.json", participations: participations, 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 diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d68fb87da..66e7a2263 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -266,6 +266,7 @@ defmodule Pleroma.Web.Router do get("/conversations/:id/statuses", PleromaAPIController, :conversation_statuses) get("/conversations/:id", PleromaAPIController, :conversation) + post("/conversations/read", PleromaAPIController, :read_conversations) end scope [] do |