From d6f4726f723823ef28bad85ead33a57a8bef9dc0 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 8 Jun 2022 13:07:05 +0200 Subject: [PATCH] try protocol layer first --- packages/status-js/src/client.ts | 1 - .../delete_handle-channel-chat-message.ts | 20 +++++++++---- .../community/handle-channel-chat-message.ts | 29 ++++++++++++------- .../src/client/community/handle-community.ts | 29 +++++++++++++++++-- packages/status-js/src/debug.ts | 2 +- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index 5c8f9e30..f3ccbe89 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -1,4 +1,3 @@ -// todo: try protocol layer; then application data layer // todo: replies; normalize messages (e.g. replies) prior returning // todo: tests diff --git a/packages/status-js/src/client/community/delete_handle-channel-chat-message.ts b/packages/status-js/src/client/community/delete_handle-channel-chat-message.ts index 4c94a7ef..f67da8c3 100644 --- a/packages/status-js/src/client/community/delete_handle-channel-chat-message.ts +++ b/packages/status-js/src/client/community/delete_handle-channel-chat-message.ts @@ -19,26 +19,34 @@ export function handleChannelChatMessage( return } - const decodedProtocol = ProtocolMessage.decode(wakuMessage.payload) + let messageToDecode = wakuMessage.payload + let decodedProtocol + try { + decodedProtocol = ProtocolMessage.decode(messageToDecode) + if (decodedProtocol) { + messageToDecode = decodedProtocol.publicMessage + } + } catch {} + + // fixme!: remove after replacing payloadToId if (!decodedProtocol) { return } - const decodedMetadata = ApplicationMetadataMessage.decode( - decodedProtocol.publicMessage - ) + const decodedMetadata = ApplicationMetadataMessage.decode(messageToDecode) if (!decodedMetadata.payload) { return } + messageToDecode = decodedMetadata.payload - // todo?: process other types of messages + // todo: merge and process other types of messages if ( decodedMetadata.type !== ApplicationMetadataMessage.Type.TYPE_CHAT_MESSAGE ) { return } - const decodedPayload = ChatMessage.decode(decodedMetadata.payload) + const decodedPayload = ChatMessage.decode(messageToDecode) const messageId = payloadToId( decodedProtocol.publicMessage, 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 b996d9e5..76401abc 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 @@ -35,28 +35,37 @@ export function handleChannelChatMessage( return result } - const decodedProtocol = ProtocolMessage.decode(wakuMessage.payload) + let messageToDecode = wakuMessage.payload + let decodedProtocol + + try { + decodedProtocol = ProtocolMessage.decode(wakuMessage.payload) + if (decodedProtocol) { + messageToDecode = decodedProtocol.publicMessage + } + } catch {} + + // fixme!: remove after replacing payloadToId if (!decodedProtocol) { return result } - const decodedMetadata = ApplicationMetadataMessage.decode( - decodedProtocol.publicMessage - ) - + const decodedMetadata = ApplicationMetadataMessage.decode(messageToDecode) if (!decodedMetadata.payload) { return result } + messageToDecode = decodedMetadata.payload // todo?: // if (!decodedMetadata.identity) { // break // } + // todo: merge and process other types of messages // TODO?: ignore messages which are messageType !== COMMUNITY_CHAT switch (decodedMetadata.type) { case ApplicationMetadataMessage.Type.TYPE_CHAT_MESSAGE: { - const decodedPayload = ChatMessage.decode(decodedMetadata.payload) + const decodedPayload = ChatMessage.decode(messageToDecode) const messageId = payloadToId( decodedProtocol.publicMessage, @@ -99,7 +108,7 @@ export function handleChannelChatMessage( break } case ApplicationMetadataMessage.Type.TYPE_EDIT_MESSAGE: { - const decodedPayload = EditMessage.decode(decodedMetadata.payload) + const decodedPayload = EditMessage.decode(messageToDecode) const messageId = decodedPayload.messageId const channelId = getChannelId(decodedPayload.chatId) @@ -139,7 +148,7 @@ export function handleChannelChatMessage( break } case ApplicationMetadataMessage.Type.TYPE_DELETE_MESSAGE: { - const decodedPayload = DeleteMessage.decode(decodedMetadata.payload) + const decodedPayload = DeleteMessage.decode(messageToDecode) const messageId = decodedPayload.messageId const channelId = getChannelId(decodedPayload.chatId) @@ -168,7 +177,7 @@ export function handleChannelChatMessage( break } case ApplicationMetadataMessage.Type.TYPE_PIN_MESSAGE: { - const decodedPayload = PinMessage.decode(decodedMetadata.payload) + const decodedPayload = PinMessage.decode(messageToDecode) const messageId = decodedPayload.messageId const channelId = getChannelId(decodedPayload.chatId) @@ -196,7 +205,7 @@ export function handleChannelChatMessage( break } case ApplicationMetadataMessage.Type.TYPE_EMOJI_REACTION: { - const decodedPayload = EmojiReaction.decode(decodedMetadata.payload) + const decodedPayload = EmojiReaction.decode(messageToDecode) const messageId = decodedPayload.messageId const channelId = getChannelId(decodedPayload.chatId) diff --git a/packages/status-js/src/client/community/handle-community.ts b/packages/status-js/src/client/community/handle-community.ts index c3390c6d..e0488374 100644 --- a/packages/status-js/src/client/community/handle-community.ts +++ b/packages/status-js/src/client/community/handle-community.ts @@ -1,4 +1,5 @@ import { ApplicationMetadataMessage } from '../../../protos/application-metadata-message' +import { ProtocolMessage } from '../../../protos/protocol-message' import { CommunityDescription } from '../../wire/community_description' import type { CommunityMetadataType } from './community' @@ -11,12 +12,34 @@ export function handleCommunity( return } - const decodedMetadata = ApplicationMetadataMessage.decode(wakuMessage.payload) - if (!decodedMetadata.payload) { + if (!wakuMessage.signaturePublicKey) { return } - const decodedPayload = CommunityDescription.decode(decodedMetadata.payload) + let messageToDecode = wakuMessage.payload + + try { + const decodedProtocol = ProtocolMessage.decode(messageToDecode) + if (decodedProtocol) { + messageToDecode = decodedProtocol.publicMessage + } + } catch {} + + const decodedMetadata = ApplicationMetadataMessage.decode(messageToDecode) + if (!decodedMetadata.payload) { + return + } + messageToDecode = decodedMetadata.payload + + // todo: merge and process other types of messages + if ( + decodedMetadata.type !== + ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION + ) { + return + } + + const decodedPayload = CommunityDescription.decode(messageToDecode) // todo!: explain if (!decodedPayload.identity) { return diff --git a/packages/status-js/src/debug.ts b/packages/status-js/src/debug.ts index a42c4a5c..76012182 100644 --- a/packages/status-js/src/debug.ts +++ b/packages/status-js/src/debug.ts @@ -13,7 +13,7 @@ const CHANNEL_ID = await client.createAccount() - const communityMetadata = client.community.fetchCommunity() + const communityMetadata = await client.community.fetchCommunity() const fetchChannelMessages = await client.community.createFetchChannelMessages(CHANNEL_ID, messages =>