From b04328c3dec4812dbaf3cd89baa2b888d7bb7fbf Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 4 May 2020 13:10:36 +0200 Subject: [PATCH] ChatController: Add mark_as_read --- lib/pleroma/chat.ex | 6 +++++ .../web/api_spec/operations/chat_operation.ex | 22 ++++++++++++++++++ .../controllers/chat_controller.ex | 11 ++++++++- lib/pleroma/web/router.ex | 1 + .../controllers/chat_controller_test.exs | 23 +++++++++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/chat.ex b/lib/pleroma/chat.ex index 6b1f832ce..6008196e4 100644 --- a/lib/pleroma/chat.ex +++ b/lib/pleroma/chat.ex @@ -60,4 +60,10 @@ def bump_or_create(user_id, recipient) do conflict_target: [:user_id, :recipient] ) end + + def mark_as_read(chat) do + chat + |> change(%{unread: 0}) + |> Repo.update() + end end diff --git a/lib/pleroma/web/api_spec/operations/chat_operation.ex b/lib/pleroma/web/api_spec/operations/chat_operation.ex index e8b5eff1f..0fe0e07b2 100644 --- a/lib/pleroma/web/api_spec/operations/chat_operation.ex +++ b/lib/pleroma/web/api_spec/operations/chat_operation.ex @@ -16,6 +16,28 @@ def open_api_operation(action) do apply(__MODULE__, operation, []) end + def mark_as_read_operation do + %Operation{ + tags: ["chat"], + summary: "Mark all messages in the chat as read", + operationId: "ChatController.mark_as_read", + parameters: [Operation.parameter(:id, :path, :string, "The ID of the Chat")], + responses: %{ + 200 => + Operation.response( + "The updated chat", + "application/json", + Chat + ) + }, + security: [ + %{ + "oAuth" => ["write"] + } + ] + } + end + def create_operation do %Operation{ tags: ["chat"], diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex index 175257921..bedae73bd 100644 --- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex @@ -23,7 +23,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do plug( OAuthScopesPlug, - %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create] + %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read] ) plug( @@ -51,6 +51,15 @@ def post_chat_message( end end + def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do + with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id), + {:ok, chat} <- Chat.mark_as_read(chat) do + conn + |> put_view(ChatView) + |> render("show.json", chat: chat) + end + end + def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do messages = diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 3a5063d4a..d6803e8ac 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -293,6 +293,7 @@ defmodule Pleroma.Web.Router do get("/chats", ChatController, :index) get("/chats/:id/messages", ChatController, :messages) post("/chats/:id/messages", ChatController, :post_chat_message) + post("/chats/:id/read", ChatController, :mark_as_read) end scope [] do diff --git a/test/web/pleroma_api/controllers/chat_controller_test.exs b/test/web/pleroma_api/controllers/chat_controller_test.exs index b1044574b..cdb2683c8 100644 --- a/test/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/web/pleroma_api/controllers/chat_controller_test.exs @@ -9,6 +9,29 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do import Pleroma.Factory + describe "POST /api/v1/pleroma/chats/:id/read" do + setup do: oauth_access(["write:statuses"]) + + test "it marks all messages in a chat as read", %{conn: conn, user: user} do + other_user = insert(:user) + + {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id) + + assert chat.unread == 1 + + result = + conn + |> post("/api/v1/pleroma/chats/#{chat.id}/read") + |> json_response_and_validate_schema(200) + + assert result["unread"] == 0 + + {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id) + + assert chat.unread == 0 + end + end + describe "POST /api/v1/pleroma/chats/:id/messages" do setup do: oauth_access(["write:statuses"])