From 81a850fc598923ad71e45411cd160dafb0ea3d48 Mon Sep 17 00:00:00 2001 From: Midhun Suresh <midhunr@element.io> Date: Sat, 1 Jan 2022 12:18:33 +0530 Subject: [PATCH] Refactor Chatterbox class from main.ts --- src/Chatterbox.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 48 +++++++++-------------------------------------- src/random.ts | 10 ++++++++++ 3 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 src/Chatterbox.ts create mode 100644 src/random.ts diff --git a/src/Chatterbox.ts b/src/Chatterbox.ts new file mode 100644 index 0000000..1e8c687 --- /dev/null +++ b/src/Chatterbox.ts @@ -0,0 +1,48 @@ +import { IChatterboxConfig } from "./types/IChatterboxConfig"; +import { Hydrogen } from "./Hydrogen"; +import { generateRandomString } from "./random"; + + +export class Chatterbox { + private _config: IChatterboxConfig; + private _hydrogen: Hydrogen; + + constructor(config: IChatterboxConfig, root: HTMLDivElement) { + this._config = config; + this._hydrogen = new Hydrogen(this._homeserver, root); + } + + async start(): Promise<void> { + console.log("Checking if session already exists"); + const sessionAlreadyExists = await this._hydrogen.attemptStartWithExistingSession(); + if (sessionAlreadyExists) { + console.log("Starting hydrogen with existing session"); + } else { + console.log("Session does not exist!"); + await this._registerAndLogin(); + } + + console.log("Attempting to mount Timeline"); + await this._hydrogen.mountTimeline(this._roomToJoin); + console.log("Mounted Timeline"); + } + + private async _registerAndLogin(): Promise<void> { + const username = generateRandomString(7); + const password = generateRandomString(10); + console.log( `Attempting to register with username = ${username} and password = ${password}`); + await this._hydrogen.register( username, password, "Chatterbox"); + console.log("Registration done"); + console.log("Attempting to login with same credentials"); + await this._hydrogen.login(username, password); + console.log("Login successful"); + } + + private get _homeserver(): string { + return this._config.homeserver; + } + + private get _roomToJoin(): string { + return this._config.auto_join_room; + } +} diff --git a/src/main.ts b/src/main.ts index 318564b..f697989 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,55 +1,25 @@ -import { Hydrogen } from "./Hydrogen"; +import { Chatterbox } from "./Chatterbox"; import type { IChatterboxConfig } from "./types/IChatterboxConfig"; -async function fetchConfig(): Promise<IChatterboxConfig> { - const root = document.querySelector("#chatterbox") as HTMLDivElement; - if (!root) { - throw new Error("No element with id as 'chatterbox' found!"); - } +const rootDivId = "#chatterbox"; +async function fetchConfig(root: HTMLDivElement): Promise<IChatterboxConfig> { const configLink = root?.dataset.configLink; if (!configLink) { throw new Error("Root element does not have config specified"); } - const config: IChatterboxConfig = await (await fetch(configLink)).json(); return config; } async function main() { - const root = document.querySelector("#chatterbox") as HTMLDivElement; - const { homeserver, auto_join_room } = await fetchConfig(); - const hydrogen = new Hydrogen(homeserver, root); - console.log("Checking if session already exists"); - const sessionAlreadyExists = await hydrogen.attemptStartWithExistingSession(); - if (sessionAlreadyExists) { - console.log("Starting Hydrogen with existing session"); - } - else { - console.log("Session does not exist!"); - const username = generateRandomString(7); - const password = generateRandomString(10); - console.log(`Attempting to register with username = ${username} and password = ${password}`); - await hydrogen.register(username, password, "Chatterbox"); - console.log("Registration done"); - console.log("Attempting to login with same credentials"); - await hydrogen.login(username, password); - console.log("Login successful"); - } - - console.log("Attempting to mount Timeline"); - await hydrogen.mountTimeline(auto_join_room); - console.log("Mounted Timeline"); -} - -function generateRandomString(length: number): string { - let result = ""; - const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - var charactersLength = characters.length; - for (let i = 0; i < length; i++) { - result += characters.charAt(Math.floor(Math.random() * charactersLength)); + const root = document.querySelector(rootDivId) as HTMLDivElement; + if (!root) { + throw new Error("No element with id as 'chatterbox' found!"); } - return result; + const config = await fetchConfig(root); + const chatterbox = new Chatterbox(config, root); + chatterbox.start(); } main(); diff --git a/src/random.ts b/src/random.ts new file mode 100644 index 0000000..0854c1d --- /dev/null +++ b/src/random.ts @@ -0,0 +1,10 @@ +// todo: do we need something better than this? +export function generateRandomString(length: number): string { + let result = ""; + const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + var charactersLength = characters.length; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} -- GitLab