feat : update
This commit is contained in:
+18
-3
@@ -10,6 +10,7 @@ type AdminPresence = {
|
||||
name: string;
|
||||
email: string;
|
||||
guideId: string;
|
||||
section: string | null;
|
||||
};
|
||||
|
||||
function prefix(): string {
|
||||
@@ -56,6 +57,10 @@ export async function updateVisitorPresence({
|
||||
guide: guideId
|
||||
? scopes.filter((scope) => scope === guideId).length
|
||||
: null,
|
||||
byGuide: scopes.reduce<Record<string, number>>((counts, scope) => {
|
||||
if (scope) counts[scope] = (counts[scope] ?? 0) + 1;
|
||||
return counts;
|
||||
}, {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -63,11 +68,13 @@ export async function updateAdminPresence({
|
||||
memberId,
|
||||
user,
|
||||
guideId,
|
||||
section,
|
||||
active,
|
||||
}: {
|
||||
memberId: string;
|
||||
user: { id: string; name?: string | null; email: string };
|
||||
guideId: string | null;
|
||||
section: string | null;
|
||||
active: boolean;
|
||||
}) {
|
||||
const redis = await getRedisClient();
|
||||
@@ -80,6 +87,7 @@ export async function updateAdminPresence({
|
||||
name: user.name?.trim() || user.email,
|
||||
email: user.email,
|
||||
guideId,
|
||||
section,
|
||||
};
|
||||
await redis.multi()
|
||||
.zadd(activeKey, now + PRESENCE_TTL_MS, memberId)
|
||||
@@ -91,14 +99,21 @@ export async function updateAdminPresence({
|
||||
await purge(redis, activeKey, detailKey, now);
|
||||
const ids = await redis.zrangebyscore(activeKey, now + 1, "+inf");
|
||||
const serialized = ids.length ? await redis.hmget(detailKey, ...ids) : [];
|
||||
const editorsByGuide: Record<string, Array<{ userId: string; name: string }>> = {};
|
||||
const editorsByGuide: Record<string, Array<{ userId: string; name: string; sections: string[] }>> = {};
|
||||
for (const value of serialized) {
|
||||
if (!value) continue;
|
||||
try {
|
||||
const detail = JSON.parse(value) as AdminPresence;
|
||||
const editors = editorsByGuide[detail.guideId] ?? [];
|
||||
if (!editors.some((editor) => editor.userId === detail.userId)) {
|
||||
editors.push({ userId: detail.userId, name: detail.name || detail.email });
|
||||
const existing = editors.find((editor) => editor.userId === detail.userId);
|
||||
if (existing && detail.section && !existing.sections.includes(detail.section)) {
|
||||
existing.sections.push(detail.section);
|
||||
} else if (!existing) {
|
||||
editors.push({
|
||||
userId: detail.userId,
|
||||
name: detail.name || detail.email,
|
||||
sections: detail.section ? [detail.section] : [],
|
||||
});
|
||||
}
|
||||
editorsByGuide[detail.guideId] = editors;
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user