handle replies

This commit is contained in:
Felicio Mununga 2022-06-08 15:02:16 +02:00
parent 89279dbfbb
commit fb07201081
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
3 changed files with 41 additions and 17 deletions

View File

@ -1,19 +1,19 @@
// todo: replies; normalize messages (e.g. replies) prior returning // todo?: already received (by messageId or event)
// todo: ignore not found
// todo: handle updates together with fetching history (chat messages)
// todo: handle diff waku messages on diff topics
// todo: tests // todo: tests
// todo?: use clock for sorting // todo?: use clock for sorting
// todo: handle diff waku messages on diff topics
// todo: handle disconnections; no messages after sleep; libp2p; // todo: handle disconnections; no messages after sleep; libp2p;
// todo: identities/members? // todo: identities/members?
// todo: validate sig // todo?: observe channels
// todo: observer contact updates
// todo: observer channels
// todo?: rename channels to chats // todo?: rename channels to chats
// todo: change relative import paths
// todo?: multiple communityCallback // todo?: multiple communityCallback
// todo?: call onChannel* separately // todo?: call onChannel* separately
// todo?: ignore messages of not yet approved users
// todo?: ignore messages with invalid signature
import { Waku } from 'js-waku' import { Waku } from 'js-waku'
@ -45,6 +45,7 @@ class Client {
default: false, default: false,
peers: [ peers: [
'/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS', '/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS',
// '/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ',
], ],
}, },
}) })

View File

@ -21,6 +21,7 @@ export type MessageType = ChatMessage & {
pinned: boolean pinned: boolean
reactions: Reactions reactions: Reactions
channelId: string channelId: string
responseToMessage?: Omit<MessageType, 'responseToMessage'>
} }
export class Community { export class Community {

View File

@ -69,17 +69,13 @@ export function handleChannelChatMessage(
const messageId = payloadToId( const messageId = payloadToId(
decodedProtocol.publicMessage, decodedProtocol.publicMessage,
// fixme!: replace for recoverPublicKeyFromMetadata
wakuMessage.signaturePublicKey wakuMessage.signaturePublicKey
) )
const channelId = getChannelId(decodedPayload.chatId) const channelId = getChannelId(decodedPayload.chatId)
const _messages = messages[channelId] || [] const _messages = messages[channelId] || []
// already received
if (_messages.find(message => message.messageId === messageId)) {
break
}
const message = mapChatMessage(decodedPayload, { messageId, channelId }) const message = mapChatMessage(decodedPayload, { messageId, channelId })
// findIndexLeft // findIndexLeft
@ -87,9 +83,16 @@ export function handleChannelChatMessage(
// new Date(Number(timestamp)) > new Date(Number(message.timestamp)) // new Date(Number(timestamp)) > new Date(Number(message.timestamp))
// }) // })
// findIndexRight // findIndexRight
let index = _messages.length let messageIndex = _messages.length
while (index >= 0) { while (messageIndex > 0) {
const _message = _messages[index - 1] const _message = _messages[messageIndex - 1]
// // already received
// if (_message.messageId === messageId) {
// messageIndex = -1
// break
// }
if ( if (
new Date(Number(_message.timestamp)) <= new Date(Number(_message.timestamp)) <=
@ -98,10 +101,29 @@ export function handleChannelChatMessage(
break break
} }
index-- messageIndex--
} }
_messages.splice(index, 0, message) // // already received
// if (messageIndex < 0) {
// break
// }
// replied
let responsedToMessageIndex = _messages.length
while (--responsedToMessageIndex >= 0) {
const _message = _messages[responsedToMessageIndex]
if (_message.messageId === message.responseTo) {
break
}
}
if (responsedToMessageIndex >= 0) {
message.responseToMessage = _messages[responsedToMessageIndex]
}
_messages.splice(messageIndex, 0, message)
result = _messages result = _messages