video.ts 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. import * as Bluebird from 'bluebird'
  2. import { maxBy } from 'lodash'
  3. import * as magnetUtil from 'magnet-uri'
  4. import * as parseTorrent from 'parse-torrent'
  5. import { join } from 'path'
  6. import {
  7. CountOptions,
  8. FindOptions,
  9. IncludeOptions,
  10. ModelIndexesOptions,
  11. Op,
  12. QueryTypes,
  13. ScopeOptions,
  14. Sequelize,
  15. Transaction,
  16. WhereOptions
  17. } from 'sequelize'
  18. import {
  19. AllowNull,
  20. BeforeDestroy,
  21. BelongsTo,
  22. BelongsToMany,
  23. Column,
  24. CreatedAt,
  25. DataType,
  26. Default,
  27. ForeignKey,
  28. HasMany,
  29. HasOne,
  30. Is,
  31. IsInt,
  32. IsUUID,
  33. Min,
  34. Model,
  35. Scopes,
  36. Table,
  37. UpdatedAt
  38. } from 'sequelize-typescript'
  39. import { UserRight, VideoPrivacy, VideoState } from '../../../shared'
  40. import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
  41. import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
  42. import { VideoFilter } from '../../../shared/models/videos/video-query.type'
  43. import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils'
  44. import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
  45. import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc'
  46. import {
  47. isVideoCategoryValid,
  48. isVideoDescriptionValid,
  49. isVideoDurationValid,
  50. isVideoLanguageValid,
  51. isVideoLicenceValid,
  52. isVideoNameValid,
  53. isVideoPrivacyValid,
  54. isVideoStateValid,
  55. isVideoSupportValid
  56. } from '../../helpers/custom-validators/videos'
  57. import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
  58. import { logger } from '../../helpers/logger'
  59. import { getServerActor } from '../../helpers/utils'
  60. import {
  61. ACTIVITY_PUB,
  62. API_VERSION,
  63. CONSTRAINTS_FIELDS,
  64. HLS_REDUNDANCY_DIRECTORY,
  65. HLS_STREAMING_PLAYLIST_DIRECTORY,
  66. REMOTE_SCHEME,
  67. STATIC_DOWNLOAD_PATHS,
  68. STATIC_PATHS,
  69. VIDEO_CATEGORIES,
  70. VIDEO_LANGUAGES,
  71. VIDEO_LICENCES,
  72. VIDEO_PRIVACIES,
  73. VIDEO_STATES,
  74. WEBSERVER
  75. } from '../../initializers/constants'
  76. import { sendDeleteVideo } from '../../lib/activitypub/send'
  77. import { AccountModel } from '../account/account'
  78. import { AccountVideoRateModel } from '../account/account-video-rate'
  79. import { ActorModel } from '../activitypub/actor'
  80. import { AvatarModel } from '../avatar/avatar'
  81. import { ServerModel } from '../server/server'
  82. import {
  83. buildBlockedAccountSQL,
  84. buildTrigramSearchIndex,
  85. buildWhereIdOrUUID,
  86. createSimilarityAttribute,
  87. getVideoSort,
  88. isOutdated,
  89. throwIfNotValid
  90. } from '../utils'
  91. import { TagModel } from './tag'
  92. import { VideoAbuseModel } from './video-abuse'
  93. import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
  94. import { VideoCommentModel } from './video-comment'
  95. import { VideoFileModel } from './video-file'
  96. import { VideoShareModel } from './video-share'
  97. import { VideoTagModel } from './video-tag'
  98. import { ScheduleVideoUpdateModel } from './schedule-video-update'
  99. import { VideoCaptionModel } from './video-caption'
  100. import { VideoBlacklistModel } from './video-blacklist'
  101. import { remove, writeFile } from 'fs-extra'
  102. import { VideoViewModel } from './video-views'
  103. import { VideoRedundancyModel } from '../redundancy/video-redundancy'
  104. import {
  105. videoFilesModelToFormattedJSON,
  106. VideoFormattingJSONOptions,
  107. videoModelToActivityPubObject,
  108. videoModelToFormattedDetailsJSON,
  109. videoModelToFormattedJSON
  110. } from './video-format-utils'
  111. import { UserVideoHistoryModel } from '../account/user-video-history'
  112. import { UserModel } from '../account/user'
  113. import { VideoImportModel } from './video-import'
  114. import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
  115. import { VideoPlaylistElementModel } from './video-playlist-element'
  116. import { CONFIG } from '../../initializers/config'
  117. import { ThumbnailModel } from './thumbnail'
  118. import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
  119. // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
  120. const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [
  121. buildTrigramSearchIndex('video_name_trigram', 'name'),
  122. { fields: [ 'createdAt' ] },
  123. { fields: [ 'publishedAt' ] },
  124. { fields: [ 'duration' ] },
  125. { fields: [ 'views' ] },
  126. { fields: [ 'channelId' ] },
  127. {
  128. fields: [ 'originallyPublishedAt' ],
  129. where: {
  130. originallyPublishedAt: {
  131. [Op.ne]: null
  132. }
  133. }
  134. },
  135. {
  136. fields: [ 'category' ], // We don't care videos with an unknown category
  137. where: {
  138. category: {
  139. [Op.ne]: null
  140. }
  141. }
  142. },
  143. {
  144. fields: [ 'licence' ], // We don't care videos with an unknown licence
  145. where: {
  146. licence: {
  147. [Op.ne]: null
  148. }
  149. }
  150. },
  151. {
  152. fields: [ 'language' ], // We don't care videos with an unknown language
  153. where: {
  154. language: {
  155. [Op.ne]: null
  156. }
  157. }
  158. },
  159. {
  160. fields: [ 'nsfw' ], // Most of the videos are not NSFW
  161. where: {
  162. nsfw: true
  163. }
  164. },
  165. {
  166. fields: [ 'remote' ], // Only index local videos
  167. where: {
  168. remote: false
  169. }
  170. },
  171. {
  172. fields: [ 'uuid' ],
  173. unique: true
  174. },
  175. {
  176. fields: [ 'url' ],
  177. unique: true
  178. }
  179. ]
  180. export enum ScopeNames {
  181. AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
  182. FOR_API = 'FOR_API',
  183. WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
  184. WITH_TAGS = 'WITH_TAGS',
  185. WITH_FILES = 'WITH_FILES',
  186. WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE',
  187. WITH_BLACKLISTED = 'WITH_BLACKLISTED',
  188. WITH_USER_HISTORY = 'WITH_USER_HISTORY',
  189. WITH_STREAMING_PLAYLISTS = 'WITH_STREAMING_PLAYLISTS',
  190. WITH_USER_ID = 'WITH_USER_ID',
  191. WITH_THUMBNAILS = 'WITH_THUMBNAILS'
  192. }
  193. type ForAPIOptions = {
  194. ids: number[]
  195. videoPlaylistId?: number
  196. withFiles?: boolean
  197. }
  198. type AvailableForListIDsOptions = {
  199. serverAccountId: number
  200. followerActorId: number
  201. includeLocalVideos: boolean
  202. withoutId?: boolean
  203. filter?: VideoFilter
  204. categoryOneOf?: number[]
  205. nsfw?: boolean
  206. licenceOneOf?: number[]
  207. languageOneOf?: string[]
  208. tagsOneOf?: string[]
  209. tagsAllOf?: string[]
  210. withFiles?: boolean
  211. accountId?: number
  212. videoChannelId?: number
  213. videoPlaylistId?: number
  214. trendingDays?: number
  215. user?: UserModel,
  216. historyOfUser?: UserModel
  217. }
  218. @Scopes(() => ({
  219. [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => {
  220. const query: FindOptions = {
  221. where: {
  222. id: {
  223. [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken
  224. }
  225. },
  226. include: [
  227. {
  228. model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
  229. required: true
  230. },
  231. {
  232. attributes: [ 'type', 'filename' ],
  233. model: ThumbnailModel,
  234. required: false
  235. }
  236. ]
  237. }
  238. if (options.withFiles === true) {
  239. query.include.push({
  240. model: VideoFileModel.unscoped(),
  241. required: true
  242. })
  243. }
  244. if (options.videoPlaylistId) {
  245. query.include.push({
  246. model: VideoPlaylistElementModel.unscoped(),
  247. required: true,
  248. where: {
  249. videoPlaylistId: options.videoPlaylistId
  250. }
  251. })
  252. }
  253. return query
  254. },
  255. [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => {
  256. const attributes = options.withoutId === true ? [] : [ 'id' ]
  257. const query: FindOptions = {
  258. raw: true,
  259. attributes,
  260. where: {
  261. id: {
  262. [ Op.and ]: [
  263. {
  264. [ Op.notIn ]: Sequelize.literal(
  265. '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
  266. )
  267. }
  268. ]
  269. },
  270. channelId: {
  271. [ Op.notIn ]: Sequelize.literal(
  272. '(' +
  273. 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' +
  274. buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) +
  275. ')' +
  276. ')'
  277. )
  278. }
  279. },
  280. include: []
  281. }
  282. // Only list public/published videos
  283. if (!options.filter || options.filter !== 'all-local') {
  284. const privacyWhere = {
  285. // Always list public videos
  286. privacy: VideoPrivacy.PUBLIC,
  287. // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
  288. [ Op.or ]: [
  289. {
  290. state: VideoState.PUBLISHED
  291. },
  292. {
  293. [ Op.and ]: {
  294. state: VideoState.TO_TRANSCODE,
  295. waitTranscoding: false
  296. }
  297. }
  298. ]
  299. }
  300. Object.assign(query.where, privacyWhere)
  301. }
  302. if (options.videoPlaylistId) {
  303. query.include.push({
  304. attributes: [],
  305. model: VideoPlaylistElementModel.unscoped(),
  306. required: true,
  307. where: {
  308. videoPlaylistId: options.videoPlaylistId
  309. }
  310. })
  311. query.subQuery = false
  312. }
  313. if (options.filter || options.accountId || options.videoChannelId) {
  314. const videoChannelInclude: IncludeOptions = {
  315. attributes: [],
  316. model: VideoChannelModel.unscoped(),
  317. required: true
  318. }
  319. if (options.videoChannelId) {
  320. videoChannelInclude.where = {
  321. id: options.videoChannelId
  322. }
  323. }
  324. if (options.filter || options.accountId) {
  325. const accountInclude: IncludeOptions = {
  326. attributes: [],
  327. model: AccountModel.unscoped(),
  328. required: true
  329. }
  330. if (options.filter) {
  331. accountInclude.include = [
  332. {
  333. attributes: [],
  334. model: ActorModel.unscoped(),
  335. required: true,
  336. where: VideoModel.buildActorWhereWithFilter(options.filter)
  337. }
  338. ]
  339. }
  340. if (options.accountId) {
  341. accountInclude.where = { id: options.accountId }
  342. }
  343. videoChannelInclude.include = [ accountInclude ]
  344. }
  345. query.include.push(videoChannelInclude)
  346. }
  347. if (options.followerActorId) {
  348. let localVideosReq = ''
  349. if (options.includeLocalVideos === true) {
  350. localVideosReq = ' UNION ALL ' +
  351. 'SELECT "video"."id" AS "id" FROM "video" ' +
  352. 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
  353. 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
  354. 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
  355. 'WHERE "actor"."serverId" IS NULL'
  356. }
  357. // Force actorId to be a number to avoid SQL injections
  358. const actorIdNumber = parseInt(options.followerActorId.toString(), 10)
  359. query.where[ 'id' ][ Op.and ].push({
  360. [ Op.in ]: Sequelize.literal(
  361. '(' +
  362. 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
  363. 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
  364. 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
  365. ' UNION ALL ' +
  366. 'SELECT "video"."id" AS "id" FROM "video" ' +
  367. 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
  368. 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
  369. 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
  370. 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
  371. 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
  372. localVideosReq +
  373. ')'
  374. )
  375. })
  376. }
  377. if (options.withFiles === true) {
  378. query.where[ 'id' ][ Op.and ].push({
  379. [ Op.in ]: Sequelize.literal(
  380. '(SELECT "videoId" FROM "videoFile")'
  381. )
  382. })
  383. }
  384. // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN()
  385. if (options.tagsAllOf || options.tagsOneOf) {
  386. const createTagsIn = (tags: string[]) => {
  387. return tags.map(t => VideoModel.sequelize.escape(t))
  388. .join(', ')
  389. }
  390. if (options.tagsOneOf) {
  391. query.where[ 'id' ][ Op.and ].push({
  392. [ Op.in ]: Sequelize.literal(
  393. '(' +
  394. 'SELECT "videoId" FROM "videoTag" ' +
  395. 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
  396. 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' +
  397. ')'
  398. )
  399. })
  400. }
  401. if (options.tagsAllOf) {
  402. query.where[ 'id' ][ Op.and ].push({
  403. [ Op.in ]: Sequelize.literal(
  404. '(' +
  405. 'SELECT "videoId" FROM "videoTag" ' +
  406. 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
  407. 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' +
  408. 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length +
  409. ')'
  410. )
  411. })
  412. }
  413. }
  414. if (options.nsfw === true || options.nsfw === false) {
  415. query.where[ 'nsfw' ] = options.nsfw
  416. }
  417. if (options.categoryOneOf) {
  418. query.where[ 'category' ] = {
  419. [ Op.or ]: options.categoryOneOf
  420. }
  421. }
  422. if (options.licenceOneOf) {
  423. query.where[ 'licence' ] = {
  424. [ Op.or ]: options.licenceOneOf
  425. }
  426. }
  427. if (options.languageOneOf) {
  428. query.where[ 'language' ] = {
  429. [ Op.or ]: options.languageOneOf
  430. }
  431. }
  432. if (options.trendingDays) {
  433. query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
  434. query.subQuery = false
  435. }
  436. if (options.historyOfUser) {
  437. query.include.push({
  438. model: UserVideoHistoryModel,
  439. required: true,
  440. where: {
  441. userId: options.historyOfUser.id
  442. }
  443. })
  444. // Even if the relation is n:m, we know that a user only have 0..1 video history
  445. // So we won't have multiple rows for the same video
  446. // Without this, we would not be able to sort on "updatedAt" column of UserVideoHistoryModel
  447. query.subQuery = false
  448. }
  449. return query
  450. },
  451. [ ScopeNames.WITH_THUMBNAILS ]: {
  452. include: [
  453. {
  454. model: ThumbnailModel,
  455. required: false
  456. }
  457. ]
  458. },
  459. [ ScopeNames.WITH_USER_ID ]: {
  460. include: [
  461. {
  462. attributes: [ 'accountId' ],
  463. model: VideoChannelModel.unscoped(),
  464. required: true,
  465. include: [
  466. {
  467. attributes: [ 'userId' ],
  468. model: AccountModel.unscoped(),
  469. required: true
  470. }
  471. ]
  472. }
  473. ]
  474. },
  475. [ ScopeNames.WITH_ACCOUNT_DETAILS ]: {
  476. include: [
  477. {
  478. model: VideoChannelModel.unscoped(),
  479. required: true,
  480. include: [
  481. {
  482. attributes: {
  483. exclude: [ 'privateKey', 'publicKey' ]
  484. },
  485. model: ActorModel.unscoped(),
  486. required: true,
  487. include: [
  488. {
  489. attributes: [ 'host' ],
  490. model: ServerModel.unscoped(),
  491. required: false
  492. },
  493. {
  494. model: AvatarModel.unscoped(),
  495. required: false
  496. }
  497. ]
  498. },
  499. {
  500. model: AccountModel.unscoped(),
  501. required: true,
  502. include: [
  503. {
  504. model: ActorModel.unscoped(),
  505. attributes: {
  506. exclude: [ 'privateKey', 'publicKey' ]
  507. },
  508. required: true,
  509. include: [
  510. {
  511. attributes: [ 'host' ],
  512. model: ServerModel.unscoped(),
  513. required: false
  514. },
  515. {
  516. model: AvatarModel.unscoped(),
  517. required: false
  518. }
  519. ]
  520. }
  521. ]
  522. }
  523. ]
  524. }
  525. ]
  526. },
  527. [ ScopeNames.WITH_TAGS ]: {
  528. include: [ TagModel ]
  529. },
  530. [ ScopeNames.WITH_BLACKLISTED ]: {
  531. include: [
  532. {
  533. attributes: [ 'id', 'reason' ],
  534. model: VideoBlacklistModel,
  535. required: false
  536. }
  537. ]
  538. },
  539. [ ScopeNames.WITH_FILES ]: (withRedundancies = false) => {
  540. let subInclude: any[] = []
  541. if (withRedundancies === true) {
  542. subInclude = [
  543. {
  544. attributes: [ 'fileUrl' ],
  545. model: VideoRedundancyModel.unscoped(),
  546. required: false
  547. }
  548. ]
  549. }
  550. return {
  551. include: [
  552. {
  553. model: VideoFileModel.unscoped(),
  554. separate: true, // We may have multiple files, having multiple redundancies so let's separate this join
  555. required: false,
  556. include: subInclude
  557. }
  558. ]
  559. }
  560. },
  561. [ ScopeNames.WITH_STREAMING_PLAYLISTS ]: (withRedundancies = false) => {
  562. let subInclude: any[] = []
  563. if (withRedundancies === true) {
  564. subInclude = [
  565. {
  566. attributes: [ 'fileUrl' ],
  567. model: VideoRedundancyModel.unscoped(),
  568. required: false
  569. }
  570. ]
  571. }
  572. return {
  573. include: [
  574. {
  575. model: VideoStreamingPlaylistModel.unscoped(),
  576. separate: true, // We may have multiple streaming playlists, having multiple redundancies so let's separate this join
  577. required: false,
  578. include: subInclude
  579. }
  580. ]
  581. }
  582. },
  583. [ ScopeNames.WITH_SCHEDULED_UPDATE ]: {
  584. include: [
  585. {
  586. model: ScheduleVideoUpdateModel.unscoped(),
  587. required: false
  588. }
  589. ]
  590. },
  591. [ ScopeNames.WITH_USER_HISTORY ]: (userId: number) => {
  592. return {
  593. include: [
  594. {
  595. attributes: [ 'currentTime' ],
  596. model: UserVideoHistoryModel.unscoped(),
  597. required: false,
  598. where: {
  599. userId
  600. }
  601. }
  602. ]
  603. }
  604. }
  605. }))
  606. @Table({
  607. tableName: 'video',
  608. indexes
  609. })
  610. export class VideoModel extends Model<VideoModel> {
  611. @AllowNull(false)
  612. @Default(DataType.UUIDV4)
  613. @IsUUID(4)
  614. @Column(DataType.UUID)
  615. uuid: string
  616. @AllowNull(false)
  617. @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
  618. @Column
  619. name: string
  620. @AllowNull(true)
  621. @Default(null)
  622. @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category', true))
  623. @Column
  624. category: number
  625. @AllowNull(true)
  626. @Default(null)
  627. @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence', true))
  628. @Column
  629. licence: number
  630. @AllowNull(true)
  631. @Default(null)
  632. @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language', true))
  633. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
  634. language: string
  635. @AllowNull(false)
  636. @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
  637. @Column
  638. privacy: number
  639. @AllowNull(false)
  640. @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
  641. @Column
  642. nsfw: boolean
  643. @AllowNull(true)
  644. @Default(null)
  645. @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description', true))
  646. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
  647. description: string
  648. @AllowNull(true)
  649. @Default(null)
  650. @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support', true))
  651. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
  652. support: string
  653. @AllowNull(false)
  654. @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
  655. @Column
  656. duration: number
  657. @AllowNull(false)
  658. @Default(0)
  659. @IsInt
  660. @Min(0)
  661. @Column
  662. views: number
  663. @AllowNull(false)
  664. @Default(0)
  665. @IsInt
  666. @Min(0)
  667. @Column
  668. likes: number
  669. @AllowNull(false)
  670. @Default(0)
  671. @IsInt
  672. @Min(0)
  673. @Column
  674. dislikes: number
  675. @AllowNull(false)
  676. @Column
  677. remote: boolean
  678. @AllowNull(false)
  679. @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
  680. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
  681. url: string
  682. @AllowNull(false)
  683. @Column
  684. commentsEnabled: boolean
  685. @AllowNull(false)
  686. @Column
  687. downloadEnabled: boolean
  688. @AllowNull(false)
  689. @Column
  690. waitTranscoding: boolean
  691. @AllowNull(false)
  692. @Default(null)
  693. @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
  694. @Column
  695. state: VideoState
  696. @CreatedAt
  697. createdAt: Date
  698. @UpdatedAt
  699. updatedAt: Date
  700. @AllowNull(false)
  701. @Default(DataType.NOW)
  702. @Column
  703. publishedAt: Date
  704. @AllowNull(true)
  705. @Default(null)
  706. @Column
  707. originallyPublishedAt: Date
  708. @ForeignKey(() => VideoChannelModel)
  709. @Column
  710. channelId: number
  711. @BelongsTo(() => VideoChannelModel, {
  712. foreignKey: {
  713. allowNull: true
  714. },
  715. hooks: true
  716. })
  717. VideoChannel: VideoChannelModel
  718. @BelongsToMany(() => TagModel, {
  719. foreignKey: 'videoId',
  720. through: () => VideoTagModel,
  721. onDelete: 'CASCADE'
  722. })
  723. Tags: TagModel[]
  724. @HasMany(() => ThumbnailModel, {
  725. foreignKey: {
  726. name: 'videoId',
  727. allowNull: true
  728. },
  729. hooks: true,
  730. onDelete: 'cascade'
  731. })
  732. Thumbnails: ThumbnailModel[]
  733. @HasMany(() => VideoPlaylistElementModel, {
  734. foreignKey: {
  735. name: 'videoId',
  736. allowNull: false
  737. },
  738. onDelete: 'cascade'
  739. })
  740. VideoPlaylistElements: VideoPlaylistElementModel[]
  741. @HasMany(() => VideoAbuseModel, {
  742. foreignKey: {
  743. name: 'videoId',
  744. allowNull: false
  745. },
  746. onDelete: 'cascade'
  747. })
  748. VideoAbuses: VideoAbuseModel[]
  749. @HasMany(() => VideoFileModel, {
  750. foreignKey: {
  751. name: 'videoId',
  752. allowNull: false
  753. },
  754. hooks: true,
  755. onDelete: 'cascade'
  756. })
  757. VideoFiles: VideoFileModel[]
  758. @HasMany(() => VideoStreamingPlaylistModel, {
  759. foreignKey: {
  760. name: 'videoId',
  761. allowNull: false
  762. },
  763. hooks: true,
  764. onDelete: 'cascade'
  765. })
  766. VideoStreamingPlaylists: VideoStreamingPlaylistModel[]
  767. @HasMany(() => VideoShareModel, {
  768. foreignKey: {
  769. name: 'videoId',
  770. allowNull: false
  771. },
  772. onDelete: 'cascade'
  773. })
  774. VideoShares: VideoShareModel[]
  775. @HasMany(() => AccountVideoRateModel, {
  776. foreignKey: {
  777. name: 'videoId',
  778. allowNull: false
  779. },
  780. onDelete: 'cascade'
  781. })
  782. AccountVideoRates: AccountVideoRateModel[]
  783. @HasMany(() => VideoCommentModel, {
  784. foreignKey: {
  785. name: 'videoId',
  786. allowNull: false
  787. },
  788. onDelete: 'cascade',
  789. hooks: true
  790. })
  791. VideoComments: VideoCommentModel[]
  792. @HasMany(() => VideoViewModel, {
  793. foreignKey: {
  794. name: 'videoId',
  795. allowNull: false
  796. },
  797. onDelete: 'cascade'
  798. })
  799. VideoViews: VideoViewModel[]
  800. @HasMany(() => UserVideoHistoryModel, {
  801. foreignKey: {
  802. name: 'videoId',
  803. allowNull: false
  804. },
  805. onDelete: 'cascade'
  806. })
  807. UserVideoHistories: UserVideoHistoryModel[]
  808. @HasOne(() => ScheduleVideoUpdateModel, {
  809. foreignKey: {
  810. name: 'videoId',
  811. allowNull: false
  812. },
  813. onDelete: 'cascade'
  814. })
  815. ScheduleVideoUpdate: ScheduleVideoUpdateModel
  816. @HasOne(() => VideoBlacklistModel, {
  817. foreignKey: {
  818. name: 'videoId',
  819. allowNull: false
  820. },
  821. onDelete: 'cascade'
  822. })
  823. VideoBlacklist: VideoBlacklistModel
  824. @HasOne(() => VideoImportModel, {
  825. foreignKey: {
  826. name: 'videoId',
  827. allowNull: true
  828. },
  829. onDelete: 'set null'
  830. })
  831. VideoImport: VideoImportModel
  832. @HasMany(() => VideoCaptionModel, {
  833. foreignKey: {
  834. name: 'videoId',
  835. allowNull: false
  836. },
  837. onDelete: 'cascade',
  838. hooks: true,
  839. [ 'separate' as any ]: true
  840. })
  841. VideoCaptions: VideoCaptionModel[]
  842. @BeforeDestroy
  843. static async sendDelete (instance: VideoModel, options) {
  844. if (instance.isOwned()) {
  845. if (!instance.VideoChannel) {
  846. instance.VideoChannel = await instance.$get('VideoChannel', {
  847. include: [
  848. {
  849. model: AccountModel,
  850. include: [ ActorModel ]
  851. }
  852. ],
  853. transaction: options.transaction
  854. }) as VideoChannelModel
  855. }
  856. return sendDeleteVideo(instance, options.transaction)
  857. }
  858. return undefined
  859. }
  860. @BeforeDestroy
  861. static async removeFiles (instance: VideoModel) {
  862. const tasks: Promise<any>[] = []
  863. logger.info('Removing files of video %s.', instance.url)
  864. if (instance.isOwned()) {
  865. if (!Array.isArray(instance.VideoFiles)) {
  866. instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
  867. }
  868. // Remove physical files and torrents
  869. instance.VideoFiles.forEach(file => {
  870. tasks.push(instance.removeFile(file))
  871. tasks.push(instance.removeTorrent(file))
  872. })
  873. // Remove playlists file
  874. tasks.push(instance.removeStreamingPlaylist())
  875. }
  876. // Do not wait video deletion because we could be in a transaction
  877. Promise.all(tasks)
  878. .catch(err => {
  879. logger.error('Some errors when removing files of video %s in before destroy hook.', instance.uuid, { err })
  880. })
  881. return undefined
  882. }
  883. static listLocal () {
  884. const query = {
  885. where: {
  886. remote: false
  887. }
  888. }
  889. return VideoModel.scope([
  890. ScopeNames.WITH_FILES,
  891. ScopeNames.WITH_STREAMING_PLAYLISTS,
  892. ScopeNames.WITH_THUMBNAILS
  893. ]).findAll(query)
  894. }
  895. static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
  896. function getRawQuery (select: string) {
  897. const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
  898. 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
  899. 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
  900. 'WHERE "Account"."actorId" = ' + actorId
  901. const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
  902. 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
  903. 'WHERE "VideoShare"."actorId" = ' + actorId
  904. return `(${queryVideo}) UNION (${queryVideoShare})`
  905. }
  906. const rawQuery = getRawQuery('"Video"."id"')
  907. const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
  908. const query = {
  909. distinct: true,
  910. offset: start,
  911. limit: count,
  912. order: getVideoSort('createdAt', [ 'Tags', 'name', 'ASC' ] as any), // FIXME: sequelize typings
  913. where: {
  914. id: {
  915. [ Op.in ]: Sequelize.literal('(' + rawQuery + ')')
  916. },
  917. [ Op.or ]: [
  918. { privacy: VideoPrivacy.PUBLIC },
  919. { privacy: VideoPrivacy.UNLISTED }
  920. ]
  921. },
  922. include: [
  923. {
  924. attributes: [ 'language' ],
  925. model: VideoCaptionModel.unscoped(),
  926. required: false
  927. },
  928. {
  929. attributes: [ 'id', 'url' ],
  930. model: VideoShareModel.unscoped(),
  931. required: false,
  932. // We only want videos shared by this actor
  933. where: {
  934. [ Op.and ]: [
  935. {
  936. id: {
  937. [ Op.not ]: null
  938. }
  939. },
  940. {
  941. actorId
  942. }
  943. ]
  944. },
  945. include: [
  946. {
  947. attributes: [ 'id', 'url' ],
  948. model: ActorModel.unscoped()
  949. }
  950. ]
  951. },
  952. {
  953. model: VideoChannelModel.unscoped(),
  954. required: true,
  955. include: [
  956. {
  957. attributes: [ 'name' ],
  958. model: AccountModel.unscoped(),
  959. required: true,
  960. include: [
  961. {
  962. attributes: [ 'id', 'url', 'followersUrl' ],
  963. model: ActorModel.unscoped(),
  964. required: true
  965. }
  966. ]
  967. },
  968. {
  969. attributes: [ 'id', 'url', 'followersUrl' ],
  970. model: ActorModel.unscoped(),
  971. required: true
  972. }
  973. ]
  974. },
  975. VideoFileModel,
  976. TagModel
  977. ]
  978. }
  979. return Bluebird.all([
  980. VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findAll(query),
  981. VideoModel.sequelize.query<{ total: string }>(rawCountQuery, { type: QueryTypes.SELECT })
  982. ]).then(([ rows, totals ]) => {
  983. // totals: totalVideos + totalVideoShares
  984. let totalVideos = 0
  985. let totalVideoShares = 0
  986. if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10)
  987. if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10)
  988. const total = totalVideos + totalVideoShares
  989. return {
  990. data: rows,
  991. total: total
  992. }
  993. })
  994. }
  995. static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) {
  996. function buildBaseQuery (): FindOptions {
  997. return {
  998. offset: start,
  999. limit: count,
  1000. order: getVideoSort(sort),
  1001. include: [
  1002. {
  1003. model: VideoChannelModel,
  1004. required: true,
  1005. include: [
  1006. {
  1007. model: AccountModel,
  1008. where: {
  1009. id: accountId
  1010. },
  1011. required: true
  1012. }
  1013. ]
  1014. }
  1015. ]
  1016. }
  1017. }
  1018. const countQuery = buildBaseQuery()
  1019. const findQuery = buildBaseQuery()
  1020. findQuery.include.push({
  1021. model: ScheduleVideoUpdateModel,
  1022. required: false
  1023. })
  1024. findQuery.include.push({
  1025. model: VideoBlacklistModel,
  1026. required: false
  1027. })
  1028. if (withFiles === true) {
  1029. findQuery.include.push({
  1030. model: VideoFileModel.unscoped(),
  1031. required: true
  1032. })
  1033. }
  1034. return Promise.all([
  1035. VideoModel.count(countQuery),
  1036. VideoModel.findAll(findQuery)
  1037. ]).then(([ count, rows ]) => {
  1038. return {
  1039. data: rows,
  1040. total: count
  1041. }
  1042. })
  1043. }
  1044. static async listForApi (options: {
  1045. start: number,
  1046. count: number,
  1047. sort: string,
  1048. nsfw: boolean,
  1049. includeLocalVideos: boolean,
  1050. withFiles: boolean,
  1051. categoryOneOf?: number[],
  1052. licenceOneOf?: number[],
  1053. languageOneOf?: string[],
  1054. tagsOneOf?: string[],
  1055. tagsAllOf?: string[],
  1056. filter?: VideoFilter,
  1057. accountId?: number,
  1058. videoChannelId?: number,
  1059. followerActorId?: number
  1060. videoPlaylistId?: number,
  1061. trendingDays?: number,
  1062. user?: UserModel,
  1063. historyOfUser?: UserModel
  1064. }, countVideos = true) {
  1065. if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) {
  1066. throw new Error('Try to filter all-local but no user has not the see all videos right')
  1067. }
  1068. const query: FindOptions = {
  1069. offset: options.start,
  1070. limit: options.count,
  1071. order: getVideoSort(options.sort)
  1072. }
  1073. let trendingDays: number
  1074. if (options.sort.endsWith('trending')) {
  1075. trendingDays = CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
  1076. query.group = 'VideoModel.id'
  1077. }
  1078. const serverActor = await getServerActor()
  1079. // followerActorId === null has a meaning, so just check undefined
  1080. const followerActorId = options.followerActorId !== undefined ? options.followerActorId : serverActor.id
  1081. const queryOptions = {
  1082. followerActorId,
  1083. serverAccountId: serverActor.Account.id,
  1084. nsfw: options.nsfw,
  1085. categoryOneOf: options.categoryOneOf,
  1086. licenceOneOf: options.licenceOneOf,
  1087. languageOneOf: options.languageOneOf,
  1088. tagsOneOf: options.tagsOneOf,
  1089. tagsAllOf: options.tagsAllOf,
  1090. filter: options.filter,
  1091. withFiles: options.withFiles,
  1092. accountId: options.accountId,
  1093. videoChannelId: options.videoChannelId,
  1094. videoPlaylistId: options.videoPlaylistId,
  1095. includeLocalVideos: options.includeLocalVideos,
  1096. user: options.user,
  1097. historyOfUser: options.historyOfUser,
  1098. trendingDays
  1099. }
  1100. return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
  1101. }
  1102. static async searchAndPopulateAccountAndServer (options: {
  1103. includeLocalVideos: boolean
  1104. search?: string
  1105. start?: number
  1106. count?: number
  1107. sort?: string
  1108. startDate?: string // ISO 8601
  1109. endDate?: string // ISO 8601
  1110. originallyPublishedStartDate?: string
  1111. originallyPublishedEndDate?: string
  1112. nsfw?: boolean
  1113. categoryOneOf?: number[]
  1114. licenceOneOf?: number[]
  1115. languageOneOf?: string[]
  1116. tagsOneOf?: string[]
  1117. tagsAllOf?: string[]
  1118. durationMin?: number // seconds
  1119. durationMax?: number // seconds
  1120. user?: UserModel,
  1121. filter?: VideoFilter
  1122. }) {
  1123. const whereAnd = []
  1124. if (options.startDate || options.endDate) {
  1125. const publishedAtRange = {}
  1126. if (options.startDate) publishedAtRange[ Op.gte ] = options.startDate
  1127. if (options.endDate) publishedAtRange[ Op.lte ] = options.endDate
  1128. whereAnd.push({ publishedAt: publishedAtRange })
  1129. }
  1130. if (options.originallyPublishedStartDate || options.originallyPublishedEndDate) {
  1131. const originallyPublishedAtRange = {}
  1132. if (options.originallyPublishedStartDate) originallyPublishedAtRange[ Op.gte ] = options.originallyPublishedStartDate
  1133. if (options.originallyPublishedEndDate) originallyPublishedAtRange[ Op.lte ] = options.originallyPublishedEndDate
  1134. whereAnd.push({ originallyPublishedAt: originallyPublishedAtRange })
  1135. }
  1136. if (options.durationMin || options.durationMax) {
  1137. const durationRange = {}
  1138. if (options.durationMin) durationRange[ Op.gte ] = options.durationMin
  1139. if (options.durationMax) durationRange[ Op.lte ] = options.durationMax
  1140. whereAnd.push({ duration: durationRange })
  1141. }
  1142. const attributesInclude = []
  1143. const escapedSearch = VideoModel.sequelize.escape(options.search)
  1144. const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
  1145. if (options.search) {
  1146. whereAnd.push(
  1147. {
  1148. id: {
  1149. [ Op.in ]: Sequelize.literal(
  1150. '(' +
  1151. 'SELECT "video"."id" FROM "video" ' +
  1152. 'WHERE ' +
  1153. 'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
  1154. 'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
  1155. 'UNION ALL ' +
  1156. 'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' +
  1157. 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
  1158. 'WHERE "tag"."name" = ' + escapedSearch +
  1159. ')'
  1160. )
  1161. }
  1162. }
  1163. )
  1164. attributesInclude.push(createSimilarityAttribute('VideoModel.name', options.search))
  1165. }
  1166. // Cannot search on similarity if we don't have a search
  1167. if (!options.search) {
  1168. attributesInclude.push(
  1169. Sequelize.literal('0 as similarity')
  1170. )
  1171. }
  1172. const query: FindOptions = {
  1173. attributes: {
  1174. include: attributesInclude
  1175. },
  1176. offset: options.start,
  1177. limit: options.count,
  1178. order: getVideoSort(options.sort),
  1179. where: {
  1180. [ Op.and ]: whereAnd
  1181. }
  1182. }
  1183. const serverActor = await getServerActor()
  1184. const queryOptions = {
  1185. followerActorId: serverActor.id,
  1186. serverAccountId: serverActor.Account.id,
  1187. includeLocalVideos: options.includeLocalVideos,
  1188. nsfw: options.nsfw,
  1189. categoryOneOf: options.categoryOneOf,
  1190. licenceOneOf: options.licenceOneOf,
  1191. languageOneOf: options.languageOneOf,
  1192. tagsOneOf: options.tagsOneOf,
  1193. tagsAllOf: options.tagsAllOf,
  1194. user: options.user,
  1195. filter: options.filter
  1196. }
  1197. return VideoModel.getAvailableForApi(query, queryOptions)
  1198. }
  1199. static load (id: number | string, t?: Transaction) {
  1200. const where = buildWhereIdOrUUID(id)
  1201. const options = {
  1202. where,
  1203. transaction: t
  1204. }
  1205. return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options)
  1206. }
  1207. static loadWithRights (id: number | string, t?: Transaction) {
  1208. const where = buildWhereIdOrUUID(id)
  1209. const options = {
  1210. where,
  1211. transaction: t
  1212. }
  1213. return VideoModel.scope([
  1214. ScopeNames.WITH_BLACKLISTED,
  1215. ScopeNames.WITH_USER_ID,
  1216. ScopeNames.WITH_THUMBNAILS
  1217. ]).findOne(options)
  1218. }
  1219. static loadOnlyId (id: number | string, t?: Transaction) {
  1220. const where = buildWhereIdOrUUID(id)
  1221. const options = {
  1222. attributes: [ 'id' ],
  1223. where,
  1224. transaction: t
  1225. }
  1226. return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options)
  1227. }
  1228. static loadWithFiles (id: number, t?: Transaction, logging?: boolean) {
  1229. return VideoModel.scope([
  1230. ScopeNames.WITH_FILES,
  1231. ScopeNames.WITH_STREAMING_PLAYLISTS,
  1232. ScopeNames.WITH_THUMBNAILS
  1233. ]).findByPk(id, { transaction: t, logging })
  1234. }
  1235. static loadByUUIDWithFile (uuid: string) {
  1236. const options = {
  1237. where: {
  1238. uuid
  1239. }
  1240. }
  1241. return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options)
  1242. }
  1243. static loadByUrl (url: string, transaction?: Transaction) {
  1244. const query: FindOptions = {
  1245. where: {
  1246. url
  1247. },
  1248. transaction
  1249. }
  1250. return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query)
  1251. }
  1252. static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction) {
  1253. const query: FindOptions = {
  1254. where: {
  1255. url
  1256. },
  1257. transaction
  1258. }
  1259. return VideoModel.scope([
  1260. ScopeNames.WITH_ACCOUNT_DETAILS,
  1261. ScopeNames.WITH_FILES,
  1262. ScopeNames.WITH_STREAMING_PLAYLISTS,
  1263. ScopeNames.WITH_THUMBNAILS
  1264. ]).findOne(query)
  1265. }
  1266. static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number) {
  1267. const where = buildWhereIdOrUUID(id)
  1268. const options = {
  1269. order: [ [ 'Tags', 'name', 'ASC' ] ] as any,
  1270. where,
  1271. transaction: t
  1272. }
  1273. const scopes: (string | ScopeOptions)[] = [
  1274. ScopeNames.WITH_TAGS,
  1275. ScopeNames.WITH_BLACKLISTED,
  1276. ScopeNames.WITH_ACCOUNT_DETAILS,
  1277. ScopeNames.WITH_SCHEDULED_UPDATE,
  1278. ScopeNames.WITH_FILES,
  1279. ScopeNames.WITH_STREAMING_PLAYLISTS,
  1280. ScopeNames.WITH_THUMBNAILS
  1281. ]
  1282. if (userId) {
  1283. scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] })
  1284. }
  1285. return VideoModel
  1286. .scope(scopes)
  1287. .findOne(options)
  1288. }
  1289. static loadForGetAPI (id: number | string, t?: Transaction, userId?: number) {
  1290. const where = buildWhereIdOrUUID(id)
  1291. const options = {
  1292. order: [ [ 'Tags', 'name', 'ASC' ] ] as any, // FIXME: sequelize typings
  1293. where,
  1294. transaction: t
  1295. }
  1296. const scopes: (string | ScopeOptions)[] = [
  1297. ScopeNames.WITH_TAGS,
  1298. ScopeNames.WITH_BLACKLISTED,
  1299. ScopeNames.WITH_ACCOUNT_DETAILS,
  1300. ScopeNames.WITH_SCHEDULED_UPDATE,
  1301. ScopeNames.WITH_THUMBNAILS,
  1302. { method: [ ScopeNames.WITH_FILES, true ] },
  1303. { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] }
  1304. ]
  1305. if (userId) {
  1306. scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] })
  1307. }
  1308. return VideoModel
  1309. .scope(scopes)
  1310. .findOne(options)
  1311. }
  1312. static async getStats () {
  1313. const totalLocalVideos = await VideoModel.count({
  1314. where: {
  1315. remote: false
  1316. }
  1317. })
  1318. const totalVideos = await VideoModel.count()
  1319. let totalLocalVideoViews = await VideoModel.sum('views', {
  1320. where: {
  1321. remote: false
  1322. }
  1323. })
  1324. // Sequelize could return null...
  1325. if (!totalLocalVideoViews) totalLocalVideoViews = 0
  1326. return {
  1327. totalLocalVideos,
  1328. totalLocalVideoViews,
  1329. totalVideos
  1330. }
  1331. }
  1332. static incrementViews (id: number, views: number) {
  1333. return VideoModel.increment('views', {
  1334. by: views,
  1335. where: {
  1336. id
  1337. }
  1338. })
  1339. }
  1340. static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) {
  1341. // Instances only share videos
  1342. const query = 'SELECT 1 FROM "videoShare" ' +
  1343. 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
  1344. 'WHERE "actorFollow"."actorId" = $followerActorId AND "videoShare"."videoId" = $videoId ' +
  1345. 'LIMIT 1'
  1346. const options = {
  1347. type: QueryTypes.SELECT,
  1348. bind: { followerActorId, videoId },
  1349. raw: true
  1350. }
  1351. return VideoModel.sequelize.query(query, options)
  1352. .then(results => results.length === 1)
  1353. }
  1354. // threshold corresponds to how many video the field should have to be returned
  1355. static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
  1356. const serverActor = await getServerActor()
  1357. const followerActorId = serverActor.id
  1358. const scopeOptions: AvailableForListIDsOptions = {
  1359. serverAccountId: serverActor.Account.id,
  1360. followerActorId,
  1361. includeLocalVideos: true,
  1362. withoutId: true // Don't break aggregation
  1363. }
  1364. const query: FindOptions = {
  1365. attributes: [ field ],
  1366. limit: count,
  1367. group: field,
  1368. having: Sequelize.where(
  1369. Sequelize.fn('COUNT', Sequelize.col(field)), { [ Op.gte ]: threshold }
  1370. ),
  1371. order: [ (this.sequelize as any).random() ]
  1372. }
  1373. return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] })
  1374. .findAll(query)
  1375. .then(rows => rows.map(r => r[ field ]))
  1376. }
  1377. static buildTrendingQuery (trendingDays: number) {
  1378. return {
  1379. attributes: [],
  1380. subQuery: false,
  1381. model: VideoViewModel,
  1382. required: false,
  1383. where: {
  1384. startDate: {
  1385. [ Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
  1386. }
  1387. }
  1388. }
  1389. }
  1390. private static buildActorWhereWithFilter (filter?: VideoFilter) {
  1391. if (filter && (filter === 'local' || filter === 'all-local')) {
  1392. return {
  1393. serverId: null
  1394. }
  1395. }
  1396. return {}
  1397. }
  1398. private static async getAvailableForApi (
  1399. query: FindOptions,
  1400. options: AvailableForListIDsOptions,
  1401. countVideos = true
  1402. ) {
  1403. const idsScope: ScopeOptions = {
  1404. method: [
  1405. ScopeNames.AVAILABLE_FOR_LIST_IDS, options
  1406. ]
  1407. }
  1408. // Remove trending sort on count, because it uses a group by
  1409. const countOptions = Object.assign({}, options, { trendingDays: undefined })
  1410. const countQuery: CountOptions = Object.assign({}, query, { attributes: undefined, group: undefined })
  1411. const countScope: ScopeOptions = {
  1412. method: [
  1413. ScopeNames.AVAILABLE_FOR_LIST_IDS, countOptions
  1414. ]
  1415. }
  1416. const [ count, rowsId ] = await Promise.all([
  1417. countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve<number>(undefined),
  1418. VideoModel.scope(idsScope).findAll(query)
  1419. ])
  1420. const ids = rowsId.map(r => r.id)
  1421. if (ids.length === 0) return { data: [], total: count }
  1422. const secondQuery: FindOptions = {
  1423. offset: 0,
  1424. limit: query.limit,
  1425. attributes: query.attributes,
  1426. order: [ // Keep original order
  1427. Sequelize.literal(
  1428. ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ')
  1429. )
  1430. ]
  1431. }
  1432. const apiScope: (string | ScopeOptions)[] = []
  1433. if (options.user) {
  1434. apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
  1435. }
  1436. apiScope.push({
  1437. method: [
  1438. ScopeNames.FOR_API, {
  1439. ids,
  1440. withFiles: options.withFiles,
  1441. videoPlaylistId: options.videoPlaylistId
  1442. } as ForAPIOptions
  1443. ]
  1444. })
  1445. const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
  1446. return {
  1447. data: rows,
  1448. total: count
  1449. }
  1450. }
  1451. static getCategoryLabel (id: number) {
  1452. return VIDEO_CATEGORIES[ id ] || 'Misc'
  1453. }
  1454. static getLicenceLabel (id: number) {
  1455. return VIDEO_LICENCES[ id ] || 'Unknown'
  1456. }
  1457. static getLanguageLabel (id: string) {
  1458. return VIDEO_LANGUAGES[ id ] || 'Unknown'
  1459. }
  1460. static getPrivacyLabel (id: number) {
  1461. return VIDEO_PRIVACIES[ id ] || 'Unknown'
  1462. }
  1463. static getStateLabel (id: number) {
  1464. return VIDEO_STATES[ id ] || 'Unknown'
  1465. }
  1466. getOriginalFile () {
  1467. if (Array.isArray(this.VideoFiles) === false) return undefined
  1468. // The original file is the file that have the higher resolution
  1469. return maxBy(this.VideoFiles, file => file.resolution)
  1470. }
  1471. async addAndSaveThumbnail (thumbnail: ThumbnailModel, transaction: Transaction) {
  1472. thumbnail.videoId = this.id
  1473. const savedThumbnail = await thumbnail.save({ transaction })
  1474. if (Array.isArray(this.Thumbnails) === false) this.Thumbnails = []
  1475. // Already have this thumbnail, skip
  1476. if (this.Thumbnails.find(t => t.id === savedThumbnail.id)) return
  1477. this.Thumbnails.push(savedThumbnail)
  1478. }
  1479. getVideoFilename (videoFile: VideoFileModel) {
  1480. return this.uuid + '-' + videoFile.resolution + videoFile.extname
  1481. }
  1482. generateThumbnailName () {
  1483. return this.uuid + '.jpg'
  1484. }
  1485. getMiniature () {
  1486. if (Array.isArray(this.Thumbnails) === false) return undefined
  1487. return this.Thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
  1488. }
  1489. generatePreviewName () {
  1490. return this.uuid + '.jpg'
  1491. }
  1492. getPreview () {
  1493. if (Array.isArray(this.Thumbnails) === false) return undefined
  1494. return this.Thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
  1495. }
  1496. getTorrentFileName (videoFile: VideoFileModel) {
  1497. const extension = '.torrent'
  1498. return this.uuid + '-' + videoFile.resolution + extension
  1499. }
  1500. isOwned () {
  1501. return this.remote === false
  1502. }
  1503. getTorrentFilePath (videoFile: VideoFileModel) {
  1504. return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
  1505. }
  1506. getVideoFilePath (videoFile: VideoFileModel) {
  1507. return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
  1508. }
  1509. async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
  1510. const options = {
  1511. // Keep the extname, it's used by the client to stream the file inside a web browser
  1512. name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
  1513. createdBy: 'PeerTube',
  1514. announceList: [
  1515. [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
  1516. [ WEBSERVER.URL + '/tracker/announce' ]
  1517. ],
  1518. urlList: [ WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
  1519. }
  1520. const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
  1521. const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
  1522. logger.info('Creating torrent %s.', filePath)
  1523. await writeFile(filePath, torrent)
  1524. const parsedTorrent = parseTorrent(torrent)
  1525. videoFile.infoHash = parsedTorrent.infoHash
  1526. }
  1527. getWatchStaticPath () {
  1528. return '/videos/watch/' + this.uuid
  1529. }
  1530. getEmbedStaticPath () {
  1531. return '/videos/embed/' + this.uuid
  1532. }
  1533. getMiniatureStaticPath () {
  1534. const thumbnail = this.getMiniature()
  1535. if (!thumbnail) return null
  1536. return join(STATIC_PATHS.THUMBNAILS, thumbnail.filename)
  1537. }
  1538. getPreviewStaticPath () {
  1539. const preview = this.getPreview()
  1540. if (!preview) return null
  1541. // We use a local cache, so specify our cache endpoint instead of potential remote URL
  1542. return join(STATIC_PATHS.PREVIEWS, preview.filename)
  1543. }
  1544. toFormattedJSON (options?: VideoFormattingJSONOptions): Video {
  1545. return videoModelToFormattedJSON(this, options)
  1546. }
  1547. toFormattedDetailsJSON (): VideoDetails {
  1548. return videoModelToFormattedDetailsJSON(this)
  1549. }
  1550. getFormattedVideoFilesJSON (): VideoFile[] {
  1551. return videoFilesModelToFormattedJSON(this, this.VideoFiles)
  1552. }
  1553. toActivityPubObject (): VideoTorrentObject {
  1554. return videoModelToActivityPubObject(this)
  1555. }
  1556. getTruncatedDescription () {
  1557. if (!this.description) return null
  1558. const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
  1559. return peertubeTruncate(this.description, maxLength)
  1560. }
  1561. getOriginalFileResolution () {
  1562. const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
  1563. return getVideoFileResolution(originalFilePath)
  1564. }
  1565. getDescriptionAPIPath () {
  1566. return `/api/${API_VERSION}/videos/${this.uuid}/description`
  1567. }
  1568. removeFile (videoFile: VideoFileModel, isRedundancy = false) {
  1569. const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
  1570. const filePath = join(baseDir, this.getVideoFilename(videoFile))
  1571. return remove(filePath)
  1572. .catch(err => logger.warn('Cannot delete file %s.', filePath, { err }))
  1573. }
  1574. removeTorrent (videoFile: VideoFileModel) {
  1575. const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
  1576. return remove(torrentPath)
  1577. .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
  1578. }
  1579. removeStreamingPlaylist (isRedundancy = false) {
  1580. const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY
  1581. const filePath = join(baseDir, this.uuid)
  1582. return remove(filePath)
  1583. .catch(err => logger.warn('Cannot delete playlist directory %s.', filePath, { err }))
  1584. }
  1585. isOutdated () {
  1586. if (this.isOwned()) return false
  1587. return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL)
  1588. }
  1589. setAsRefreshed () {
  1590. this.changed('updatedAt', true)
  1591. return this.save()
  1592. }
  1593. getBaseUrls () {
  1594. let baseUrlHttp
  1595. let baseUrlWs
  1596. if (this.isOwned()) {
  1597. baseUrlHttp = WEBSERVER.URL
  1598. baseUrlWs = WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT
  1599. } else {
  1600. baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
  1601. baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
  1602. }
  1603. return { baseUrlHttp, baseUrlWs }
  1604. }
  1605. generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
  1606. const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
  1607. const announce = this.getTrackerUrls(baseUrlHttp, baseUrlWs)
  1608. let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
  1609. const redundancies = videoFile.RedundancyVideos
  1610. if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
  1611. const magnetHash = {
  1612. xs,
  1613. announce,
  1614. urlList,
  1615. infoHash: videoFile.infoHash,
  1616. name: this.name
  1617. }
  1618. return magnetUtil.encode(magnetHash)
  1619. }
  1620. getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
  1621. return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
  1622. }
  1623. getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
  1624. return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
  1625. }
  1626. getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
  1627. return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
  1628. }
  1629. getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
  1630. return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
  1631. }
  1632. getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
  1633. return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile)
  1634. }
  1635. getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
  1636. return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
  1637. }
  1638. getBandwidthBits (videoFile: VideoFileModel) {
  1639. return Math.ceil((videoFile.size * 8) / this.duration)
  1640. }
  1641. }