ChatController: Make last_read_id mandatory.
authorlain <lain@soykaf.club>
Sun, 7 Jun 2020 18:22:08 +0000 (20:22 +0200)
committerlain <lain@soykaf.club>
Sun, 7 Jun 2020 18:22:08 +0000 (20:22 +0200)
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 45fbad3119b2361a3548a726c2b9be1292008457..cf299bfc264a39cbe05ed9fc554095bf1b7a45ad 100644 (file)
@@ -340,10 +340,11 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
       title: "MarkAsReadRequest",
       description: "POST body for marking a number of chat messages as read",
       type: :object,
+      required: [:last_read_id],
       properties: %{
         last_read_id: %Schema{
           type: :string,
-          description: "The content of your message. Optional."
+          description: "The content of your message."
         }
       },
       example: %{
index 002b750829bf684fe9bc56d2d2974ef9d5272373..b9949236c5a5074369da4647048b856b72768332 100644 (file)
@@ -109,10 +109,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
     end
   end
 
-  def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
+  def mark_as_read(
+        %{body_params: %{last_read_id: last_read_id}, 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, conn.body_params[:last_read_id]) do
+           MessageReference.set_all_seen_for_chat(chat, last_read_id) do
       conn
       |> put_view(ChatView)
       |> render("show.json", chat: chat)
index 63cd89c735adedbf941ae164a62200e7c321a2d8..c2960956dbdfab5a77bfc01f57474fc45c454d49 100644 (file)
@@ -43,30 +43,10 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   describe "POST /api/v1/pleroma/chats/:id/read" do
     setup do: oauth_access(["write:chats"])
 
-    test "it marks all messages in a chat as read", %{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
-        |> post("/api/v1/pleroma/chats/#{chat.id}/read")
-        |> json_response_and_validate_schema(200)
-
-      assert result["unread"] == 0
-
-      cm_ref = MessageReference.for_chat_and_object(chat, object)
-
-      assert cm_ref.unread == false
-    end
-
-    test "it given a `last_read_id` ", %{conn: conn, user: user} do
+    test "given a `last_read_id`, it marks everything until then as read", %{
+      conn: conn,
+      user: user
+    } do
       other_user = insert(:user)
 
       {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")