From 5dd44d30948c5bf7eeac24ce6aa8ac6511afd6cc Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 30 Aug 2022 10:10:31 -0400 Subject: [PATCH] Add last_message_id param to API --- app/soapbox/features/chats/components/chat-box.tsx | 6 ------ app/soapbox/features/chats/components/chat-message-list.tsx | 6 +++++- app/soapbox/queries/chats.ts | 6 +++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/soapbox/features/chats/components/chat-box.tsx b/app/soapbox/features/chats/components/chat-box.tsx index db3b71fe9..f3da9c620 100644 --- a/app/soapbox/features/chats/components/chat-box.tsx +++ b/app/soapbox/features/chats/components/chat-box.tsx @@ -56,12 +56,6 @@ const ChatBox: React.FC = ({ chat, onSetInputRef, autosize, inputRef } const isSubmitDisabled = content.length === 0 && !attachment; - const markAsRead = useMutation(() => markChatAsRead(), { - onSuccess: () => { - queryClient.invalidateQueries(['chats']); - }, - }); - const submitMessage = useMutation(({ chatId, content }: any) => { return createChatMessage(chatId, content); }, { diff --git a/app/soapbox/features/chats/components/chat-message-list.tsx b/app/soapbox/features/chats/components/chat-message-list.tsx index dcad9582f..36ddc799f 100644 --- a/app/soapbox/features/chats/components/chat-message-list.tsx +++ b/app/soapbox/features/chats/components/chat-message-list.tsx @@ -347,7 +347,11 @@ const ChatMessageList: React.FC = ({ chat, autosize }) => { }, [formattedChatMessages.length]); useEffect(() => { - markChatAsRead(); + const lastMessageId = formattedChatMessages.pop()?.id; + + if (lastMessageId) { + markChatAsRead(lastMessageId); + } }, [formattedChatMessages.length]); useEffect(() => { diff --git a/app/soapbox/queries/chats.ts b/app/soapbox/queries/chats.ts index 6cbc6fd99..4c7352a7e 100644 --- a/app/soapbox/queries/chats.ts +++ b/app/soapbox/queries/chats.ts @@ -135,7 +135,11 @@ const useChat = (chatId: string) => { const api = useApi(); const { setChat, setEditing } = useChatContext(); - const markChatAsRead = () => api.post(`/api/v1/pleroma/chats/${chatId}/read`); + const markChatAsRead = (lastReadId: string) => { + api.post(`/api/v1/pleroma/chats/${chatId}/read`, { last_read_id: lastReadId }) + .then(() => queryClient.invalidateQueries(['chats'])) + .catch(() => null); + }; const createChatMessage = (chatId: string, content: string) => { return api.post(`/api/v1/pleroma/chats/${chatId}/messages`, { content });