-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
78 lines (67 loc) · 2.36 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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 const connection = maelink.ws
maelink.ws.onopen = () => {
screen?.logs?.push('open')
}
export const home: any[] = [];
let screen: Screen;
export function setScreen(screenN: Screen) {
screen = screenN
}
interface Post {
_id: string,
p: string,
u: string,
e: string,
reply_to: null | string,
post_id: string
}
maelink.on('message', (e) => {
screen.logs.push("INC: " + JSON.stringify(e))
})
maelink.on("post", (post: Post) => {
screen.logs.push("MESSAGE: " + JSON.stringify(post))
home.push(post);
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;
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 maelink.login(username, password)
screen.logs.push(`got auth response (${authr.error ? "error" : "not error"})`)
token = authr.token;
screen.logs.push(`Got token ${token}`);
}
export async function loadHome(screen: Screen) {
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())
screen.render()
}
export function sendHome(post:string) {
screen.logs.push("sendHome ran", home.length.toString())
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()}`))
}