summary refs log tree commit diff
path: root/lib/sd.js
blob: b2c7622f21f1898e11daac77c6843d25121ba20a (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import "https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.js"

export default class SoktDeer {
    /** @type {string} */
    token = '';
    /** @type {boolean} */
    loggedIn = false;
    /** @type {WebSocket} */
    ws;
    /** @type {import("node:events").EventEmitter} */
    wsEvents = new EventEmitter3();
    /** @type {any[]} */
    messages = [];
    constructor(wsUri = "wss://sokt.meltland.dev") {
        console.log('uh 21')
        this.ws = new WebSocket(wsUri);
        this.ws.onmessage = (rdata) => {
            const data = JSON.parse(rdata.data.toString());
            console.info("SD", "INCOMING", data);
            if (data.listener != null) return this.wsEvents.emit(`listener-${data.listener}`)
            if ('command' in data) return this.wsEvents.emit(data.command, data);
            if ('error' in data
                && Object.keys(data).filter(k => !['error', 'code'].includes(k)).length > 0)
                return this.wsEvents.emit(
                    Object.keys(data).filter(k => !['error', 'code'].includes(k))[0],
                    data
                )
            if ('error' in data) return this.wsEvents.emit('error', data);
        }
        this.wsEvents.on('greet', greetp => {
            this.messages = greetp.messages.reverse()
        })
        this.wsEvents.on('new_post', ({ data: post }) => {
            this.messages.push(post)
        })
        this.ws.onopen = () => setInterval(()=>this.ping.call(this), 5000)
    }

    login(username, password) {
        console.log('cred login')
        return new Promise((resolve, reject) => {
            this.ws.send(JSON.stringify({
                command: "login_pswd",
                username,
                password
            }))
            this.wsEvents.once('token', ({token}) => {
                this.token = token;
                resolve(token)
            })
            this.wsEvents.once('error', error => {
                if (error.error) {
                    reject(error.code)
                }
            })
        })
    }

    //TODO - implement this
    loginToken(token, username) {
        console.log('token login')
        return new Promise((resolve, reject) => {
            this.ws.send(JSON.stringify({
                command: "login_token",
                token,
                username
            }))
            this.wsEvents.once('error', error => {
                if (error.error) reject(error.code)
                else resolve()
            })
        })
    }

    getUser(username) {
        return new Promise((resolve, reject) => {
            this.ws.send(JSON.stringify({
                command: "get_user",
                username
            }))
            this.wsEvents.once('user', resp => {
                if (resp.error) reject(resp.code)
                else resolve(resp.user)
            })
        })
    }
    
    post(post) {
        if(!post.replies) post.replies = [];
        if(!post.attachments) post.attachments = [];
        this.ws.send(JSON.stringify({
            command: "post",
            ...post
        }))
    }

    ping() {
        this.ws.send(JSON.stringify({ command: "ping" }))
    }
}