ChatController: Add function to mark single message as read.

This commit is contained in:
lain 2020-06-03 19:21:23 +02:00
parent c020fd4352
commit b3407344d3
5 changed files with 86 additions and 3 deletions

View file

@ -92,6 +92,12 @@ def unread_count_for_chat(chat) 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()

View file

@ -39,6 +39,31 @@ def mark_as_read_operation 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 @@ def chat_messages_response 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 @@ def chat_messages_response 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
}
]
}

View file

@ -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 @@ def post_chat_message(
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

View file

@ -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)

View file

@ -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 @@ 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 = ChatMessageReference.for_chat_and_object(chat, object)