aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/chat.ex6
-rw-r--r--lib/pleroma/web/api_spec/operations/chat_operation.ex22
-rw-r--r--lib/pleroma/web/pleroma_api/controllers/chat_controller.ex11
-rw-r--r--lib/pleroma/web/router.ex1
4 files changed, 39 insertions, 1 deletions
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 @@ defmodule Pleroma.Chat 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 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation 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 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
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