diff --git a/.version b/.version index 09e8a661ea..63982f32ff 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.1.193 +0.1.194 diff --git a/packages/cockroach/src/init.ts b/packages/cockroach/src/init.ts index ce0a98409c..1701f49b82 100644 --- a/packages/cockroach/src/init.ts +++ b/packages/cockroach/src/init.ts @@ -110,7 +110,8 @@ function getMigrations(): [string, string][] { migrationV2_5(), migrationV2_6(), migrationV2_7(), - migrationV3_1() + migrationV3_1(), + migrationV4_1() ] } @@ -361,3 +362,10 @@ function migrationV3_1(): [string, string] { return ['init_link_preview-v3_1', sql] } + +function migrationV4_1(): [string, string] { + const sql = ` + CREATE INDEX IF NOT EXISTS notifications_context_id_read_created_desc_idx ON communication.notifications (context_id, read, created DESC); + ` + return ['add_index_notifications_context_id_read_created_desc-v4_1', sql] +} diff --git a/packages/query/src/notification-contexts/query.ts b/packages/query/src/notification-contexts/query.ts index aa7f031ffb..8ffa47fdf8 100644 --- a/packages/query/src/notification-contexts/query.ts +++ b/packages/query/src/notification-contexts/query.ts @@ -512,7 +512,7 @@ export class NotificationContextsQuery implements PagedQuery= this.params.limit && this.result.length < this.params.limit) { diff --git a/packages/query/src/notifications/query.ts b/packages/query/src/notifications/query.ts index a651bf7977..720ffafc4f 100644 --- a/packages/query/src/notifications/query.ts +++ b/packages/query/src/notifications/query.ts @@ -321,16 +321,16 @@ export class NotificationQuery implements PagedQuery { if (this.result instanceof Promise) this.result = await this.result - if (this.params.context != null && this.params.context !== event.context) return + if (this.params.context != null && this.params.context !== event.context.id) return - if (event.context === this.params.context) { + if (event.context.id === this.params.context) { if (this.result.length === 0) return this.result.deleteAll() this.result.setHead(true) this.result.setTail(true) void this.notify() } else { - const toRemove = this.result.getResult().filter((it) => it.context === event.context) + const toRemove = this.result.getResult().filter((it) => it.context === event.context.id) if (toRemove.length === 0) return const length = this.result.length diff --git a/packages/sdk-types/src/responseEvents/notification.ts b/packages/sdk-types/src/responseEvents/notification.ts index 232d7f7820..d8495f3068 100644 --- a/packages/sdk-types/src/responseEvents/notification.ts +++ b/packages/sdk-types/src/responseEvents/notification.ts @@ -74,8 +74,7 @@ export interface NotificationContextCreatedEvent extends BaseResponseEvent { export interface NotificationContextRemovedEvent extends BaseResponseEvent { type: NotificationResponseEventType.NotificationContextRemoved - context: ContextID - account: AccountID + context: NotificationContext } export interface NotificationContextUpdatedEvent extends BaseResponseEvent { diff --git a/packages/server/src/middleware/broadcast.ts b/packages/server/src/middleware/broadcast.ts index 0fa160b74e..cf2acf65ff 100644 --- a/packages/server/src/middleware/broadcast.ts +++ b/packages/server/src/middleware/broadcast.ts @@ -216,7 +216,7 @@ export class BroadcastMiddleware extends BaseMiddleware implements Middleware { case NotificationResponseEventType.NotificationContextCreated: return info.account === event.context.account case NotificationResponseEventType.NotificationContextRemoved: - return info.account === event.account + return info.account === event.context.account case NotificationResponseEventType.NotificationContextUpdated: return info.account === event.account case MessageResponseEventType.MessagesGroupCreated: diff --git a/packages/server/src/middleware/db.ts b/packages/server/src/middleware/db.ts index 6874edf326..c89b90d75a 100644 --- a/packages/server/src/middleware/db.ts +++ b/packages/server/src/middleware/db.ts @@ -526,6 +526,9 @@ export class DatabaseMiddleware extends BaseMiddleware implements Middleware { } private async removeNotificationContext(event: RemoveNotificationContextEvent): Promise { + const context = (await this.db.findNotificationContexts({ id: event.context, account: event.account }))[0] + if (context === undefined) return {} + await this.db.removeContexts({ id: event.context, account: event.account @@ -533,8 +536,7 @@ export class DatabaseMiddleware extends BaseMiddleware implements Middleware { const responseEvent: NotificationContextRemovedEvent = { _id: event._id, type: NotificationResponseEventType.NotificationContextRemoved, - context: event.context, - account: event.account + context } return { responseEvent diff --git a/packages/server/src/triggers/notification.ts b/packages/server/src/triggers/notification.ts index b5d2ffdfb9..603f3d8fe3 100644 --- a/packages/server/src/triggers/notification.ts +++ b/packages/server/src/triggers/notification.ts @@ -18,6 +18,7 @@ import { LabelRequestEventType, MessageRequestEventType, MessageResponseEventType, + type NotificationContextRemovedEvent, type NotificationContextUpdatedEvent, NotificationRequestEventType, NotificationResponseEventType, @@ -165,6 +166,24 @@ async function onNotificationContextUpdated( return result } +async function onNotificationContextRemoved( + ctx: TriggerCtx, + event: NotificationContextRemovedEvent +): Promise { + const { context } = event + + const result: RequestEvent[] = [] + + result.push({ + type: LabelRequestEventType.RemoveLabel, + label: NewMessageLabelID, + card: context.card, + account: context.account + }) + + return result +} + async function onMessagesRemoved(ctx: TriggerCtx, event: PatchCreatedEvent): Promise { if (event.patch.type !== PatchType.remove) return [] @@ -196,6 +215,11 @@ const triggers: Triggers = [ NotificationResponseEventType.NotificationContextUpdated, onNotificationContextUpdated as TriggerFn ], + [ + 'on_notification_context_removed', + NotificationResponseEventType.NotificationContextRemoved, + onNotificationContextRemoved as TriggerFn + ], ['on_added_collaborators', NotificationResponseEventType.AddedCollaborators, onAddedCollaborators as TriggerFn], ['on_removed_collaborators', NotificationResponseEventType.RemovedCollaborators, onRemovedCollaborators as TriggerFn], ['remove_notifications_on_messages_removed', MessageResponseEventType.PatchCreated, onMessagesRemoved as TriggerFn]