mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-07 22:13:07 +00:00
i dont really know what i added
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
e3c09d7f0d
commit
7b563f5c31
24 changed files with 797 additions and 151 deletions
|
@ -5,6 +5,8 @@ import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|||
import { profiles, users } from "../db/schema.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
//! TEMP Also not sanitized like at all
|
||||
//! Also make sure user isnt logged in before doing this
|
||||
export default function(app: Express, db: NodePgDatabase) {
|
||||
app.get("/register", (req, res) => {
|
||||
if (req.session["loggedIn"]) {
|
||||
|
|
161
routes/updates.ts
Normal file
161
routes/updates.ts
Normal file
|
@ -0,0 +1,161 @@
|
|||
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import { Express } from "express";
|
||||
import {
|
||||
follows,
|
||||
journalEntries,
|
||||
profiles,
|
||||
updates,
|
||||
users
|
||||
} from "../db/schema.js";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
import { getMoods, render } from "./util.js";
|
||||
|
||||
export default async function (app: Express, db: NodePgDatabase) {
|
||||
const { moods, moodsSorted } = await getMoods();
|
||||
|
||||
// DASHBOARD
|
||||
app.get("/dashboard", async (req, res) => {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
return;
|
||||
}
|
||||
const user = (
|
||||
await db
|
||||
.select({
|
||||
name: users.name,
|
||||
bio: profiles.bio,
|
||||
website: profiles.website //! validate this
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.name, req.session["user"]))
|
||||
.leftJoin(profiles, eq(users.id, profiles.user))
|
||||
)[0];
|
||||
|
||||
const now = dayjs();
|
||||
const moodHistory = (
|
||||
await db
|
||||
.select({ mood: updates.mood, date: updates.date })
|
||||
.from(updates)
|
||||
.where(eq(updates.user, req.session["uid"]))
|
||||
.orderBy(desc(updates.date))
|
||||
.limit(10)
|
||||
).map((e) => {
|
||||
return { mood: moods[e.mood], date: now.to(dayjs(e.date)) };
|
||||
});
|
||||
|
||||
const recentUpdates = (
|
||||
await db
|
||||
.select({
|
||||
user: users.name,
|
||||
mood: updates.mood,
|
||||
desc: updates.description,
|
||||
date: updates.date
|
||||
})
|
||||
.from(updates)
|
||||
.innerJoin(
|
||||
follows,
|
||||
and(
|
||||
eq(follows.userId, updates.user),
|
||||
eq(follows.followerId, req.session["uid"])
|
||||
)
|
||||
)
|
||||
.leftJoin(users, eq(updates.user, users.id))
|
||||
.orderBy(desc(updates.date))
|
||||
.limit(25)
|
||||
).map((e) => {
|
||||
return {
|
||||
user: e.user,
|
||||
mood: moods[e.mood],
|
||||
desc: e.desc,
|
||||
date: now.to(dayjs(e.date))
|
||||
};
|
||||
});
|
||||
|
||||
render(db, "dashboard", "Dashboard", res, req, {
|
||||
user,
|
||||
moods,
|
||||
moodsSorted,
|
||||
moodHistory,
|
||||
recentUpdates,
|
||||
feed: []
|
||||
});
|
||||
});
|
||||
app.post("/update/mood", async (req, res) => {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
return;
|
||||
}
|
||||
const moodIndex = moods.indexOf(req.body.mood.trim());
|
||||
if (moodIndex === -1) {
|
||||
req.flash(
|
||||
"error",
|
||||
"That mood doesn't exist in the database, WTF are you trying to do??"
|
||||
);
|
||||
res.redirect("/dashboard");
|
||||
return;
|
||||
}
|
||||
if (req.body.desc.length > 512) {
|
||||
req.flash(
|
||||
"error",
|
||||
"Mood description can't be longer than 512 characters"
|
||||
);
|
||||
res.redirect("/dashboard");
|
||||
}
|
||||
|
||||
await db
|
||||
.insert(updates)
|
||||
// @ts-expect-error
|
||||
.values({
|
||||
user: req.session["uid"],
|
||||
mood: moodIndex,
|
||||
description: req.body.desc,
|
||||
date: new Date(Date.now())
|
||||
});
|
||||
req.flash("success", "Mood updated!");
|
||||
res.redirect("/dashboard");
|
||||
});
|
||||
|
||||
// JOURNAL
|
||||
app.get("/journal", async (req, res) => {
|
||||
render(db, "journal", "Journal", res, req);
|
||||
});
|
||||
app.post("/update/journal", async (req, res) => {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
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");
|
||||
return;
|
||||
}
|
||||
|
||||
let id: number;
|
||||
try {
|
||||
// @ts-expect-error
|
||||
const entry = await db.insert(journalEntries).values({
|
||||
user: req.session["uid"],
|
||||
moodChange,
|
||||
visibility,
|
||||
entry: req.body.description,
|
||||
date: new Date(Date.now())
|
||||
}).returning({ id: journalEntries.id });
|
||||
id = entry[0].id;
|
||||
} catch (err) {
|
||||
req.flash("error", "Failed to create your entry. Try again later or send these logs to roxwize so she can know what's up:<br><br>"+err);
|
||||
res.redirect("/journal");
|
||||
return;
|
||||
}
|
||||
req.flash("success", `Your journal entry is now available as <a href="/journal/view?id=${id}">#${id}</a>!`);
|
||||
res.redirect("/journal");
|
||||
});
|
||||
}
|
|
@ -4,6 +4,7 @@ import { follows, profiles, updates, users } from "../db/schema.js";
|
|||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { getMoods, render } from "./util.js";
|
||||
import { PgColumn } from "drizzle-orm/pg-core";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export default async function (app: Express, db: NodePgDatabase) {
|
||||
const { moods } = await getMoods();
|
||||
|
@ -57,6 +58,19 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
.limit(1)
|
||||
)[0];
|
||||
|
||||
// feed
|
||||
const now = dayjs();
|
||||
const userMoodFeed = (await db
|
||||
.select({
|
||||
mood: updates.mood,
|
||||
date: updates.date,
|
||||
desc: updates.description
|
||||
})
|
||||
.from(updates)
|
||||
.where(eq(updates.user, user.id))).map((e) => {
|
||||
return { user: user.name, mood: moods[e.mood], date: now.to(dayjs(e.date)), desc: e.desc }
|
||||
});
|
||||
|
||||
if (!isSelf) {
|
||||
userMood.mood = moods[userMood.mood as number];
|
||||
}
|
||||
|
@ -65,6 +79,7 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
user,
|
||||
isSelf,
|
||||
userMood,
|
||||
userMoodFeed,
|
||||
isFollowing
|
||||
});
|
||||
});
|
||||
|
|
|
@ -4,6 +4,21 @@ import { updates } from "../db/schema.js";
|
|||
import { desc, eq } from "drizzle-orm";
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
const nonceChars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
|
||||
let nonce: string;
|
||||
export function setNonce() {
|
||||
nonce = "";
|
||||
for (let i = 0; i < 32; i++)
|
||||
nonce += nonceChars[Math.floor(Math.random() * nonceChars.length)];
|
||||
return nonce;
|
||||
}
|
||||
export function getNonce() {
|
||||
if (!nonce)
|
||||
throw new Error("Nonce doesn't exist");
|
||||
return nonce;
|
||||
}
|
||||
|
||||
let moods: string[], moodsSorted: string[];
|
||||
export async function getMoods() {
|
||||
if (!moods)
|
||||
|
@ -42,7 +57,8 @@ export async function render(
|
|||
session: req.session,
|
||||
flashes: req.flash(),
|
||||
moods,
|
||||
currentMood
|
||||
currentMood,
|
||||
nonce
|
||||
};
|
||||
res.render(page, { ...o, ...stuff });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue