diff --git a/TODO.md b/TODO.md index 8416870..e660023 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,6 @@ - [ ] You do realize using toLocaleString on the server only makes it use your locale right - [ ] Make recent updates also account for new journal entries - [ ] View all previous moods and journal entries +- [ ] Visibility indicator on journal entries +- [ ] Hide journal entries from feed that are hidden unless current user is a moderator +- [ ] Edit/delete journal entries? diff --git a/main.ts b/main.ts index 4447de6..e35c83d 100644 --- a/main.ts +++ b/main.ts @@ -20,7 +20,7 @@ import adminRoutes from "./routes/admin.js"; import loginRoutes from "./routes/login.js"; import userRoutes from "./routes/users.js"; import updateRoutes from "./routes/updates.js"; -import { createInviteCode, getMoods, render, setNonce, UserStatus } from "./routes/util.js"; +import { createInviteCode, getMoods, render, render404, setNonce, UserStatus } from "./routes/util.js"; const db = drizzle(process.env.DATABASE_URL!); @@ -109,6 +109,10 @@ object-src 'none'; base-uri 'none';" await updateRoutes(app, db); loginRoutes(app, db); + app.get("*", (req, res) => { + render404(db, res, req); + }); + app.listen(1337, () => { console.log("Listening on http://127.0.0.1:1337/"); }); diff --git a/routes/admin.ts b/routes/admin.ts index 5b169e3..b144656 100644 --- a/routes/admin.ts +++ b/routes/admin.ts @@ -1,6 +1,6 @@ import { NodePgDatabase } from "drizzle-orm/node-postgres"; import { Express } from "express"; -import { createInviteCode, render, UserStatus } from "./util.js"; +import { createInviteCode, render, render404, UserStatus } from "./util.js"; import { inviteCodes, users } from "../db/schema.js"; import { and, count, desc, eq, sql } from "drizzle-orm"; import dayjs from "dayjs"; @@ -13,7 +13,7 @@ export default function (app: Express, db: NodePgDatabase) { !req.session["loggedIn"] || !(req.session["status"] & UserStatus.MODERATOR) ) { - res.redirect("/"); + render404(db, res, req); return; } diff --git a/routes/updates.ts b/routes/updates.ts index 9334c9e..ef24fde 100644 --- a/routes/updates.ts +++ b/routes/updates.ts @@ -10,7 +10,7 @@ import { } from "../db/schema.js"; import { and, count, desc, eq, sql } from "drizzle-orm"; import dayjs from "dayjs"; -import { getMoods, render, render404 } from "./util.js"; +import { getMoods, render, render404, UserStatus } from "./util.js"; export default async function (app: Express, db: NodePgDatabase) { const { moods, moodsSorted } = await getMoods(); @@ -155,19 +155,54 @@ export default async function (app: Express, db: NodePgDatabase) { render(db, "journal", "your journal", res, req); }); app.get("/journal/:id", async (req, res) => { - const entry = ( + const entry: { + uname: string, + title: string, + content: string, + moodChange: number, + moodString?: string, + date: Date, + visibility: number + } = ( await db .select({ uname: users.name, title: journalEntries.title, content: journalEntries.entry, - date: journalEntries.date + moodChange: journalEntries.moodChange, + date: journalEntries.date, + visibility: journalEntries.visibility }) .from(journalEntries) .where(eq(journalEntries.id, parseInt(req.params.id))) .leftJoin(users, eq(journalEntries.user, users.id)) )[0]; - if (!entry) { + + //? put into util function? + //? also GOD + switch (entry.moodChange) { + case -2: + entry.moodString = "much worse" + break; + case -1: + entry.moodString = "worse"; + break; + default: + case 0: + entry.moodString = "about the same"; + break; + case 1: + entry.moodString = "better"; + break; + case 2: + entry.moodString = "much better"; + break; + } + + if ( + !entry || + (entry.visibility === 0 && entry.uname !== req.session["user"] && !(req.session["status"] & UserStatus.MODERATOR)) + ) { render404(db, res, req); return; } diff --git a/views/journal_view.pug b/views/journal_view.pug index 2cfecad..e9099cf 100644 --- a/views/journal_view.pug +++ b/views/journal_view.pug @@ -9,4 +9,8 @@ block content span(title=entry.date.toLocaleString()) #{entryTimestamp} br br + div + | Mood: + strong= entry.moodString + br div!= entry.content