summary refs log tree commit diff
path: root/v2/client.ts
diff options
context:
space:
mode:
Diffstat (limited to 'v2/client.ts')
-rw-r--r--v2/client.ts45
1 files changed, 39 insertions, 6 deletions
diff --git a/v2/client.ts b/v2/client.ts
index eedc578..0411425 100644
--- a/v2/client.ts
+++ b/v2/client.ts
@@ -1,7 +1,12 @@
 import { Screen } from "./screen.ts";
+import type { Text } from "./elements.ts";
+import strftime from "./strftime.js";
 
 export let token: string;
 export let account;
+export const connection = new WebSocket("wss://api.meower.org/v0/cloudlink?v=1")
+
+export let home: any[] = [];
 
 let screen: Screen;
 
@@ -10,6 +15,7 @@ export function setScreen(screenN: Screen) {
 }
 
 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: {
@@ -20,12 +26,39 @@ export async function login(username: string, password: string) {
             password
         })
     })).json();
-
+    screen.logs.push(`got auth response (${authr.error ? "error" : "not error"})`)
     token = authr.token;
-    account = authr.account
+    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")}] ${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() {
-    const home = (await (await fetch("https://api.meower.org/home")).json()).autoget;
-    
-}
\ No newline at end of file
+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 homeElem: Text = screen.elements.get("home") as Text;
+    homeElem.text = textHome.join("\n")+"\n";
+    screen.render()
+}
+
+export async function sendHome(post:string) {
+    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()}`))
+}