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 { hexToBytes } from 'ethereum-cryptography/utils'
import { Waku, WakuMessage } from 'js-waku' import { Waku, WakuMessage } from 'js-waku'
import { ApplicationMetadataMessage } from '~/protos/application-metadata-message'
import { Account } from './account' import { Account } from './account'
import { Community } from './client/community/community' import { Community } from './client/community/community'
import * as ams from './proto/status/v1/application_metadata_message'
export interface ClientOptions { export interface ClientOptions {
publicKey: string publicKey: string
@ -81,7 +82,7 @@ class Client {
// } // }
public sendMessage = async ( public sendMessage = async (
type: keyof typeof ams.ApplicationMetadataMessage_Type, type: keyof typeof ApplicationMetadataMessage.Type,
payload: Uint8Array, payload: Uint8Array,
contentTopic: string, contentTopic: string,
symKey: Uint8Array symKey: Uint8Array
@ -96,11 +97,11 @@ class Client {
const signature = await this.account.sign(payload) const signature = await this.account.sign(payload)
const message = ams.ApplicationMetadataMessage.encode({ const message = ApplicationMetadataMessage.encode({
type: ams.ApplicationMetadataMessage_Type[type], type: type as ApplicationMetadataMessage.Type,
signature, signature,
payload, payload,
}).finish() })
const wakuMesage = await WakuMessage.fromBytes(message, contentTopic, { const wakuMesage = await WakuMessage.fromBytes(message, contentTopic, {
sigPrivKey: hexToBytes(this.account.privateKey), 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 { hexToBytes } from 'js-waku/build/main/lib/utils'
import difference from 'lodash/difference' 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 { EmojiReaction } from '~/protos/emoji-reaction'
import { ChatMessage } from '../../../protos/chat-message'
import { idToContentTopic } from '../../contentTopic' import { idToContentTopic } from '../../contentTopic'
import { createSymKeyFromPassword } from '../../encryption' import { createSymKeyFromPassword } from '../../encryption'
import { createChannelContentTopics } from './create-channel-content-topics' import { createChannelContentTopics } from './create-channel-content-topics'
@ -214,7 +214,7 @@ export class Community {
this.channelMessages[channelId] = messages this.channelMessages[channelId] = messages
// callback // callback
// todo!: review use of ! // todo!: review use of ! why not just use messages defined above?
this.channelMessagesCallbacks[channelId]?.(this.channelMessages[channelId]!) 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] const chat = this.communityMetadata.chats[chatUuid]
if (!chat) { if (!chat) {
throw new Error('Chat not found') 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 channelContentTopic = idToContentTopic(chatId)
const symKey = await createSymKeyFromPassword(chatId) const symKey = await createSymKeyFromPassword(chatId)
// TODO: protos does not support optional fields // TODO: protos does not support optional fields :-(
// @ts-ignore
const payload = ChatMessage.encode({ const payload = ChatMessage.encode({
clock: BigInt(Date.now()), clock: BigInt(Date.now()),
timestamp: BigInt(Date.now()), timestamp: BigInt(Date.now()),
text: message, // string text,
responseTo: '', // string responseTo: responseTo ?? '',
ensName: '', // string ensName: '',
chatId: chatId, // string chatId,
messageType: MessageType.COMMUNITY_CHAT, messageType: 'COMMUNITY_CHAT',
contentType: ChatMessage.ContentType.TEXT_PLAIN, contentType: ChatMessage.ContentType.TEXT_PLAIN,
// sticker: '', // StickerMessage sticker: { hash: '', pack: 0 },
// image: '', // ImageMessage image: {
// audio: '', // AudioMessage type: 'JPEG',
// community: '', // Uint8Array payload: new 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( await this.client.sendMessage(
@ -296,21 +307,25 @@ export class Community {
const payload = ChatMessage.encode({ const payload = ChatMessage.encode({
clock: BigInt(Date.now()), clock: BigInt(Date.now()),
timestamp: BigInt(Date.now()), timestamp: BigInt(Date.now()),
responseTo: '', // string text: '',
ensName: '', // string responseTo: responseTo ?? '',
chatId: chatId, // string ensName: '',
messageType: MessageType.COMMUNITY_CHAT, chatId,
contentType: ChatMessage.ContentType.IMAGE, messageType: 'COMMUNITY_CHAT',
contentType: ChatMessage.ContentType.TEXT_PLAIN,
sticker: { hash: '', pack: 0 },
image: { image: {
type: image.type, type: image.type,
payload: image.payload, payload: image.payload,
}, },
// sticker: '', // StickerMessage audio: {
// image: '', // ImageMessage type: 'AAC',
// audio: '', // AudioMessage payload: new Uint8Array([]),
// community: '', // Uint8Array durationMs: BigInt(0),
// grant: '', // Uint8Array },
// displayName: '', // string community: new Uint8Array([]),
grant: new Uint8Array([]),
displayName: '',
}) })
await this.client.sendMessage( await this.client.sendMessage(
@ -322,30 +337,30 @@ export class Community {
} }
public sendReaction = async ( public sendReaction = async (
chatUuid: string, chatId: string,
messageId: string, messageId: string,
reaction: EmojiReaction.Type reaction: EmojiReaction.Type
) => { ) => {
const chat = this.communityMetadata.chats[chatUuid] // const chat = this.communityMetadata.chats[chatId]
if (!chat) { // if (!chat) {
throw new Error('Chat not found') // throw new Error('Chat not found')
} // }
// TODO: move to chat instance // TODO: move to chat instance
const chatId = `${this.communityPublicKey}${chatUuid}` // const chatId = `${this.communityPublicKey}${chatUuid}`
const channelContentTopic = idToContentTopic(chatId) const channelContentTopic = idToContentTopic(chatId)
const symKey = await createSymKeyFromPassword(chatId) const symKey = await createSymKeyFromPassword(chatId)
// TODO: protos does not support optional fields :-(
const payload = EmojiReaction.encode({ const payload = EmojiReaction.encode({
clock: BigInt(Date.now()), clock: BigInt(Date.now()),
chatId, chatId: chatId,
messageType: MessageType.COMMUNITY_CHAT, messageType: 'COMMUNITY_CHAT',
messageId,
type: reaction,
// TODO: get message by id and derive state
retracted: false,
grant: new Uint8Array([]), grant: new Uint8Array([]),
messageId,
retracted: false,
type: reaction,
}) })
await this.client.sendMessage( await this.client.sendMessage(
@ -371,8 +386,8 @@ export class Community {
const chatId = `${this.communityPublicKey}${chatUuid}` const chatId = `${this.communityPublicKey}${chatUuid}`
const payload = CommunityRequestToJoin.encode({ const payload = CommunityRequestToJoin.encode({
chatId,
clock: BigInt(Date.now()), clock: BigInt(Date.now()),
chatId,
communityId: hexToBytes(this.communityPublicKey), communityId: hexToBytes(this.communityPublicKey),
ensName: '', ensName: '',
}) })