Request push subscriptions with one find (#6518)

This commit is contained in:
Kristina 2024-09-11 06:15:11 +04:00 committed by GitHub
parent 42abc31ab4
commit a7b60c6a83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 13 deletions

View File

@ -253,6 +253,9 @@ export async function getPersonNotificationTxes (
modifiedOn: originTx.modifiedOn, modifiedOn: originTx.modifiedOn,
modifiedBy: sender._id modifiedBy: sender._id
} }
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
user: receiverInfo._id
})
await applyNotificationProviders( await applyNotificationProviders(
notificationData, notificationData,
notifyResult, notifyResult,
@ -262,7 +265,8 @@ export async function getPersonNotificationTxes (
res, res,
doc, doc,
receiverInfo, receiverInfo,
senderInfo senderInfo,
subscriptions
) )
} }
} }

View File

@ -284,8 +284,12 @@ export async function OnKnock (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const body = await translate(love.string.IsKnocking, { const body = await translate(love.string.IsKnocking, {
name: formatName(from.name, control.branding?.lastNameFirst) name: formatName(from.name, control.branding?.lastNameFirst)
}) })
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
user: userAcc[0]._id
})
// TODO: Select proper account target // TODO: Select proper account target
await createPushNotification(control, userAcc[0]._id, title, body, request._id, from, path) await createPushNotification(control, userAcc[0]._id, title, body, request._id, subscriptions, from, path)
} }
} }
return res return res
@ -323,8 +327,11 @@ export async function OnInvite (tx: Tx, control: TriggerControl): Promise<Tx[]>
name: formatName(from.name, control.branding?.lastNameFirst) name: formatName(from.name, control.branding?.lastNameFirst)
}) })
: await translate(love.string.InivitingLabel, {}) : await translate(love.string.InivitingLabel, {})
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
user: userAcc[0]._id
})
// TODO: Select a proper user // TODO: Select a proper user
await createPushNotification(control, userAcc[0]._id, title, body, invite._id, from, path) await createPushNotification(control, userAcc[0]._id, title, body, invite._id, subscriptions, from, path)
} }
} }
} }

View File

