From 74a2cb0ce5961292f89ba69177a5fa8e4da0a068 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Thu, 9 Jun 2022 19:22:58 +0200 Subject: [PATCH] return chat messages from history fetch only if new found --- .../src/client/community/community.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/status-js/src/client/community/community.ts b/packages/status-js/src/client/community/community.ts index b3950bf9..b3f41bc5 100644 --- a/packages/status-js/src/client/community/community.ts +++ b/packages/status-js/src/client/community/community.ts @@ -114,12 +114,15 @@ export class Community { const endTime = new Date() const _messages = this.channelMessages[channelId] || [] + let _oldestMessageTime: Date | undefined = undefined if (_messages.length) { - const oldestMessageTime = new Date(Number(_messages[0].timestamp)) + _oldestMessageTime = new Date(Number(_messages[0].timestamp)) - if (oldestMessageTime <= options.start) { + if (_oldestMessageTime <= options.start) { callback(_messages) + + return } } @@ -140,8 +143,18 @@ export class Community { }, }) - // todo: call abck only if oldestMessageTime has changed // callback + if ( + _oldestMessageTime && + this.channelMessages[channelId]?.length && + _oldestMessageTime >= + new Date(Number(this.channelMessages[channelId]![0].timestamp)) + ) { + callback([]) + + return + } + callback(this.channelMessages[channelId] ?? []) } }