mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-07 22:13:07 +00:00
Rae from website
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
453a143bfa
commit
a3e6df6ce8
13 changed files with 691 additions and 35 deletions
|
@ -10,7 +10,14 @@ import {
|
|||
} from "../db/schema.js";
|
||||
import { and, count, desc, eq, sql } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
import { getMoods, render, render404, UserStatus } from "./util.js";
|
||||
import {
|
||||
confirm,
|
||||
getMoods,
|
||||
journalMoodString,
|
||||
render,
|
||||
render404,
|
||||
UserStatus
|
||||
} from "./util.js";
|
||||
|
||||
export default async function (app: Express, db: NodePgDatabase) {
|
||||
const { moods, moodsSorted } = await getMoods();
|
||||
|
@ -155,17 +162,28 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
render(db, "journal", "your journal", res, req);
|
||||
});
|
||||
app.get("/journal/:id", async (req, res) => {
|
||||
const id = parseInt(req.params.id);
|
||||
if (isNaN(id)) {
|
||||
req.flash("error", "Invalid ID");
|
||||
render404(db, res, req);
|
||||
return;
|
||||
}
|
||||
|
||||
const entry: {
|
||||
uname: string,
|
||||
title: string,
|
||||
content: string,
|
||||
moodChange: number,
|
||||
moodString?: string,
|
||||
date: Date,
|
||||
visibility: number
|
||||
id: number;
|
||||
uid: number;
|
||||
uname: string;
|
||||
title: string;
|
||||
content: string;
|
||||
moodChange: number;
|
||||
moodString?: string;
|
||||
date: Date;
|
||||
visibility: number;
|
||||
} = (
|
||||
await db
|
||||
.select({
|
||||
id: journalEntries.id,
|
||||
uid: users.id,
|
||||
uname: users.name,
|
||||
title: journalEntries.title,
|
||||
content: journalEntries.entry,
|
||||
|
@ -174,45 +192,88 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
visibility: journalEntries.visibility
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.id, parseInt(req.params.id)))
|
||||
.where(eq(journalEntries.id, id))
|
||||
.leftJoin(users, eq(journalEntries.user, users.id))
|
||||
)[0];
|
||||
|
||||
//? 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;
|
||||
}
|
||||
entry.moodString = journalMoodString(entry.moodChange);
|
||||
|
||||
const isMod = req.session["status"] & UserStatus.MODERATOR;
|
||||
if (
|
||||
!entry ||
|
||||
(entry.visibility === 0 && entry.uname !== req.session["user"] && !(req.session["status"] & UserStatus.MODERATOR))
|
||||
(entry.visibility === 0 &&
|
||||
entry.uname !== req.session["user"] && !isMod)
|
||||
) {
|
||||
render404(db, res, req);
|
||||
return;
|
||||
}
|
||||
|
||||
// maybe turn ([x] !== req.session["user"] && !(req.session["status"] & UserStatus.MODERATOR)) into util function
|
||||
entry.content =
|
||||
entry.visibility === 2 &&
|
||||
entry.uid !== req.session["uid"] &&
|
||||
!(req.session["status"] & UserStatus.MODERATOR)
|
||||
? "This journal entry's contents have been made private."
|
||||
: entry.content;
|
||||
|
||||
const entryTimestamp = dayjs(entry.date).fromNow();
|
||||
const isSelf = entry.uid === req.session["uid"];
|
||||
render(db, "journal_view", entry.title, res, req, {
|
||||
entry,
|
||||
entryTimestamp
|
||||
entryTimestamp,
|
||||
isSelf,
|
||||
isMod
|
||||
});
|
||||
});
|
||||
app.post("/journal/:id/edit", async (req, res) => {
|
||||
const id = parseInt(req.params.id);
|
||||
if (isNaN(id)) {
|
||||
req.flash("error", "Invalid ID");
|
||||
render404(db, res, req);
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = (
|
||||
await db
|
||||
.select({
|
||||
uid: journalEntries.user
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.id, id))
|
||||
.limit(1)
|
||||
)[0];
|
||||
|
||||
const isMod = req.session["status"] & UserStatus.MODERATOR;
|
||||
if (
|
||||
!entry ||
|
||||
(entry?.uid !== req.session["uid"] &&
|
||||
!isMod)
|
||||
) {
|
||||
render404(db, res, req);
|
||||
return;
|
||||
}
|
||||
if (isMod && entry.uid !== req.session["uid"] && req.body.action !== "delete") {
|
||||
req.flash("error", "Moderators can only delete other users' posts.");
|
||||
res.redirect(`/journal/${req.params.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.body.action === "edit") {
|
||||
// TODO!!
|
||||
} else if (req.body.action === "delete") {
|
||||
if (!req.body.confirm) {
|
||||
confirm(db, res, req);
|
||||
return;
|
||||
}
|
||||
await db.delete(journalEntries).where(eq(journalEntries.id, id));
|
||||
req.flash("success", "Journal entry deleted ;w;");
|
||||
res.redirect("/");
|
||||
} else {
|
||||
render404(db, res, req);
|
||||
}
|
||||
});
|
||||
app.post("/update/journal", async (req, res) => {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
|
|
|
@ -7,7 +7,7 @@ import {
|
|||
updates,
|
||||
users
|
||||
} from "../db/schema.js";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { and, desc, eq, ne } from "drizzle-orm";
|
||||
import { getMoods, render, render404, UserStatus } from "./util.js";
|
||||
import { PgColumn } from "drizzle-orm/pg-core";
|
||||
import dayjs from "dayjs";
|
||||
|
@ -79,10 +79,15 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
.select({
|
||||
id: journalEntries.id,
|
||||
title: journalEntries.title,
|
||||
date: journalEntries.date
|
||||
date: journalEntries.date,
|
||||
visibility: journalEntries.visibility
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.user, user.id))
|
||||
.where(
|
||||
user.id === req.session["uid"] || req.session["status"] & UserStatus.MODERATOR
|
||||
? eq(journalEntries.user, user.id)
|
||||
: and(eq(journalEntries.user, user.id), ne(journalEntries.visibility, 0))
|
||||
)
|
||||
.orderBy(desc(journalEntries.date))
|
||||
.limit(5)
|
||||
).map((e) => {
|
||||
|
@ -90,6 +95,7 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
id: e.id,
|
||||
title: e.title,
|
||||
date: e.date,
|
||||
visibility: e.visibility,
|
||||
relativeDate: now.to(e.date)
|
||||
};
|
||||
});
|
||||
|
@ -115,7 +121,7 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
};
|
||||
});
|
||||
|
||||
if (!isSelf) {
|
||||
if (!isSelf && userMood) {
|
||||
userMood.mood = moods[userMood.mood as number];
|
||||
}
|
||||
|
||||
|
|
|
@ -115,3 +115,27 @@ export async function createInviteCode(
|
|||
});
|
||||
return token;
|
||||
}
|
||||
|
||||
export function journalMoodString(mood: number) {
|
||||
switch (mood) {
|
||||
case -2:
|
||||
return "much worse";
|
||||
case -1:
|
||||
return "worse";
|
||||
default:
|
||||
case 0:
|
||||
return "about the same";
|
||||
case 1:
|
||||
return "better";
|
||||
case 2:
|
||||
return "much better";
|
||||
}
|
||||
}
|
||||
|
||||
export function confirm(
|
||||
db: NodePgDatabase,
|
||||
res: Response,
|
||||
req: Request
|
||||
) {
|
||||
render(db, "confirm", "Confirm action", res, req, { body: req.body, url: req.url });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue