manage expired nessage deletion in setExpired

pull/1786/head
Brice-W 4 years ago
parent 6bbabce8e3
commit beca14982c

@ -28,16 +28,17 @@ import { isFileDangerous } from '../../util/isFileDangerous';
import _ from 'lodash'; import _ from 'lodash';
import { animation, contextMenu, Item, Menu } from 'react-contexify'; import { animation, contextMenu, Item, Menu } from 'react-contexify';
import uuid from 'uuid'; import uuid from 'uuid';
import { InView } from 'react-intersection-observer';
import { withTheme } from 'styled-components'; import { withTheme } from 'styled-components';
import { MessageMetadata } from './message/MessageMetadata'; import { MessageMetadata } from './message/MessageMetadata';
import { PubKey } from '../../session/types'; import { PubKey } from '../../session/types';
import { getConversationController } from '../../session/conversations';
import { MessageRegularProps } from '../../models/messageType'; import { MessageRegularProps } from '../../models/messageType';
import { import {
addSenderAsModerator, addSenderAsModerator,
removeSenderFromModerator, removeSenderFromModerator,
} from '../../interactions/messageInteractions'; } from '../../interactions/messageInteractions';
import { updateUserDetailsModal } from '../../state/ducks/modalDialog'; import { updateUserDetailsModal } from '../../state/ducks/modalDialog';
import { actions as conversationActions } from '../../state/ducks/conversations';
import { MessageInteraction } from '../../interactions'; import { MessageInteraction } from '../../interactions';
import autoBind from 'auto-bind'; import autoBind from 'auto-bind';
import { AudioPlayerWithEncryptedFile } from './H5AudioPlayer'; import { AudioPlayerWithEncryptedFile } from './H5AudioPlayer';
@ -104,7 +105,7 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
public checkExpired() { public checkExpired() {
const now = Date.now(); const now = Date.now();
const { isExpired, expirationTimestamp, expirationLength } = this.props; const { isExpired, expirationTimestamp, expirationLength, convoId, id } = this.props;
if (!expirationTimestamp || !expirationLength) { if (!expirationTimestamp || !expirationLength) {
return; return;
@ -118,10 +119,19 @@ class MessageInner extends React.PureComponent<MessageRegularProps, State> {
expiring: true, expiring: true,
}); });
const setExpired = () => { const setExpired = async () => {
this.setState({ this.setState({
expired: true, 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); this.expiredTimeout = setTimeout(setExpired, EXPIRED_DELAY);
} }

@ -637,7 +637,7 @@ export async function saveMessages(arrayOfMessages: Array<MessageAttributes>): P
} }
export async function removeMessage(id: string): Promise<void> { export async function removeMessage(id: string): Promise<void> {
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 // 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. // 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); return channels.getMessageIdsFromServerIds(serverIds, conversationId);
} }
export async function getMessageById(id: string): Promise<MessageModel | null> { export async function getMessageById(
id: string,
skipTimerInit: boolean = false
): Promise<MessageModel | null> {
const message = await channels.getMessageById(id); const message = await channels.getMessageById(id);
if (!message) { if (!message) {
return null; return null;
} }
if (skipTimerInit) {
message.skipTimerInit = skipTimerInit;
}
return new MessageModel(message); return new MessageModel(message);
} }
@ -748,13 +754,18 @@ export async function getUnreadCountByConversation(conversationId: string): Prom
export async function getMessagesByConversation( export async function getMessagesByConversation(
conversationId: string, conversationId: string,
{ limit = 100, receivedAt = Number.MAX_VALUE, type = '%' } { limit = 100, receivedAt = Number.MAX_VALUE, type = '%', skipTimerInit = false }
): Promise<MessageCollection> { ): Promise<MessageCollection> {
const messages = await channels.getMessagesByConversation(conversationId, { const messages = await channels.getMessagesByConversation(conversationId, {
limit, limit,
receivedAt, receivedAt,
type, type,
}); });
if (skipTimerInit) {
for (const message of messages) {
message.skipTimerInit = skipTimerInit;
}
}
return new MessageCollection(messages); return new MessageCollection(messages);
} }

@ -758,6 +758,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
} }
const messages = await getMessagesByConversation(this.id, { const messages = await getMessagesByConversation(this.id, {
limit: 1, limit: 1,
skipTimerInit: true,
}); });
const lastMessageModel = messages.at(0); const lastMessageModel = messages.at(0);
const lastMessageJSON = lastMessageModel ? lastMessageModel.toJSON() : null; const lastMessageJSON = lastMessageModel ? lastMessageModel.toJSON() : null;
@ -947,7 +948,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// Build the list of updated message models so we can mark them all as read on a single sqlite call // 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) { for (const nowRead of oldUnreadNowRead) {
await nowRead.markReadNoCommit(options.readAt); nowRead.markReadNoCommit(options.readAt);
const errors = nowRead.get('errors'); const errors = nowRead.get('errors');
read.push({ read.push({

@ -63,7 +63,9 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
} }
// this.on('expired', this.onExpired); // this.on('expired', this.onExpired);
void this.setToExpire(); if (!filledAttrs.skipTimerInit) {
void this.setToExpire();
}
autoBind(this); autoBind(this);
window.contextMenuShown = false; window.contextMenuShown = false;
@ -1045,20 +1047,20 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
} }
public async markRead(readAt: number) { public async markRead(readAt: number) {
await this.markReadNoCommit(readAt); this.markReadNoCommit(readAt);
this.getConversation()?.markRead(this.attributes.received_at); this.getConversation()?.markRead(this.attributes.received_at);
await this.commit(); await this.commit();
} }
public async markReadNoCommit(readAt: number) { public markReadNoCommit(readAt: number) {
this.set({ unread: 0 }); this.set({ unread: 0 });
if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) { if (this.get('expireTimer') && !this.get('expirationStartTimestamp')) {
const expirationStartTimestamp = Math.min(Date.now(), readAt || Date.now()); const expirationStartTimestamp = Math.min(Date.now(), readAt || Date.now());
this.set({ expirationStartTimestamp }); this.set({ expirationStartTimestamp });
await this.setToExpire(false); //await this.setToExpire(false);
} }
window.Whisper.Notifications.remove( window.Whisper.Notifications.remove(

@ -101,6 +101,12 @@ export interface MessageAttributes {
* We display a small message just below the message referenced * We display a small message just below the message referenced
*/ */
dataExtractionNotification?: DataExtractionNotificationMsg; 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 { export interface DataExtractionNotificationMsg {
@ -165,6 +171,7 @@ export interface MessageAttributesOptionals {
sync?: boolean; sync?: boolean;
snippet?: any; snippet?: any;
direction?: any; direction?: any;
skipTimerInit?: boolean;
} }
/** /**

Loading…
Cancel
Save