diff --git a/services/mail/mail-common/src/channel.ts b/services/mail/mail-common/src/channel.ts index 1da3165df6..fe06e0fc1a 100644 --- a/services/mail/mail-common/src/channel.ts +++ b/services/mail/mail-common/src/channel.ts @@ -19,7 +19,7 @@ import chat from '@hcengineering/chat' import mail from '@hcengineering/mail' import { PersonSpace } from '@hcengineering/contact' import { SyncMutex } from './mutex' -import { normalizeEmail } from './utils' +import { MessageTimeShift, normalizeEmail } from './utils' const createMutex = new SyncMutex() @@ -144,7 +144,7 @@ export class ChannelCache { modifiedBy: personId }, generateId(), - Date.now(), + Date.now() + MessageTimeShift.Channel, personId ) @@ -155,7 +155,7 @@ export class ChannelCache { space, mail.tag.MailChannel, {}, - Date.now(), + Date.now() + MessageTimeShift.MailTag, personId ) diff --git a/services/mail/mail-common/src/message.ts b/services/mail/mail-common/src/message.ts index 19abbf04a7..3e2a391879 100644 --- a/services/mail/mail-common/src/message.ts +++ b/services/mail/mail-common/src/message.ts @@ -44,7 +44,7 @@ import { generateMessageId } from '@hcengineering/communication-shared' import { BaseConfig, SyncOptions, type Attachment } from './types' import { EmailMessage, MailRecipient, MessageData } from './types' -import { getBlobMetadata, getMdContent } from './utils' +import { getBlobMetadata, getMdContent, MessageTimeShift } from './utils' import { PersonCacheFactory } from './person' import { PersonSpacesCacheFactory } from './personSpaces' import { ChannelCache, ChannelCacheFactory } from './channel' @@ -229,10 +229,10 @@ async function saveMessageToSpaces ( createdBy: modifiedBy, modifiedBy, parent: channel, - createdOn: createdDate - 4 // Add a small shift to ensure correct ordering + createdOn: createdDate + MessageTimeShift.ThreadCard // Add a small shift to ensure correct ordering }, generateId(), - createdDate - 4, + createdDate + MessageTimeShift.ThreadCard, modifiedBy ) threadId = newThreadId as Ref @@ -281,7 +281,7 @@ async function createMailThread ( cardType: chat.masterTag.Thread, content: data.subject, socialId: data.modifiedBy, - date: new Date(data.created.getTime() - 1), + date: new Date(data.created.getTime() + MessageTimeShift.Subject), messageId: subjectId, options: { noNotify: options?.noNotify @@ -300,7 +300,7 @@ async function createMailThread ( threadType: chat.masterTag.Thread }, socialId: data.modifiedBy, - date: new Date(data.created.getTime() - 3) + date: new Date(data.created.getTime() + MessageTimeShift.Thread) } const thread = Buffer.from(JSON.stringify(threadEvent)) await sendToCommunicationTopic(producer, config, data, thread) @@ -392,7 +392,7 @@ async function addCollaborators ( cardType: chat.masterTag.Thread, collaborators: [data.recipient.uuid as AccountUuid], socialId: data.modifiedBy, - date: new Date(data.created.getTime() - 2) + date: new Date(data.created.getTime() + MessageTimeShift.Collaborator) } const createMessageData = Buffer.from(JSON.stringify(addCollaboratorsEvent)) await sendToCommunicationTopic(producer, config, data, createMessageData) diff --git a/services/mail/mail-common/src/utils.ts b/services/mail/mail-common/src/utils.ts index 16914b60f7..2c64a37b43 100644 --- a/services/mail/mail-common/src/utils.ts +++ b/services/mail/mail-common/src/utils.ts @@ -150,3 +150,13 @@ export function getBlobMetadata (ctx: MeasureContext, attachment: Attachment): B return undefined } } + +// Define different time shifts for objects to ensure correct ordering +export enum MessageTimeShift { + Channel = -6, + MailTag = -5, + ThreadCard = -4, + Thread = -3, + Collaborator = -2, + Subject = -1 +}