Merge branch 'develop' into refactor/notification_settings
[akkoma] / lib / pleroma / web / pleroma_api / controllers / chat_controller.ex
index d6b3415d1b3a46ba50696cbb71af26bb35351534..c8ef3d91511a247531749918bb981ec79e592687 100644 (file)
@@ -17,13 +17,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
   alias Pleroma.Web.PleromaAPI.ChatView
 
   import Ecto.Query
-  import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
 
   action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
 
   plug(
     OAuthScopesPlug,
-    %{scopes: ["write:statuses"]}
+    %{scopes: ["write:chats"]}
     when action in [
            :post_chat_message,
            :create,
@@ -35,7 +34,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
 
   plug(
     OAuthScopesPlug,
-    %{scopes: ["read:statuses"]} when action in [:messages, :index, :show]
+    %{scopes: ["read:chats"]} when action in [:messages, :index, :show]
   )
 
   plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
@@ -109,9 +108,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) do
+         {_n, _} <-
+           MessageReference.set_all_seen_for_chat(chat, last_read_id) do
       conn
       |> put_view(ChatView)
       |> render("show.json", chat: chat)
@@ -123,7 +126,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
       cm_refs =
         chat
         |> MessageReference.for_chat_query()
-        |> Pagination.fetch_paginated(params |> stringify_keys())
+        |> Pagination.fetch_paginated(params)
 
       conn
       |> put_view(MessageReferenceView)
@@ -136,7 +139,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
     end
   end
 
-  def index(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do
+  def index(%{assigns: %{user: %{id: user_id} = user}} = conn, _params) do
     blocked_ap_ids = User.blocked_users_ap_ids(user)
 
     chats =
@@ -145,7 +148,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
         where: c.recipient not in ^blocked_ap_ids,
         order_by: [desc: c.updated_at]
       )
-      |> Pagination.fetch_paginated(params |> stringify_keys)
+      |> Repo.all()
 
     conn
     |> put_view(ChatView)