Fix messages query (#46)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina
2025-04-21 21:27:55 +04:00
committed by GitHub
parent e6543090e5
commit 28355ea25a
7 changed files with 46 additions and 26 deletions
+2
View File
@@ -14,3 +14,5 @@
//
export * from './lq'
export type { MessageQueryParams } from './types'
+2 -2
View File
@@ -34,7 +34,7 @@ import type {
FindClient
} from '@hcengineering/communication-sdk-types'
import type { FindParams, QueryId, AnyQuery } from './types'
import type { FindParams, QueryId, AnyQuery, MessageQueryParams } from './types'
import { MessagesQuery } from './messages/query'
import { NotificationQuery } from './notifications/query'
import { NotificationContextsQuery } from './notification-contexts/query'
@@ -76,7 +76,7 @@ export class LiveQueries {
}
}
queryMessages(params: FindMessagesParams, callback: PagedQueryCallback<Message>): CreateQueryResult {
queryMessages(params: MessageQueryParams, callback: PagedQueryCallback<Message>): CreateQueryResult {
return this.createAndStoreQuery<Message, FindMessagesParams, MessagesQuery>(
params,
callback,
+18
View File
@@ -652,11 +652,29 @@ export class MessagesQuery implements PagedQuery<Message, MessageQueryParams> {
if (tmp) this.result.delete(tmp)
this.tmpMessages.delete(eventId)
}
const lastMessage = this.result.getLast()
const firstMessage = this.result.getFirst()
function shouldResort(order: SortingOrder): boolean {
if (firstMessage == null || lastMessage == null) return false
if (order === SortingOrder.Ascending) {
return lastMessage.created > message.created
}
return firstMessage.created > message.created
}
if (this.params.order === SortingOrder.Ascending) {
this.result.push(message)
} else {
this.result.unshift(message)
}
if (shouldResort(this.params.order ?? SortingOrder.Ascending)) {
this.result.sort((a, b) =>
this.params.order === SortingOrder.Ascending
? a.created.getTime() - b.created.getTime()
: b.created.getTime() - a.created.getTime()
)
}
await this.notify()
}
}