From a9a0996e779c77acdd4c9ed5ca33c1311767109d Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 25 Nov 2024 14:12:44 -0500 Subject: [PATCH] add messages --- client/messages.js | 25 ++++++++++++++++ db.js | 2 ++ form/auth.js | 18 ++++++++++-- form/comment.js | 15 +++++++++- routes.js | 4 ++- static/img/read.svg | 59 ++++++++++++++++++++++++++++++++++++++ static/img/unread.svg | 65 ++++++++++++++++++++++++++++++++++++++++++ static/main.css | 17 ++++++++--- static/main.js | 4 ++- views/header_block.ejs | 3 ++ views/messages.ejs | 18 ++++++++++++ 11 files changed, 220 insertions(+), 10 deletions(-) create mode 100644 client/messages.js create mode 100644 static/img/read.svg create mode 100644 static/img/unread.svg create mode 100644 views/messages.ejs diff --git a/client/messages.js b/client/messages.js new file mode 100644 index 0000000..fa1defc --- /dev/null +++ b/client/messages.js @@ -0,0 +1,25 @@ +import Route from "../route.js"; +import auth from "../form/auth.js"; +import initDb from "../db.js"; + +let db = await initDb(); + +let main = new Route([auth], async function (req, res, input) { + let { username } = input; + + let msgs = await db.all('SELECT * FROM message WHERE username = ? ORDER BY date DESC', [ + username + ]); + + await db.run('UPDATE message SET read = ? WHERE username = ?', [ + 'true', + username + ]); + + return res.render('messages', { + ...input, + msgs + }); +}); + +export default main; \ No newline at end of file diff --git a/db.js b/db.js index 8d5ccfc..2246ba5 100644 --- a/db.js +++ b/db.js @@ -22,6 +22,8 @@ async function initDb() { await db.run(`CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT);`); await db.run(`CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT);`); + await db.run(`CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);`); + return db; } diff --git a/form/auth.js b/form/auth.js index ffe8752..73ec711 100644 --- a/form/auth.js +++ b/form/auth.js @@ -4,8 +4,8 @@ import initDb from "../db.js"; let db = await initDb(); let main = new Route([], async function (req, res, input) { - let {route} = req.params; - let {id} = req.query; + let { route } = req.params; + let { id } = req.query; let body = { ...req.cookies, ...req.body }; @@ -21,13 +21,25 @@ let main = new Route([], async function (req, res, input) { username ]); + let isRead = false; + + if (username) { + let msgs = await db.all('SELECT * FROM message WHERE username = ? AND read = ?', [ + username, + 'false' + ]); + + isRead = msgs.length > 0 + } + return { username, valid: valid[0] ? valid[0].valid : 'noexist', url: `${process.env.URL}/client/${route}?id=${id || ''}`, icon: `${process.env.URL}/static/img/logo.png`, rootUrl: process.env.URL, - ogType: 'website' + ogType: 'website', + isRead: isRead ? 'unread' : 'read' }; }); diff --git a/form/comment.js b/form/comment.js index bbd1304..ba4088a 100644 --- a/form/comment.js +++ b/form/comment.js @@ -23,7 +23,20 @@ let main = new Route([auth], async function (req, res, input) { id ]); - res.send({ 'message': 'Comment sent', redirect: `/client/${targetType}?id=${targetId}` }); + let u = `/client/${targetType}?id=${targetId}`; + + if (targetType == 'user') { + await db.run('INSERT INTO message (username, targetType, targetId, date, content, read) VALUES (?,?,?,?,?,?)', [ + targetId, + 'profile comment', + u, + +new Date(), + content, + 'false' + ]); + } + + res.send({ 'message': 'Comment sent', redirect: u }); }); export default main; \ No newline at end of file diff --git a/routes.js b/routes.js index 843fa41..dca2298 100644 --- a/routes.js +++ b/routes.js @@ -8,6 +8,7 @@ import settings from "./client/settings.js"; import e404 from "./client/e404.js"; import tou from "./client/tou.js"; import captcha from './client/captcha.js'; +import messages from './client/messages.js'; import loginB from "./form/login.js"; import registerB from "./form/register.js"; @@ -35,7 +36,8 @@ routes.client = { settings, e404, tou, - captcha + captcha, + messages } routes.get = { diff --git a/static/img/read.svg b/static/img/read.svg new file mode 100644 index 0000000..6ba6fa3 --- /dev/null +++ b/static/img/read.svg @@ -0,0 +1,59 @@ + + + + + + + + + + + diff --git a/static/img/unread.svg b/static/img/unread.svg new file mode 100644 index 0000000..0f7fb01 --- /dev/null +++ b/static/img/unread.svg @@ -0,0 +1,65 @@ + + + + + + + + + + + + diff --git a/static/main.css b/static/main.css index bf76f30..9730ea3 100644 --- a/static/main.css +++ b/static/main.css @@ -72,20 +72,29 @@ body { .header-link, .link { - text-decoration: none; font-weight: 700; color: var(--white) } .header-link { + + text-decoration: none; padding-left: .5em; margin-left: .5em; - border-left: solid var(--white) 2px + border-left: solid var(--white) 2px; +} + +.header>a { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + height: 1.5em; } .header-img { - height: 2em; - width: 2em; + height: 1.5em; + width: 1.5em; object-fit: cover } diff --git a/static/main.js b/static/main.js index e5bd064..bfe0db3 100644 --- a/static/main.js +++ b/static/main.js @@ -16,7 +16,9 @@ async function formClick(ev) { let json = await fetched.json(); - target.querySelector('.captcha').src += '&a=b'; + let c = target.querySelector('.captcha'); + + if (c) c.src += '&a=b'; target.querySelector('.form-message').textContent = json.message; diff --git a/views/header_block.ejs b/views/header_block.ejs index 26be8cd..8940d0b 100644 --- a/views/header_block.ejs +++ b/views/header_block.ejs @@ -12,5 +12,8 @@ Settings + + + <% } %> \ No newline at end of file diff --git a/views/messages.ejs b/views/messages.ejs new file mode 100644 index 0000000..9815540 --- /dev/null +++ b/views/messages.ejs @@ -0,0 +1,18 @@ +<%- include('header.ejs') -%> +
+

Messages

+ <% for (let message of msgs) { %> +
+
+ <%= (new Date(message.date)+'').split(/(GMT|UTC)/g)[0] %> +
+

A + <%= message.targetType %> + was sent

+
+
<%= message.content %>
+
+
+ <% } %> +
+ <%- include('footer.ejs') -%> \ No newline at end of file