X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=ec3d5ddb06cb22fb772fd73092ee0e85f50240e2;hb=30ff39e7f07898ebb716c938123825c678b4e5af;hp=329cebd28a6ed37db9d1b477fd10af8b4c325017;hpb=1735c825726edaa0af5035cb6cbb0cc0db502c6d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 329cebd28..ec3d5ddb0 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -40,7 +40,7 @@ import { UserRight, VideoPrivacy, VideoState } from '../../../shared' import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' import { VideoFilter } from '../../../shared/models/videos/video-query.type' -import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils' +import { peertubeTruncate } from '../../helpers/core-utils' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc' import { @@ -83,6 +83,7 @@ import { buildBlockedAccountSQL, buildTrigramSearchIndex, buildWhereIdOrUUID, + createSafeIn, createSimilarityAttribute, getVideoSort, isOutdated, @@ -116,6 +117,7 @@ import { VideoPlaylistElementModel } from './video-playlist-element' import { CONFIG } from '../../initializers/config' import { ThumbnailModel } from './thumbnail' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' +import { createTorrentPromise } from '../../helpers/webtorrent' // 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 })[] = [ @@ -207,6 +209,8 @@ type AvailableForListIDsOptions = { followerActorId: number includeLocalVideos: boolean + withoutId?: boolean + filter?: VideoFilter categoryOneOf?: number[] nsfw?: boolean @@ -225,20 +229,27 @@ type AvailableForListIDsOptions = { trendingDays?: number user?: UserModel, historyOfUser?: UserModel + + baseWhere?: WhereOptions[] } -@Scopes({ +@Scopes(() => ({ [ ScopeNames.FOR_API ]: (options: ForAPIOptions) => { const query: FindOptions = { where: { id: { - [ Op.in ]: options.ids // FIXME: sequelize any seems broken + [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken } }, include: [ { model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }), required: true + }, + { + attributes: [ 'type', 'filename' ], + model: ThumbnailModel, + required: false } ] } @@ -263,32 +274,34 @@ type AvailableForListIDsOptions = { return query }, [ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => { + const whereAnd = options.baseWhere ? options.baseWhere : [] + const query: FindOptions = { raw: true, - attributes: [ 'id' ], - where: { - id: { - [ Op.and ]: [ - { - [ Op.notIn ]: Sequelize.literal( - '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")' - ) - } - ] - }, - channelId: { - [ Op.notIn ]: Sequelize.literal( - '(' + - 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' + - buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) + - ')' + - ')' - ) - } - }, + attributes: options.withoutId === true ? [] : [ 'id' ], include: [] } + whereAnd.push({ + id: { + [ Op.notIn ]: Sequelize.literal( + '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")' + ) + } + }) + + whereAnd.push({ + channelId: { + [ Op.notIn ]: Sequelize.literal( + '(' + + 'SELECT id FROM "videoChannel" WHERE "accountId" IN (' + + buildBlockedAccountSQL(options.serverAccountId, options.user ? options.user.Account.id : undefined) + + ')' + + ')' + ) + } + }) + // Only list public/published videos if (!options.filter || options.filter !== 'all-local') { const privacyWhere = { @@ -308,7 +321,7 @@ type AvailableForListIDsOptions = { ] } - Object.assign(query.where, privacyWhere) + whereAnd.push(privacyWhere) } if (options.videoPlaylistId) { @@ -378,86 +391,114 @@ type AvailableForListIDsOptions = { // Force actorId to be a number to avoid SQL injections const actorIdNumber = parseInt(options.followerActorId.toString(), 10) - query.where[ 'id' ][ Op.and ].push({ - [ Op.in ]: Sequelize.literal( - '(' + - 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + - 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + - 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + - ' UNION ALL ' + - 'SELECT "video"."id" AS "id" FROM "video" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + - 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + - 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + - 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + - localVideosReq + - ')' - ) + whereAnd.push({ + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + + 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + + 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + + ' UNION ALL ' + + 'SELECT "video"."id" AS "id" FROM "video" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + + 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + + 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + + 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + + localVideosReq + + ')' + ) + } }) } if (options.withFiles === true) { - query.where[ 'id' ][ Op.and ].push({ - [ Op.in ]: Sequelize.literal( - '(SELECT "videoId" FROM "videoFile")' - ) + whereAnd.push({ + id: { + [ Op.in ]: Sequelize.literal( + '(SELECT "videoId" FROM "videoFile")' + ) + } }) } // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN() if (options.tagsAllOf || options.tagsOneOf) { - const createTagsIn = (tags: string[]) => { - return tags.map(t => VideoModel.sequelize.escape(t)) - .join(', ') - } - if (options.tagsOneOf) { - query.where[ 'id' ][ Op.and ].push({ - [ Op.in ]: Sequelize.literal( - '(' + - 'SELECT "videoId" FROM "videoTag" ' + - 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + - 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsOneOf) + ')' + - ')' - ) + whereAnd.push({ + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "videoId" FROM "videoTag" ' + + 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + + 'WHERE "tag"."name" IN (' + createSafeIn(VideoModel, options.tagsOneOf) + ')' + + ')' + ) + } }) } if (options.tagsAllOf) { - query.where[ 'id' ][ Op.and ].push({ - [ Op.in ]: Sequelize.literal( - '(' + - 'SELECT "videoId" FROM "videoTag" ' + - 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + - 'WHERE "tag"."name" IN (' + createTagsIn(options.tagsAllOf) + ')' + - 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length + - ')' - ) + whereAnd.push({ + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "videoId" FROM "videoTag" ' + + 'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + + 'WHERE "tag"."name" IN (' + createSafeIn(VideoModel, options.tagsAllOf) + ')' + + 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + options.tagsAllOf.length + + ')' + ) + } }) } } if (options.nsfw === true || options.nsfw === false) { - query.where[ 'nsfw' ] = options.nsfw + whereAnd.push({ nsfw: options.nsfw }) } if (options.categoryOneOf) { - query.where[ 'category' ] = { - [ Op.or ]: options.categoryOneOf - } + whereAnd.push({ + category: { + [ Op.or ]: options.categoryOneOf + } + }) } if (options.licenceOneOf) { - query.where[ 'licence' ] = { - [ Op.or ]: options.licenceOneOf - } + whereAnd.push({ + licence: { + [ Op.or ]: options.licenceOneOf + } + }) } if (options.languageOneOf) { - query.where[ 'language' ] = { - [ Op.or ]: options.languageOneOf + let videoLanguages = options.languageOneOf + if (options.languageOneOf.find(l => l === '_unknown')) { + videoLanguages = videoLanguages.concat([ null ]) } + + whereAnd.push({ + [Op.or]: [ + { + language: { + [ Op.or ]: videoLanguages + } + }, + { + id: { + [ Op.in ]: Sequelize.literal( + '(' + + 'SELECT "videoId" FROM "videoCaption" ' + + 'WHERE "language" IN (' + createSafeIn(VideoModel, options.languageOneOf) + ') ' + + ')' + ) + } + } + ] + }) } if (options.trendingDays) { @@ -481,12 +522,16 @@ type AvailableForListIDsOptions = { query.subQuery = false } + query.where = { + [ Op.and ]: whereAnd + } + return query }, [ ScopeNames.WITH_THUMBNAILS ]: { include: [ { - model: () => ThumbnailModel, + model: ThumbnailModel, required: false } ] @@ -495,48 +540,48 @@ type AvailableForListIDsOptions = { include: [ { attributes: [ 'accountId' ], - model: () => VideoChannelModel.unscoped(), + model: VideoChannelModel.unscoped(), required: true, include: [ { attributes: [ 'userId' ], - model: () => AccountModel.unscoped(), + model: AccountModel.unscoped(), required: true } ] } - ] as any // FIXME: sequelize typings + ] }, [ ScopeNames.WITH_ACCOUNT_DETAILS ]: { include: [ { - model: () => VideoChannelModel.unscoped(), + model: VideoChannelModel.unscoped(), required: true, include: [ { attributes: { exclude: [ 'privateKey', 'publicKey' ] }, - model: () => ActorModel.unscoped(), + model: ActorModel.unscoped(), required: true, include: [ { attributes: [ 'host' ], - model: () => ServerModel.unscoped(), + model: ServerModel.unscoped(), required: false }, { - model: () => AvatarModel.unscoped(), + model: AvatarModel.unscoped(), required: false } ] }, { - model: () => AccountModel.unscoped(), + model: AccountModel.unscoped(), required: true, include: [ { - model: () => ActorModel.unscoped(), + model: ActorModel.unscoped(), attributes: { exclude: [ 'privateKey', 'publicKey' ] }, @@ -544,11 +589,11 @@ type AvailableForListIDsOptions = { include: [ { attributes: [ 'host' ], - model: () => ServerModel.unscoped(), + model: ServerModel.unscoped(), required: false }, { - model: () => AvatarModel.unscoped(), + model: AvatarModel.unscoped(), required: false } ] @@ -557,16 +602,16 @@ type AvailableForListIDsOptions = { } ] } - ] as any // FIXME: sequelize typings + ] }, [ ScopeNames.WITH_TAGS ]: { - include: [ () => TagModel ] + include: [ TagModel ] }, [ ScopeNames.WITH_BLACKLISTED ]: { include: [ { attributes: [ 'id', 'reason' ], - model: () => VideoBlacklistModel, + model: VideoBlacklistModel, required: false } ] @@ -588,8 +633,7 @@ type AvailableForListIDsOptions = { include: [ { model: VideoFileModel.unscoped(), - // FIXME: typings - [ 'separate' as any ]: true, // We may have multiple files, having multiple redundancies so let's separate this join + separate: true, // We may have multiple files, having multiple redundancies so let's separate this join required: false, include: subInclude } @@ -613,8 +657,7 @@ type AvailableForListIDsOptions = { include: [ { model: VideoStreamingPlaylistModel.unscoped(), - // FIXME: typings - [ 'separate' as any ]: true, // We may have multiple streaming playlists, having multiple redundancies so let's separate this join + separate: true, // We may have multiple streaming playlists, having multiple redundancies so let's separate this join required: false, include: subInclude } @@ -624,7 +667,7 @@ type AvailableForListIDsOptions = { [ ScopeNames.WITH_SCHEDULED_UPDATE ]: { include: [ { - model: () => ScheduleVideoUpdateModel.unscoped(), + model: ScheduleVideoUpdateModel.unscoped(), required: false } ] @@ -643,7 +686,7 @@ type AvailableForListIDsOptions = { ] } } -}) +})) @Table({ tableName: 'video', indexes @@ -1075,15 +1118,14 @@ export class VideoModel extends Model { } return Bluebird.all([ - // FIXME: typing issue - VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findAll(query as any), - VideoModel.sequelize.query<{ total: number }>(rawCountQuery, { type: QueryTypes.SELECT }) + VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findAll(query), + VideoModel.sequelize.query<{ total: string }>(rawCountQuery, { type: QueryTypes.SELECT }) ]).then(([ rows, totals ]) => { // totals: totalVideos + totalVideoShares let totalVideos = 0 let totalVideoShares = 0 - if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total + '', 10) - if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total + '', 10) + if (totals[ 0 ]) totalVideos = parseInt(totals[ 0 ].total, 10) + if (totals[ 1 ]) totalVideoShares = parseInt(totals[ 1 ].total, 10) const total = totalVideos + totalVideoShares return { @@ -1094,50 +1136,54 @@ export class VideoModel extends Model { } static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { - const query: FindOptions = { - offset: start, - limit: count, - order: getVideoSort(sort), - include: [ - { - model: VideoChannelModel, - required: true, - include: [ - { - model: AccountModel, - where: { - id: accountId - }, - required: true - } - ] - }, - { - model: ScheduleVideoUpdateModel, - required: false - }, - { - model: VideoBlacklistModel, - required: false - } - ] + function buildBaseQuery (): FindOptions { + return { + offset: start, + limit: count, + order: getVideoSort(sort), + include: [ + { + model: VideoChannelModel, + required: true, + include: [ + { + model: AccountModel, + where: { + id: accountId + }, + required: true + } + ] + } + ] + } } + const countQuery = buildBaseQuery() + const findQuery = buildBaseQuery() + + const findScopes = [ + ScopeNames.WITH_SCHEDULED_UPDATE, + ScopeNames.WITH_BLACKLISTED, + ScopeNames.WITH_THUMBNAILS + ] + if (withFiles === true) { - query.include.push({ + findQuery.include.push({ model: VideoFileModel.unscoped(), required: true }) } - return VideoModel.scope(ScopeNames.WITH_THUMBNAILS) - .findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + VideoModel.count(countQuery), + VideoModel.scope(findScopes).findAll(findQuery) + ]).then(([ count, rows ]) => { + return { + data: rows, + total: count + } + }) } static async listForApi (options: { @@ -1165,7 +1211,7 @@ export class VideoModel extends Model { throw new Error('Try to filter all-local but no user has not the see all videos right') } - const query: FindOptions = { + const query: FindOptions & { where?: null } = { offset: options.start, limit: options.count, order: getVideoSort(options.sort) @@ -1289,16 +1335,13 @@ export class VideoModel extends Model { ) } - const query: FindOptions = { + const query = { attributes: { include: attributesInclude }, offset: options.start, limit: options.count, - order: getVideoSort(options.sort), - where: { - [ Op.and ]: whereAnd - } + order: getVideoSort(options.sort) } const serverActor = await getServerActor() @@ -1313,7 +1356,8 @@ export class VideoModel extends Model { tagsOneOf: options.tagsOneOf, tagsAllOf: options.tagsAllOf, user: options.user, - filter: options.filter + filter: options.filter, + baseWhere: whereAnd } return VideoModel.getAvailableForApi(query, queryOptions) @@ -1404,12 +1448,12 @@ export class VideoModel extends Model { const where = buildWhereIdOrUUID(id) const options = { - order: [ [ 'Tags', 'name', 'ASC' ] ] as any, // FIXME: sequelize typings + order: [ [ 'Tags', 'name', 'ASC' ] ] as any, where, transaction: t } - const scopes = [ + const scopes: (string | ScopeOptions)[] = [ ScopeNames.WITH_TAGS, ScopeNames.WITH_BLACKLISTED, ScopeNames.WITH_ACCOUNT_DETAILS, @@ -1420,7 +1464,7 @@ export class VideoModel extends Model { ] if (userId) { - scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings + scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] }) } return VideoModel @@ -1437,18 +1481,18 @@ export class VideoModel extends Model { transaction: t } - const scopes = [ + const scopes: (string | ScopeOptions)[] = [ ScopeNames.WITH_TAGS, ScopeNames.WITH_BLACKLISTED, ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE, ScopeNames.WITH_THUMBNAILS, - { method: [ ScopeNames.WITH_FILES, true ] } as any, // FIXME: typings - { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] } as any // FIXME: typings + { method: [ ScopeNames.WITH_FILES, true ] }, + { method: [ ScopeNames.WITH_STREAMING_PLAYLISTS, true ] } ] if (userId) { - scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] } as any) // FIXME: typings + scopes.push({ method: [ ScopeNames.WITH_USER_HISTORY, userId ] }) } return VideoModel @@ -1505,6 +1549,29 @@ export class VideoModel extends Model { .then(results => results.length === 1) } + static bulkUpdateSupportField (videoChannel: VideoChannelModel, t: Transaction) { + const options = { + where: { + channelId: videoChannel.id + }, + transaction: t + } + + return VideoModel.update({ support: videoChannel.support }, options) + } + + static getAllIdsFromChannel (videoChannel: VideoChannelModel) { + const query = { + attributes: [ 'id' ], + where: { + channelId: videoChannel.id + } + } + + return VideoModel.findAll(query) + .then(videos => videos.map(v => v.id)) + } + // threshold corresponds to how many video the field should have to be returned static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) { const serverActor = await getServerActor() @@ -1513,16 +1580,17 @@ export class VideoModel extends Model { const scopeOptions: AvailableForListIDsOptions = { serverAccountId: serverActor.Account.id, followerActorId, - includeLocalVideos: true + includeLocalVideos: true, + withoutId: true // Don't break aggregation } const query: FindOptions = { attributes: [ field ], limit: count, group: field, - having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), { - [ Op.gte ]: threshold - }) as any, // FIXME: typings + having: Sequelize.where( + Sequelize.fn('COUNT', Sequelize.col(field)), { [ Op.gte ]: threshold } + ), order: [ (this.sequelize as any).random() ] } @@ -1556,7 +1624,7 @@ export class VideoModel extends Model { } private static async getAvailableForApi ( - query: FindOptions, + query: FindOptions & { where?: null }, // Forbid where field in query options: AvailableForListIDsOptions, countVideos = true ) { @@ -1575,11 +1643,15 @@ export class VideoModel extends Model { ] } - const [ count, rowsId ] = await Promise.all([ - countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve(undefined), - VideoModel.scope(idsScope).findAll(query) + const [ count, ids ] = await Promise.all([ + countVideos + ? VideoModel.scope(countScope).count(countQuery) + : Promise.resolve(undefined), + + VideoModel.scope(idsScope) + .findAll(query) + .then(rows => rows.map(r => r.id)) ]) - const ids = rowsId.map(r => r.id) if (ids.length === 0) return { data: [], total: count } @@ -1594,16 +1666,10 @@ export class VideoModel extends Model { ] } - // FIXME: typing - const apiScope: any[] = [ ScopeNames.WITH_THUMBNAILS ] + const apiScope: (string | ScopeOptions)[] = [] if (options.user) { apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] }) - - // Even if the relation is n:m, we know that a user only have 0..1 video history - // So we won't have multiple rows for the same video - // A subquery adds some bugs in our query so disable it - secondQuery.subQuery = false } apiScope.push({ @@ -1651,13 +1717,17 @@ export class VideoModel extends Model { return maxBy(this.VideoFiles, file => file.resolution) } - addThumbnail (thumbnail: ThumbnailModel) { + async addAndSaveThumbnail (thumbnail: ThumbnailModel, transaction: Transaction) { + thumbnail.videoId = this.id + + const savedThumbnail = await thumbnail.save({ transaction }) + if (Array.isArray(this.Thumbnails) === false) this.Thumbnails = [] // Already have this thumbnail, skip - if (this.Thumbnails.find(t => t.id === thumbnail.id)) return + if (this.Thumbnails.find(t => t.id === savedThumbnail.id)) return - this.Thumbnails.push(thumbnail) + this.Thumbnails.push(savedThumbnail) } getVideoFilename (videoFile: VideoFileModel) { @@ -1668,10 +1738,10 @@ export class VideoModel extends Model { return this.uuid + '.jpg' } - getThumbnail () { + getMiniature () { if (Array.isArray(this.Thumbnails) === false) return undefined - return this.Thumbnails.find(t => t.type === ThumbnailType.THUMBNAIL) + return this.Thumbnails.find(t => t.type === ThumbnailType.MINIATURE) } generatePreviewName () { @@ -1732,8 +1802,8 @@ export class VideoModel extends Model { return '/videos/embed/' + this.uuid } - getThumbnailStaticPath () { - const thumbnail = this.getThumbnail() + getMiniatureStaticPath () { + const thumbnail = this.getMiniature() if (!thumbnail) return null return join(STATIC_PATHS.THUMBNAILS, thumbnail.filename)