mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-07 22:13:07 +00:00
Today is Journal Day
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
f7e44cddca
commit
927193c102
14 changed files with 742 additions and 55 deletions
|
@ -8,7 +8,7 @@ import {
|
|||
updates,
|
||||
users
|
||||
} from "../db/schema.js";
|
||||
import { and, count, desc, eq, sql } from "drizzle-orm";
|
||||
import { and, count, desc, eq, ne, sql } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
import {
|
||||
confirm,
|
||||
|
@ -83,6 +83,47 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
};
|
||||
});
|
||||
|
||||
// recent journal entries
|
||||
const recentJournalUpdates = (
|
||||
await db
|
||||
.select({
|
||||
id: journalEntries.id,
|
||||
user: users.name,
|
||||
title: journalEntries.title,
|
||||
date: journalEntries.date,
|
||||
visibility: journalEntries.visibility
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(
|
||||
users.id === req.session["uid"] ||
|
||||
req.session["status"] & UserStatus.MODERATOR
|
||||
? eq(journalEntries.user, users.id)
|
||||
: and(
|
||||
eq(journalEntries.user, users.id),
|
||||
ne(journalEntries.visibility, 0)
|
||||
)
|
||||
)
|
||||
.innerJoin(
|
||||
follows,
|
||||
and(
|
||||
eq(follows.userId, journalEntries.user),
|
||||
eq(follows.followerId, req.session["uid"])
|
||||
)
|
||||
)
|
||||
.leftJoin(users, eq(journalEntries.user, users.id))
|
||||
.orderBy(desc(journalEntries.date))
|
||||
.limit(9)
|
||||
).map((e) => {
|
||||
return {
|
||||
id: e.id,
|
||||
user: e.user,
|
||||
title: e.title,
|
||||
date: e.date,
|
||||
visibility: e.visibility,
|
||||
relativeDate: now.to(e.date)
|
||||
};
|
||||
});
|
||||
|
||||
// user invite codes
|
||||
const codes = (
|
||||
await db
|
||||
|
@ -126,10 +167,13 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
moodsSorted,
|
||||
moodHistory,
|
||||
recentUpdates,
|
||||
recentJournalUpdates,
|
||||
codes,
|
||||
codesUsed,
|
||||
followed,
|
||||
isTrusted: req.session["status"] & (UserStatus.MODERATOR | UserStatus.TRUSTED),
|
||||
isTrusted:
|
||||
req.session["status"] &
|
||||
(UserStatus.MODERATOR | UserStatus.TRUSTED),
|
||||
feed: []
|
||||
});
|
||||
});
|
||||
|
@ -140,9 +184,19 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
}
|
||||
// make sure the user isnt updating too fast
|
||||
//! TODO: also do this for journal entries
|
||||
const lastUpdate = (await db.select({ date: updates.date }).from(updates).where(eq(updates.user, req.session["uid"])).orderBy(desc(updates.date)).limit(1))?.[0];
|
||||
const lastUpdate = (
|
||||
await db
|
||||
.select({ date: updates.date })
|
||||
.from(updates)
|
||||
.where(eq(updates.user, req.session["uid"]))
|
||||
.orderBy(desc(updates.date))
|
||||
.limit(1)
|
||||
)?.[0];
|
||||
if (Date.now() < lastUpdate?.date?.getTime() + 10 * 1000) {
|
||||
req.flash("error", "You're updating your mood too fast! Wait ten seconds between updates.");
|
||||
req.flash(
|
||||
"error",
|
||||
"You're updating your mood too fast! Wait ten seconds between updates."
|
||||
);
|
||||
res.redirect(req.get("Referrer") || "/");
|
||||
return;
|
||||
}
|
||||
|
@ -183,8 +237,18 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
return;
|
||||
}
|
||||
|
||||
const { user } = (await db.select({user: updates.user}).from(updates).where(eq(updates.id, u)).limit(1))[0];
|
||||
if (!user || (user !== req.session["uid"] && !(req.session["status"] & UserStatus.MODERATOR))) {
|
||||
const { user } = (
|
||||
await db
|
||||
.select({ user: updates.user })
|
||||
.from(updates)
|
||||
.where(eq(updates.id, u))
|
||||
.limit(1)
|
||||
)[0];
|
||||
if (
|
||||
!user ||
|
||||
(user !== req.session["uid"] &&
|
||||
!(req.session["status"] & UserStatus.MODERATOR))
|
||||
) {
|
||||
render404(db, res, req);
|
||||
return;
|
||||
}
|
||||
|
@ -222,6 +286,7 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
moodString?: string;
|
||||
date: Date;
|
||||
visibility: number;
|
||||
updated: Date;
|
||||
} = (
|
||||
await db
|
||||
.select({
|
||||
|
@ -232,7 +297,8 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
content: journalEntries.entry,
|
||||
moodChange: journalEntries.moodChange,
|
||||
date: journalEntries.date,
|
||||
visibility: journalEntries.visibility
|
||||
visibility: journalEntries.visibility,
|
||||
updated: journalEntries.updated
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.id, id))
|
||||
|
@ -263,10 +329,12 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
: entry.content;
|
||||
|
||||
const entryTimestamp = dayjs(entry.date).fromNow();
|
||||
const updatedTimestamp = dayjs(entry.updated).fromNow();
|
||||
const isSelf = entry.uid === req.session["uid"];
|
||||
render(db, "journal_view", entry.title, res, req, {
|
||||
entry,
|
||||
entryTimestamp,
|
||||
updatedTimestamp,
|
||||
isSelf,
|
||||
isMod
|
||||
});
|
||||
|
@ -282,7 +350,11 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
const entry = (
|
||||
await db
|
||||
.select({
|
||||
uid: journalEntries.user
|
||||
uid: journalEntries.user,
|
||||
title: journalEntries.title,
|
||||
entry: journalEntries.entry,
|
||||
visibility: journalEntries.visibility,
|
||||
mood: journalEntries.moodChange
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.id, id))
|
||||
|
@ -308,7 +380,62 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
}
|
||||
|
||||
if (req.body.action === "edit") {
|
||||
// TODO!!
|
||||
if (
|
||||
req.body.title ||
|
||||
req.body.description ||
|
||||
req.body.moodDelta ||
|
||||
req.body.visiblity
|
||||
) {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
return;
|
||||
}
|
||||
if (req.body.title.length > 64) {
|
||||
req.flash("error", "Title too long!");
|
||||
res.redirect("/journal");
|
||||
return;
|
||||
}
|
||||
if (req.body.description.length > 4096) {
|
||||
req.flash("error", "Entry too long!");
|
||||
res.redirect("/journal");
|
||||
return;
|
||||
}
|
||||
|
||||
const moodChange = parseInt(req.body.moodDelta);
|
||||
const visibility = parseInt(req.body.visibility);
|
||||
if (isNaN(moodChange) || isNaN(visibility)) {
|
||||
req.flash(
|
||||
"error",
|
||||
"One of the values was improperly specified."
|
||||
);
|
||||
res.redirect(`/journal/${req.params.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await db
|
||||
.update(journalEntries)
|
||||
.set({
|
||||
//@ts-expect-error
|
||||
title: req.body.title,
|
||||
entry: req.body.description
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll("\n", "<br>"),
|
||||
moodChange: req.body.moodDelta,
|
||||
visibility: req.body.visibility,
|
||||
updated: new Date(Date.now())
|
||||
})
|
||||
.where(eq(journalEntries.id, id));
|
||||
|
||||
res.redirect(`/journal/${req.params.id}`);
|
||||
return;
|
||||
}
|
||||
render(db, "journal", `edit ${entry.title}`, res, req, {
|
||||
entry,
|
||||
id,
|
||||
edit: true
|
||||
});
|
||||
} else if (req.body.action === "delete") {
|
||||
if (!confirm(db, res, req, `/journal/${req.params.id}`)) {
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue