From 53a740bcc36e86ae7e289b29e05a5982f38ed740 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 8 Jun 2022 15:02:16 +0200 Subject: [PATCH] handle replies --- packages/status-js/src/client.ts | 15 +++---- .../src/client/community/community.ts | 1 + .../community/handle-channel-chat-message.ts | 42 ++++++++++++++----- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index f3ccbe8..5395ff3 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -1,19 +1,19 @@ -// todo: replies; normalize messages (e.g. replies) prior returning +// todo?: already received (by messageId or event) +// todo: ignore not found +// todo: handle updates together with fetching history (chat messages) +// todo: handle diff waku messages on diff topics // todo: tests // todo?: use clock for sorting -// todo: handle diff waku messages on diff topics - // todo: handle disconnections; no messages after sleep; libp2p; // todo: identities/members? -// todo: validate sig -// todo: observer contact updates -// todo: observer channels +// todo?: observe channels // todo?: rename channels to chats -// todo: change relative import paths // todo?: multiple communityCallback // todo?: call onChannel* separately +// todo?: ignore messages of not yet approved users +// todo?: ignore messages with invalid signature import { Waku } from 'js-waku' @@ -45,6 +45,7 @@ class Client { default: false, peers: [ '/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS', + // '/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ', ], }, }) diff --git a/packages/status-js/src/client/community/community.ts b/packages/status-js/src/client/community/community.ts index cd97cac..3ab2727 100644 --- a/packages/status-js/src/client/community/community.ts +++ b/packages/status-js/src/client/community/community.ts @@ -21,6 +21,7 @@ export type MessageType = ChatMessage & { pinned: boolean reactions: Reactions channelId: string + responseToMessage?: Omit } export class Community { diff --git a/packages/status-js/src/client/community/handle-channel-chat-message.ts b/packages/status-js/src/client/community/handle-channel-chat-message.ts index 76401ab..fb014e0 100644 --- a/packages/status-js/src/client/community/handle-channel-chat-message.ts +++ b/packages/status-js/src/client/community/handle-channel-chat-message.ts @@ -69,17 +69,13 @@ export function handleChannelChatMessage( const messageId = payloadToId( decodedProtocol.publicMessage, + // fixme!: replace for recoverPublicKeyFromMetadata wakuMessage.signaturePublicKey ) const channelId = getChannelId(decodedPayload.chatId) const _messages = messages[channelId] || [] - // already received - if (_messages.find(message => message.messageId === messageId)) { - break - } - const message = mapChatMessage(decodedPayload, { messageId, channelId }) // findIndexLeft @@ -87,9 +83,16 @@ export function handleChannelChatMessage( // new Date(Number(timestamp)) > new Date(Number(message.timestamp)) // }) // findIndexRight - let index = _messages.length - while (index >= 0) { - const _message = _messages[index - 1] + let messageIndex = _messages.length + while (messageIndex > 0) { + const _message = _messages[messageIndex - 1] + + // // already received + // if (_message.messageId === messageId) { + // messageIndex = -1 + + // break + // } if ( new Date(Number(_message.timestamp)) <= @@ -98,10 +101,29 @@ export function handleChannelChatMessage( break } - index-- + messageIndex-- } - _messages.splice(index, 0, message) + // // already received + // if (messageIndex < 0) { + // break + // } + + // replied + let responsedToMessageIndex = _messages.length + while (--responsedToMessageIndex >= 0) { + const _message = _messages[responsedToMessageIndex] + + if (_message.messageId === message.responseTo) { + break + } + } + + if (responsedToMessageIndex >= 0) { + message.responseToMessage = _messages[responsedToMessageIndex] + } + + _messages.splice(messageIndex, 0, message) result = _messages