From 5423aa5047782f87c1f683079b01d95c360ce0e7 Mon Sep 17 00:00:00 2001 From: Kristina Date: Mon, 21 Apr 2025 10:06:20 +0400 Subject: [PATCH] Fix notifications query (#8644) Signed-off-by: Kristina Fefelova --- .../notification-resources/src/index.ts | 89 +++-------------- server/middleware/package.json | 1 - server/middleware/src/index.ts | 1 - server/middleware/src/notifications.ts | 99 ------------------- server/server-pipeline/src/pipeline.ts | 2 - 5 files changed, 13 insertions(+), 179 deletions(-) delete mode 100644 server/middleware/src/notifications.ts diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index f1f4b45ecc..5a101544ef 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -18,7 +18,7 @@ import activity, { ActivityMessage, DocUpdateMessage } from '@hcengineering/acti import chunter, { ChatMessage } from '@hcengineering/chunter' import contact, { Employee, type Person } from '@hcengineering/contact' import core, { - PersonId, + AccountUuid, AnyAttribute, ArrOf, AttachedDoc, @@ -30,6 +30,8 @@ import core, { DocumentUpdate, MeasureContext, MixinUpdate, + notEmpty, + PersonId, Ref, RefTo, SortingOrder, @@ -41,11 +43,7 @@ import core, { TxMixin, TxProcessor, TxRemoveDoc, - TxUpdateDoc, - AccountUuid, - notEmpty, - generateId, - toIdMap + TxUpdateDoc } from '@hcengineering/core' import notification, { ActivityInboxNotification, @@ -78,21 +76,21 @@ import { createPullCollaboratorsTx, createPushCollaboratorsTx, getHTMLPresenter, + getNotificationContent, getNotificationLink, getNotificationProviderControl, getObjectSpace, - getTextPresenter, getReceiversInfo, + getSenderInfo, + getTextPresenter, isAllowed, isMixinTx, isShouldNotifyTx, isUserEmployeeInFieldValueTypeMatch, messageToMarkup, - replaceAll, - updateNotifyContextsSpace, type NotificationProviderControl, - getNotificationContent, - getSenderInfo + replaceAll, + updateNotifyContextsSpace } from './utils' import { PushNotificationsHandler } from './push' @@ -564,11 +562,6 @@ async function createNotifyContext ( contextsCache.contexts.set(cacheKey, createTx.objectId) control.cache.set(ContextsCacheKey, contextsCache) await ctx.with('apply', {}, () => control.apply(control.ctx, [createTx])) - control.ctx.contextData.broadcast.targets['docNotifyContext' + createTx._id] = (it) => { - if (it._id === createTx._id) { - return [receiver.account] - } - } return createTx.objectId } @@ -668,12 +661,6 @@ async function updateContextsTimestamp ( }) res.push(updateTx) - - control.ctx.contextData.broadcast.targets['docNotifyContext' + updateTx._id] = (it) => { - if (it._id === updateTx._id) { - return [context.user] - } - } } if (res.length > 0) { @@ -700,12 +687,6 @@ async function removeContexts ( const removeTx = control.txFactory.createTxRemoveDoc(context._class, context.space, context._id) res.push(removeTx) - - control.ctx.contextData.broadcast.targets['docNotifyContext' + removeTx._id] = (it) => { - if (it._id === removeTx._id) { - return [context.user] - } - } } await control.apply(control.ctx, res) @@ -815,13 +796,7 @@ export async function createCollabDocInfo ( docMessages, settings ) - const ids = new Set(targetRes.map((it) => it._id)) - const id = generateId() as string - control.ctx.contextData.broadcast.targets[id] = (it) => { - if (ids.has(it._id)) { - return [receiver.account] - } - } + res = res.concat(targetRes) } return res @@ -1448,41 +1423,6 @@ export async function OnAttributeUpdate (txes: Tx[], control: TriggerControl): P return result } -async function applyUserTxes (ctx: MeasureContext, control: TriggerControl, txes: Tx[]): Promise { - const map: Map = new Map() - const res: Tx[] = [] - - for (const tx of txes) { - const ttx = tx as TxCUD - if ( - control.hierarchy.isDerived(ttx.objectClass, notification.class.InboxNotification) && - ttx._class === core.class.TxCreateDoc - ) { - const notification = TxProcessor.createDoc2Doc(ttx as TxCreateDoc) - - if (map.has(notification.user)) { - map.get(notification.user)?.push(tx) - } else { - map.set(notification.user, [tx]) - } - } else { - res.push(tx) - } - } - - for (const [user, txs] of map.entries()) { - await control.apply(ctx, txs) - const m1 = toIdMap(txs) - control.ctx.contextData.broadcast.targets.docNotifyContext = (it) => { - if (m1.has(it._id)) { - return [user] - } - } - } - - return res -} - async function updateCollaborators ( ctx: MeasureContext, control: TriggerControl, @@ -1565,19 +1505,16 @@ export async function createCollaboratorNotifications ( } if (tx.attachedTo !== undefined && !ignoreCollection) { - const res = await ctx.with('collectionCollabDoc', {}, (ctx) => + return await ctx.with('collectionCollabDoc', {}, (ctx) => collectionCollabDoc(ctx, tx as TxCUD, control, activityMessages, cache, true) ) - return await applyUserTxes(ctx, control, res) } switch (tx._class) { case core.class.TxCreateDoc: { - const res = await ctx.with('createCollaboratorDoc', {}, (ctx) => + return await ctx.with('createCollaboratorDoc', {}, (ctx) => createCollaboratorDoc(ctx, tx as TxCreateDoc, control, activityMessages, cache) ) - - return await applyUserTxes(ctx, control, res) } case core.class.TxUpdateDoc: case core.class.TxMixin: { @@ -1596,7 +1533,7 @@ export async function createCollaboratorNotifications ( ) ) ) - return await applyUserTxes(ctx, control, res) + return res } } diff --git a/server/middleware/package.json b/server/middleware/package.json index 92054c9a27..35d1fa81bb 100644 --- a/server/middleware/package.json +++ b/server/middleware/package.json @@ -42,7 +42,6 @@ "@hcengineering/platform": "^0.6.11", "@hcengineering/server-core": "^0.6.1", "@hcengineering/server-preference": "^0.6.0", - "@hcengineering/server-notification": "^0.6.1", "@hcengineering/query": "^0.6.12", "@hcengineering/analytics": "^0.6.0", "fast-equals": "^5.2.2" diff --git a/server/middleware/src/index.ts b/server/middleware/src/index.ts index bf65de207e..2b787927c8 100644 --- a/server/middleware/src/index.ts +++ b/server/middleware/src/index.ts @@ -28,7 +28,6 @@ export * from './lookup' export * from './lowLevel' export * from './model' export * from './modified' -export * from './notifications' export * from './private' export * from './queryJoin' export * from './spacePermissions' diff --git a/server/middleware/src/notifications.ts b/server/middleware/src/notifications.ts deleted file mode 100644 index b20e3d45ab..0000000000 --- a/server/middleware/src/notifications.ts +++ /dev/null @@ -1,99 +0,0 @@ -// -// Copyright © 2024 Hardcore Engineering Inc. -// -// Licensed under the Eclipse Public License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. You may -// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import core, { - Doc, - MeasureContext, - Tx, - TxCUD, - TxProcessor, - type SessionData, - TxApplyIf, - systemAccountUuid, - AccountUuid -} from '@hcengineering/core' -import platform, { PlatformError, Severity, Status } from '@hcengineering/platform' -import { BaseMiddleware, Middleware, TxMiddlewareResult, type PipelineContext } from '@hcengineering/server-core' -import { DOMAIN_USER_NOTIFY, DOMAIN_NOTIFICATION, DOMAIN_DOC_NOTIFY } from '@hcengineering/server-notification' - -/** - * @public - */ -export class NotificationsMiddleware extends BaseMiddleware implements Middleware { - private readonly targetDomains = [DOMAIN_USER_NOTIFY, DOMAIN_NOTIFICATION, DOMAIN_DOC_NOTIFY] - - private constructor (context: PipelineContext, next?: Middleware) { - super(context, next) - } - - static async create ( - ctx: MeasureContext, - context: PipelineContext, - next: Middleware | undefined - ): Promise { - return new NotificationsMiddleware(context, next) - } - - isTargetDomain (tx: Tx): boolean { - if (TxProcessor.isExtendsCUD(tx._class)) { - const txCUD = tx as TxCUD - const domain = this.context.hierarchy.getDomain(txCUD.objectClass) - return this.targetDomains.includes(domain) - } - return false - } - - processTx (ctx: MeasureContext, tx: Tx): void { - let target: AccountUuid[] | undefined - if (this.isTargetDomain(tx)) { - const account = ctx.contextData.account - if (!account.socialIds.includes(tx.modifiedBy) && account.uuid !== systemAccountUuid) { - throw new PlatformError(new Status(Severity.ERROR, platform.status.Forbidden, {})) - } - const modifiedByAccount = ctx.contextData.socialStringsToUsers.get(tx.modifiedBy) - target = [account.uuid, systemAccountUuid] - if (modifiedByAccount !== undefined && !target.includes(modifiedByAccount)) { - target.push(modifiedByAccount) - } - ctx.contextData.broadcast.targets['checkDomain' + account.uuid] = (tx) => { - if (this.isTargetDomain(tx)) { - return target - } - } - } - } - - tx (ctx: MeasureContext, txes: Tx[]): Promise { - for (const tx of txes) { - if (this.context.hierarchy.isDerived(tx._class, core.class.TxApplyIf)) { - for (const ttx of (tx as TxApplyIf).txes) { - this.processTx(ctx, ttx) - } - } else { - this.processTx(ctx, tx) - } - } - - return this.provideTx(ctx, txes) - } - - isAvailable (ctx: MeasureContext, doc: Doc): boolean { - const domain = this.context.hierarchy.getDomain(doc._class) - if (!this.targetDomains.includes(domain)) return true - const account = ctx.contextData.account - const socialStrings = account.socialIds - return (doc.createdBy !== undefined && socialStrings.includes(doc.createdBy)) || account.uuid === systemAccountUuid - } -} diff --git a/server/server-pipeline/src/pipeline.ts b/server/server-pipeline/src/pipeline.ts index 71006982c6..59351e0de3 100644 --- a/server/server-pipeline/src/pipeline.ts +++ b/server/server-pipeline/src/pipeline.ts @@ -30,7 +30,6 @@ import { MarkDerivedEntryMiddleware, ModelMiddleware, ModifiedMiddleware, - NotificationsMiddleware, PluginConfigurationMiddleware, PrivateMiddleware, QueryJoinMiddleware, @@ -122,7 +121,6 @@ export function createServerPipeline ( ModifiedMiddleware.create, PluginConfigurationMiddleware.create, PrivateMiddleware.create, - NotificationsMiddleware.create, (ctx: MeasureContext, context: PipelineContext, next?: Middleware) => SpaceSecurityMiddleware.create(opt.adapterSecurity ?? false, ctx, context, next), SpacePermissionsMiddleware.create,