Fix context removing (#60)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina
2025-05-28 08:48:39 +04:00
committed by GitHub
parent 29458d3723
commit 015e7a0acb
8 changed files with 44 additions and 11 deletions
+1 -1
View File
@@ -1 +1 @@
0.1.193
0.1.194
+9 -1
View File
@@ -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]
}
@@ -512,7 +512,7 @@ export class NotificationContextsQuery implements PagedQuery<NotificationContext
if (this.result instanceof Promise) this.result = await this.result
const length = this.result.length
const deleted = this.result.delete(event.context)
const deleted = this.result.delete(event.context.id)
if (deleted != null) {
if (this.params.limit && length >= this.params.limit && this.result.length < this.params.limit) {
+3 -3
View File
@@ -321,16 +321,16 @@ export class NotificationQuery implements PagedQuery<Notification, FindNotificat
private async onRemoveNotificationContextEvent(event: NotificationContextRemovedEvent): Promise<void> {
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
@@ -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 {
+1 -1
View File
@@ -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:
+4 -2
View File
@@ -526,6 +526,9 @@ export class DatabaseMiddleware extends BaseMiddleware implements Middleware {
}
private async removeNotificationContext(event: RemoveNotificationContextEvent): Promise<Result> {
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
@@ -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<RequestEvent[]> {
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<RequestEvent[]> {
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]