fix sending messages

This commit is contained in:
Pavel Prichodko 2022-06-09 15:56:43 +02:00 committed by Felicio Mununga
parent 6ce1344af1
commit bf3e6de2a7
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 62 additions and 46 deletions

View File

@ -18,9 +18,10 @@
import { hexToBytes } from 'ethereum-cryptography/utils'
import { Waku, WakuMessage } from 'js-waku'
import { ApplicationMetadataMessage } from '~/protos/application-metadata-message'
import { Account } from './account'
import { Community } from './client/community/community'
import * as ams from './proto/status/v1/application_metadata_message'
export interface ClientOptions {
publicKey: string
@ -81,7 +82,7 @@ class Client {
// }
public sendMessage = async (
type: keyof typeof ams.ApplicationMetadataMessage_Type,
type: keyof typeof ApplicationMetadataMessage.Type,
payload: Uint8Array,
contentTopic: string,
symKey: Uint8Array
@ -96,11 +97,11 @@ class Client {
const signature = await this.account.sign(payload)
const message = ams.ApplicationMetadataMessage.encode({
type: ams.ApplicationMetadataMessage_Type[type],
const message = ApplicationMetadataMessage.encode({
type: type as ApplicationMetadataMessage.Type,
signature,
payload,
}).finish()
})
const wakuMesage = await WakuMessage.fromBytes(message, contentTopic, {
sigPrivKey: hexToBytes(this.account.privateKey),

View File

@ -2,10 +2,10 @@ import { waku_message } from 'js-waku'
import { hexToBytes } from 'js-waku/build/main/lib/utils'
import difference from 'lodash/difference'
import { CommunityRequestToJoin, MessageType } from '~/protos/communities'
import { ChatMessage } from '~/protos/chat-message'
import { CommunityRequestToJoin } from '~/protos/communities'
import { EmojiReaction } from '~/protos/emoji-reaction'
import { ChatMessage } from '../../../protos/chat-message'
import { idToContentTopic } from '../../contentTopic'
import { createSymKeyFromPassword } from '../../encryption'
import { createChannelContentTopics } from './create-channel-content-topics'
@ -214,7 +214,7 @@ export class Community {
this.channelMessages[channelId] = messages
// callback
// todo!: review use of !
// todo!: review use of ! why not just use messages defined above?
this.channelMessagesCallbacks[channelId]?.(this.channelMessages[channelId]!)
}
@ -243,34 +243,45 @@ export class Community {
}
}
public sendTextMessage = async (chatUuid: string, message: string) => {
public sendTextMessage = async (
chatUuid: string,
text: string,
responseTo?: string
) => {
const chat = this.communityMetadata.chats[chatUuid]
if (!chat) {
throw new Error('Chat not found')
}
// TODO: move to chat instance
const chatId = `${this.communityPublicKey}${chatUuid}`
const channelContentTopic = idToContentTopic(chatId)
const symKey = await createSymKeyFromPassword(chatId)
// TODO: protos does not support optional fields
// @ts-ignore
// TODO: protos does not support optional fields :-(
const payload = ChatMessage.encode({
clock: BigInt(Date.now()),
timestamp: BigInt(Date.now()),
text: message, // string
responseTo: '', // string
ensName: '', // string
chatId: chatId, // string
messageType: MessageType.COMMUNITY_CHAT,
text,
responseTo: responseTo ?? '',
ensName: '',
chatId,
messageType: 'COMMUNITY_CHAT',
contentType: ChatMessage.ContentType.TEXT_PLAIN,
// sticker: '', // StickerMessage
// image: '', // ImageMessage
// audio: '', // AudioMessage
// community: '', // Uint8Array
// grant: '', // Uint8Array
// displayName: '', // string
sticker: { hash: '', pack: 0 },
image: {
type: 'JPEG',
payload: new Uint8Array([]),
},
audio: {
type: 'AAC',
payload: new Uint8Array([]),
durationMs: BigInt(0),
},
community: new Uint8Array([]),
grant: new Uint8Array([]),
displayName: '',
})
await this.client.sendMessage(
@ -296,21 +307,25 @@ export class Community {
const payload = ChatMessage.encode({
clock: BigInt(Date.now()),
timestamp: BigInt(Date.now()),
responseTo: '', // string
ensName: '', // string
chatId: chatId, // string
messageType: MessageType.COMMUNITY_CHAT,
contentType: ChatMessage.ContentType.IMAGE,
text: '',
responseTo: responseTo ?? '',
ensName: '',
chatId,
messageType: 'COMMUNITY_CHAT',
contentType: ChatMessage.ContentType.TEXT_PLAIN,
sticker: { hash: '', pack: 0 },
image: {
type: image.type,
payload: image.payload,
},
// sticker: '', // StickerMessage
// image: '', // ImageMessage
// audio: '', // AudioMessage
// community: '', // Uint8Array
// grant: '', // Uint8Array
// displayName: '', // string
audio: {
type: 'AAC',
payload: new Uint8Array([]),
durationMs: BigInt(0),
},
community: new Uint8Array([]),
grant: new Uint8Array([]),
displayName: '',
})
await this.client.sendMessage(
@ -322,30 +337,30 @@ export class Community {
}
public sendReaction = async (
chatUuid: string,
chatId: string,
messageId: string,
reaction: EmojiReaction.Type
) => {
const chat = this.communityMetadata.chats[chatUuid]
// const chat = this.communityMetadata.chats[chatId]
if (!chat) {
throw new Error('Chat not found')
}
// if (!chat) {
// throw new Error('Chat not found')
// }
// TODO: move to chat instance
const chatId = `${this.communityPublicKey}${chatUuid}`
// const chatId = `${this.communityPublicKey}${chatUuid}`
const channelContentTopic = idToContentTopic(chatId)
const symKey = await createSymKeyFromPassword(chatId)
// TODO: protos does not support optional fields :-(
const payload = EmojiReaction.encode({
clock: BigInt(Date.now()),
chatId,
messageType: MessageType.COMMUNITY_CHAT,
messageId,
type: reaction,
// TODO: get message by id and derive state
retracted: false,
chatId: chatId,
messageType: 'COMMUNITY_CHAT',
grant: new Uint8Array([]),
messageId,
retracted: false,
type: reaction,
})
await this.client.sendMessage(
@ -371,8 +386,8 @@ export class Community {
const chatId = `${this.communityPublicKey}${chatUuid}`
const payload = CommunityRequestToJoin.encode({
chatId,
clock: BigInt(Date.now()),
chatId,
communityId: hexToBytes(this.communityPublicKey),
ensName: '',
})