diff options
author | Tor Andersson <tor@ccxvii.net> | 2024-01-11 18:23:06 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2024-01-11 18:23:18 +0100 |
commit | 4f227e1d0ad24aadfc7e1169e4f62f516ec62810 (patch) | |
tree | 60be84eed24682dcbf00161a6dd05bf38351fc1c | |
parent | bbde94883a3c7efb5e3b2e43041e6574661b4d94 (diff) | |
download | server-4f227e1d0ad24aadfc7e1169e4f62f516ec62810.tar.gz |
Send chat message when a user leaves games by deleting their account.
-rw-r--r-- | schema.sql | 3 | ||||
-rw-r--r-- | server.js | 9 |
2 files changed, 11 insertions, 1 deletions
@@ -618,8 +618,9 @@ begin delete from threads where author_id = old.user_id; delete from game_chat where user_id = old.user_id; delete from ratings where user_id = old.user_id; - update games set owner_id = 0 where owner_id = old.user_id; + delete from players where user_id = old.user_id and game_id in (select game_id from games where status <= 1); update players set user_id = 0 where user_id = old.user_id; + update games set owner_id = 0 where owner_id = old.user_id; end; drop trigger if exists trigger_delete_on_threads; @@ -577,6 +577,10 @@ app.get("/delete-account", must_be_logged_in, function (req, res) { res.render("delete_account.pug", { user: req.user }) }) +const SQL_SELECT_GAME_ROLE_FOR_DELETED_USER = SQL(` + select game_id, role from players where user_id = ? and game_id in (select game_id from games where status <= 1) + `) + app.post("/delete-account", must_be_logged_in, function (req, res) { let password = req.body.password // Get full user record including password and salt @@ -584,6 +588,11 @@ app.post("/delete-account", must_be_logged_in, function (req, res) { let hash = hash_password(password, user.salt) if (hash !== user.password) return res.render("delete_account.pug", { user: req.user, flash: "Wrong password!" }) + + let list = SQL_SELECT_GAME_ROLE_FOR_DELETED_USER.all(req.user.user_id) + for (let item of list) + send_chat_message(item.game_id, null, null, `${user.name} (${item.role}) left the game.`) + SQL_DELETE_USER.run(req.user.user_id) return res.send("Goodbye!") }) |