File size: 2,579 Bytes
a3f1817
 
ac7030c
a3f1817
 
 
6967c22
f70dd7e
3d4392e
1185ec1
a3f1817
 
 
 
 
8f2b05f
a3f1817
b161bd3
a3f1817
 
 
8f2b05f
 
ac7030c
a3f1817
b161bd3
0f35d4c
8f2b05f
 
 
 
 
 
a3f1817
8f2b05f
 
 
 
a3f1817
8f2b05f
ac7030c
8f2b05f
 
1185ec1
 
 
 
 
 
 
 
 
 
8f2b05f
1185ec1
8f2b05f
 
 
 
 
 
 
1f1caeb
 
 
 
 
8f2b05f
a3f1817
8f2b05f
 
 
 
 
a3f1817
8f2b05f
 
 
 
 
 
a3f1817
8f2b05f
 
f70dd7e
8f2b05f
 
 
 
 
 
 
 
 
f70dd7e
8f2b05f
 
a3f1817
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use server"

import { ChannelInfo, MediaInfo, VideoStatus } from "@/types/general"

import { getVideoRequestsFromChannel  } from "./getVideoRequestsFromChannel"
import { adminApiKey } from "../config"
import { getVideoIndex } from "./getVideoIndex"
import { extendVideosWithStats } from "./extendVideosWithStats"
import { computeOrientationProjectionWidthHeight } from "../../utils/computeOrientationProjectionWidthHeight"
import { defaultVideoModel } from "@/app/config"

// return 
export async function getChannelVideos({
  channel,
  status,
  neverThrow,
}: {
  channel?: ChannelInfo

  // filter videos by status
  status?: VideoStatus

  neverThrow?: boolean
}): Promise<MediaInfo[]> {

  if (!channel) { return [] }

  try {
    const videos = await getVideoRequestsFromChannel({
      channel,
      apiKey: adminApiKey,
      renewCache: true
    })

    // TODO: use a database instead
    // normally 
    const queued = await getVideoIndex({ status: "queued" })
    const published = await getVideoIndex({ status: "published" })

    const validVideos = videos.map(v => {
    let video: MediaInfo = {
        id: v.id,
        status: "submitted",
        label: v.label || "",
        description: v.description || "",
        prompt: v.prompt || "",
        thumbnailUrl: v.thumbnailUrl || "",
        clapUrl: v.clapUrl || "",
        model: v.model || defaultVideoModel,
        lora: v.lora || "",
        style: v.style || "",
        voice: v.voice || "",
        music: v.music || "",
        assetUrl: "",
        assetUrlHd: "",
        numberOfViews: 0,
        numberOfLikes: 0,
        numberOfDislikes: 0,
        updatedAt: v.updatedAt,
        tags: v.tags,
        channel,
        duration: v.duration || 0,
        ...computeOrientationProjectionWidthHeight({
          lora: v.lora,
          orientation: v.orientation,
          // projection, // <- will be extrapolated from the LoRA for now
        }),
      }

      if (queued[v.id]) {
        video = queued[v.id]
      } else if (published[v.id]) {
        video = published[v.id]
      }

      return video
    }).filter(video => {
      // if no filter is requested, we always return the video
      if (!status || typeof status === "undefined") {
        return true
      }

      return video.status === status
    })

    // ask Redis for the freshest stats
    const results = await extendVideosWithStats(validVideos)

    return results
  } catch (err) {
    if (neverThrow) {
      console.error("failed to get channel videos:", err)
      return []
    }

    throw err
  }
}