From cb88ee7a62cc4eab5184c80a1cdcf897a0afc990 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Tue, 21 Jun 2022 09:17:15 +0200 Subject: [PATCH] handle only MessageType.COMMUNITY_CHAT (#281) --- .../client/community/handle-waku-message.ts | 115 +++++++++++++----- 1 file changed, 83 insertions(+), 32 deletions(-) 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 16505955..9178c08d 100644 --- a/packages/status-js/src/client/community/handle-waku-message.ts +++ b/packages/status-js/src/client/community/handle-waku-message.ts @@ -5,6 +5,7 @@ import { ChatMessage, DeleteMessage, EditMessage, + MessageType, } from '../../../protos/chat-message' import { EmojiReaction } from '../../../protos/emoji-reaction' import { PinMessage } from '../../../protos/pin-message' @@ -68,8 +69,8 @@ export function handleWakuMessage( client.wakuMessages.add(messageId) - // decode, map and handle (events) try { + // decode, map and handle (events) switch (decodedMetadata.type) { case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: { // decode @@ -85,17 +86,26 @@ export function handleWakuMessage( // decode const decodedPayload = ChatMessage.decode(messageToDecode) - // TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT - const chatUuid = getChatUuid(decodedPayload.chatId) + switch (decodedPayload.messageType) { + case MessageType.COMMUNITY_CHAT: { + const chatUuid = getChatUuid(decodedPayload.chatId) - // map - const chatMessage = mapChatMessage(decodedPayload, { - messageId, - chatUuid, - }) + // map + const chatMessage = mapChatMessage(decodedPayload, { + messageId, + chatUuid, + }) - // handle - community.chats.get(chatUuid)?.handleNewMessage(chatMessage) + // handle + community.chats.get(chatUuid)?.handleNewMessage(chatMessage) + + break + } + + default: { + break + } + } break } @@ -103,12 +113,22 @@ export function handleWakuMessage( case ApplicationMetadataMessage.Type.TYPE_EDIT_MESSAGE: { const decodedPayload = EditMessage.decode(messageToDecode) - const messageId = decodedPayload.messageId - const chatUuid = getChatUuid(decodedPayload.chatId) + switch (decodedPayload.messageType) { + case MessageType.COMMUNITY_CHAT: { + const messageId = decodedPayload.messageId + const chatUuid = getChatUuid(decodedPayload.chatId) - community.chats - .get(chatUuid) - ?.handleEditedMessage(messageId, decodedPayload.text) + community.chats + .get(chatUuid) + ?.handleEditedMessage(messageId, decodedPayload.text) + + break + } + + default: { + break + } + } break } @@ -116,10 +136,20 @@ export function handleWakuMessage( case ApplicationMetadataMessage.Type.TYPE_DELETE_MESSAGE: { const decodedPayload = DeleteMessage.decode(messageToDecode) - const messageId = decodedPayload.messageId - const chatUuid = getChatUuid(decodedPayload.chatId) + switch (decodedPayload.messageType) { + case MessageType.COMMUNITY_CHAT: { + const messageId = decodedPayload.messageId + const chatUuid = getChatUuid(decodedPayload.chatId) - community.chats.get(chatUuid)?.handleDeletedMessage(messageId) + community.chats.get(chatUuid)?.handleDeletedMessage(messageId) + + break + } + + default: { + break + } + } break } @@ -127,12 +157,22 @@ export function handleWakuMessage( case ApplicationMetadataMessage.Type.TYPE_PIN_MESSAGE: { const decodedPayload = PinMessage.decode(messageToDecode) - const messageId = decodedPayload.messageId - const chatUuid = getChatUuid(decodedPayload.chatId) + switch (decodedPayload.messageType) { + case MessageType.COMMUNITY_CHAT: { + const messageId = decodedPayload.messageId + const chatUuid = getChatUuid(decodedPayload.chatId) - community.chats - .get(chatUuid) - ?.handlePinnedMessage(messageId, decodedPayload.pinned) + community.chats + .get(chatUuid) + ?.handlePinnedMessage(messageId, decodedPayload.pinned) + + break + } + + default: { + break + } + } break } @@ -140,22 +180,33 @@ export function handleWakuMessage( case ApplicationMetadataMessage.Type.TYPE_EMOJI_REACTION: { const decodedPayload = EmojiReaction.decode(messageToDecode) - const messageId = decodedPayload.messageId - const chatUuid = getChatUuid(decodedPayload.chatId) + switch (decodedPayload.messageType) { + case MessageType.COMMUNITY_CHAT: { + const messageId = decodedPayload.messageId + const chatUuid = getChatUuid(decodedPayload.chatId) - const chat = community.chats.get(chatUuid) + const chat = community.chats.get(chatUuid) - chat?.handleEmojiReaction( - messageId, - decodedPayload, - `0x${bytesToHex(publicKey)}` - ) + chat?.handleEmojiReaction( + messageId, + decodedPayload, + `0x${bytesToHex(publicKey)}` + ) + + break + } + + default: { + break + } + } break } - default: + default: { break + } } } catch { // protons-runtime throws when trying to decode invalid protocol buffers