ChatController: Add `last_read_id` option to mark_as_read.
authorlain <lain@soykaf.club>
Sun, 7 Jun 2020 13:38:33 +0000 (15:38 +0200)
committerlain <lain@soykaf.club>
Sun, 7 Jun 2020 13:38:33 +0000 (15:38 +0200)
lib/pleroma/chat/message_reference.ex
lib/pleroma/web/api_spec/operations/chat_operation.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index 7ee7508ca8c24f2adf06523114efe3b57462ee82..131ae01863e2445af29fa7705816bfff25b1151a 100644 (file)
@@ -98,12 +98,20 @@ defmodule Pleroma.Chat.MessageReference do
     |> Repo.update()
   end
 
-  def set_all_seen_for_chat(chat) do
-    chat
-    |> for_chat_query()
-    |> exclude(:order_by)
-    |> exclude(:preload)
-    |> where([cmr], cmr.unread == true)
+  def set_all_seen_for_chat(chat, last_read_id \\ nil) do
+    query =
+      chat
+      |> for_chat_query()
+      |> exclude(:order_by)
+      |> exclude(:preload)
+      |> where([cmr], cmr.unread == true)
+
+    if last_read_id do
+      query
+      |> where([cmr], cmr.id <= ^last_read_id)
+    else
+      query
+    end
     |> Repo.update_all(set: [unread: false])
   end
 end
index 74c3ad0bdf904fe639a38cfbd0d1b86a3812eea3..45fbad3119b2361a3548a726c2b9be1292008457 100644 (file)
@@ -23,6 +23,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
       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")],
+      requestBody: request_body("Parameters", mark_as_read()),
       responses: %{
         200 =>
           Operation.response(
@@ -333,4 +334,21 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
       }
     }
   end
+
+  def mark_as_read do
+    %Schema{
+      title: "MarkAsReadRequest",
+      description: "POST body for marking a number of chat messages as read",
+      type: :object,
+      properties: %{
+        last_read_id: %Schema{
+          type: :string,
+          description: "The content of your message. Optional."
+        }
+      },
+      example: %{
+        "last_read_id" => "abcdef12456"
+      }
+    }
+  end
 end
index 983550b134eaee0c5a52c4cb270171f0505bc3ba..002b750829bf684fe9bc56d2d2974ef9d5272373 100644 (file)
@@ -111,7 +111,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
 
   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),
-         {_n, _} <- MessageReference.set_all_seen_for_chat(chat) do
+         {_n, _} <-
+           MessageReference.set_all_seen_for_chat(chat, conn.body_params[:last_read_id]) do
       conn
       |> put_view(ChatView)
       |> render("show.json", chat: chat)
index 2128fd9dd1b782c08db3663ed327c74dc3f1116e..63cd89c735adedbf941ae164a62200e7c321a2d8 100644 (file)
@@ -65,6 +65,30 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       assert cm_ref.unread == false
     end
+
+    test "it given a `last_read_id` ", %{conn: conn, user: user} do
+      other_user = insert(:user)
+
+      {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
+      {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
+      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+      object = Object.normalize(create, false)
+      cm_ref = MessageReference.for_chat_and_object(chat, object)
+
+      assert cm_ref.unread == true
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/pleroma/chats/#{chat.id}/read", %{"last_read_id" => cm_ref.id})
+        |> json_response_and_validate_schema(200)
+
+      assert result["unread"] == 1
+
+      cm_ref = MessageReference.for_chat_and_object(chat, object)
+
+      assert cm_ref.unread == false
+    end
   end
 
   describe "POST /api/v1/pleroma/chats/:id/messages" do