summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitmodules3
-rw-r--r--client.ts90
-rw-r--r--main.ts12
m---------mljs0
-rw-r--r--screen.ts9
-rw-r--r--screen/home.ts14
-rw-r--r--screen/login.ts13
-rw-r--r--screenbuilder.ts7
8 files changed, 92 insertions, 56 deletions
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..612b4f3
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "mljs"]
+	path = mljs
+	url = https://github.com/WlodekM/maelink-js.git
diff --git a/client.ts b/client.ts
index 5f47fb4..93b3dc5 100644
--- a/client.ts
+++ b/client.ts
@@ -1,52 +1,67 @@
 // deno-lint-ignore-file no-explicit-any
-
+import Maelink from "./mljs/main.ts";
 import { Screen } from "./screen.ts";
 import type { Text } from "./elements.ts";
 import strftime from "./strftime.js";
 
+export const maelink = new Maelink()
 export let token: string;
-export let account: any;
-export const connection = new WebSocket("wss://api.meower.org/v0/cloudlink?v=1")
+export const connection = maelink.ws
+console.debug('ass')
+
+maelink.ws.onopen = () => {
+    console.debug('open')
+}
 
-export let home: any[] = [];
+export const home: any[] = [];
 
 let screen: Screen;
 
 export function setScreen(screenN: Screen) {
     screen = screenN
+    // console.log(screen)
+}
+
+interface Post {
+    _id: string,
+    p: string,
+    u: string,
+    e: string,
+    reply_to: null | string,
+    post_id: string
 }
 
