From 70289448796f6649d74a2ec22b300c3b0075b762 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 9 Nov 2021 22:33:53 +0100 Subject: Show user profiles. --- server.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/sql/schema.txt | 1 + views/forum_thread.ejs | 3 ++- views/forum_view.ejs | 9 ++++++--- views/games.ejs | 4 ++-- views/info.ejs | 9 ++++++--- views/message_inbox.ejs | 10 ++++------ views/message_outbox.ejs | 15 +++++++++----- views/profile.ejs | 15 +++++++------- views/users.ejs | 2 +- 10 files changed, 92 insertions(+), 28 deletions(-) diff --git a/server.js b/server.js index f8ab0c3..56f6de5 100644 --- a/server.js +++ b/server.js @@ -129,6 +129,18 @@ function humanize(rows) { } } +function linkify_player_names(games) { + for (let i = 0; i < games.length; ++i) { + let game = games[i]; + if (game.player_names) { + game.player_names = game.player_names + .split(", ") + .map(x => `${x}`) + .join(", "); + } + } +} + function humanize_one(row) { row.ctime = human_date(row.ctime); row.mtime = human_date(row.mtime); @@ -192,6 +204,8 @@ const sql_subscribe = db.prepare("UPDATE users SET notifications = 1 WHERE user_ const sql_unsubscribe = db.prepare("UPDATE users SET notifications = 0 WHERE user_id = ?"); const sql_count_unread_messages = db.prepare("SELECT COUNT(*) FROM messages WHERE to_id = ? AND read = 0 AND deleted_from_inbox = 0").pluck(); +const sql_fetch_user_by_name = db.prepare("SELECT * FROM users WHERE user_id = ? OR name = ?"); +const sql_fetch_user_by_id = db.prepare("SELECT * FROM users WHERE user_id = ?"); passport.serializeUser(function (user, done) { return done(null, user.user_id); @@ -346,6 +360,12 @@ app.get('/change_mail', must_be_logged_in, function (req, res) { res.render('change_mail.ejs', { user: req.user, message: req.flash('message') }); }); +app.get('/change_about', must_be_logged_in, function (req, res) { + LOG(req, "GET /change_about"); + let about = sql_fetch_user_by_id.get(req.user.user_id).about; + res.render('change_about.ejs', { user: req.user, about: about || "", message: req.flash('message') }); +}); + app.get('/subscribe', must_be_logged_in, function (req, res) { LOG(req, "GET /subscribe"); sql_subscribe.run(req.user.user_id); @@ -498,6 +518,7 @@ const sql_change_name = db.prepare("UPDATE users SET name = ? WHERE user_id = ?" const sql_is_mail_taken = db.prepare("SELECT EXISTS ( SELECT 1 FROM users WHERE mail = ? )").pluck(); const sql_change_mail = db.prepare("UPDATE users SET mail = ? WHERE user_id = ?"); +const sql_change_about = db.prepare("UPDATE users SET about = ? WHERE user_id = ?"); app.post('/change_name', must_be_logged_in, function (req, res) { try { @@ -543,6 +564,18 @@ app.post('/change_mail', must_be_logged_in, function (req, res) { } }); +app.post('/change_about', must_be_logged_in, function (req, res) { + try { + LOG(req, "POST /change_about", req.user.name); + sql_change_about.run(req.body.about, req.user.user_id); + return res.redirect('/profile'); + } catch (err) { + console.log(err); + req.flash('message', err.message); + return res.redirect('/change_about'); + } +}); + /* * GAME LOBBY */ @@ -661,6 +694,7 @@ app.get('/profile', must_be_logged_in, function (req, res) { let avatar = get_avatar(req.user.mail); let games = QUERY_LIST_GAMES_OF_USER.all({user_id: req.user.user_id}); humanize(games); + linkify_player_names(games); let open_games = games.filter(game => game.status === 0); let active_games = games.filter(game => game.status === 1); let finished_games = games.filter(game => game.status === 2); @@ -682,6 +716,7 @@ app.get('/info/:title_id', function (req, res) { if (req.isAuthenticated()) { let games = QUERY_LIST_GAMES_OF_TITLE.all({user_id: req.user.user_id, title_id: title_id}); humanize(games); + linkify_player_names(games); let open_games = games.filter(game => game.status === 0); let active_games = games.filter(game => game.status === 1); let finished_games = games.filter(game => game.status === 2); @@ -1486,6 +1521,15 @@ app.get('/games', must_be_logged_in, function (req, res) { }); }); +app.get('/user/:who_id', function (req, res) { + LOG(req, "GET /user/" + req.params.who_id); + let who = sql_fetch_user_by_name.get(req.params.who_id, req.params.who_id); + who.avatar = get_avatar(who.mail); + who.ctime = human_date(who.ctime); + who.atime = human_date(who.atime); + res.render('user.ejs', { user: req.user, who: who, message: req.flash('message') }); +}); + // FORUM const FORUM_PAGE_SIZE = 15; @@ -1621,6 +1665,7 @@ app.post('/forum/reply/:thread_id', must_be_logged_in, function (req, res) { }); // MESSAGES + const MESSAGE_GET_USER = db.prepare("SELECT user_id, name, mail, notifications FROM users WHERE name = ?"); const MESSAGE_GET_USER_ID = db.prepare("SELECT user_id FROM users WHERE name = ?").pluck(); const MESSAGE_GET_USER_NAME = db.prepare("SELECT name FROM users WHERE user_id = ?").pluck(); @@ -1642,6 +1687,7 @@ const MESSAGE_SEND = db.prepare("INSERT INTO messages ( from_id, to_id, subject, const MESSAGE_MARK_READ = db.prepare("UPDATE messages SET read = 1 WHERE message_id = ?"); const MESSAGE_DELETE_INBOX = db.prepare("UPDATE messages SET deleted_from_inbox = 1 WHERE message_id = ? AND to_id = ?"); const MESSAGE_DELETE_OUTBOX = db.prepare("UPDATE messages SET deleted_from_outbox = 1 WHERE message_id = ? AND from_id = ?"); +const MESSAGE_DELETE_ALL_OUTBOX = db.prepare("UPDATE messages SET deleted_from_outbox = 1 WHERE from_id = ?"); app.get('/inbox', must_be_logged_in, function (req, res) { LOG(req, "GET /inbox"); @@ -1770,3 +1816,9 @@ app.get('/message/delete/:message_id', must_be_logged_in, function (req, res) { MESSAGE_DELETE_OUTBOX.run(message_id, req.user.user_id); res.redirect('/inbox'); }); + +app.get('/outbox/delete', must_be_logged_in, function (req, res) { + LOG(req, "POST /outbox/delete"); + MESSAGE_DELETE_ALL_OUTBOX.run(req.user.user_id); + res.redirect('/outbox'); +}); diff --git a/tools/sql/schema.txt b/tools/sql/schema.txt index cd1a7ee..6344470 100644 --- a/tools/sql/schema.txt +++ b/tools/sql/schema.txt @@ -2,6 +2,7 @@ CREATE TABLE IF NOT EXISTS users ( user_id INTEGER PRIMARY KEY, name TEXT UNIQUE COLLATE NOCASE, mail TEXT UNIQUE COLLATE NOCASE, + about TEXT, password TEXT, salt TEXT, ctime TIMESTAMP, diff --git a/views/forum_thread.ejs b/views/forum_thread.ejs index fad8829..0041f20 100644 --- a/views/forum_thread.ejs +++ b/views/forum_thread.ejs @@ -2,6 +2,7 @@ - <% threads.forEach((row) => { %>
SubjectAuthor -RepliesTime +
SubjectAuthor +RepliesTime
<%= row.subject %> -<%= row.author_name %> +<%= row.author_name %> <%= row.reply_count %> <%= row.mtime %> <% }); %> diff --git a/views/games.ejs b/views/games.ejs index e25df1f..b74dbba 100644 --- a/views/games.ejs +++ b/views/games.ejs @@ -12,7 +12,7 @@ td.nowrap a { color: black; text-decoration: none; } <%= row.game_id %> <%= row.title_name %> <%= row.scenario %> -<%= row.player_names || row.owner_name %> +<%- row.player_names || row.owner_name %> <%= row.description %> <%= row.ctime %> Join @@ -30,7 +30,7 @@ td.nowrap a { color: black; text-decoration: none; } <%= row.game_id %> <%= row.title_name %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.mtime %> "><%= row.active_role %> diff --git a/views/info.ejs b/views/info.ejs index 778974e..159e732 100644 --- a/views/info.ejs +++ b/views/info.ejs @@ -1,4 +1,7 @@ <%- include('header', { title: title.title_name, refresh: (user ? 300 : 0) }) %> + <%- include('../public/' + title.title_id + '/about.html') %>

@@ -15,7 +18,7 @@ Read more about the game on

<%= row.game_id %> <%= row.scenario %> -<%= row.player_names || row.owner_name %> +<%- row.player_names || `${row.owner_name}` %> <%= row.description %> <%= row.ctime %> Join @@ -35,7 +38,7 @@ Read more about the game on
<%= row.game_id %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.mtime %> "><%= row.active_role %> @@ -52,7 +55,7 @@ Read more about the game on
<%= row.game_id %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.mtime %> <%= row.result %> diff --git a/views/message_inbox.ejs b/views/message_inbox.ejs index 91490ac..16b0c46 100644 --- a/views/message_inbox.ejs +++ b/views/message_inbox.ejs @@ -1,23 +1,21 @@ <%- include('header', { title: "Inbox" }) %> -

Send message -
Outbox +

Send message "> - <% } %>
FromSubjectDate <% if (messages.length > 0) { messages.forEach((row) => { %>
<%= row.from_name %> +<%= row.from_name %> <%= row.subject %> <%= row.time %> <% }); } else { %>
No messages
+

Outbox diff --git a/views/message_outbox.ejs b/views/message_outbox.ejs index c460bd3..e5c53e3 100644 --- a/views/message_outbox.ejs +++ b/views/message_outbox.ejs @@ -1,4 +1,11 @@ <%- include('header', { title: "Outbox" }) %> + -

Send message -
Inbox "> - <% } %>
ToSubjectDate <% if (messages.length > 0) { messages.forEach((row) => { %>
<%= row.to_name %> +<%= row.to_name %> <%= row.subject %> <%= row.time %> <% }); } else { %>
No messages
+

+ diff --git a/views/profile.ejs b/views/profile.ejs index 8d03234..3c49014 100644 --- a/views/profile.ejs +++ b/views/profile.ejs @@ -1,6 +1,6 @@ <%- include('header', { title: "Rally the Troops!", refresh: (active_games.length > 0 ? 300 : 0) }) %> @@ -23,6 +23,7 @@ Your mail address is <%= user.mail %>.

  • Change password
  • Change mail address
  • Change name +
  • Change profile text
  • Logout @@ -33,9 +34,9 @@ Your mail address is <%= user.mail %>. <% open_games.forEach((row) => { %>
  • <%= row.game_id %> -<%= row.title_name %> +<%= row.title_name %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.ctime %> Join @@ -50,9 +51,9 @@ Your mail address is <%= user.mail %>. <% active_games.forEach((row) => { %>
    <%= row.game_id %> -<%= row.title_name %> +<%= row.title_name %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.mtime %> <% @@ -78,9 +79,9 @@ Your mail address is <%= user.mail %>. <% finished_games.forEach((row) => { %>
    <%= row.game_id %> -<%= row.title_name %> +<%= row.title_name %> <%= row.scenario %> -<%= row.player_names %> +<%- row.player_names %> <%= row.description %> <%= row.mtime %> <%= row.result %> diff --git a/views/users.ejs b/views/users.ejs index 526b722..d6e4d4a 100644 --- a/views/users.ejs +++ b/views/users.ejs @@ -10,7 +10,7 @@ td.avatar img{display:block;width:80px;height:80px;} <% userList.forEach((row) => { %>
    -<%= row.name %> +<%= row.name %> <%= row.ctime %> <%= row.atime %> <% }); %> -- cgit v1.2.3