diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 0a48442ebcf4..22c57e343853 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -2528,6 +2528,9 @@ export class MessageModel extends window.Backbone.Model { await DataWriter.saveMessage(this.attributes, { ourAci: window.textsecure.storage.user.getCheckedAci(), }); + window.reduxActions.conversations.markOpenConversationRead( + conversation.id + ); } } diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 3b595eaa2731..1a74ff2fb6f7 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -611,6 +611,7 @@ const PUSH_PANEL = 'conversations/PUSH_PANEL'; const POP_PANEL = 'conversations/POP_PANEL'; const PANEL_ANIMATION_DONE = 'conversations/PANEL_ANIMATION_DONE'; const PANEL_ANIMATION_STARTED = 'conversations/PANEL_ANIMATION_STARTED'; +export const MARK_READ = 'conversations/MARK_READ'; export const MESSAGE_CHANGED = 'MESSAGE_CHANGED'; export const MESSAGE_DELETED = 'MESSAGE_DELETED'; export const MESSAGE_EXPIRED = 'conversations/MESSAGE_EXPIRED'; @@ -780,6 +781,12 @@ type ConversationStoppedByMissingVerificationActionType = ReadonlyDeep<{ untrustedServiceIds: ReadonlyArray; }; }>; +export type MarkReadActionType = ReadonlyDeep<{ + type: typeof MARK_READ; + payload: { + conversationId: string; + }; +}>; export type MessageChangedActionType = ReadonlyDeep<{ type: typeof MESSAGE_CHANGED; payload: { @@ -1027,6 +1034,7 @@ export type ConversationActionType = | CreateGroupRejectedActionType | CustomColorRemovedActionType | DiscardMessagesActionType + | MarkReadActionType | MessageChangedActionType | MessageDeletedActionType | MessageExpandedActionType @@ -1122,6 +1130,7 @@ export const actions = { loadRecentMediaItems, markAttachmentAsCorrupted, markMessageRead, + markOpenConversationRead, messageChanged, messageDeleted, messageExpanded, @@ -2888,6 +2897,26 @@ function conversationStoppedByMissingVerification(payload: { }; } +export function markOpenConversationRead( + conversationId: string +): ThunkAction { + return async (dispatch, getState) => { + const state = getState(); + const { nav } = state; + + if (nav.selectedNavTab !== NavTab.Chats) { + return; + } + + dispatch({ + type: MARK_READ, + payload: { + conversationId, + }, + }); + }; +} + export function messageChanged( id: string, conversationId: string, @@ -5524,6 +5553,28 @@ export function reducer( }; } + if (action.type === MARK_READ) { + const { conversationId } = action.payload; + const existingConversation = state.messagesByConversation[conversationId]; + + // We don't keep track of messages unless their conversation is loaded... + if (!existingConversation) { + return state; + } + + return { + ...state, + messagesByConversation: { + ...state.messagesByConversation, + [conversationId]: { + ...existingConversation, + messageChangeCounter: + (existingConversation.messageChangeCounter || 0) + 1, + }, + }, + }; + } + if (action.type === MESSAGE_CHANGED) { const { id, conversationId, data } = action.payload; const existingConversation = state.messagesByConversation[conversationId]; @@ -5553,8 +5604,6 @@ export function reducer( const hasNewEdit = existingMessage.editHistory?.length !== data.editHistory?.length ? 1 : 0; - const toIncrement = data.reactions?.length || hasNewEdit; - const updatedMessage = { ...data, displayLimit: existingMessage.displayLimit, @@ -5572,14 +5621,6 @@ export function reducer( state ), preloadData: undefined, - messagesByConversation: { - ...state.messagesByConversation, - [conversationId]: { - ...existingConversation, - messageChangeCounter: - (existingConversation.messageChangeCounter || 0) + toIncrement, - }, - }, messagesLookup: { ...state.messagesLookup, [id]: updatedMessage, diff --git a/ts/test-electron/backup/calling_test.ts b/ts/test-electron/backup/calling_test.ts index cf563f877cb2..5e2d66045c01 100644 --- a/ts/test-electron/backup/calling_test.ts +++ b/ts/test-electron/backup/calling_test.ts @@ -68,8 +68,7 @@ describe('backup/calling', () => { callLink = { rootKey: rootKey.toString(), roomId: getRoomIdFromRootKey(rootKey), - // TODO: DESKTOP-7511 - adminKey: fromAdminKeyBytes(Buffer.concat([adminKey, adminKey])), + adminKey: fromAdminKeyBytes(adminKey), name: "Let's Talk Rocks", restrictions: CallLinkRestrictions.AdminApproval, revoked: false, diff --git a/ts/test-electron/state/ducks/conversations_test.ts b/ts/test-electron/state/ducks/conversations_test.ts index a6dfc0053ec3..462c3a0f603d 100644 --- a/ts/test-electron/state/ducks/conversations_test.ts +++ b/ts/test-electron/state/ducks/conversations_test.ts @@ -1577,64 +1577,6 @@ describe('both/state/ducks/conversations', () => { 0 ); }); - - it('increments message change counter if new message has reactions', () => { - const changedMessageWithReaction: MessageType = { - ...changedMessage, - reactions: [ - { - emoji: '✨', - fromId: 'some-other-id', - timestamp: 2222, - receivedAtDate: 3333, - targetTimestamp: 1111, - }, - ], - }; - const state = reducer( - startState, - messageChanged(messageId, conversationId, changedMessageWithReaction) - ); - - assert.deepEqual( - state.messagesLookup[messageId], - changedMessageWithReaction - ); - assert.strictEqual( - state.messagesByConversation[conversationId]?.messageChangeCounter, - 1 - ); - }); - - it('does not increment message change counter if only old message had reactions', () => { - const updatedStartState = { - ...startState, - messagesLookup: { - [messageId]: { - ...startState.messagesLookup[messageId], - reactions: [ - { - emoji: '✨', - fromId: 'some-other-id', - timestamp: 2222, - receivedAtDate: 3333, - targetTimestamp: 1111, - }, - ], - }, - }, - }; - const state = reducer( - updatedStartState, - messageChanged(messageId, conversationId, changedMessage) - ); - - assert.deepEqual(state.messagesLookup[messageId], changedMessage); - assert.strictEqual( - state.messagesByConversation[conversationId]?.messageChangeCounter, - 0 - ); - }); }); describe('SHOW_ARCHIVED_CONVERSATIONS', () => { diff --git a/ts/util/handleEditMessage.ts b/ts/util/handleEditMessage.ts index 5f402fb36713..21c7900c061b 100644 --- a/ts/util/handleEditMessage.ts +++ b/ts/util/handleEditMessage.ts @@ -370,6 +370,10 @@ export async function handleEditMessage( isFirstRun: false, skipEdits: true, }); + + window.reduxActions.conversations.markOpenConversationRead( + mainMessageConversation.id + ); } // Apply any other pending edits that target this message