diff --git a/src/Chatterbox.ts b/src/Chatterbox.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e8c687351c010db7006943c4986c80d45afc638 --- /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 318564b0a2e6de456e01fdd209dd0be5863fc809..f6979894e71404cc4ac2d435d77a90044da26207 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 0000000000000000000000000000000000000000..0854c1d61fb609243019fe688488a1d484fd9602 --- /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; +}