Skip to content

Commit

Permalink
Extract trigger for pushes (#7767)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristina Fefelova <[email protected]>
  • Loading branch information
kristina-fefelova authored Jan 22, 2025
1 parent 86da890 commit bba3b48
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 234 deletions.
3 changes: 3 additions & 0 deletions models/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ export class TInboxNotification extends TDoc implements InboxNotification {
@Prop(TypeBoolean(), core.string.Boolean)
archived!: boolean

objectId!: Ref<Doc>
objectClass!: Ref<Class<Doc>>

declare space: Ref<PersonSpace>

title?: IntlString
Expand Down
41 changes: 41 additions & 0 deletions models/notification/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,43 @@ export async function migrateSettings (client: MigrationClient): Promise<void> {
)
}

export async function migrateNotificationsObject (client: MigrationClient): Promise<void> {
while (true) {
const notifications = await client.find<InboxNotification>(
DOMAIN_NOTIFICATION,
{ objectId: { $exists: false }, docNotifyContext: { $exists: true } },
{ limit: 500 }
)

if (notifications.length === 0) return

const contextIds = Array.from(new Set(notifications.map((n) => n.docNotifyContext)))
const contexts = await client.find<DocNotifyContext>(DOMAIN_DOC_NOTIFY, { _id: { $in: contextIds } })

for (const context of contexts) {
await client.update(
DOMAIN_NOTIFICATION,
{ docNotifyContext: context._id, objectId: { $exists: false } },
{ objectId: context.objectId, objectClass: context.objectClass }
)
}

const toDelete: Ref<InboxNotification>[] = []

for (const notification of notifications) {
const context = contexts.find((c) => c._id === notification.docNotifyContext)

if (context === undefined) {
toDelete.push(notification._id)
}
}

if (toDelete.length > 0) {
await client.deleteMany(DOMAIN_NOTIFICATION, { _id: { $in: toDelete } })
}
}
}

export const notificationOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {
await tryMigrate(client, notificationId, [
Expand Down Expand Up @@ -429,6 +466,10 @@ export const notificationOperation: MigrateOperation = {
func: async (client) => {
await client.update(DOMAIN_DOC_NOTIFY, { space: core.space.Space }, { space: core.space.Workspace })
}
},
{
state: 'migrate-notifications-object',
func: migrateNotificationsObject
}
])
},
Expand Down
9 changes: 9 additions & 0 deletions models/server-notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,13 @@ export function createModel (builder: Builder): void {
mixin: contact.mixin.Employee
}
})

builder.createDoc(serverCore.class.Trigger, core.space.Model, {
trigger: serverNotification.trigger.PushNotificationsHandler,
isAsync: true,
txMatch: {
_class: core.class.TxCreateDoc,
objectClass: notification.class.InboxNotification
}
})
}
2 changes: 2 additions & 0 deletions plugins/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ export interface InboxNotification extends Doc<PersonSpace> {
isViewed: boolean

docNotifyContext: Ref<DocNotifyContext>
objectId: Ref<Doc>
objectClass: Ref<Class<Doc>>

// For browser notifications
title?: IntlString
Expand Down
8 changes: 2 additions & 6 deletions server-plugins/activity-resources/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export async function getPersonNotificationTxes (
messageHtml: reference.message,
mentionedIn: reference.attachedDocId ?? reference.srcDocId,
mentionedInClass: reference.attachedDocClass ?? reference.srcDocClass,
objectId: reference.srcDocId,
objectClass: reference.srcDocClass,
user: receiver[0]._id,
isViewed: false,
archived: false
Expand Down Expand Up @@ -238,24 +240,18 @@ export async function getPersonNotificationTxes (
modifiedOn: originTx.modifiedOn,
modifiedBy: sender._id
}
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
user: receiverInfo._id
})

const msg = control.hierarchy.isDerived(data.mentionedInClass, activity.class.ActivityMessage)
? (await control.findAll(control.ctx, data.mentionedInClass, { _id: data.mentionedIn }))[0]
: undefined
await applyNotificationProviders(
notificationData,
notifyResult,
reference.srcDocId,
reference.srcDocClass,
control,
res,
doc,
receiverInfo,
senderInfo,
subscriptions,
notification.class.MentionInboxNotification,
msg as ActivityMessage
)
Expand Down
Loading

0 comments on commit bba3b48

Please sign in to comment.