X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=20e1f1c4aa97c043243001a231b75d23df473f17;hb=0374b6b5cd685316f924874b2a3068bb345eb0dd;hp=164122ee20d3084f5fa6b75ca31af972dc1f5bf2;hpb=227eb02f9ac100e1314cab4c4751e450bbe14d21;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 164122ee2..20e1f1c4a 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1,18 +1,7 @@ import * as Bluebird from 'bluebird' import { maxBy, minBy } from 'lodash' import { join } from 'path' -import { - CountOptions, - FindOptions, - IncludeOptions, - ModelIndexesOptions, - Op, - QueryTypes, - ScopeOptions, - Sequelize, - Transaction, - WhereOptions -} from 'sequelize' +import { CountOptions, FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -142,75 +131,7 @@ import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/mode import { MThumbnail } from '../../typings/models/video/thumbnail' import { VideoFile } from '@shared/models/videos/video-file.model' import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' -import * as validator from 'validator' - -// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation -const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [ - buildTrigramSearchIndex('video_name_trigram', 'name'), - - { fields: [ 'createdAt' ] }, - { - fields: [ - { name: 'publishedAt', order: 'DESC' }, - { name: 'id', order: 'ASC' } - ] - }, - { fields: [ 'duration' ] }, - { fields: [ 'views' ] }, - { fields: [ 'channelId' ] }, - { - fields: [ 'originallyPublishedAt' ], - where: { - originallyPublishedAt: { - [Op.ne]: null - } - } - }, - { - fields: [ 'category' ], // We don't care videos with an unknown category - where: { - category: { - [Op.ne]: null - } - } - }, - { - fields: [ 'licence' ], // We don't care videos with an unknown licence - where: { - licence: { - [Op.ne]: null - } - } - }, - { - fields: [ 'language' ], // We don't care videos with an unknown language - where: { - language: { - [Op.ne]: null - } - } - }, - { - fields: [ 'nsfw' ], // Most of the videos are not NSFW - where: { - nsfw: true - } - }, - { - fields: [ 'remote' ], // Only index local videos - where: { - remote: false - } - }, - { - fields: [ 'uuid' ], - unique: true - }, - { - fields: [ 'url' ], - unique: true - } -] +import validator from 'validator' export enum ScopeNames { AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS', @@ -291,7 +212,7 @@ export type AvailableForListIDsOptions = { if (options.ids) { query.where = { id: { - [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken + [ Op.in ]: options.ids } } } @@ -452,7 +373,14 @@ export type AvailableForListIDsOptions = { 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + - ' UNION ALL ' + + ')' + ) + } + }, + { + id: { + [ Op.in ]: Sequelize.literal( + '(' + 'SELECT "video"."id" AS "id" FROM "video" ' + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + @@ -752,7 +680,72 @@ export type AvailableForListIDsOptions = { })) @Table({ tableName: 'video', - indexes + indexes: [ + buildTrigramSearchIndex('video_name_trigram', 'name'), + + { fields: [ 'createdAt' ] }, + { + fields: [ + { name: 'publishedAt', order: 'DESC' }, + { name: 'id', order: 'ASC' } + ] + }, + { fields: [ 'duration' ] }, + { fields: [ 'views' ] }, + { fields: [ 'channelId' ] }, + { + fields: [ 'originallyPublishedAt' ], + where: { + originallyPublishedAt: { + [Op.ne]: null + } + } + }, + { + fields: [ 'category' ], // We don't care videos with an unknown category + where: { + category: { + [Op.ne]: null + } + } + }, + { + fields: [ 'licence' ], // We don't care videos with an unknown licence + where: { + licence: { + [Op.ne]: null + } + } + }, + { + fields: [ 'language' ], // We don't care videos with an unknown language + where: { + language: { + [Op.ne]: null + } + } + }, + { + fields: [ 'nsfw' ], // Most of the videos are not NSFW + where: { + nsfw: true + } + }, + { + fields: [ 'remote' ], // Only index local videos + where: { + remote: false + } + }, + { + fields: [ 'uuid' ], + unique: true + }, + { + fields: [ 'url' ], + unique: true + } + ] }) export class VideoModel extends Model { @@ -1054,7 +1047,7 @@ export class VideoModel extends Model { if (instance.isOwned()) { if (!Array.isArray(instance.VideoFiles)) { - instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[] + instance.VideoFiles = await instance.$get('VideoFiles') } // Remove physical files and torrents @@ -1064,7 +1057,13 @@ export class VideoModel extends Model { }) // Remove playlists file - tasks.push(instance.removeStreamingPlaylist()) + if (!Array.isArray(instance.VideoStreamingPlaylists)) { + instance.VideoStreamingPlaylists = await instance.$get('VideoStreamingPlaylists') + } + + for (const p of instance.VideoStreamingPlaylists) { + tasks.push(instance.removeStreamingPlaylistFiles(p)) + } } // Do not wait video deletion because we could be in a transaction @@ -1196,9 +1195,15 @@ export class VideoModel extends Model { }) } - static listUserVideosForApi (accountId: number, start: number, count: number, sort: string) { + static listUserVideosForApi ( + accountId: number, + start: number, + count: number, + sort: string, + search?: string + ) { function buildBaseQuery (): FindOptions { - return { + let baseQuery = { offset: start, limit: count, order: getVideoSort(sort), @@ -1218,12 +1223,24 @@ export class VideoModel extends Model { } ] } + + if (search) { + baseQuery = Object.assign(baseQuery, { + where: { + name: { + [ Op.iLike ]: '%' + search + '%' + } + } + }) + } + + return baseQuery } const countQuery = buildBaseQuery() const findQuery = buildBaseQuery() - const findScopes = [ + const findScopes: (string | ScopeOptions)[] = [ ScopeNames.WITH_SCHEDULED_UPDATE, ScopeNames.WITH_BLACKLISTED, ScopeNames.WITH_THUMBNAILS @@ -1259,8 +1276,9 @@ export class VideoModel extends Model { videoPlaylistId?: number, trendingDays?: number, user?: MUserAccountId, - historyOfUser?: MUserId - }, countVideos = true) { + historyOfUser?: MUserId, + countVideos?: boolean + }) { if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { throw new Error('Try to filter all-local but no user has not the see all videos right') } @@ -1303,7 +1321,7 @@ export class VideoModel extends Model { trendingDays } - return VideoModel.getAvailableForApi(query, queryOptions, countVideos) + return VideoModel.getAvailableForApi(query, queryOptions, options.countVideos) } static async searchAndPopulateAccountAndServer (options: { @@ -1725,17 +1743,25 @@ export class VideoModel extends Model { ] } - const [ count, ids ] = await Promise.all([ + const [ count, rows ] = await Promise.all([ countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve(undefined), VideoModel.scope(idsScope) - .findAll(query) + .findAll(Object.assign({}, query, { raw: true })) .then(rows => rows.map(r => r.id)) + .then(ids => VideoModel.loadCompleteVideosForApi(ids, query, options)) ]) - if (ids.length === 0) return { data: [], total: count } + return { + data: rows, + total: count + } + } + + private static loadCompleteVideosForApi (ids: number[], query: FindOptions, options: AvailableForListIDsOptions) { + if (ids.length === 0) return [] const secondQuery: FindOptions = { offset: 0, @@ -1764,16 +1790,13 @@ export class VideoModel extends Model { ] }) - const rows = await VideoModel.scope(apiScope).findAll(secondQuery) - - return { - data: rows, - total: count - } + return VideoModel.scope(apiScope).findAll(secondQuery) } private static isPrivacyForFederation (privacy: VideoPrivacy) { - return privacy === VideoPrivacy.PUBLIC || privacy === VideoPrivacy.UNLISTED + const castedPrivacy = parseInt(privacy + '', 10) + + return castedPrivacy === VideoPrivacy.PUBLIC || castedPrivacy === VideoPrivacy.UNLISTED } static getCategoryLabel (id: number) { @@ -1970,11 +1993,24 @@ export class VideoModel extends Model { .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) } - removeStreamingPlaylist (isRedundancy = false) { + async removeStreamingPlaylistFiles (streamingPlaylist: MStreamingPlaylist, isRedundancy = false) { const directoryPath = getHLSDirectory(this, isRedundancy) - return remove(directoryPath) - .catch(err => logger.warn('Cannot delete playlist directory %s.', directoryPath, { err })) + await remove(directoryPath) + + if (isRedundancy !== true) { + let streamingPlaylistWithFiles = streamingPlaylist as MStreamingPlaylistFilesVideo + streamingPlaylistWithFiles.Video = this + + if (!Array.isArray(streamingPlaylistWithFiles.VideoFiles)) { + streamingPlaylistWithFiles.VideoFiles = await streamingPlaylistWithFiles.$get('VideoFiles') + } + + // Remove physical files and torrents + await Promise.all( + streamingPlaylistWithFiles.VideoFiles.map(file => streamingPlaylistWithFiles.removeTorrent(file)) + ) + } } isOutdated () {