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
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Notification as APINotification } from "@cap/web-api-contract";
import type { ImageUpload } from "@cap/web-domain";
import { faComment, faEye, faReply } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import clsx from "clsx";
import moment from "moment";
import Link from "next/link";
import { markAsRead } from "@/actions/notifications/mark-as-read";
import { SignedImageUrl } from "@/components/SignedImageUrl";
import type { NotificationType } from "@/lib/Notification";

type NotificationItemProps = {
Expand Down Expand Up @@ -45,17 +47,12 @@ export const NotificationItem = ({
>
{/* Avatar */}
<div className="relative flex-shrink-0">
{notification.author.avatar ? (
<img
src={notification.author.avatar}
alt={notification.author.name}
className="object-cover rounded-full size-10"
/>
) : (
<div className="flex justify-center items-center text-xl font-medium text-white bg-purple-500 rounded-full size-10">
{notification.author.name.charAt(0)}
</div>
)}
<SignedImageUrl
image={notification.author.avatar as ImageUpload.ImageUrl | null}
name={notification.author.name}
className="relative flex-shrink-0 size-7"
letterClass="text-sm"
/>
{notification.readAt === null && (
<div className="absolute top-0 right-0 size-2.5 rounded-full bg-red-500 border-2 border-gray-1"></div>
)}
Expand Down
71 changes: 43 additions & 28 deletions apps/web/app/api/notifications/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { notifications, users } from "@cap/database/schema";
import { Notification as APINotification } from "@cap/web-api-contract";
import { and, ColumnBaseConfig, desc, eq, isNull, sql } from "drizzle-orm";
import { MySqlColumn } from "drizzle-orm/mysql-core";
import { ImageUploads } from "@cap/web-backend";
import { and, desc, eq, isNull, sql } from "drizzle-orm";
import { Effect } from "effect";
import { NextResponse } from "next/server";
import { AvcProfileInfo } from "node_modules/@remotion/media-parser/dist/containers/avc/parse-avc";
import { z } from "zod";
import type { NotificationType } from "@/lib/Notification";
import { runPromise } from "@/lib/server";
import { jsonExtractString } from "@/utils/sql";

const notificationDataSchema = z.object({
Expand Down Expand Up @@ -90,32 +91,46 @@ export async function GET() {
formattedCountResults[type] = Number(count);
});

const formattedNotifications = notificationsWithAuthors
.map(({ notification, author }) => {
try {
// all notifications currently require an author
if (!author) return;
const formattedNotifications = await Effect.gen(function* () {
const imageUploads = yield* ImageUploads;

return APINotification.parse({
id: notification.id,
type: notification.type,
readAt: notification.readAt,
videoId: notification.data.videoId,
createdAt: notification.createdAt,
data: notification.data,
comment: notification.data.comment,
author: {
id: author.id,
name: author.name ?? "Unknown",
avatar: author.avatar,
},
});
} catch (error) {
console.error("Invalid notification data:", error);
return null;
}
})
.filter(Boolean);
return yield* Effect.all(
notificationsWithAuthors.map(({ notification, author }) =>
Effect.gen(function* () {
// all notifications currently require an author
if (!author) return null;

const resolvedAvatar = author.avatar
? yield* imageUploads
.resolveImageUrl(author.avatar)
.pipe(Effect.catchAll(() => Effect.succeed(null)))
: null;

return APINotification.parse({
id: notification.id,
type: notification.type,
readAt: notification.readAt,
videoId: notification.data.videoId,
createdAt: notification.createdAt,
data: notification.data,
comment: notification.data.comment,
author: {
id: author.id,
name: author.name ?? "Unknown",
avatar: resolvedAvatar,
},
});
}).pipe(
Effect.catchAll((error) => {
console.error("Invalid notification data:", error);
return Effect.succeed(null);
}),
),
),
);
})
.pipe(runPromise)
.then((results) => results.filter(Boolean));

return NextResponse.json({
notifications: formattedNotifications,
Expand Down