From af7abf71238f4ac5c89d69f9bbc89c7bb3f3f00d Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Fri, 10 Jun 2022 02:11:29 +0200 Subject: [PATCH] use Set for waku message IDs --- packages/status-js/src/client.ts | 10 ++++- .../src/client/community/community.ts | 16 +++---- .../client/community/handle-waku-message.ts | 42 +++++++++++++++---- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index 6c718d5d..b43bcdbd 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -1,6 +1,11 @@ +/** + * @see https://specs.status.im/spec/1 + */ + // todo!: handle already received (emoji) events (by messageId or event) // todo: use Map // todo: write class for channels +// todo: use single proto runtime // todo: tests @@ -31,6 +36,7 @@ export interface ClientOptions { class Client { private waku: Waku + public readonly wakuMessages: Set public account?: Account public community: Community @@ -38,6 +44,8 @@ class Client { constructor(waku: Waku, options: ClientOptions) { // Waku this.waku = waku + this.wakuMessages = new Set() + // Community this.community = new Community(this, waku, options.publicKey) } @@ -113,7 +121,7 @@ class Client { } public handleWakuMessage = (wakuMessage: WakuMessage): void => { - handleWakuMessage(wakuMessage, this.community, this.account) + handleWakuMessage(wakuMessage, this, this.community, this.account) } } diff --git a/packages/status-js/src/client/community/community.ts b/packages/status-js/src/client/community/community.ts index 4524903f..b01cc6b3 100644 --- a/packages/status-js/src/client/community/community.ts +++ b/packages/status-js/src/client/community/community.ts @@ -243,11 +243,11 @@ export class Community { while (messageIndex > 0) { const _message = _messages[messageIndex - 1] - if (_message.messageId === chatMessage.messageId) { - messageIndex = -1 + // if (_message.messageId === chatMessage.messageId) { + // messageIndex = -1 - break - } + // break + // } if (_message.clock <= chatMessage.clock) { break @@ -256,10 +256,10 @@ export class Community { messageIndex-- } - // already received - if (messageIndex < 0) { - return - } + // // already received + // if (messageIndex < 0) { + // return + // } // replied let responsedToMessageIndex = _messages.length diff --git a/packages/status-js/src/client/community/handle-waku-message.ts b/packages/status-js/src/client/community/handle-waku-message.ts index 2b3fa27d..6303620a 100644 --- a/packages/status-js/src/client/community/handle-waku-message.ts +++ b/packages/status-js/src/client/community/handle-waku-message.ts @@ -20,6 +20,7 @@ import { getReactions } from './get-reactions' import { mapChatMessage } from './map-chat-message' import type { Account } from '../../account' +import type { Client } from '../../client' import type { Community /*, MessageType*/ } from './community' import type { WakuMessage } from 'js-waku' @@ -37,6 +38,7 @@ import type { WakuMessage } from 'js-waku' export function handleWakuMessage( wakuMessage: WakuMessage, // state + client: Client, community: Community, account?: Account ): void { @@ -65,12 +67,26 @@ export function handleWakuMessage( } messageToDecode = decodedMetadata.payload - // recover public key const publicKey = recoverPublicKey( decodedMetadata.signature, decodedMetadata.payload ) + // fixme?: handle decodedProtocol.encryptedMessage + const wakuMessageId = payloadToId( + decodedProtocol?.publicMessage || decodedMetadata.payload, + publicKey + ) + + // already handled + if (client.wakuMessages.has(wakuMessageId)) { + console.log('already received') + + return + } + + let success = false + // decode, map and handle (events) switch (decodedMetadata.type) { case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: { @@ -81,6 +97,8 @@ export function handleWakuMessage( // handle (state and callback) community.handleCommunityMetadataEvent(decodedPayload) + success = true + break } @@ -88,26 +106,22 @@ export function handleWakuMessage( // decode const decodedPayload = ChatMessage.decode(messageToDecode) - if (!decodedProtocol) { - break - } - // TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT // map - // fixme?: handle decodedProtocol.encryptedMessage - const messageId = payloadToId(decodedProtocol.publicMessage, publicKey) // todo?: use full chatId (incl. pub key) instead const channelId = getChannelId(decodedPayload.chatId) const chatMessage = mapChatMessage(decodedPayload, { - messageId, + messageId: wakuMessageId, channelId, }) // handle community.handleChannelChatMessageNewEvent(chatMessage) + success = true + break } @@ -153,6 +167,8 @@ export function handleWakuMessage( community.channelMessages[channelId]! ) + success = true + break } @@ -189,6 +205,8 @@ export function handleWakuMessage( community.channelMessages[channelId]! ) + success = true + break } @@ -225,6 +243,8 @@ export function handleWakuMessage( community.channelMessages[channelId]! ) + success = true + break } @@ -269,6 +289,8 @@ export function handleWakuMessage( community.channelMessages[channelId]! ) + success = true + break } @@ -276,5 +298,9 @@ export function handleWakuMessage( break } + if (success) { + client.wakuMessages.add(wakuMessageId) + } + return }