Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/[lang]/u/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { ANALYTICS_EVENTS } from '@/lib/analytics/events';
import { trackEvent } from '@/lib/analytics/track.server';
import { getCurrentUser } from '@/lib/auth';
import { notifyProfileView } from '@/lib/notifications/profileView';
import { PublicProfileClient } from './PublicProfileClient';

export default async function PublicProfileRoute({
Expand All @@ -32,11 +33,16 @@ export default async function PublicProfileRoute({
let currentProfileId: string | undefined;
let viewerTimezone: string | undefined;
let viewerUserId: string | undefined;
let viewerActor: { name: string; avatarUrl: string | null } | null = null;

if (user) {
isLoggedIn = true;
const persistedUser = await upsertDiscordUser(user);
viewerUserId = persistedUser.id;
viewerActor = {
name: persistedUser.displayName,
avatarUrl: persistedUser.avatarUrl,
};
const viewerProfile = await getProfileByUserId(persistedUser.id);
currentProfileId = viewerProfile?.profile.id;
savedProfileIds = await listSavedProfileIds(persistedUser.id);
Expand All @@ -54,6 +60,13 @@ export default async function PublicProfileRoute({
locale: lang,
});

if (viewerUserId !== row.profile.userId) {
await notifyProfileView({
ownerUserId: row.profile.userId,
actor: viewerActor,
});
}

return (
<PublicProfileClient
locale={lang}
Expand Down
25 changes: 24 additions & 1 deletion src/db/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'server-only';

import { and, desc, eq } from 'drizzle-orm';
import { and, desc, eq, gte } from 'drizzle-orm';
import { db } from './client';
import {
type NewNotificationRecord,
Expand Down Expand Up @@ -58,6 +58,29 @@ export const createNotification = async (values: NewNotificationRecord) => {
return created;
};

export const hasRecentViewNotification = async (
userId: string,
actorName: string | null,
windowStart: Date,
): Promise<boolean> => {
const [existing] = await db
.select({ id: notifications.id })
.from(notifications)
.where(
and(
eq(notifications.userId, userId),
eq(notifications.kind, 'view'),
actorName === null
? eq(notifications.isGuest, true)
: eq(notifications.actorName, actorName),
gte(notifications.createdAt, windowStart),
),
)
.limit(1);

return existing !== undefined;
};

export const setNotificationRead = async (
userId: string,
notificationId: string,
Expand Down
41 changes: 41 additions & 0 deletions src/lib/notifications/profileView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'server-only';

import { createNotification, hasRecentViewNotification } from '@/db';

const VIEW_DEDUPE_WINDOW_MS = 60 * 60 * 1000;

type ProfileViewActor = {
name: string;
avatarUrl: string | null;
};

export const notifyProfileView = async ({
ownerUserId,
actor,
}: {
ownerUserId: string;
actor: ProfileViewActor | null;
}): Promise<void> => {
try {
const windowStart = new Date(Date.now() - VIEW_DEDUPE_WINDOW_MS);
const duplicate = await hasRecentViewNotification(
ownerUserId,
actor?.name ?? null,
windowStart,
);

if (duplicate) {
return;
}

await createNotification({
userId: ownerUserId,
kind: 'view',
actorName: actor?.name ?? null,
actorAvatarUrl: actor?.avatarUrl ?? null,
isGuest: actor === null,
});
} catch (error) {
console.error('profile view notification failed', error);
}
};
Loading