chore: cleanup the lastMessage update logic

pull/2620/head
Audric Ackermann 3 years ago
parent 1c50aacc34
commit c3a9d19882

@ -79,7 +79,6 @@ import { SessionUtilConvoInfoVolatile } from '../session/utils/libsession/libses
import { SessionUtilUserGroups } from '../session/utils/libsession/libsession_utils_user_groups'; import { SessionUtilUserGroups } from '../session/utils/libsession/libsession_utils_user_groups';
import { forceSyncConfigurationNowIfNeeded } from '../session/utils/sync/syncUtils'; import { forceSyncConfigurationNowIfNeeded } from '../session/utils/sync/syncUtils';
import { getOurProfile } from '../session/utils/User'; import { getOurProfile } from '../session/utils/User';
import { createLastMessageUpdate } from '../types/Conversation';
import { import {
deleteExternalFilesOfConversation, deleteExternalFilesOfConversation,
getAbsoluteAttachmentPath, getAbsoluteAttachmentPath,
@ -1847,7 +1846,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
} }
private async bouncyUpdateLastMessage() { private async bouncyUpdateLastMessage() {
if (!this.id || !this.get('active_at')) { if (!this.id || !this.get('active_at') || this.isHidden()) {
return; return;
} }
const messages = await Data.getLastMessagesByConversation(this.id, 1, true); const messages = await Data.getLastMessagesByConversation(this.id, 1, true);
@ -1856,34 +1855,38 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
return; return;
} }
const lastMessageModel = messages.at(0); const lastMessageModel = messages.at(0);
const lastMessageStatusModel = lastMessageModel const lastMessageStatus = lastMessageModel?.getMessagePropStatus() || undefined;
? lastMessageModel.getMessagePropStatus() const lastMessageNotificationText = lastMessageModel?.getNotificationText() || undefined;
: undefined; // we just want to set the `status` to `undefined` if there are no `lastMessageNotificationText`
const lastMessageUpdate = createLastMessageUpdate({ const lastMessageUpdate =
lastMessageStatus: lastMessageStatusModel, !!lastMessageNotificationText && !isEmpty(lastMessageNotificationText)
lastMessageNotificationText: lastMessageModel ? {
? lastMessageModel.getNotificationText() lastMessage: lastMessageNotificationText || '',
: undefined, lastMessageStatus,
}); }
: { lastMessage: '', lastMessageStatus: undefined };
const existingLastMessageAttribute = this.get('lastMessage');
const existingLastMessageStatus = this.get('lastMessageStatus');
if ( if (
lastMessageUpdate.lastMessage !== this.get('lastMessage') || lastMessageUpdate.lastMessage !== existingLastMessageAttribute ||
lastMessageUpdate.lastMessageStatus !== this.get('lastMessageStatus') lastMessageUpdate.lastMessageStatus !== existingLastMessageStatus
) { ) {
const lastMessageAttribute = this.get('lastMessage');
if ( if (
lastMessageUpdate.lastMessageStatus === this.get('lastMessageStatus') && lastMessageUpdate.lastMessageStatus === existingLastMessageStatus &&
lastMessageUpdate.lastMessage && lastMessageUpdate.lastMessage &&
lastMessageUpdate.lastMessage.length > 40 && lastMessageUpdate.lastMessage.length > 40 &&
lastMessageAttribute && existingLastMessageAttribute &&
lastMessageAttribute.length > 40 && existingLastMessageAttribute.length > 40 &&
lastMessageUpdate.lastMessage.startsWith(lastMessageAttribute) lastMessageUpdate.lastMessage.startsWith(existingLastMessageAttribute)
) { ) {
// if status is the same, and text has a long length which starts with the db status, do not trigger an update. // if status is the same, and text has a long length which starts with the db status, do not trigger an update.
// we only store the first 60 chars in the db for the lastMessage attributes (see sql.ts) // we only store the first 60 chars in the db for the lastMessage attributes (see sql.ts)
return; return;
} }
this.set(lastMessageUpdate); this.set({
...lastMessageUpdate,
});
await this.commit(); await this.commit();
} }
} }

@ -71,7 +71,7 @@ export interface ConversationAttributes {
avatarInProfile?: string; // this is the avatar path locally once downloaded and stored in the application attachments folder avatarInProfile?: string; // this is the avatar path locally once downloaded and stored in the application attachments folder
isTrustedForAttachmentDownload: boolean; isTrustedForAttachmentDownload: boolean; // not synced accross devices, this field is used if we should auto download attachments from this conversation or not
conversationIdOrigin?: string; // Blinded message requests ONLY: The community from which this conversation originated from conversationIdOrigin?: string; // Blinded message requests ONLY: The community from which this conversation originated from
@ -82,7 +82,8 @@ export interface ConversationAttributes {
// =========================================================================== // ===========================================================================
// All of the items below are duplicated one way or the other with libsession. // All of the items below are duplicated one way or the other with libsession.
// It would be nice to at some point be able to only rely on libsession dumps // It would be nice to at some point be able to only rely on libsession dumps
// for those so there is no need to keep them in sync, but just have them in the dumps // for those so there is no need to keep them in sync, but just have them in the dumps.
// Note: If we do remove them, we also need to add some logic to the wrappers. For instance, we can currently search by nickname or display name and that works through the DB.
displayNameInProfile?: string; // no matter the type of conversation, this is the real name as set by the user/name of the open or closed group displayNameInProfile?: string; // no matter the type of conversation, this is the real name as set by the user/name of the open or closed group
nickname?: string; // this is the name WE gave to that user (only applicable to private chats, not closed group neither opengroups) nickname?: string; // this is the name WE gave to that user (only applicable to private chats, not closed group neither opengroups)

@ -1,26 +0,0 @@
import { LastMessageStatusType } from '../state/ducks/conversations';
interface ConversationLastMessageUpdate {
lastMessage: string;
lastMessageStatus: LastMessageStatusType;
}
export const createLastMessageUpdate = ({
lastMessageStatus,
lastMessageNotificationText,
}: {
lastMessageStatus?: LastMessageStatusType;
lastMessageNotificationText?: string;
}): ConversationLastMessageUpdate => {
if (!lastMessageNotificationText) {
return {
lastMessage: '',
lastMessageStatus: undefined,
};
}
return {
lastMessage: lastMessageNotificationText || '',
lastMessageStatus: lastMessageStatus || undefined,
};
};
Loading…
Cancel
Save