summary refs log tree commit diff
path: root/client.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client.ts')
-rw-r--r--client.ts90
1 files changed, 53 insertions, 37 deletions
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()}`))
 }