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,17 +15,27 @@ 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 } }) => {
const videos = await prisma.video.findMany({
where: { where: {
userId: ctx.session.user.id, userId: session.user.id,
}, },
}); });
posthog.capture({
distinctId: session.user.id,
event: "viewing video list",
properties: {
videoAmount: videos.length,
},
});
void posthog.shutdownAsync();
const videosWithThumbnailUrl = await Promise.all( const videosWithThumbnailUrl = await Promise.all(
videos.map(async (video) => { videos.map(async (video) => {
const thumbnailUrl = await getSignedUrl( const thumbnailUrl = await getSignedUrl(
ctx.s3, s3,
new GetObjectCommand({ new GetObjectCommand({
Bucket: env.AWS_BUCKET_NAME, Bucket: env.AWS_BUCKET_NAME,
Key: video.userId + "/" + video.id + "-thumbnail", Key: video.userId + "/" + video.id + "-thumbnail",
@ -37,7 +47,8 @@ export const videoRouter = createTRPCRouter({
); );
return videosWithThumbnailUrl; 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 }) => {