mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-10 10:09:02 +00:00
ChatController: Add function to mark single message as read.
This commit is contained in:
parent
c020fd4352
commit
b3407344d3
|
@ -92,6 +92,12 @@ defmodule Pleroma.ChatMessageReference do
|
|||
|> Repo.aggregate(:count)
|
||||
end
|
||||
|
||||
def mark_as_read(cm_ref) do
|
||||
cm_ref
|
||||
|> changeset(%{seen: true})
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def set_all_seen_for_chat(chat) do
|
||||
chat
|
||||
|> for_chat_query()
|
||||
|
|
|
@ -39,6 +39,31 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
|
|||
}
|
||||
end
|
||||
|
||||
def mark_message_as_read_operation do
|
||||
%Operation{
|
||||
tags: ["chat"],
|
||||
summary: "Mark one message in the chat as read",
|
||||
operationId: "ChatController.mark_message_as_read",
|
||||
parameters: [
|
||||
Operation.parameter(:id, :path, :string, "The ID of the Chat"),
|
||||
Operation.parameter(:message_id, :path, :string, "The ID of the message")
|
||||
],
|
||||
responses: %{
|
||||
200 =>
|
||||
Operation.response(
|
||||
"The read ChatMessage",
|
||||
"application/json",
|
||||
ChatMessage
|
||||
)
|
||||
},
|
||||
security: [
|
||||
%{
|
||||
"oAuth" => ["write"]
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def show_operation do
|
||||
%Operation{
|
||||
tags: ["chat"],
|
||||
|
@ -274,7 +299,8 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
|
|||
"content" => "Check this out :firefox:",
|
||||
"id" => "13",
|
||||
"chat_id" => "1",
|
||||
"actor_id" => "someflakeid"
|
||||
"actor_id" => "someflakeid",
|
||||
"unread" => false
|
||||
},
|
||||
%{
|
||||
"actor_id" => "someflakeid",
|
||||
|
@ -282,7 +308,8 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
|
|||
"id" => "12",
|
||||
"chat_id" => "1",
|
||||
"emojis" => [],
|
||||
"created_at" => "2020-04-21T15:06:45.000Z"
|
||||
"created_at" => "2020-04-21T15:06:45.000Z",
|
||||
"unread" => false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -24,7 +24,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
|||
plug(
|
||||
OAuthScopesPlug,
|
||||
%{scopes: ["write:statuses"]}
|
||||
when action in [:post_chat_message, :create, :mark_as_read, :delete_message]
|
||||
when action in [
|
||||
:post_chat_message,
|
||||
:create,
|
||||
:mark_as_read,
|
||||
:mark_message_as_read,
|
||||
:delete_message
|
||||
]
|
||||
)
|
||||
|
||||
plug(
|
||||
|
@ -88,6 +94,21 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
|||
end
|
||||
end
|
||||
|
||||
def mark_message_as_read(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
|
||||
id: chat_id,
|
||||
message_id: message_id
|
||||
}) do
|
||||
with %ChatMessageReference{} = cm_ref <-
|
||||
ChatMessageReference.get_by_id(message_id),
|
||||
^chat_id <- cm_ref.chat_id |> to_string(),
|
||||
%Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
|
||||
{:ok, cm_ref} <- ChatMessageReference.mark_as_read(cm_ref) do
|
||||
conn
|
||||
|> put_view(ChatMessageReferenceView)
|
||||
|> render("show.json", for: user, chat_message_reference: cm_ref)
|
||||
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),
|
||||
{_n, _} <- ChatMessageReference.set_all_seen_for_chat(chat) do
|
||||
|
|
|
@ -313,6 +313,7 @@ defmodule Pleroma.Web.Router do
|
|||
post("/chats/:id/messages", ChatController, :post_chat_message)
|
||||
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
||||
post("/chats/:id/read", ChatController, :mark_as_read)
|
||||
post("/chats/:id/messages/:message_id/read", ChatController, :mark_message_as_read)
|
||||
|
||||
get("/conversations/:id/statuses", ConversationController, :statuses)
|
||||
get("/conversations/:id", ConversationController, :show)
|
||||
|
|
|
@ -13,6 +13,33 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
|
|||
|
||||
import Pleroma.Factory
|
||||
|
||||
describe "POST /api/v1/pleroma/chats/:id/messages/:message_id/read" do
|
||||
setup do: oauth_access(["write:statuses"])
|
||||
|
||||
test "it marks one message 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 = ChatMessageReference.for_chat_and_object(chat, object)
|
||||
|
||||
assert cm_ref.seen == false
|
||||
|
||||
result =
|
||||
conn
|
||||
|> post("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}/read")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|
||||
assert result["unread"] == false
|
||||
|
||||
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
|
||||
|
||||
assert cm_ref.seen == true
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /api/v1/pleroma/chats/:id/read" do
|
||||
setup do: oauth_access(["write:statuses"])
|
||||
|
||||
|
@ -20,6 +47,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest 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 = ChatMessageReference.for_chat_and_object(chat, object)
|
||||
|
|
Loading…
Reference in a new issue