use Set for waku message IDs
This commit is contained in:
parent
e1b1036695
commit
c34eeea0d6
|
@ -1,6 +1,11 @@
|
|||
/**
|
||||
* @see https://specs.status.im/spec/1
|
||||
*/
|
||||
|
||||
// todo!: handle already received (emoji) events (by messageId or event)
|
||||
// todo: use Map
|
||||
// todo: write class for channels
|
||||
// todo: use single proto runtime
|
||||
|
||||
// todo: tests
|
||||
|
||||
|
@ -31,6 +36,7 @@ export interface ClientOptions {
|
|||
|
||||
class Client {
|
||||
private waku: Waku
|
||||
public readonly wakuMessages: Set<string>
|
||||
|
||||
public account?: Account
|
||||
public community: Community
|
||||
|
@ -38,6 +44,8 @@ class Client {
|
|||
constructor(waku: Waku, options: ClientOptions) {
|
||||
// Waku
|
||||
this.waku = waku
|
||||
this.wakuMessages = new Set()
|
||||
|
||||
// Community
|
||||
this.community = new Community(this, waku, options.publicKey)
|
||||
}
|
||||
|
@ -113,7 +121,7 @@ class Client {
|
|||
}
|
||||
|
||||
public handleWakuMessage = (wakuMessage: WakuMessage): void => {
|
||||
handleWakuMessage(wakuMessage, this.community, this.account)
|
||||
handleWakuMessage(wakuMessage, this, this.community, this.account)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -243,11 +243,11 @@ export class Community {
|
|||
while (messageIndex > 0) {
|
||||
const _message = _messages[messageIndex - 1]
|
||||
|
||||
if (_message.messageId === chatMessage.messageId) {
|
||||
messageIndex = -1
|
||||
// if (_message.messageId === chatMessage.messageId) {
|
||||
// messageIndex = -1
|
||||
|
||||
break
|
||||
}
|
||||
// break
|
||||
// }
|
||||
|
||||
if (_message.clock <= chatMessage.clock) {
|
||||
break
|
||||
|
@ -256,10 +256,10 @@ export class Community {
|
|||
messageIndex--
|
||||
}
|
||||
|
||||
// already received
|
||||
if (messageIndex < 0) {
|
||||
return
|
||||
}
|
||||
// // already received
|
||||
// if (messageIndex < 0) {
|
||||
// return
|
||||
// }
|
||||
|
||||
// replied
|
||||
let responsedToMessageIndex = _messages.length
|
||||
|
|
|
@ -20,6 +20,7 @@ import { getReactions } from './get-reactions'
|
|||
import { mapChatMessage } from './map-chat-message'
|
||||
|
||||
import type { Account } from '../../account'
|
||||
import type { Client } from '../../client'
|
||||
import type { Community /*, MessageType*/ } from './community'
|
||||
import type { WakuMessage } from 'js-waku'
|
||||
|
||||
|
@ -37,6 +38,7 @@ import type { WakuMessage } from 'js-waku'
|
|||
export function handleWakuMessage(
|
||||
wakuMessage: WakuMessage,
|
||||
// state
|
||||
client: Client,
|
||||
community: Community,
|
||||
account?: Account
|
||||
): void {
|
||||
|
@ -65,12 +67,26 @@ export function handleWakuMessage(
|
|||
}
|
||||
messageToDecode = decodedMetadata.payload
|
||||
|
||||
// recover public key
|
||||
const publicKey = recoverPublicKey(
|
||||
decodedMetadata.signature,
|
||||
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)
|
||||
switch (decodedMetadata.type) {
|
||||
case ApplicationMetadataMessage.Type.TYPE_COMMUNITY_DESCRIPTION: {
|
||||
|
@ -81,6 +97,8 @@ export function handleWakuMessage(
|
|||
// handle (state and callback)
|
||||
community.handleCommunityMetadataEvent(decodedPayload)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -88,26 +106,22 @@ export function handleWakuMessage(
|
|||
// decode
|
||||
const decodedPayload = ChatMessage.decode(messageToDecode)
|
||||
|
||||
if (!decodedProtocol) {
|
||||
break
|
||||
}
|
||||
|
||||
// TODO?: ignore community.channelMessages which are messageType !== COMMUNITY_CHAT
|
||||
|
||||
// map
|
||||
// fixme?: handle decodedProtocol.encryptedMessage
|
||||
const messageId = payloadToId(decodedProtocol.publicMessage, publicKey)
|
||||
// todo?: use full chatId (incl. pub key) instead
|
||||
const channelId = getChannelId(decodedPayload.chatId)
|
||||
|
||||
const chatMessage = mapChatMessage(decodedPayload, {
|
||||
messageId,
|
||||
messageId: wakuMessageId,
|
||||
channelId,
|
||||
})
|
||||
|
||||
// handle
|
||||
community.handleChannelChatMessageNewEvent(chatMessage)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -153,6 +167,8 @@ export function handleWakuMessage(
|
|||
community.channelMessages[channelId]!
|
||||
)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -189,6 +205,8 @@ export function handleWakuMessage(
|
|||
community.channelMessages[channelId]!
|
||||
)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -225,6 +243,8 @@ export function handleWakuMessage(
|
|||
community.channelMessages[channelId]!
|
||||
)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -269,6 +289,8 @@ export function handleWakuMessage(
|
|||
community.channelMessages[channelId]!
|
||||
)
|
||||
|
||||
success = true
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -276,5 +298,9 @@ export function handleWakuMessage(
|
|||
break
|
||||
}
|
||||
|
||||
if (success) {
|
||||
client.wakuMessages.add(wakuMessageId)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue