You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
import { getCache, setCache, requireUser, getConfig } from "#server/utils/context";
|
|
import { dbGlobal } from "drizzle-pkg/lib/db";
|
|
import { users } from "drizzle-pkg/lib/schema/auth";
|
|
import { eq } from "drizzle-orm";
|
|
import { KNOWN_CONFIG_KEYS } from "#server/service/config/registry";
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
const user = await requireUser(event);
|
|
const cacheKey = `config:me:${user.id}`;
|
|
|
|
const cached = await getCache<{ user: unknown, config: unknown }>(cacheKey);
|
|
if (cached) return R.success(cached);
|
|
|
|
const entries = await Promise.all(
|
|
KNOWN_CONFIG_KEYS.map(async (key) => {
|
|
const value = await getConfig(event, key);
|
|
return [key, value] as const;
|
|
}),
|
|
);
|
|
|
|
const [row] = await dbGlobal
|
|
.select({
|
|
nickname: users.nickname,
|
|
avatar: users.avatar,
|
|
})
|
|
.from(users)
|
|
.where(eq(users.id, user.id))
|
|
.limit(1);
|
|
|
|
const result = {
|
|
user: {
|
|
id: user.id,
|
|
username: user.username,
|
|
role: user.role,
|
|
nickname: row?.nickname ?? null,
|
|
avatar: row?.avatar ?? null,
|
|
},
|
|
config: Object.fromEntries(entries),
|
|
};
|
|
await setCache(cacheKey, result, 60);
|
|
return R.success(result);
|
|
});
|
|
|