@ -164,6 +164,7 @@ export async function getCommonNotificationTxes (
if (notificationTx !== undefined) { if (notificationTx !== undefined) {
const notificationData = TxProcessor.createDoc2Doc(notificationTx) const notificationData = TxProcessor.createDoc2Doc(notificationTx)
const subscriptions = await control.findAll(ctx, notification.class.PushSubscription, { user: receiver._id })
await applyNotificationProviders( await applyNotificationProviders(
notificationData, notificationData,
notifyResult, notifyResult,
@ -173,7 +174,8 @@ export async function getCommonNotificationTxes (
res, res,
doc, doc,
receiver, receiver,
sender sender,
subscriptions
) )
} }
@ -492,6 +494,7 @@ export async function createPushFromInbox (
_class: Ref<Class<InboxNotification>>, _class: Ref<Class<InboxNotification>>,
sender: SenderInfo, sender: SenderInfo,
_id: Ref<Doc>, _id: Ref<Doc>,
subscriptions: PushSubscription[],
cache: Map<Ref<Doc>, Doc> = new Map<Ref<Doc>, Doc>() cache: Map<Ref<Doc>, Doc> = new Map<Ref<Doc>, Doc>()
): Promise<Tx | undefined> { ): Promise<Tx | undefined> {
let { title, body } = await getTranslatedNotificationContent(data, _class, control) let { title, body } = await getTranslatedNotificationContent(data, _class, control)
@ -521,7 +524,16 @@ export async function createPushFromInbox (
} }
const path = [workbenchId, control.workspace.workspaceUrl, notificationId, encodeObjectURI(id, attachedToClass)] const path = [workbenchId, control.workspace.workspaceUrl, notificationId, encodeObjectURI(id, attachedToClass)]
await createPushNotification(control, receiver._id as Ref<PersonAccount>, title, body, _id, senderPerson, path) await createPushNotification(
control,
receiver._id as Ref<PersonAccount>,
title,
body,
_id,
subscriptions,
senderPerson,
path
)
return control.txFactory.createTxCreateDoc(notification.class.BrowserNotification, receiver.space, { return control.txFactory.createTxCreateDoc(notification.class.BrowserNotification, receiver.space, {
user: receiver._id, user: receiver._id,
status: NotificationStatus.New, status: NotificationStatus.New,
@ -541,6 +553,7 @@ export async function createPushNotification (
title: string, title: string,
body: string, body: string,
_id: string, _id: string,
subscriptions: PushSubscription[],
senderAvatar?: Data<AvatarInfo>, senderAvatar?: Data<AvatarInfo>,
path?: string[] path?: string[]
): Promise<void> { ): Promise<void> {
@ -548,7 +561,7 @@ export async function createPushNotification (
const privateKey = getMetadata(serverNotification.metadata.PushPrivateKey) const privateKey = getMetadata(serverNotification.metadata.PushPrivateKey)
const subject = getMetadata(serverNotification.metadata.PushSubject) ?? 'mailto:hey@huly.io' const subject = getMetadata(serverNotification.metadata.PushSubject) ?? 'mailto:hey@huly.io'
if (privateKey === undefined || publicKey === undefined) return if (privateKey === undefined || publicKey === undefined) return
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, { user: target }) const userSubscriptions = subscriptions.filter((it) => it.user === target)
const data: PushData = { const data: PushData = {
title, title,
body body
@ -576,7 +589,7 @@ export async function createPushNotification (
webpush.setVapidDetails(subject, publicKey, privateKey) webpush.setVapidDetails(subject, publicKey, privateKey)
for (const subscription of subscriptions) { for (const subscription of userSubscriptions) {
void sendPushToSubscription(control, target, subscription, data) void sendPushToSubscription(control, target, subscription, data)
} }
} }
@ -648,6 +661,7 @@ export async function applyNotificationProviders (
object: Doc, object: Doc,
receiver: ReceiverInfo, receiver: ReceiverInfo,
sender: SenderInfo, sender: SenderInfo,
subscriptions: PushSubscription[],
message?: ActivityMessage message?: ActivityMessage
): Promise<void> { ): Promise<void> {
const resources = control.modelDb.findAllSync(serverNotification.class.NotificationProviderResources, {}) const resources = control.modelDb.findAllSync(serverNotification.class.NotificationProviderResources, {})
@ -662,7 +676,8 @@ export async function applyNotificationProviders (
data, data,
notification.class.ActivityInboxNotification, notification.class.ActivityInboxNotification,
sender, sender,
data._id data._id,
subscriptions
) )
if (pushTx !== undefined) { if (pushTx !== undefined) {
res.push(pushTx) res.push(pushTx)
@ -727,7 +742,8 @@ export async function getNotificationTxes (
params: NotifyParams, params: NotifyParams,
docNotifyContexts: DocNotifyContext[], docNotifyContexts: DocNotifyContext[],
activityMessages: ActivityMessage[], activityMessages: ActivityMessage[],
settings: NotificationProviderControl settings: NotificationProviderControl,
subscriptions: PushSubscription[]
): Promise<Tx[]> { ): Promise<Tx[]> {
if (receiver.account === undefined) { if (receiver.account === undefined) {
return [] return []
@ -776,6 +792,7 @@ export async function getNotificationTxes (
object, object,
receiver, receiver,
sender, sender,
subscriptions,
message message
) )
} }
@ -940,7 +957,9 @@ export async function createCollabDocInfo (
} }
const settings = await getNotificationProviderControl(ctx, control) const settings = await getNotificationProviderControl(ctx, control)
const subscriptions = await control.findAll(ctx, notification.class.PushSubscription, {
user: { $in: Array.from(targets) }
})
for (const target of targets) { for (const target of targets) {
const info: ReceiverInfo | undefined = toReceiverInfo(control.hierarchy, usersInfo.get(target)) const info: ReceiverInfo | undefined = toReceiverInfo(control.hierarchy, usersInfo.get(target))
@ -957,7 +976,8 @@ export async function createCollabDocInfo (
params, params,
notifyContexts, notifyContexts,
docMessages, docMessages,
settings settings,
subscriptions
) )
const ids = new Set(targetRes.map((it) => it._id)) const ids = new Set(targetRes.map((it) => it._id))
if (info.account?.email !== undefined) { if (info.account?.email !== undefined) {

View File

@ -168,7 +168,9 @@ async function getRequestNotificationTx (
} }
const notificationControl = await getNotificationProviderControl(ctx, control) const notificationControl = await getNotificationProviderControl(ctx, control)
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
user: { $in: collaborators }
})
for (const target of collaborators) { for (const target of collaborators) {
const targetInfo = toReceiverInfo(control.hierarchy, usersInfo.get(target)) const targetInfo = toReceiverInfo(control.hierarchy, usersInfo.get(target))
if (targetInfo === undefined) continue if (targetInfo === undefined) continue
@ -184,7 +186,8 @@ async function getRequestNotificationTx (
{ isOwn: true, isSpace: false, shouldUpdateTimestamp: true }, { isOwn: true, isSpace: false, shouldUpdateTimestamp: true },
notifyContexts, notifyContexts,
messages, messages,
notificationControl notificationControl,
subscriptions
) )
res.push(...txes) res.push(...txes)
} }