summaryrefslogtreecommitdiff
path: root/static/src/chat.md
blob: 56be112731256f61b02a33feff34fc27ec427b97 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
---
layout: page
---

<script async type="module" src="/assets/api.js"></script>

<style>
    #messages {
        max-height: 65vh;
        overflow: auto;
        padding-right: 2rem;
    }

    #messages .message {
        border: 1px solid #AAA;
        border-radius: 10px;
        margin-bottom: 1rem;
        padding: 2rem;
        overflow: auto;
    }

    #messages .message .title {
        font-weight: bold;
        font-size: 120%;
    }

    #messages .message .secondaryTitle {
        font-family: monospace;
        color: #CCC;
    }

    #messages .message p {
        font-family: monospace;
        margin: 1rem 0 0 0;
    }

</style>

<div id="messages"></div>

<span id="fail" style="color: red;"></span>

<script>

const messagesEl = document.getElementById("messages");

let messagesScrolledToBottom = true;
messagesEl.onscroll = () => {
    const el = messagesEl;
    messagesScrolledToBottom = el.scrollHeight == el.scrollTop + el.clientHeight;
};

function renderMessages(msgs) {

    messagesEl.innerHTML = '';

    msgs.forEach((msg) => {
        const el = document.createElement("div");
        el.className = "row message"

        const elWithTextContents = (tag, body) => {
            const el = document.createElement(tag);
            el.appendChild(document.createTextNode(body));
            return el;
        };

        const titleEl = document.createElement("div");
        titleEl.className = "title";
        el.appendChild(titleEl);

        const userNameEl = elWithTextContents("span", msg.userID.name);
        titleEl.appendChild(userNameEl);

        const secondaryTitleEl = document.createElement("div");
        secondaryTitleEl.className = "secondaryTitle";
        el.appendChild(secondaryTitleEl);

        const dt = new Date(msg.createdAt*1000);
        const dtStr
            = `${dt.getFullYear()}-${dt.getMonth()+1}-${dt.getDate()}`
            + ` ${dt.getHours()}:${dt.getMinutes()}:${dt.getSeconds()}`;

        const userIDEl = elWithTextContents("span", `userID:${msg.userID.id} @ ${dtStr}`);
        secondaryTitleEl.appendChild(userIDEl);

        const bodyEl = document.createElement("p");

        const bodyParts = msg.body.split("\n");
        for (const i in bodyParts) {
            if (i > 0) bodyEl.appendChild(document.createElement("br"));
            bodyEl.appendChild(document.createTextNode(bodyParts[i]));
        }

        el.appendChild(bodyEl);

        messagesEl.appendChild(el);
    });
}


(async () => {

    const failEl = document.getElementById("fail");
    setErr = (msg) => failEl.innerHTML = `${msg} (please refresh the page to retry)`;

    try {

        const api = await import("/assets/api.js");

        const history = await api.call("/api/chat/global/history");
        const msgs = history.messages;

        // history returns msgs in time descending, but we display them in time
        // ascending.
        msgs.reverse()

        const sinceID = (msgs.length > 0) ?  msgs[msgs.length-1].id : "";

        const ws = await api.ws("/api/chat/global/listen", {
            params: { sinceID },
        });

        while (true) {
            renderMessages(msgs);

            // If the user was previously scrolled to the bottom then keep them
            // there.
            if (messagesScrolledToBottom) {
                messagesEl.scrollTop = messagesEl.scrollHeight;
            }

            const msg = await ws.next();
            msgs.push(msg.message);
            renderMessages(msgs);
        }


    } catch (e) {
        e = `Failed to fetch message history: ${e}`
        setErr(e);
        console.error(e);
        return;
    }

})()

</script>

<style>
#append {
    border: 1px dashed #AAA;
    border-radius: 10px;
    padding: 2rem;
}

#append #appendBody {
    font-family: monospace;
}

#append #appendStatus {
    color: red;
}

</style>

<form id="append">
    <h5>New Message</h5>
    <div class="row">
        <div class="columns four">
            <input class="u-full-width" placeholder="Name" id="appendName" type="text" />
            <input class="u-full-width" placeholder="Secret" id="appendSecret" type="password" />
        </div>
        <div class="columns eight">
            <p>
                Your name is displayed alongside your message.

                Your name+secret is used to generate your userID, which is also
                displayed alongside your message.

                Other users can validate two messages are from the same person
                by comparing the messages' userID.
            </p>
        </div>
    </div>
    <div class="row">
        <div class="columns twelve">
            <textarea
                style="font-family: monospace"
                id="appendBody"
                class="u-full-width"
                placeholder="Well thought out statement goes here..."
                ></textarea>
        </div>
    </div>
    <div class="row">
        <div class="columns four">
            <input class="u-full-width button-primary" id="appendSubmit" type="button" value="Submit" />
        </div>
    </div>
    <span id="appendStatus"></span>
</form>

<script>

const append = document.getElementById("append");
const appendName = document.getElementById("appendName");
const appendSecret = document.getElementById("appendSecret");
const appendBody = document.getElementById("appendBody");
const appendSubmit = document.getElementById("appendSubmit");
const appendStatus = document.getElementById("appendStatus");

appendSubmit.onclick = async () => {

    const appendSubmitOrigValue = appendSubmit.value;

    appendSubmit.disabled = true;
    appendSubmit.className = "";
    appendSubmit.value = "Please hold...";

    appendStatus.innerHTML = '';

    try {

        const api = await import("/assets/api.js");

        await api.call('/api/chat/global/append', {
            body: {
                name: appendName.value,
                password: appendSecret.value,
                body: appendBody.value,
            },
            requiresPow: true,
        });

        appendBody.value = '';

    } catch (e) {

        appendStatus.innerHTML = e;

    } finally {
        appendSubmit.disabled = false;
        appendSubmit.className = "button-primary";
        appendSubmit.value = appendSubmitOrigValue;
    }
};

</script>