Skip to content

Commit 015e7a0

Browse files
Fix context removing (#60)
Signed-off-by: Kristina Fefelova <[email protected]>
1 parent 29458d3 commit 015e7a0

File tree

8 files changed

+44
-11
lines changed

8 files changed

+44
-11
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.193
1+
0.1.194

packages/cockroach/src/init.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ function getMigrations(): [string, string][] {
110110
migrationV2_5(),
111111
migrationV2_6(),
112112
migrationV2_7(),
113-
migrationV3_1()
113+
migrationV3_1(),
114+
migrationV4_1()
114115
]
115116
}
116117

@@ -361,3 +362,10 @@ function migrationV3_1(): [string, string] {
361362

362363
return ['init_link_preview-v3_1', sql]
363364
}
365+
366+
function migrationV4_1(): [string, string] {
367+
const sql = `
368+
CREATE INDEX IF NOT EXISTS notifications_context_id_read_created_desc_idx ON communication.notifications (context_id, read, created DESC);
369+
`
370+
return ['add_index_notifications_context_id_read_created_desc-v4_1', sql]
371+
}

packages/query/src/notification-contexts/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ export class NotificationContextsQuery implements PagedQuery<NotificationContext
512512
if (this.result instanceof Promise) this.result = await this.result
513513

514514
const length = this.result.length
515-
const deleted = this.result.delete(event.context)
515+
const deleted = this.result.delete(event.context.id)
516516

517517
if (deleted != null) {
518518
if (this.params.limit && length >= this.params.limit && this.result.length < this.params.limit) {

packages/query/src/notifications/query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,16 @@ export class NotificationQuery implements PagedQuery<Notification, FindNotificat
321321
private async onRemoveNotificationContextEvent(event: NotificationContextRemovedEvent): Promise<void> {
322322
if (this.result instanceof Promise) this.result = await this.result
323323

324-
if (this.params.context != null && this.params.context !== event.context) return
324+
if (this.params.context != null && this.params.context !== event.context.id) return
325325

326-
if (event.context === this.params.context) {
326+
if (event.context.id === this.params.context) {
327327
if (this.result.length === 0) return
328328
this.result.deleteAll()
329329
this.result.setHead(true)
330330
this.result.setTail(true)
331331
void this.notify()
332332
} else {
333-
const toRemove = this.result.getResult().filter((it) => it.context === event.context)
333+
const toRemove = this.result.getResult().filter((it) => it.context === event.context.id)
334334
if (toRemove.length === 0) return
335335
const length = this.result.length
336336

packages/sdk-types/src/responseEvents/notification.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ export interface NotificationContextCreatedEvent extends BaseResponseEvent {
7474

7575
export interface NotificationContextRemovedEvent extends BaseResponseEvent {
7676
type: NotificationResponseEventType.NotificationContextRemoved
77-
context: ContextID
78-
account: AccountID
77+
context: NotificationContext
7978
}
8079

8180
export interface NotificationContextUpdatedEvent extends BaseResponseEvent {

packages/server/src/middleware/broadcast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class BroadcastMiddleware extends BaseMiddleware implements Middleware {
216216
case NotificationResponseEventType.NotificationContextCreated:
217217
return info.account === event.context.account
218218
case NotificationResponseEventType.NotificationContextRemoved:
219-
return info.account === event.account
219+
return info.account === event.context.account
220220
case NotificationResponseEventType.NotificationContextUpdated:
221221
return info.account === event.account
222222
case MessageResponseEventType.MessagesGroupCreated:

packages/server/src/middleware/db.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,17 @@ export class DatabaseMiddleware extends BaseMiddleware implements Middleware {
526526
}
527527

528528
private async removeNotificationContext(event: RemoveNotificationContextEvent): Promise<Result> {
529+
const context = (await this.db.findNotificationContexts({ id: event.context, account: event.account }))[0]
530+
if (context === undefined) return {}
531+
529532
await this.db.removeContexts({
530533
id: event.context,
531534
account: event.account
532535
})
533536
const responseEvent: NotificationContextRemovedEvent = {
534537
_id: event._id,
535538
type: NotificationResponseEventType.NotificationContextRemoved,
536-
context: event.context,
537-
account: event.account
539+
context
538540
}
539541
return {
540542
responseEvent

packages/server/src/triggers/notification.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
LabelRequestEventType,
1919
MessageRequestEventType,
2020
MessageResponseEventType,
21+
type NotificationContextRemovedEvent,
2122
type NotificationContextUpdatedEvent,
2223
NotificationRequestEventType,
2324
NotificationResponseEventType,
@@ -165,6 +166,24 @@ async function onNotificationContextUpdated(
165166
return result
166167
}
167168

169+
async function onNotificationContextRemoved(
170+
ctx: TriggerCtx,
171+
event: NotificationContextRemovedEvent
172+
): Promise<RequestEvent[]> {
173+
const { context } = event
174+
175+
const result: RequestEvent[] = []
176+
177+
result.push({
178+
type: LabelRequestEventType.RemoveLabel,
179+
label: NewMessageLabelID,
180+
card: context.card,
181+
account: context.account
182+
})
183+
184+
return result
185+
}
186+
168187
async function onMessagesRemoved(ctx: TriggerCtx, event: PatchCreatedEvent): Promise<RequestEvent[]> {
169188
if (event.patch.type !== PatchType.remove) return []
170189

@@ -196,6 +215,11 @@ const triggers: Triggers = [
196215
NotificationResponseEventType.NotificationContextUpdated,
197216
onNotificationContextUpdated as TriggerFn
198217
],
218+
[
219+
'on_notification_context_removed',
220+
NotificationResponseEventType.NotificationContextRemoved,
221+
onNotificationContextRemoved as TriggerFn
222+
],
199223
['on_added_collaborators', NotificationResponseEventType.AddedCollaborators, onAddedCollaborators as TriggerFn],
200224
['on_removed_collaborators', NotificationResponseEventType.RemovedCollaborators, onRemovedCollaborators as TriggerFn],
201225
['remove_notifications_on_messages_removed', MessageResponseEventType.PatchCreated, onMessagesRemoved as TriggerFn]

0 commit comments

Comments
 (0)