Implement per-user post permissions and move stats into dedicated pages

This commit is contained in:
爱喝水的木子
2026-03-20 11:51:58 +08:00
parent 17f5f6adcb
commit 466b7c3fb6
29 changed files with 1416 additions and 475 deletions

38
lib/users.ts Normal file
View File

@@ -0,0 +1,38 @@
import { ObjectId } from "mongodb";
import { getDb } from "@/lib/mongo";
export const DEFAULT_DAILY_POST_LIMIT = 10;
const SHANGHAI_OFFSET_HOURS = 8;
export function getEffectiveDailyPostLimit(user?: { dailyPostLimit?: number | null }) {
if (typeof user?.dailyPostLimit === "number" && user.dailyPostLimit >= 0) {
return user.dailyPostLimit;
}
return DEFAULT_DAILY_POST_LIMIT;
}
export function getShanghaiDayRange(now = new Date()) {
const shifted = new Date(now.getTime() + SHANGHAI_OFFSET_HOURS * 60 * 60 * 1000);
const year = shifted.getUTCFullYear();
const month = shifted.getUTCMonth();
const day = shifted.getUTCDate();
const start = new Date(Date.UTC(year, month, day, -SHANGHAI_OFFSET_HOURS, 0, 0, 0));
const end = new Date(Date.UTC(year, month, day + 1, -SHANGHAI_OFFSET_HOURS, 0, 0, 0));
return {
start,
end,
startIso: start.toISOString(),
endIso: end.toISOString()
};
}
export async function findUserById(uid?: string | null) {
if (!uid || !ObjectId.isValid(uid)) {
return null;
}
const db = await getDb();
return db.collection("users").findOne({ _id: new ObjectId(uid) });
}