From beca14982c99e40982fbf5c5cf00e8bc2447e89c Mon Sep 17 00:00:00 2001 From: Brice-W Date: Mon, 26 Jul 2021 10:43:12 +1000 Subject: [PATCH] manage expired nessage deletion in setExpired --- ts/components/conversation/Message.tsx | 16 +++++++++++++--- ts/data/data.ts | 17 ++++++++++++++--- ts/models/conversation.ts | 3 ++- ts/models/message.ts | 10 ++++++---- ts/models/messageType.ts | 7 +++++++ 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/ts/components/conversation/Message.tsx b/ts/components/conversation/Message.tsx index dda2867bc..ffdd51a5d 100644 --- a/ts/components/conversation/Message.tsx +++ b/ts/components/conversation/Message.tsx @@ -28,16 +28,17 @@ import { isFileDangerous } from '../../util/isFileDangerous'; import _ from 'lodash'; import { animation, contextMenu, Item, Menu } from 'react-contexify'; import uuid from 'uuid'; -import { InView } from 'react-intersection-observer'; import { withTheme } from 'styled-components'; import { MessageMetadata } from './message/MessageMetadata'; import { PubKey } from '../../session/types'; +import { getConversationController } from '../../session/conversations'; import { MessageRegularProps } from '../../models/messageType'; import { addSenderAsModerator, removeSenderFromModerator, } from '../../interactions/messageInteractions'; import { updateUserDetailsModal } from '../../state/ducks/modalDialog'; +import { actions as conversationActions } from '../../state/ducks/conversations'; import { MessageInteraction } from '../../interactions'; import autoBind from 'auto-bind'; import { AudioPlayerWithEncryptedFile } from './H5AudioPlayer'; @@ -104,7 +105,7 @@ class MessageInner extends React.PureComponent { public checkExpired() { const now = Date.now(); - const { isExpired, expirationTimestamp, expirationLength } = this.props; + const { isExpired, expirationTimestamp, expirationLength, convoId, id } = this.props; if (!expirationTimestamp || !expirationLength) { return; @@ -118,10 +119,19 @@ class MessageInner extends React.PureComponent { expiring: true, }); - const setExpired = () => { + const setExpired = async () => { this.setState({ expired: true, }); + await window.Signal.Data.removeMessage(id); + window.inboxStore?.dispatch( + conversationActions.messageExpired({ + conversationKey: convoId, + messageId: id, + }) + ); + const convo = getConversationController().get(convoId); + convo.updateLastMessage(); }; this.expiredTimeout = setTimeout(setExpired, EXPIRED_DELAY); } diff --git a/ts/data/data.ts b/ts/data/data.ts index 77ca3175a..3bffa8818 100644 --- a/ts/data/data.ts +++ b/ts/data/data.ts @@ -637,7 +637,7 @@ export async function saveMessages(arrayOfMessages: Array): P } export async function removeMessage(id: string): Promise { - const message = await getMessageById(id); + const message = await getMessageById(id, true); // Note: It's important to have a fully database-hydrated model to delete here because // it needs to delete all associated on-disk files along with the database delete. @@ -659,11 +659,17 @@ export async function getMessageIdsFromServerIds( return channels.getMessageIdsFromServerIds(serverIds, conversationId); } -export async function getMessageById(id: string): Promise { +export async function getMessageById( + id: string, + skipTimerInit: boolean = false +): Promise { const message = await channels.getMessageById(id); if (!message) { return null; } + if (skipTimerInit) { + message.skipTimerInit = skipTimerInit; + } return new MessageModel(message); } @@ -748,13 +754,18 @@ export async function getUnreadCountByConversation(conversationId: string): Prom export async function getMessagesByConversation( conversationId: string, - { limit = 100, receivedAt = Number.MAX_VALUE, type = '%' } + { limit = 100, receivedAt = Number.MAX_VALUE, type = '%', skipTimerInit = false } ): Promise { const messages = await channels.getMessagesByConversation(conversationId, { limit, receivedAt, type, }); + if (skipTimerInit) { + for (const message of messages) { + message.skipTimerInit = skipTimerInit; + } + } return new MessageCollection(messages); } diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index 10c826805..363b104d3 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -758,6 +758,7 @@ export class ConversationModel extends Backbone.Model { } const messages = await getMessagesByConversation(this.id, { limit: 1, + skipTimerInit: true, }); const lastMessageModel = messages.at(0); const lastMessageJSON = lastMessageModel ? lastMessageModel.toJSON() : null; @@ -947,7 +948,7 @@ export class ConversationModel extends Backbone.Model { // Build the list of updated message models so we can mark them all as read on a single sqlite call for (const nowRead of oldUnreadNowRead) { - await nowRead.markReadNoCommit(options.readAt); + nowRead.markReadNoCommit(options.readAt); const errors = nowRead.get('errors'); read.push({ diff --git a/ts/models/message.ts b/ts/models/message.ts index f02675f3d..41811e1d8 100644 --- a/ts/models/message.ts +++ b/ts/models/message.ts @@ -63,7 +63,9 @@ export class MessageModel extends Backbone.Model { } // this.on('expired', this.onExpired); - void this.setToExpire(); + if (!filledAttrs.skipTimerInit) { + void this.setToExpire(); + } autoBind(this); window.contextMenuShown = false; @@ -1045,20 +1047,20 @@ export class MessageModel extends Backbone.Model { } public async markRead(readAt: number) { - await this.markReadNoCommit(readAt); + this.markReadNoCommit(readAt); this.getConversation()?.markRead(this.attributes.received_at); await this.commit(); } - public async markReadNoCommit(readAt: number) { + public markReadNoCommit(readAt: number) { this.set({ unread: 0 }); if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) { const expirationStartTimestamp = Math.min(Date.now(), readAt || Date.now()); this.set({ expirationStartTimestamp }); - await this.setToExpire(false); + //await this.setToExpire(false); } window.Whisper.Notifications.remove( diff --git a/ts/models/messageType.ts b/ts/models/messageType.ts index cf945bfaa..fbf1b6e41 100644 --- a/ts/models/messageType.ts +++ b/ts/models/messageType.ts @@ -101,6 +101,12 @@ export interface MessageAttributes { * We display a small message just below the message referenced */ dataExtractionNotification?: DataExtractionNotificationMsg; + + /** + * This is used to choose whether to initialize the timer or not in the MessageModel object. + * If false or undefined, timer will be in itialized. + */ + skipTimerInit?: boolean; } export interface DataExtractionNotificationMsg { @@ -165,6 +171,7 @@ export interface MessageAttributesOptionals { sync?: boolean; snippet?: any; direction?: any; + skipTimerInit?: boolean; } /**