diff --git a/src/inc/routes/subscriptions.mjs b/src/inc/routes/subscriptions.mjs new file mode 100644 index 0000000..6d8f1ba --- /dev/null +++ b/src/inc/routes/subscriptions.mjs @@ -0,0 +1,57 @@ +import db from "../sql.mjs"; + +export default (router, tpl) => { + + // Subscriptions Overview + router.get('/subscriptions', async (req, res) => { + if (!req.session) return res.redirect('/login'); + + try { + console.log('[DEBUG SUB] Fetching subscriptions for user', req.session.id); + const subs = await db` + SELECT + s.created_at as sub_date, + i.id, i.dest, i.mime, i.username as uploader_name + FROM comment_subscriptions s + JOIN items i ON s.item_id = i.id + WHERE s.user_id = ${req.session.id} + ORDER BY s.created_at DESC + `; + console.log('[DEBUG SUB] Found', subs.length, 'subscriptions'); + + const items = subs.map(i => ({ + id: i.id, + user: i.uploader_name || 'System', + sub_created: new Date(i.sub_date).toLocaleString(), + thumb: `/t/${i.id}.webp` + })); + + return res.reply({ + body: tpl.render('subscriptions', { items }, req) + }); + } catch (e) { + console.error('[DEBUG SUB ERROR]', e); + return res.reply({ code: 500, body: 'Database Error' }); + } + }); + + // Unsubscribe + router.post(/\/api\/subscriptions\/(?\d+)\/delete/, async (req, res) => { + if (!req.session) return res.reply({ code: 401, body: JSON.stringify({ success: false }) }); + + const itemId = req.params.itemid; + + try { + await db`DELETE FROM comment_subscriptions WHERE user_id = ${req.session.id} AND item_id = ${itemId}`; + return res.reply({ + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ success: true }) + }); + } catch (e) { + console.error(e); + return res.reply({ code: 500, body: JSON.stringify({ success: false }) }); + } + }); + + return router; +}; diff --git a/views/subscriptions.html b/views/subscriptions.html new file mode 100644 index 0000000..bbd3ddf --- /dev/null +++ b/views/subscriptions.html @@ -0,0 +1,127 @@ +@include(snippets/header) + +
+
+

My Subscriptions

+ + @if(items.length === 0) +
+ You haven't subscribed to any threads yet. +
+ @else +
+ @each(items as item) + + @endeach +
+ @endif +
+
+ + + + + +@include(snippets/footer) \ No newline at end of file