video.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { CONFIG } from '@server/initializers/config.js'
  2. import { VideoModel } from '@server/models/video/video.js'
  3. import {
  4. MVideoAccountLightBlacklistAllFiles,
  5. MVideoFormattableDetails,
  6. MVideoFullLight,
  7. MVideoId,
  8. MVideoImmutable,
  9. MVideoThumbnailBlacklist
  10. } from '@server/types/models/index.js'
  11. import { getOrCreateAPVideo } from '../activitypub/videos/get.js'
  12. type VideoLoadType = 'for-api' | 'all' | 'only-video-and-blacklist' | 'id' | 'none' | 'unsafe-only-immutable-attributes'
  13. function loadVideo (id: number | string, fetchType: 'for-api', userId?: number): Promise<MVideoFormattableDetails>
  14. function loadVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight>
  15. function loadVideo (id: number | string, fetchType: 'unsafe-only-immutable-attributes'): Promise<MVideoImmutable>
  16. function loadVideo (id: number | string, fetchType: 'only-video-and-blacklist', userId?: number): Promise<MVideoThumbnailBlacklist>
  17. function loadVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoId>
  18. function loadVideo (
  19. id: number | string,
  20. fetchType: VideoLoadType,
  21. userId?: number
  22. ): Promise<MVideoFullLight | MVideoThumbnailBlacklist | MVideoId | MVideoImmutable>
  23. function loadVideo (
  24. id: number | string,
  25. fetchType: VideoLoadType,
  26. userId?: number
  27. ): Promise<MVideoFullLight | MVideoThumbnailBlacklist | MVideoId | MVideoImmutable> {
  28. if (fetchType === 'for-api') return VideoModel.loadForGetAPI({ id, userId })
  29. if (fetchType === 'all') return VideoModel.loadFull(id, undefined, userId)
  30. if (fetchType === 'unsafe-only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
  31. if (fetchType === 'only-video-and-blacklist') return VideoModel.loadWithBlacklist(id)
  32. if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
  33. }
  34. type VideoLoadByUrlType = 'all' | 'only-video-and-blacklist' | 'unsafe-only-immutable-attributes'
  35. function loadVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles>
  36. function loadVideoByUrl (url: string, fetchType: 'unsafe-only-immutable-attributes'): Promise<MVideoImmutable>
  37. function loadVideoByUrl (url: string, fetchType: 'only-video-and-blacklist'): Promise<MVideoThumbnailBlacklist>
  38. function loadVideoByUrl (
  39. url: string,
  40. fetchType: VideoLoadByUrlType
  41. ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnailBlacklist | MVideoImmutable>
  42. function loadVideoByUrl (
  43. url: string,
  44. fetchType: VideoLoadByUrlType
  45. ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnailBlacklist | MVideoImmutable> {
  46. if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccountAndFiles(url)
  47. if (fetchType === 'unsafe-only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url)
  48. if (fetchType === 'only-video-and-blacklist') return VideoModel.loadByUrlWithBlacklist(url)
  49. }
  50. async function loadOrCreateVideoIfAllowedForUser (videoUrl: string) {
  51. if (CONFIG.SEARCH.REMOTE_URI.USERS) {
  52. try {
  53. const res = await getOrCreateAPVideo({
  54. videoObject: videoUrl,
  55. fetchType: 'unsafe-only-immutable-attributes',
  56. allowRefresh: false
  57. })
  58. return res?.video
  59. } catch {
  60. return undefined
  61. }
  62. }
  63. return VideoModel.loadByUrlImmutableAttributes(videoUrl)
  64. }
  65. export {
  66. loadOrCreateVideoIfAllowedForUser, loadVideo,
  67. loadVideoByUrl,
  68. type VideoLoadByUrlType,
  69. type VideoLoadType
  70. }