From bf3e6de2a7cdc10abe9cf5913077dc673196de9f Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Thu, 9 Jun 2022 15:56:43 +0200 Subject: [PATCH] fix sending messages --- packages/status-js/src/client.ts | 11 ++- .../src/client/community/community.ts | 97 +++++++++++-------- 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index 742e9657..aeff3b1b 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -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), diff --git a/packages/status-js/src/client/community/community.ts b/packages/status-js/src/client/community/community.ts index 88e3723e..50b181d9 100644 --- a/packages/status-js/src/client/community/community.ts +++ b/packages/status-js/src/client/community/community.ts @@ -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: '', })