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) |> Repo.aggregate(:count)
end end
def mark_as_read(cm_ref) do
cm_ref
|> changeset(%{seen: true})
|> Repo.update()
end
def set_all_seen_for_chat(chat) do def set_all_seen_for_chat(chat) do
chat chat
|> for_chat_query() |> for_chat_query()

View File

@ -39,6 +39,31 @@ def mark_as_read_operation do
} }
end 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 def show_operation do
%Operation{ %Operation{
tags: ["chat"], tags: ["chat"],
@ -274,7 +299,8 @@ def chat_messages_response do
"content" => "Check this out :firefox:", "content" => "Check this out :firefox:",
"id" => "13", "id" => "13",
"chat_id" => "1", "chat_id" => "1",
"actor_id" => "someflakeid" "actor_id" => "someflakeid",
"unread" => false
}, },
%{ %{
"actor_id" => "someflakeid", "actor_id" => "someflakeid",
@ -282,7 +308,8 @@ def chat_messages_response do
"id" => "12", "id" => "12",
"chat_id" => "1", "chat_id" => "1",
"emojis" => [], "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( plug(
OAuthScopesPlug, OAuthScopesPlug,
%{scopes: ["write:statuses"]} %{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( plug(
@ -88,6 +94,21 @@ def post_chat_message(
end end
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 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), with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
{_n, _} <- ChatMessageReference.set_all_seen_for_chat(chat) do {_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) post("/chats/:id/messages", ChatController, :post_chat_message)
delete("/chats/:id/messages/:message_id", ChatController, :delete_message) delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
post("/chats/:id/read", ChatController, :mark_as_read) 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/statuses", ConversationController, :statuses)
get("/conversations/:id", ConversationController, :show) get("/conversations/:id", ConversationController, :show)

View File

@ -13,6 +13,33 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
import Pleroma.Factory 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 describe "POST /api/v1/pleroma/chats/:id/read" do
setup do: oauth_access(["write:statuses"]) 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) other_user = insert(:user)
{:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup") {: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) {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
object = Object.normalize(create, false) object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object) cm_ref = ChatMessageReference.for_chat_and_object(chat, object)