aboutsummaryrefslogtreecommitdiff
path: root/main.js
diff options
context:
space:
mode:
authorcurly <curlybryce@protonmail.com>2023-02-15 11:55:08 -0700
committercurly <curlybryce@protonmail.com>2023-02-15 11:55:08 -0700
commit8e67c82dc8f30155ababb3fcc146e6bafc895619 (patch)
treec919f50cf8655104c1417c558c5650079b469caa /main.js
parent87120e5df6b4c218e12a904e7723c2c2b5674684 (diff)
downloadrocket_test_ui-8e67c82dc8f30155ababb3fcc146e6bafc895619.tar.gz
rocket_test_ui-8e67c82dc8f30155ababb3fcc146e6bafc895619.tar.bz2
rocket_test_ui-8e67c82dc8f30155ababb3fcc146e6bafc895619.zip
delete messages
Diffstat (limited to 'main.js')
-rw-r--r--main.js42
1 files changed, 30 insertions, 12 deletions
diff --git a/main.js b/main.js
index 7c2af08..1e075d1 100644
--- a/main.js
+++ b/main.js
@@ -3,8 +3,6 @@ const users = new Map();
let sent = false;
const host = location.protocol + "//" + location.host;
-// const xhttp_worker = new Worker("xhttp.js");
-
function xhttp_request(location) {
let f = fetch(`${host}/api/${location}`);
@@ -12,9 +10,9 @@ function xhttp_request(location) {
}
-function xhttp_post(location, json) {
+function xhttp_send(method, location, json) {
let f = fetch(`${host}/api/${location}`, {
- method: "POST",
+ method: method,
headers: {"Content-Type": "application/json"},
body: json
})
@@ -32,7 +30,11 @@ async function format_message(m) {
m.message = "*deleted*"
sclass = "deleted"
}
- s = `<span title="${m.id}" class="message ${sclass}" id="${m.id}">${format_date(m.date)}|${sender}: ${m.message}</span>`
+ s = `<span id="${m.id}" class="message ${sclass}"><p title="${m.id}" class="messagetext">${format_date(m.date)}|${sender}: ${m.message}</p>
+ <span class="message_buttons">
+ <button><i class="fa fa-reply" aria-hidden="true"></i></button>
+ <button onclick="delete_message(${m.id})"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
+ </span></span>`
return s
}
@@ -54,6 +56,21 @@ function format_date(d) {
return `${hours}:${minutes}:${seconds}`;
}
+function delete_message(id) {
+ xhttp_send("DELETE", "delete_message", JSON.stringify(id)).then((response) => {
+ if (response.Ok == undefined) {
+ alert("Could not delete message")
+ } else {
+ let old_msg = messages.get(id);
+ old_msg.message = "*deleted*";
+ old_msg.deleted = true;
+ messages.set(id, old_msg);
+
+ chatwindow(null).then();
+ }
+ })
+}
+
function send() {
var input = document.getElementById("input").value;
let user_id = get_user_from_name().then((user_id) => {
@@ -64,7 +81,7 @@ function send() {
let message = JSON.stringify({"sender": user_id, "message": input})
- var id = xhttp_post("send_message", message).then((id) => {
+ var id = xhttp_send("POST", "send_message", message).then((id) => {
get_message(id.Ok);
document.getElementById("input").value = ""
@@ -88,7 +105,6 @@ function get_newest_cache_id() {
}
}
-// Returns message list
function get_newest_messages() {
xhttp_request("get_message_id_newest").then(async (newest_id) => {
let newest_cache_id = get_newest_cache_id();
@@ -157,7 +173,6 @@ async function get_message_list(oldest, newest) {
return req
}
-// Returns dom
function get_message(id) {
let cached = messages.get(id)
if (cached != undefined) {
@@ -185,7 +200,7 @@ async function get_user_from_name() {
}
- let user = await xhttp_post("get_user_by_name", JSON.stringify(v));
+ let user = await xhttp_send("POST", "get_user_by_name", JSON.stringify(v));
if (user.Ok) {
users.set(user.id, user)
return user.Ok.id
@@ -195,7 +210,7 @@ async function get_user_from_name() {
}
async function create_user(username) {
- let response = await xhttp_post("create_user", JSON.stringify(username));
+ let response = await xhttp_send("POST", "create_user", JSON.stringify(username));
if (response.Ok != undefined) {
await get_user_name(response.Ok);
return response.Ok
@@ -221,14 +236,13 @@ async function get_user_name(id) {
return user.username
}
-// Returns dom
function to_dom(x) {
// Pretty hacky if I do say so myself
let dom = new DOMParser().parseFromString(x, "text/html").firstChild.lastChild.firstChild;
return dom
}
-async function chatwindow(hash = undefined) {
+async function chatwindow(hash) {
let window = document.getElementById("chatwindow");
let pre_scroll = window.scrollHeight - window.clientHeight;
window.innerHTML = "";
@@ -261,6 +275,10 @@ async function chatwindow(hash = undefined) {
window.appendChild(dom);
}
+
+ if (hash == null) {
+ return
+ }
if (window.scrollTop == pre_scroll) {
if (latest_v != undefined && hash == undefined) {