forked from AkkomaGang/akkoma
ChatController: Add mark_as_read
This commit is contained in:
parent
30590cf46b
commit
b04328c3de
5 changed files with 62 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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"],
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"])
|
||||
|
||||
|
|
Loading…
Reference in a new issue