From 729a7b8c56ff3d53c429c7d34bd7788cb3fe55c1 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Sun, 2 Mar 2025 04:09:17 -0500 Subject: [PATCH] add notifs --- db.js | 3 +- lib.js | 2 +- public/index.css | 3 +- public/mail.svg | 85 +++++++++++++++++++++++++++++++++++++++++++++ routes/commenter.js | 23 ++++++++++++ routes/init.js | 2 +- routes/you.js | 30 +++++++++++++++- views/message.ejs | 11 ++++++ views/messages.ejs | 4 +++ 9 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 public/mail.svg create mode 100644 views/message.ejs create mode 100644 views/messages.ejs diff --git a/db.js b/db.js index 739bb16..611c698 100644 --- a/db.js +++ b/db.js @@ -6,7 +6,8 @@ let db; const sql = `CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT); CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT); CREATE TABLE IF NOT EXISTS feeder (parentType TEXT, parentId TEXT, childType text, childId TEXT, sortId REAL); -CREATE TABLE IF NOT EXISTS comment (username TEXT, date REAL, content TEXT, id TEXT);` +CREATE TABLE IF NOT EXISTS comment (username TEXT, date REAL, content TEXT, id TEXT); +CREATE TABLE IF NOT EXISTS messages (username TEXT, date REAL, content TEXT, link TEXT, status TEXT)` async function initDb() { if (db) return db; diff --git a/lib.js b/lib.js index 4d3a86d..39f80ab 100644 --- a/lib.js +++ b/lib.js @@ -55,4 +55,4 @@ async function replyIterator(replies, db) { return replies; } -export { importRouters, apiStat, replyIterator }; \ No newline at end of file +export { importRouters, apiStat, replyIterator, splitUp }; \ No newline at end of file diff --git a/public/index.css b/public/index.css index f41d721..815e676 100644 --- a/public/index.css +++ b/public/index.css @@ -139,7 +139,8 @@ body { margin: 0; padding-left: 0; height: 4em; - object-fit: cover; + width: 4em; + object-fit: contain; display: flex; flex-direction: column; align-items: center; diff --git a/public/mail.svg b/public/mail.svg new file mode 100644 index 0000000..219d696 --- /dev/null +++ b/public/mail.svg @@ -0,0 +1,85 @@ + + + + diff --git a/routes/commenter.js b/routes/commenter.js index 597efe8..7a996ff 100644 --- a/routes/commenter.js +++ b/routes/commenter.js @@ -49,6 +49,29 @@ router.post('/', async (req, res, next) => { (+new Date) ]); + if (type == 'users') { + await db.run('INSERT INTO messages (username, date, content, link, status) VALUES(?,?,?,?,?)', [ + id, + (+new Date), + `A user responded to your wall.`, + `/walls/get/users/${id}/0`, + 'unread' + ]); + } else if (type == 'comment') { + let commenter = await db.all('SELECT * FROM comment WHERE id = ?', [ + id + ]); + if (commenter[0]) { + await db.run('INSERT INTO messages (username, date, content, link, status) VALUES(?,?,?,?,?)', [ + commenter[0].username, + (+new Date), + `A user responded to your post.`, + `/walls/get/comment/${id}/0`, + 'unread' + ]); + } + } + apiStat(res, next, `Comment submitted.`, '/comment/' + token) }) diff --git a/routes/init.js b/routes/init.js index 5f6674f..763fe31 100644 --- a/routes/init.js +++ b/routes/init.js @@ -80,7 +80,7 @@ function initr(req, res, next) { { link: '/walls/get/hub/main/0', icon: '/icon.svg', name: 'DervNet' }, { link: '/walls/list', icon: '/walls.svg', name: 'Explore' }, { link: '/users/' + (res.auth ? res.auth.username : ''), icon: '/login.svg', name: 'You' }, - { link: '/info/tou', icon: '/icon.svg', name: 'Rules' }, + { link: '/you/messages', icon: '/mail.svg', name: 'Messages' }, { link: '/you/logout', icon: '/logout.svg', name: 'Leave' } ]; diff --git a/routes/you.js b/routes/you.js index 3a077c2..ff01702 100644 --- a/routes/you.js +++ b/routes/you.js @@ -1,4 +1,7 @@ import { Router } from "express"; +import { initDb } from "../db.js"; + +let db = await initDb(); const router = Router(); //todo: fix jank @@ -38,7 +41,6 @@ router.get('/logout', (req, res, next) => { next(); }) - router.get('/new', (req, res, next) => { res.ctx.mainPage = 'form' res.ctx.mainCtx = { @@ -69,4 +71,30 @@ router.get('/new', (req, res, next) => { next(); }) +router.get('/messages', async (req, res, next) => { + let {username, valid} = res.auth; + + if (!valid) { + next(); + return; + } + + let messages = await db.all('SELECT * FROM messages WHERE username = ? ORDER BY date DESC LIMIT ? OFFSET ?', [ + username, + 10, + 0 + ]); + + await db.run('UPDATE messages SET status = ? WHERE username = ?', [ + 'read', + username + ]); + + res.ctx.mainPage = 'messages' + res.ctx.mainCtx = { + messages + } + next(); +}) + export default router; \ No newline at end of file diff --git a/views/message.ejs b/views/message.ejs new file mode 100644 index 0000000..d83c070 --- /dev/null +++ b/views/message.ejs @@ -0,0 +1,11 @@ +
+

+ Message +

+
+ <%= content %> +
+ <% if (!isNaN(date * 1)) { %> +
Sent on <%= new Date(date).toISOString() %>
+ <% } %> +
\ No newline at end of file diff --git a/views/messages.ejs b/views/messages.ejs new file mode 100644 index 0000000..7ae1dad --- /dev/null +++ b/views/messages.ejs @@ -0,0 +1,4 @@ + +<% for (let reply of messages) { %> +<%- include ('message.ejs', reply || {}) %> +<% } %> \ No newline at end of file