File size: 1,096 Bytes
df83860
 
ac7030c
df83860
6967c22
ac7030c
df83860
9cea1bb
 
 
 
 
 
ac7030c
9cea1bb
 
df83860
9cea1bb
 
 
6967c22
df83860
9cea1bb
df83860
9cea1bb
 
 
df83860
ac7030c
29f166e
 
 
 
 
 
 
 
 
 
f70dd7e
9cea1bb
 
 
 
 
 
 
df83860
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use server"

import { MediaInfo } from "@/types/general"

import { getVideoIndex } from "./getVideoIndex"
import { getStatsForMedias } from "../stats"

export async function getVideo({
  videoId,
  neverThrow,
}: {
  videoId?: string | string[] | null,
  neverThrow?: boolean
}): Promise<MediaInfo | undefined> {
  try {
    const id = `${videoId || ""}`

    if (!id) {
      throw new Error(`cannot get the video, invalid id: "${id}"`)
    }
    const published = await getVideoIndex({ status: "published" })

    const video = published[id] || undefined

    if (!video) {
      throw new Error(`cannot get the video, nothing found for id "${id}"`)
    }

    const allStats = await getStatsForMedias([video.id])
    
    const stats = allStats[video.id] || {
      numberOfViews: 0,
      numberOfLikes: 0,
      numberOfDislikes: 0,
    }

    video.numberOfViews = stats.numberOfViews
    video.numberOfLikes = stats.numberOfLikes
    video.numberOfDislikes = stats.numberOfDislikes

    return video
  } catch (err) {
    if (neverThrow) {
      return undefined
    }
    throw err
  }
}