handle only MessageType.COMMUNITY_CHAT (#281)

This commit is contained in:
Felicio Mununga 2022-06-21 09:17:15 +02:00 committed by GitHub
parent e338c67592
commit fa30667c51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 83 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import {
ChatMessage, ChatMessage,
DeleteMessage, DeleteMessage,
EditMessage, EditMessage,
MessageType,
} from '../../../protos/chat-message' } from '../../../protos/chat-message'
import { EmojiReaction } from '../../../protos/emoji-reaction' import { EmojiReaction } from '../../../protos/emoji-reaction'
import { PinMessage } from '../../../protos/pin-message' import { PinMessage } from '../../../protos/pin-message'
@ -68,8 +69,8 @@ export function handleWakuMessage(
client.wakuMessages.add(messageId) client.wakuMessages.add(messageId)
// decode, map and handle (events)
try { try {
// decode, map and handle (events)
switch (decodedMetadata.type) { switch (decodedMetadata.type) {
case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: { case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: {
// decode // decode
@ -85,17 +86,26 @@ export function handleWakuMessage(
// decode // decode
const decodedPayload = ChatMessage.decode(messageToDecode) const decodedPayload = ChatMessage.decode(messageToDecode)
// TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT switch (decodedPayload.messageType) {
const chatUuid = getChatUuid(decodedPayload.chatId) case MessageType.COMMUNITY_CHAT: {
const chatUuid = getChatUuid(decodedPayload.chatId)
// map // map
const chatMessage = mapChatMessage(decodedPayload, { const chatMessage = mapChatMessage(decodedPayload, {
messageId, messageId,
chatUuid, chatUuid,
}) })
// handle // handle
community.chats.get(chatUuid)?.handleNewMessage(chatMessage) community.chats.get(chatUuid)?.handleNewMessage(chatMessage)
break
}
default: {
break
}
}
break break
} }
@ -103,12 +113,22 @@ export function handleWakuMessage(
case ApplicationMetadataMessage.Type.TYPE_EDIT_MESSAGE: { case ApplicationMetadataMessage.Type.TYPE_EDIT_MESSAGE: {
const decodedPayload = EditMessage.decode(messageToDecode) const decodedPayload = EditMessage.decode(messageToDecode)
const messageId = decodedPayload.messageId switch (decodedPayload.messageType) {
const chatUuid = getChatUuid(decodedPayload.chatId) case MessageType.COMMUNITY_CHAT: {
const messageId = decodedPayload.messageId
const chatUuid = getChatUuid(decodedPayload.chatId)
community.chats community.chats
.get(chatUuid) .get(chatUuid)
?.handleEditedMessage(messageId, decodedPayload.text) ?.handleEditedMessage(messageId, decodedPayload.text)
break
}
default: {
break
}
}
break break
} }
@ -116,10 +136,20 @@ export function handleWakuMessage(
case ApplicationMetadataMessage.Type.TYPE_DELETE_MESSAGE: { case ApplicationMetadataMessage.Type.TYPE_DELETE_MESSAGE: {
const decodedPayload = DeleteMessage.decode(messageToDecode) const decodedPayload = DeleteMessage.decode(messageToDecode)
const messageId = decodedPayload.messageId switch (decodedPayload.messageType) {
const chatUuid = getChatUuid(decodedPayload.chatId) 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 break
} }
@ -127,12 +157,22 @@ export function handleWakuMessage(
case ApplicationMetadataMessage.Type.TYPE_PIN_MESSAGE: { case ApplicationMetadataMessage.Type.TYPE_PIN_MESSAGE: {
const decodedPayload = PinMessage.decode(messageToDecode) const decodedPayload = PinMessage.decode(messageToDecode)
const messageId = decodedPayload.messageId switch (decodedPayload.messageType) {
const chatUuid = getChatUuid(decodedPayload.chatId) case MessageType.COMMUNITY_CHAT: {
const messageId = decodedPayload.messageId
const chatUuid = getChatUuid(decodedPayload.chatId)
community.chats community.chats
.get(chatUuid) .get(chatUuid)
?.handlePinnedMessage(messageId, decodedPayload.pinned) ?.handlePinnedMessage(messageId, decodedPayload.pinned)
break
}
default: {
break
}
}
break break
} }
@ -140,22 +180,33 @@ export function handleWakuMessage(
case ApplicationMetadataMessage.Type.TYPE_EMOJI_REACTION: { case ApplicationMetadataMessage.Type.TYPE_EMOJI_REACTION: {
const decodedPayload = EmojiReaction.decode(messageToDecode) const decodedPayload = EmojiReaction.decode(messageToDecode)
const messageId = decodedPayload.messageId switch (decodedPayload.messageType) {
const chatUuid = getChatUuid(decodedPayload.chatId) 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( chat?.handleEmojiReaction(
messageId, messageId,
decodedPayload, decodedPayload,
`0x${bytesToHex(publicKey)}` `0x${bytesToHex(publicKey)}`
) )
break
}
default: {
break
}
}
break break
} }
default: default: {
break break
}
} }
} catch { } catch {
// protons-runtime throws when trying to decode invalid protocol buffers // protons-runtime throws when trying to decode invalid protocol buffers