feat(notifications) : add profile pictures and guide previews
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import 'server-only';
|
||||
import { and, desc, eq, isNull, or, sql } from 'drizzle-orm';
|
||||
import { and, desc, eq, inArray, isNull, or, sql } from 'drizzle-orm';
|
||||
import { alias } from 'drizzle-orm/pg-core';
|
||||
import { getDb } from '@/db';
|
||||
import { notifications, comments, commentThreads, guides } from '@/db/schema';
|
||||
import { notifications, comments, commentThreads, guides, users, media } from '@/db/schema';
|
||||
import { publicMediaUrl } from '@/lib/media/storage';
|
||||
import { notifyNotificationChange } from './events';
|
||||
|
||||
type Recipient = { userId: string; adminOnly?: boolean; url: string };
|
||||
@@ -21,8 +22,9 @@ export async function listNotifications(user: { id: string; role: string | null;
|
||||
eq(comments.hidden, false), sql`coalesce(${root.hidden}, false) = false`,
|
||||
or(isNull(commentThreads.guideId), and(eq(guides.isPublic, true), isNull(guides.trashedAt))),
|
||||
))));
|
||||
const query = () => getDb().select({ notification: notifications, cursorTime: sql<string>`to_char(${notifications.createdAt} at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` }).from(notifications)
|
||||
const query = () => getDb().select({ notification: notifications, authorName: users.name, authorImage: users.image, cursorTime: sql<string>`to_char(${notifications.createdAt} at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` }).from(notifications)
|
||||
.leftJoin(comments, eq(comments.id, notifications.commentId)).leftJoin(root, eq(root.id, comments.rootId))
|
||||
.leftJoin(users, and(eq(users.id, comments.authorId), inArray(notifications.kind, ['comment_new', 'comment_reply', 'comment_edit'])))
|
||||
.leftJoin(commentThreads, eq(commentThreads.id, comments.threadId)).leftJoin(guides, eq(guides.id, commentThreads.guideId));
|
||||
const [rows, count] = await Promise.all([
|
||||
query().where(and(visible, before ? sql`(${notifications.createdAt}, ${notifications.id}) < (${before.time}::timestamptz, ${before.id}::uuid)` : undefined)).orderBy(desc(notifications.createdAt), desc(notifications.id)).limit(21),
|
||||
@@ -31,7 +33,19 @@ export async function listNotifications(user: { id: string; role: string | null;
|
||||
.leftJoin(commentThreads, eq(commentThreads.id, comments.threadId)).leftJoin(guides, eq(guides.id, commentThreads.guideId))
|
||||
.where(and(visible, isNull(notifications.readAt))),
|
||||
]);
|
||||
const items = rows.slice(0, 20).map(row => row.notification);
|
||||
const page = rows.slice(0, 20);
|
||||
const guideIds = [...new Set(page.flatMap(({ notification }) => {
|
||||
if (notification.kind !== 'guide_published') return [];
|
||||
const match = /^guide:([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}):published$/i.exec(notification.eventKey);
|
||||
return match ? [match[1]] : [];
|
||||
}))];
|
||||
const covers = guideIds.length ? await getDb().select({ id: guides.id, objectKey: media.objectKey }).from(guides)
|
||||
.innerJoin(media, eq(media.id, guides.coverMediaId))
|
||||
.where(and(inArray(guides.id, guideIds), eq(guides.isPublic, true), isNull(guides.trashedAt))) : [];
|
||||
const guideImages = new Map(covers.map(cover => [`guide:${cover.id}:published`, publicMediaUrl(cover.objectKey)]));
|
||||
const items = page.map(row => ({ ...row.notification, authorName: row.authorName, authorImage: row.authorImage,
|
||||
imageUrl: row.notification.kind === 'guide_published' ? guideImages.get(row.notification.eventKey) ?? null : null,
|
||||
}));
|
||||
const last = rows[19];
|
||||
return { items, unread: count[0]?.count ?? 0, nextCursor: rows.length > 20 ? `${last.cursorTime}|${last.notification.id}` : null };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user