From f80f18d376f3b84779fec700f82ab41963325343 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 27 Aug 2020 16:09:03 -0500 Subject: [PATCH] Chats: mark chats as read --- app/soapbox/actions/chats.js | 27 ++++++++++++++++++- .../features/chats/components/chat_window.js | 15 +++++++++-- app/soapbox/reducers/chats.js | 11 +++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/app/soapbox/actions/chats.js b/app/soapbox/actions/chats.js index 1adc41e1e..93938e3c3 100644 --- a/app/soapbox/actions/chats.js +++ b/app/soapbox/actions/chats.js @@ -19,6 +19,10 @@ export const CHAT_FETCH_REQUEST = 'CHAT_FETCH_REQUEST'; export const CHAT_FETCH_SUCCESS = 'CHAT_FETCH_SUCCESS'; export const CHAT_FETCH_FAIL = 'CHAT_FETCH_FAIL'; +export const CHAT_READ_REQUEST = 'CHAT_READ_REQUEST'; +export const CHAT_READ_SUCCESS = 'CHAT_READ_SUCCESS'; +export const CHAT_READ_FAIL = 'CHAT_READ_FAIL'; + export function fetchChats() { return (dispatch, getState) => { dispatch({ type: CHATS_FETCH_REQUEST }); @@ -56,9 +60,12 @@ export function sendChatMessage(chatId, params) { export function openChat(chatId) { return (dispatch, getState) => { - const panes = getSettings(getState()).getIn(['chats', 'panes']); + const state = getState(); + const panes = getSettings(state).getIn(['chats', 'panes']); const idx = panes.findIndex(pane => pane.get('chat_id') === chatId); + dispatch(markChatRead(chatId)); + if (idx > -1) { return dispatch(changeSetting(['chats', 'panes', idx, 'state'], 'open')); } else { @@ -88,6 +95,7 @@ export function toggleChat(chatId) { if (idx > -1) { const state = pane.get('state') === 'minimized' ? 'open' : 'minimized'; + if (state === 'open') dispatch(markChatRead(chatId)); return dispatch(changeSetting(['chats', 'panes', idx, 'state'], state)); } else { return false; @@ -114,3 +122,20 @@ export function startChat(accountId) { }); }; } + +export function markChatRead(chatId, lastReadId) { + return (dispatch, getState) => { + const chat = getState().getIn(['chats', chatId]); + if (!lastReadId) lastReadId = chat.get('last_message'); + + if (chat.get('unread') < 1) return; + if (!lastReadId) return; + + dispatch({ type: CHAT_READ_REQUEST, chatId, lastReadId }); + api(getState).post(`/api/v1/pleroma/chats/${chatId}/read`, { last_read_id: lastReadId }).then(({ data }) => { + dispatch({ type: CHAT_READ_SUCCESS, chat: data, lastReadId }); + }).catch(error => { + dispatch({ type: CHAT_READ_FAIL, chatId, error, lastReadId }); + }); + }; +} diff --git a/app/soapbox/features/chats/components/chat_window.js b/app/soapbox/features/chats/components/chat_window.js index 7fc266309..6448e4a67 100644 --- a/app/soapbox/features/chats/components/chat_window.js +++ b/app/soapbox/features/chats/components/chat_window.js @@ -7,7 +7,13 @@ import ImmutablePureComponent from 'react-immutable-pure-component'; import Avatar from 'soapbox/components/avatar'; import { acctFull } from 'soapbox/utils/accounts'; import IconButton from 'soapbox/components/icon_button'; -import { closeChat, toggleChat, fetchChatMessages, sendChatMessage } from 'soapbox/actions/chats'; +import { + closeChat, + toggleChat, + fetchChatMessages, + sendChatMessage, + markChatRead, +} from 'soapbox/actions/chats'; import { List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable'; import ChatMessageList from './chat_message_list'; import { shortNumberFormat } from 'soapbox/utils/numbers'; @@ -65,6 +71,11 @@ class ChatWindow extends ImmutablePureComponent { this.setState({ content: e.target.value }); } + handleReadChat = (e) => { + const { dispatch, chat } = this.props; + dispatch(markChatRead(chat.get('id'))); + } + focusInput = () => { if (!this.inputElem) return; this.inputElem.focus(); @@ -99,7 +110,7 @@ class ChatWindow extends ImmutablePureComponent { const unreadCount = chat.get('unread'); return ( -
+