From 50cfb76297dee065ac52bc4b41d4a1d21149aba2 Mon Sep 17 00:00:00 2001 From: dev2-nomo Date: Thu, 16 Nov 2023 14:00:02 +0100 Subject: [PATCH] added create room test added send message test --- src/create_room/create_room.ts | 22 ++++++++++++ src/index.ts | 2 +- src/send_message/send_message.ts | 27 ++++++++++++--- test/create_room_test/create_room.test.ts | 21 ++++++++++++ test/send_message_test/send_message.test.ts | 37 +++++++++++++++++++-- test/syncing/sync_user.test.ts | 2 +- 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 src/create_room/create_room.ts create mode 100644 test/create_room_test/create_room.test.ts diff --git a/src/create_room/create_room.ts b/src/create_room/create_room.ts new file mode 100644 index 0000000..c85e823 --- /dev/null +++ b/src/create_room/create_room.ts @@ -0,0 +1,22 @@ +import axios from "axios"; +const server = 'https://zeniq.chat/'; + +/** + * TODO: document here + */ +export async function nomoCreateRoom(args: Map, accessToken: string): Promise { + const url = server + `_matrix/client/v3/createRoom`; + + try { + const response = await axios.post(url, args, { + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json' + } + }); + return response.data['room_id']; + } catch (error) { + console.error('Error sending message:', error); + return ""; + } +} diff --git a/src/index.ts b/src/index.ts index 02401c1..27c12a8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,6 @@ export {nomoRegisterOrLogin, UserMatrix, nomoLogin, nomoRegister} from "./register_login/register_login" export {nomoListenRoom} from "./listen_room/listen_room" export {nomoJoinRoom} from "./join_room/join_room" -export {nomoSendMessage} from "./send_message/send_message" +export {nomoSendMessage, SendMessageArgs} from "./send_message/send_message" export {nomoSyncUser} from "./syncing/sync_user" export {nomoCreateFilter} from "./syncing/create_filter" \ No newline at end of file diff --git a/src/send_message/send_message.ts b/src/send_message/send_message.ts index 61a9e39..415f55c 100644 --- a/src/send_message/send_message.ts +++ b/src/send_message/send_message.ts @@ -1,12 +1,29 @@ - +import axios from "axios"; +const server = 'https://zeniq.chat/'; export interface SendMessageArgs { - room: string; - content: string; + roomId: string; + content: Map; + transationID: string; + eventType: string; } /** * TODO: document here */ -export async function nomoSendMessage(args: SendMessageArgs) { - console.log("nomoSendMessage not implemented"); +export async function nomoSendMessage(args: SendMessageArgs, accessToken: string): Promise { + let { roomId, content, transationID, eventType } = args; + const url = server + `_matrix/client/v3/rooms/${roomId}/send/${eventType}/${transationID}`; + + try { + const response = await axios.put(url, content, { + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json' + } + }); + return response.data['event_id']; + } catch (error) { + console.error('Error sending message:', error); + return ""; + } } diff --git a/test/create_room_test/create_room.test.ts b/test/create_room_test/create_room.test.ts new file mode 100644 index 0000000..5f2dc61 --- /dev/null +++ b/test/create_room_test/create_room.test.ts @@ -0,0 +1,21 @@ +import { nomoRegisterOrLogin, UserMatrix, } from "../../src"; +import { nomoCreateRoom } from "../../src/create_room/create_room"; +import { generateRandomString } from "../test_util"; + +test("create room", async () => { + const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe"; + const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic); + const args: Map = new Map + { + [ + ['name', generateRandomString(25)], + ["preset", "private_chat",], + ['room_alias_name', generateRandomString(10)] + ] + }; + let roomId = await nomoCreateRoom(args, userMatrix.access_token); + console.log(roomId); + if (roomId.length == 0) { + fail("no roomId"); + } +}); diff --git a/test/send_message_test/send_message.test.ts b/test/send_message_test/send_message.test.ts index dc31d3f..27fd218 100644 --- a/test/send_message_test/send_message.test.ts +++ b/test/send_message_test/send_message.test.ts @@ -1,5 +1,38 @@ -import {nomoSendMessage} from "../../src"; +import { nomoSendMessage, nomoRegisterOrLogin, UserMatrix, SendMessageArgs } from "../../src"; +import { nomoCreateRoom } from "../../src/create_room/create_room"; +import { generateRandomString } from "../test_util"; test("send message", async () => { - await nomoSendMessage({content: "foo", room: "bar"}); + const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe"; + const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic); + const args: Map = new Map + { + [ + ['name', generateRandomString(25)], + ["preset", "private_chat",], + ['room_alias_name', generateRandomString(10)] + ] + }; + let roomId = await nomoCreateRoom(args, userMatrix.access_token); + if (roomId.length == 0) { + fail("no roomId"); + } + const contentMap: Map = new Map + { + [ + ['body', generateRandomString(25)], + ["msgtype", "m.text"], + ] + }; + const testMessage: SendMessageArgs = + { + content: contentMap, + roomId: roomId, + transationID: generateRandomString(200), + eventType: "m.room.message", + }; + let eventId = await nomoSendMessage(testMessage, userMatrix.access_token); + if (eventId.length == 0) { + fail("eventId empty"); + } }); diff --git a/test/syncing/sync_user.test.ts b/test/syncing/sync_user.test.ts index aac817b..c03e7d2 100644 --- a/test/syncing/sync_user.test.ts +++ b/test/syncing/sync_user.test.ts @@ -4,6 +4,6 @@ test("sync user", async () => { const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe"; const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic); // const filterID: string = await nomoCreateFilter(userMatrix.access_token, userMatrix.user_id); - // await nomoSyncUser(userMatrix.access_token, "", filterID, "online", false); + // await nomoSyncUser(userMatrix.access_token, "", '', "online", false); // expect(userMatrix.user_id).toBe("@0x3f0e8cF0c6eb9789348541D9D0Ce4ac847277e9B:zeniq.chat"); }, 10000); \ No newline at end of file