Remove duplicated contexts (#6366)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina 2024-08-21 19:27:38 +04:00 committed by GitHub
parent e02d0c716b
commit 29a4276bbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 70 additions and 11 deletions

View File

@ -34,7 +34,8 @@ import {
type Ref,
type Space,
type Timestamp,
type Tx
type Tx,
type TxCUD
} from '@hcengineering/core'
import {
ArrOf,
@ -216,6 +217,8 @@ export class TDocNotifyContext extends TDoc implements DocNotifyContext {
@Prop(TypeBoolean(), notification.string.Pinned)
isPinned!: boolean
tx?: Ref<TxCUD<Doc>>
}
@Model(notification.class.InboxNotification, core.class.Doc, DOMAIN_NOTIFICATION)

View File

@ -186,6 +186,42 @@ export async function migrateNotificationsSpace (client: MigrationClient): Promi
await client.deleteMany(DOMAIN_USER_NOTIFY, { _class: notification.class.BrowserNotification })
}
export async function migrateDuplicateContexts (client: MigrationClient): Promise<void> {
const personSpaces = await client.find<PersonSpace>(DOMAIN_SPACE, { _class: contact.class.PersonSpace }, {})
for (const space of personSpaces) {
const contexts = await client.find<DocNotifyContext>(
DOMAIN_DOC_NOTIFY,
{ _class: notification.class.DocNotifyContext, space: space._id },
{}
)
const toRemove = new Set<Ref<DocNotifyContext>>()
const contextByUser = new Map<string, DocNotifyContext>()
for (const context of contexts) {
const key = context.objectId + '.' + context.user
const existContext = contextByUser.get(key)
if (existContext != null) {
const existLastViewedTimestamp = existContext.lastViewedTimestamp ?? 0
const newLastViewedTimestamp = context.lastViewedTimestamp ?? 0
if (existLastViewedTimestamp > newLastViewedTimestamp) {
toRemove.add(context._id)
} else {
toRemove.add(existContext._id)
contextByUser.set(key, context)
}
} else {
contextByUser.set(key, context)
}
}
if (toRemove.size > 0) {
await client.deleteMany(DOMAIN_DOC_NOTIFY, { _id: { $in: Array.from(toRemove) } })
await client.deleteMany(DOMAIN_NOTIFICATION, { docNotifyContext: { $in: Array.from(toRemove) } })
}
}
}
export async function migrateSettings (client: MigrationClient): Promise<void> {
await client.update(
DOMAIN_PREFERENCE,
@ -343,6 +379,10 @@ export const notificationOperation: MigrateOperation = {
{ objectSpace: core.space.Space }
)
}
},
{
state: 'migrate-duplicated-contexts-v1',
func: migrateDuplicateContexts
}
])

View File

@ -27,6 +27,7 @@ import {
Space,
Timestamp,
Tx,
TxCUD,
TxOperations
} from '@hcengineering/core'
import type { Asset, IntlString, Metadata, Plugin, Resource } from '@hcengineering/platform'
@ -286,6 +287,9 @@ export interface DocNotifyContext extends Doc<PersonSpace> {
isPinned: boolean
lastViewedTimestamp?: Timestamp
lastUpdateTimestamp?: Timestamp
// Only for debug
tx?: Ref<TxCUD<Doc>>
}
/**

View File

@ -227,7 +227,8 @@ export async function getPersonNotificationTxes (
doc.space,
originTx.modifiedOn,
notifyResult,
notification.class.MentionInboxNotification
notification.class.MentionInboxNotification,
originTx
)
res.push(...txes)
} else {

View File

@ -136,7 +136,8 @@ export async function getCommonNotificationTxes (
space: Ref<Space>,
modifiedOn: Timestamp,
notifyResult: NotifyResult,
_class = notification.class.CommonInboxNotification
_class = notification.class.CommonInboxNotification,
tx?: TxCUD<Doc>
): Promise<Tx[]> {
if (notifyResult.size === 0 || !notifyResult.has(notification.providers.InboxNotificationProvider)) {
return []
@ -156,7 +157,9 @@ export async function getCommonNotificationTxes (
notifyContexts,
data,
_class,
modifiedOn
modifiedOn,
true,
tx
)
if (notificationTx !== undefined) {
@ -340,7 +343,8 @@ export async function pushInboxNotifications (
data: Partial<Data<InboxNotification>>,
_class: Ref<Class<InboxNotification>>,
modifiedOn: Timestamp,
shouldUpdateTimestamp = true
shouldUpdateTimestamp = true,
tx?: TxCUD<Doc>
): Promise<TxCreateDoc<InboxNotification> | undefined> {
const context = getDocNotifyContext(control, contexts, objectId, receiver._id)
let docNotifyContextId: Ref<DocNotifyContext>
@ -353,7 +357,8 @@ export async function pushInboxNotifications (
objectClass,
objectSpace,
receiver,
shouldUpdateTimestamp ? modifiedOn : undefined
shouldUpdateTimestamp ? modifiedOn : undefined,
tx
)
} else {
docNotifyContextId = context._id
@ -618,7 +623,8 @@ export async function pushActivityInboxNotifications (
data,
notification.class.ActivityInboxNotification,
activityMessage.modifiedOn,
shouldUpdateTimestamp
shouldUpdateTimestamp,
originTx
)
}
@ -675,7 +681,8 @@ async function createNotifyContext (
objectClass: Ref<Class<Doc>>,
objectSpace: Ref<Space>,
receiver: ReceiverInfo,
updateTimestamp?: Timestamp
updateTimestamp?: Timestamp,
tx?: TxCUD<Doc>
): Promise<Ref<DocNotifyContext>> {
const createTx = control.txFactory.createTxCreateDoc(notification.class.DocNotifyContext, receiver.space, {
user: receiver._id,
@ -683,6 +690,7 @@ async function createNotifyContext (
objectClass,
objectSpace,
isPinned: false,
tx: tx?._id,
lastUpdateTimestamp: updateTimestamp
})
await ctx.with('apply', {}, () => control.apply([createTx]))
@ -770,7 +778,8 @@ export async function getNotificationTxes (
message.attachedToClass,
message.space,
receiver,
params.shouldUpdateTimestamp ? originTx.modifiedOn : undefined
params.shouldUpdateTimestamp ? originTx.modifiedOn : undefined,
tx
)
}
}
@ -1632,7 +1641,7 @@ async function updateCollaborators (ctx: MeasureContext, control: TriggerControl
if (info === undefined) continue
const context = getDocNotifyContext(control, contexts, objectId, info._id)
if (context !== undefined) continue
await createNotifyContext(ctx, control, objectId, objectClass, objectSpace, info)
await createNotifyContext(ctx, control, objectId, objectClass, objectSpace, info, undefined, tx)
}
await removeContexts(ctx, contexts, removedCollaborators as Ref<PersonAccount>[], control)

View File

@ -241,7 +241,9 @@ export async function OnToDoCreate (tx: TxCUD<Doc>, control: TriggerControl): Pr
object._class,
object.space,
createTx.modifiedOn,
notifyResult
notifyResult,
notification.class.CommonInboxNotification,
tx
)
await control.apply(txes)