use Set for waku message IDs

This commit is contained in:
Felicio Mununga 2022-06-10 02:11:29 +02:00
parent e1b1036695
commit c34eeea0d6
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
3 changed files with 51 additions and 17 deletions

View File

@ -1,6 +1,11 @@
/**
* @see https://specs.status.im/spec/1
*/
// todo!: handle already received (emoji) events (by messageId or event) // todo!: handle already received (emoji) events (by messageId or event)
// todo: use Map // todo: use Map
// todo: write class for channels // todo: write class for channels
// todo: use single proto runtime
// todo: tests // todo: tests
@ -31,6 +36,7 @@ export interface ClientOptions {
class Client { class Client {
private waku: Waku private waku: Waku
public readonly wakuMessages: Set<string>
public account?: Account public account?: Account
public community: Community public community: Community
@ -38,6 +44,8 @@ class Client {
constructor(waku: Waku, options: ClientOptions) { constructor(waku: Waku, options: ClientOptions) {
// Waku // Waku
this.waku = waku this.waku = waku
this.wakuMessages = new Set()
// Community // Community
this.community = new Community(this, waku, options.publicKey) this.community = new Community(this, waku, options.publicKey)
} }
@ -113,7 +121,7 @@ class Client {
} }
public handleWakuMessage = (wakuMessage: WakuMessage): void => { public handleWakuMessage = (wakuMessage: WakuMessage): void => {
handleWakuMessage(wakuMessage, this.community, this.account) handleWakuMessage(wakuMessage, this, this.community, this.account)
} }
} }

View File

@ -243,11 +243,11 @@ export class Community {
while (messageIndex > 0) { while (messageIndex > 0) {
const _message = _messages[messageIndex - 1] const _message = _messages[messageIndex - 1]
if (_message.messageId === chatMessage.messageId) { // if (_message.messageId === chatMessage.messageId) {
messageIndex = -1 // messageIndex = -1
break // break
} // }
if (_message.clock <= chatMessage.clock) { if (_message.clock <= chatMessage.clock) {
break break
@ -256,10 +256,10 @@ export class Community {
messageIndex-- messageIndex--
} }
// already received // // already received
if (messageIndex < 0) { // if (messageIndex < 0) {
return // return
} // }
// replied // replied
let responsedToMessageIndex = _messages.length let responsedToMessageIndex = _messages.length

View File

@ -20,6 +20,7 @@ import { getReactions } from './get-reactions'
import { mapChatMessage } from './map-chat-message' import { mapChatMessage } from './map-chat-message'
import type { Account } from '../../account' import type { Account } from '../../account'
import type { Client } from '../../client'
import type { Community /*, MessageType*/ } from './community' import type { Community /*, MessageType*/ } from './community'
import type { WakuMessage } from 'js-waku' import type { WakuMessage } from 'js-waku'
@ -37,6 +38,7 @@ import type { WakuMessage } from 'js-waku'
export function handleWakuMessage( export function handleWakuMessage(
wakuMessage: WakuMessage, wakuMessage: WakuMessage,
// state // state
client: Client,
community: Community, community: Community,
account?: Account account?: Account
): void { ): void {
@ -65,12 +67,26 @@ export function handleWakuMessage(
} }
messageToDecode = decodedMetadata.payload messageToDecode = decodedMetadata.payload
// recover public key
const publicKey = recoverPublicKey( const publicKey = recoverPublicKey(
decodedMetadata.signature, decodedMetadata.signature,
decodedMetadata.payload 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) // decode, map and handle (events)
switch (decodedMetadata.type) { switch (decodedMetadata.type) {
case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: { case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: {
@ -81,6 +97,8 @@ export function handleWakuMessage(
// handle (state and callback) // handle (state and callback)
community.handleCommunityMetadataEvent(decodedPayload) community.handleCommunityMetadataEvent(decodedPayload)
success = true
break break
} }
@ -88,26 +106,22 @@ export function handleWakuMessage(
// decode // decode
const decodedPayload = ChatMessage.decode(messageToDecode) const decodedPayload = ChatMessage.decode(messageToDecode)
if (!decodedProtocol) {
break
}
// TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT // TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT
// map // map
// fixme?: handle decodedProtocol.encryptedMessage
const messageId = payloadToId(decodedProtocol.publicMessage, publicKey)
// todo?: use full chatId (incl. pub key) instead // todo?: use full chatId (incl. pub key) instead
const channelId = getChannelId(decodedPayload.chatId) const channelId = getChannelId(decodedPayload.chatId)
const chatMessage = mapChatMessage(decodedPayload, { const chatMessage = mapChatMessage(decodedPayload, {
messageId, messageId: wakuMessageId,
channelId, channelId,
}) })
// handle // handle
community.handleChannelChatMessageNewEvent(chatMessage) community.handleChannelChatMessageNewEvent(chatMessage)
success = true
break break
} }
@ -153,6 +167,8 @@ export function handleWakuMessage(
community.channelMessages[channelId]! community.channelMessages[channelId]!
) )
success = true
break break
} }
@ -189,6 +205,8 @@ export function handleWakuMessage(
community.channelMessages[channelId]! community.channelMessages[channelId]!
) )
success = true
break break
} }
@ -225,6 +243,8 @@ export function handleWakuMessage(
community.channelMessages[channelId]! community.channelMessages[channelId]!
) )
success = true
break break
} }
@ -269,6 +289,8 @@ export function handleWakuMessage(
community.channelMessages[channelId]! community.channelMessages[channelId]!
) )
success = true
break break
} }
@ -276,5 +298,9 @@ export function handleWakuMessage(
break break
} }
if (success) {
client.wakuMessages.add(wakuMessageId)
}
return return
} }