43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import "server-only";
|
|
import { eq } from "drizzle-orm";
|
|
import { WebsiteProfilePrivacy } from "@/db/profile-privacy";
|
|
import { db } from "@/lib/db";
|
|
export const profilePrivacyKeys = [
|
|
"wallet",
|
|
"online",
|
|
"friends",
|
|
"photos",
|
|
"registered",
|
|
] as const;
|
|
export type ProfilePrivacy = Record<
|
|
(typeof profilePrivacyKeys)[number],
|
|
boolean
|
|
>;
|
|
export const defaultProfilePrivacy: ProfilePrivacy = {
|
|
wallet: false,
|
|
online: true,
|
|
friends: true,
|
|
photos: true,
|
|
registered: true,
|
|
};
|
|
const hiddenProfilePrivacy: ProfilePrivacy = {
|
|
wallet: false,
|
|
online: false,
|
|
friends: false,
|
|
photos: false,
|
|
registered: false,
|
|
};
|
|
export async function loadProfilePrivacy(userId: number) {
|
|
try {
|
|
const [row] = await db
|
|
.select()
|
|
.from(WebsiteProfilePrivacy)
|
|
.where(eq(WebsiteProfilePrivacy.userId, userId))
|
|
.limit(1);
|
|
return { values: row ?? defaultProfilePrivacy, unavailable: false };
|
|
} catch {
|
|
// A failed privacy lookup must never expose fields whose visibility is unknown.
|
|
return { values: hiddenProfilePrivacy, unavailable: true };
|
|
}
|
|
}
|