+maelink.on('message', (e) => {
+    console.debug(e)
+    screen.logs.push("INC: " + e)
+})
+
+maelink.on("post", (post: Post) => {
+    console.debug('assss')
+    screen.logs.push("POST: " + JSON.stringify(post))
+    home.push(post);
+    console.debug(post, 'uh', home)
+    const textHome: string[] = home.map(p => `[${strftime("%H:%M:%S", new Date(Number(JSON.parse(p.e).t) * 1000))}] ${p.u}: ${p.p}`);
+    const homeElem: Text = screen.elements.get("home") as Text;
+    console.debug(homeElem, screen)
+    if(homeElem) homeElem.text = textHome.join("\n")+"\n";
+    screen.render()        
+})
+
 export async function login(username: string, password: string) {
     screen.logs.push(`logging in as ${username}`)
-    const authr = await (await fetch("https://api.meower.org/auth/login", {
-        method: "post",
-        headers: {
-            "content-type": "application/json"
-        },
-        body: JSON.stringify({
-            username,
-            password
-        })
-    })).json();
+    const authr = await maelink.login(username, password)
     screen.logs.push(`got auth response (${authr.error ? "error" : "not error"})`)
     token = authr.token;
-    account = authr.account;
     screen.logs.push(`Got token ${token}`);
-    connection.addEventListener("message", (ev) => {
-        const data = JSON.parse(ev.data.toString());
-        screen.logs.push("INC: " + JSON.stringify(data))
-        if(data.cmd != "post") return;
-        home.push(data.val);
-        const textHome: string[] = home.map(p => `[${strftime("%H:%M:%S", new Date(p.t.e * 1000))}] ${p.u}: ${p.p}`);
-        const homeElem: Text = screen.elements.get("home") as Text;
-        homeElem.text = textHome.join("\n")+"\n";
-        screen.render()        
-    })
 }
 
 export async function loadHome(screen: Screen) {
-    home = (await (await fetch("https://api.meower.org/home")).json()).autoget.reverse();
-    const textHome: string[] = home.map(p => `[${strftime("%H:%M:%S")}] ${p.u}: ${p.p}`);
+    const homef = (await maelink.fetchMessages(0)).reverse()
+    home.push(...homef)
+    const textHome: string[] = home.map(p => {
+        return `[${strftime("%H:%M:%S", new Date(Number(JSON.parse(p.e).t) * 1000))}] ${p.u}: ${p.p}`
+    });
     const homeElem: Text = screen.elements.get("home") as Text;
     homeElem.text = textHome.join("\n")+"\n";
     screen.logs.push("loadHome ran", home.length.toString())
@@ -55,14 +70,15 @@ export async function loadHome(screen: Screen) {
 
 export function sendHome(post:string) {
     screen.logs.push("sendHome ran", home.length.toString())
-    fetch("https://api.meower.org/home", {
-        method: "POST",
-        headers: {
-            token,
-            "content-type": "application/json"
-        },
-        body: JSON.stringify({
-            content: post
-        })
-    }).then(async r=>screen.logs.push(`Got send response (${r.status} ${r.statusText}) ${await r.text()}`))
+    maelink.sendMessage(post)
+    // fetch("https://api.meower.org/home", {
+    //     method: "POST",
+    //     headers: {
+    //         token,
+    //         "content-type": "application/json"
+    //     },
+    //     body: JSON.stringify({
+    //         content: post
+    //     })
+    // }).then(async r=>screen.logs.push(`Got send response (${r.status} ${r.statusText}) ${await r.text()}`))
 }
diff --git a/main.ts b/main.ts
index 82d54f8..4fe68ff 100644
--- a/main.ts
+++ b/main.ts
@@ -2,9 +2,19 @@
 import LoginScreen from "./screen/login.ts";
 import { build } from "./screenbuilder.ts";
 import readline from 'node:readline';
+import * as client from "./client.ts"
 
 readline.emitKeypressEvents(process.stdin);
 
 if (process.stdin.isTTY) process.stdin.setRawMode(true); // makes the terminal send stdin without the user pressing enter
 
-build(LoginScreen)
\ No newline at end of file
+let screen;
+try {
+    screen = build(LoginScreen, client);
+    client.setScreen(screen)
+} catch (error) {
+    console.error(error);
+    for (const log of screen?.logs ?? []) {
+        console.log(log)
+    }
+}
\ No newline at end of file
diff --git a/mljs b/mljs
new file mode 160000
+Subproject c4320bf10d2ee15510826eafd5d776a9e2b5137
diff --git a/screen.ts b/screen.ts
index 7d47412..69e3e1d 100644
--- a/screen.ts
+++ b/screen.ts
@@ -10,7 +10,7 @@ import process from "node:process";
 const logs: string[] = [];
 
 function onexit() {
-    console.clear()
+    // console.clear()
     console.log("\nQuitting meower CL")
     for (const log of logs) {
         console.log(log)
@@ -22,9 +22,11 @@ export class Screen {
     name: string;
     focusedElementId: string = '';
     logs = logs
+    client;
 
-    constructor(name: string) {
+    constructor(name: string, client: any) {
         this.name = name;
+        this.client = client
     }
 
     call(function_name:string, ...args:any) {
@@ -37,6 +39,7 @@ export class Screen {
     handleKeypress(_chunk: Buffer, key: Key, screen: Screen) {
         const focusableIDs = Object.keys(screen.getFocusable());
         const focusedIndex = focusableIDs.indexOf(screen.focusedElementId);
+        // console.debug(this.client.ws.readyState)
         if (key && key.name == 'escape' || key.name == "c" && key.ctrl) {
             onexit();
             Deno.exit(0);
@@ -81,6 +84,8 @@ export class Screen {
     }
 
     render() {
+        // console.debug('render')
+        // return;
         console.clear()
         process.stdout.write("\u001b[2J") // use an ansi escape code to clear the screen if console.clear doesn't clear fully
         this.elements.forEach(element => {
diff --git a/screen/home.ts b/screen/home.ts
index 0be3ef9..c6f6c19 100644
--- a/screen/home.ts
+++ b/screen/home.ts
@@ -1,14 +1,14 @@
 import { ElemType } from "../screenbuilder.ts";
 import { Screen } from "../screen.ts";
-import type { Input, Element, Text } from "../elements.ts";
-import * as client from "../client.ts"
+import type { Input, Text } from "../elements.ts";
+import process from "node:process";
 
 export default {
     elements: [
         {
             type: ElemType.TextElem,
             id: 'home',
-            data: ["Loading home posts...\n", function (this: Text, text: string) {
+            data: ["Loading home posts...\n", function (this: Text) {
                 const msgInput: Input = this.screen.elements.get("msg-input") as Input;
                 const inputValueHeight = msgInput.value.split("\n").length + 1;
                 const termHeight = process.stdout.rows;
@@ -36,9 +36,9 @@ export default {
         {
             type: ElemType.ButtonElem,
             id: 'done-btn',
-            data: ["Send", async function (this: Screen) {
+            data: ["Send", function (this: Screen) {
                 const msgInput: Input = this.elements.get('msg-input') as Input
-                client.sendHome(msgInput.value);
+                this.client.sendHome(msgInput.value);
                 msgInput.value = ""
                 this.render()
             }]
@@ -47,7 +47,7 @@ export default {
     focus: "msg-input",
     name: 'home',
     onload (screen: Screen) {
-        client.setScreen(screen)
-        client.loadHome(screen)
+        screen.client.setScreen(screen)
+        screen.client.loadHome(screen)
     }
 }
\ No newline at end of file
diff --git a/screen/login.ts b/screen/login.ts
index bc39655..57121bb 100644
--- a/screen/login.ts
+++ b/screen/login.ts
@@ -1,8 +1,8 @@
 import { ElemType } from "../screenbuilder.ts";
 import { Screen } from "../screen.ts";
-import * as client from "../client.ts"
 import { build } from "../screenbuilder.ts";
 import HomeScreen from "./home.ts";
+import { Input } from "../elements.ts";
 
 export default {
     elements: [
@@ -38,15 +38,16 @@ export default {
             type: ElemType.ButtonElem,
             id: 'done-btn',
             data: ["Done", async function (this: Screen) {
-                client.setScreen(this)
+                this.client.setScreen(this)
                 this.off()
                 this.logs.push(`clicked button`)
                 console.clear()
                 console.log("logging in...")
-                //@ts-ignore
-                await client.login(this.elements.get("username-input").value, this.elements.get("password-input").value)
-                build(HomeScreen);
-                client
+                const usernameInput = this.elements.get("username-input") as Input;
+                const passwordInput = this.elements.get("password-input") as Input;
+                await this.client.login(usernameInput.value, passwordInput.value)
+                build(HomeScreen, this.client);
+                this.client
             }]
         }
     ],
diff --git a/screenbuilder.ts b/screenbuilder.ts
index 98af374..ca4ce5d 100644
--- a/screenbuilder.ts
+++ b/screenbuilder.ts
@@ -31,8 +31,8 @@ type Data = {
     onload?: (screen: Screen) => any
 }
 
-export function build(data: Data) {
-    const screen = new Screen(data.name);
+export function build(data: Data, client: any) {
+    const screen = new Screen(data.name, client);
     for (const element of data.elements) {
         if (!element.data) element.data = []
         //@ts-ignore
@@ -40,5 +40,6 @@ export function build(data: Data) {
     }
     if (data.focus) screen.focus(data.focus);
     screen.ready()
-    if (data.onload) data.onload(screen)
+    if (data.onload) data.onload(screen);
+    return screen
 }
\ No newline at end of file