add viewing video list event

This commit is contained in:
MarconLP 2023-04-23 13:17:52 +02:00
parent ba7766f2f6
commit ea5166c416
No known key found for this signature in database
GPG key ID: A08A9C8B623F5EA5

View file

@ -15,29 +15,40 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
export const videoRouter = createTRPCRouter({ export const videoRouter = createTRPCRouter({
getAll: protectedProcedure.query(async ({ ctx }) => { getAll: protectedProcedure.query(
const videos = await ctx.prisma.video.findMany({ async ({ ctx: { prisma, session, s3, posthog } }) => {
where: { const videos = await prisma.video.findMany({
userId: ctx.session.user.id, where: {
}, userId: session.user.id,
}); },
});
const videosWithThumbnailUrl = await Promise.all( posthog.capture({
videos.map(async (video) => { distinctId: session.user.id,
const thumbnailUrl = await getSignedUrl( event: "viewing video list",
ctx.s3, properties: {
new GetObjectCommand({ videoAmount: videos.length,
Bucket: env.AWS_BUCKET_NAME, },
Key: video.userId + "/" + video.id + "-thumbnail", });
}) void posthog.shutdownAsync();
);
return { ...video, thumbnailUrl }; const videosWithThumbnailUrl = await Promise.all(
}) videos.map(async (video) => {
); const thumbnailUrl = await getSignedUrl(
s3,
new GetObjectCommand({
Bucket: env.AWS_BUCKET_NAME,
Key: video.userId + "/" + video.id + "-thumbnail",
})
);
return videosWithThumbnailUrl; return { ...video, thumbnailUrl };
}), })
);
return videosWithThumbnailUrl;
}
),
get: publicProcedure get: publicProcedure
.input(z.object({ videoId: z.string() })) .input(z.object({ videoId: z.string() }))
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {