Skip to content

Commit

Permalink
Remove duplicated contexts (#6366)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristina Fefelova <[email protected]>
  • Loading branch information
kristina-fefelova authored Aug 21, 2024
1 parent e02d0c7 commit 29a4276
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
5 changes: 4 additions & 1 deletion models/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
type Ref,
type Space,
type Timestamp,
type Tx
type Tx,
type TxCUD
} from '@hcengineering/core'
import {
ArrOf,
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions models/notification/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -343,6 +379,10 @@ export const notificationOperation: MigrateOperation = {
{ objectSpace: core.space.Space }
)
}
},
{
state: 'migrate-duplicated-contexts-v1',
func: migrateDuplicateContexts
}
])

Expand Down
4 changes: 4 additions & 0 deletions plugins/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Space,
Timestamp,
Tx,
TxCUD,
TxOperations
} from '@hcengineering/core'
import type { Asset, IntlString, Metadata, Plugin, Resource } from '@hcengineering/platform'
Expand Down Expand Up @@ -286,6 +287,9 @@ export interface DocNotifyContext extends Doc<PersonSpace> {
isPinned: boolean
lastViewedTimestamp?: Timestamp
lastUpdateTimestamp?: Timestamp

// Only for debug
tx?: Ref<TxCUD<Doc>>
}

/**
Expand Down
3 changes: 2 additions & 1 deletion server-plugins/activity-resources/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ export async function getPersonNotificationTxes (
doc.space,
originTx.modifiedOn,
notifyResult,
notification.class.MentionInboxNotification
notification.class.MentionInboxNotification,
originTx
)
res.push(...txes)
} else {
Expand Down
25 changes: 17 additions & 8 deletions server-plugins/notification-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand All @@ -156,7 +157,9 @@ export async function getCommonNotificationTxes (
notifyContexts,
data,
_class,
modifiedOn
modifiedOn,
true,
tx
)

if (notificationTx !== undefined) {
Expand Down Expand Up @@ -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>
Expand All @@ -353,7 +357,8 @@ export async function pushInboxNotifications (
objectClass,
objectSpace,
receiver,
shouldUpdateTimestamp ? modifiedOn : undefined
shouldUpdateTimestamp ? modifiedOn : undefined,
tx
)
} else {
docNotifyContextId = context._id
Expand Down Expand Up @@ -618,7 +623,8 @@ export async function pushActivityInboxNotifications (
data,
notification.class.ActivityInboxNotification,
activityMessage.modifiedOn,
shouldUpdateTimestamp
shouldUpdateTimestamp,
originTx
)
}

Expand Down Expand Up @@ -675,14 +681,16 @@ 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,
objectId,
objectClass,
objectSpace,
isPinned: false,
tx: tx?._id,
lastUpdateTimestamp: updateTimestamp
})
await ctx.with('apply', {}, () => control.apply([createTx]))
Expand Down Expand Up @@ -770,7 +778,8 @@ export async function getNotificationTxes (
message.attachedToClass,
message.space,
receiver,
params.shouldUpdateTimestamp ? originTx.modifiedOn : undefined
params.shouldUpdateTimestamp ? originTx.modifiedOn : undefined,
tx
)
}
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion server-plugins/time-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 29a4276

Please sign in to comment.