ChatController: Make last_read_id mandatory.

This commit is contained in:
lain 2020-06-07 20:22:08 +02:00
parent 680fa5fa36
commit fe2a5d0614
3 changed files with 11 additions and 27 deletions

View File

@ -340,10 +340,11 @@ def mark_as_read do
title: "MarkAsReadRequest", title: "MarkAsReadRequest",
description: "POST body for marking a number of chat messages as read", description: "POST body for marking a number of chat messages as read",
type: :object, type: :object,
required: [:last_read_id],
properties: %{ properties: %{
last_read_id: %Schema{ last_read_id: %Schema{
type: :string, type: :string,
description: "The content of your message. Optional." description: "The content of your message."
} }
}, },
example: %{ example: %{

View File

@ -109,10 +109,13 @@ def mark_message_as_read(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
end end
end end
def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do def mark_as_read(
%{body_params: %{last_read_id: last_read_id}, 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, _} <- {_n, _} <-
MessageReference.set_all_seen_for_chat(chat, conn.body_params[:last_read_id]) do MessageReference.set_all_seen_for_chat(chat, last_read_id) do
conn conn
|> put_view(ChatView) |> put_view(ChatView)
|> render("show.json", chat: chat) |> render("show.json", chat: chat)

View File

@ -43,30 +43,10 @@ test "it marks one message as read", %{conn: conn, user: user} do
describe "POST /api/v1/pleroma/chats/:id/read" do describe "POST /api/v1/pleroma/chats/:id/read" do
setup do: oauth_access(["write:chats"]) setup do: oauth_access(["write:chats"])
test "it marks all messages in a chat as read", %{conn: conn, user: user} do test "given a `last_read_id`, it marks everything until then as read", %{
other_user = insert(:user) conn: conn,
user: user
{:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup") } do
{: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 = MessageReference.for_chat_and_object(chat, object)
assert cm_ref.unread == true
result =
conn
|> post("/api/v1/pleroma/chats/#{chat.id}/read")
|> json_response_and_validate_schema(200)
assert result["unread"] == 0
cm_ref = MessageReference.for_chat_and_object(chat, object)
assert cm_ref.unread == false
end
test "it given a `last_read_id` ", %{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")