X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=6856dcd9f057baa5f7d0f7c895afee3d7df8e43f;hb=282e61e6c11f79e919c543871783fe1a00298d18;hp=b59df397d470a2ccce64866bef5ed8d5133c73c5;hpb=e2600d8b261994abbbeb1ff921edaefd267fc122;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index b59df397d..6856dcd9f 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -36,7 +36,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { UserRight, VideoPrivacy, VideoResolution, VideoState } from '../../../shared' +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' @@ -111,7 +111,6 @@ import { videoModelToFormattedJSON } from './video-format-utils' import { UserVideoHistoryModel } from '../account/user-video-history' -import { UserModel } from '../account/user' import { VideoImportModel } from './video-import' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' import { VideoPlaylistElementModel } from './video-playlist-element' @@ -120,6 +119,29 @@ import { ThumbnailModel } from './thumbnail' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { createTorrentPromise } from '../../helpers/webtorrent' import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' +import { + MChannel, + MChannelAccountDefault, + MChannelId, + MUserAccountId, + MUserId, + MVideoAccountLight, + MVideoAccountLightBlacklistAllFiles, + MVideoAP, + MVideoDetails, + MVideoFormattable, + MVideoFormattableDetails, + MVideoForUser, + MVideoFullLight, + MVideoIdThumbnail, + MVideoThumbnail, + MVideoThumbnailBlacklist, + MVideoWithAllFiles, + MVideoWithFile, + MVideoWithRights +} from '../../typings/models' +import { MVideoFile, MVideoFileRedundanciesOpt } from '../../typings/models/video/video-file' +import { MThumbnail } from '../../typings/models/video/thumbnail' // 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 })[] = [ @@ -232,8 +254,8 @@ export type AvailableForListIDsOptions = { videoPlaylistId?: number trendingDays?: number - user?: UserModel, - historyOfUser?: UserModel + user?: MUserAccountId + historyOfUser?: MUserId baseWhere?: WhereOptions[] } @@ -446,13 +468,15 @@ export type AvailableForListIDsOptions = { // FIXME: issues with sequelize count when making a join on n:m relation, so we just make a IN() if (options.tagsAllOf || options.tagsOneOf) { if (options.tagsOneOf) { + const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase()) + 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) + ')' + + 'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsOneOfLower) + ')' + ')' ) } @@ -460,14 +484,16 @@ export type AvailableForListIDsOptions = { } if (options.tagsAllOf) { + const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase()) + 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 + + 'WHERE lower("tag"."name") IN (' + createSafeIn(VideoModel, tagsAllOfLower) + ')' + + 'GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length + ')' ) } @@ -634,7 +660,7 @@ export type AvailableForListIDsOptions = { [ ScopeNames.WITH_BLACKLISTED ]: { include: [ { - attributes: [ 'id', 'reason' ], + attributes: [ 'id', 'reason', 'unfederated' ], model: VideoBlacklistModel, required: false } @@ -989,18 +1015,16 @@ export class VideoModel extends Model { VideoCaptions: VideoCaptionModel[] @BeforeDestroy - static async sendDelete (instance: VideoModel, options) { + static async sendDelete (instance: MVideoAccountLight, options) { if (instance.isOwned()) { if (!instance.VideoChannel) { instance.VideoChannel = await instance.$get('VideoChannel', { include: [ - { - model: AccountModel, - include: [ ActorModel ] - } + ActorModel, + AccountModel ], transaction: options.transaction - }) as VideoChannelModel + }) as MChannelAccountDefault } return sendDeleteVideo(instance, options.transaction) @@ -1039,7 +1063,7 @@ export class VideoModel extends Model { return undefined } - static listLocal () { + static listLocal (): Bluebird { const query = { where: { remote: false @@ -1159,7 +1183,7 @@ export class VideoModel extends Model { }) } - static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, withFiles = false) { + static listUserVideosForApi (accountId: number, start: number, count: number, sort: string) { function buildBaseQuery (): FindOptions { return { offset: start, @@ -1192,16 +1216,9 @@ export class VideoModel extends Model { ScopeNames.WITH_THUMBNAILS ] - if (withFiles === true) { - findQuery.include.push({ - model: VideoFileModel.unscoped(), - required: true - }) - } - return Promise.all([ VideoModel.count(countQuery), - VideoModel.scope(findScopes).findAll(findQuery) + VideoModel.scope(findScopes).findAll(findQuery) ]).then(([ count, rows ]) => { return { data: rows, @@ -1228,8 +1245,8 @@ export class VideoModel extends Model { followerActorId?: number videoPlaylistId?: number, trendingDays?: number, - user?: UserModel, - historyOfUser?: UserModel + user?: MUserAccountId, + historyOfUser?: MUserId }, countVideos = true) { 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') @@ -1294,7 +1311,7 @@ export class VideoModel extends Model { tagsAllOf?: string[] durationMin?: number // seconds durationMax?: number // seconds - user?: UserModel, + user?: MUserAccountId, filter?: VideoFilter }) { const whereAnd = [] @@ -1387,7 +1404,7 @@ export class VideoModel extends Model { return VideoModel.getAvailableForApi(query, queryOptions) } - static load (id: number | string, t?: Transaction) { + static load (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1397,7 +1414,20 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithRights (id: number | string, t?: Transaction) { + static loadWithBlacklist (id: number | string, t?: Transaction): Bluebird { + const where = buildWhereIdOrUUID(id) + const options = { + where, + transaction: t + } + + return VideoModel.scope([ + ScopeNames.WITH_THUMBNAILS, + ScopeNames.WITH_BLACKLISTED + ]).findOne(options) + } + + static loadWithRights (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { where, @@ -1411,7 +1441,7 @@ export class VideoModel extends Model { ]).findOne(options) } - static loadOnlyId (id: number | string, t?: Transaction) { + static loadOnlyId (id: number | string, t?: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const options = { @@ -1423,7 +1453,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean) { + static loadWithFiles (id: number | string, t?: Transaction, logging?: boolean): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -1439,7 +1469,7 @@ export class VideoModel extends Model { ]).findOne(query) } - static loadByUUID (uuid: string) { + static loadByUUID (uuid: string): Bluebird { const options = { where: { uuid @@ -1449,7 +1479,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(options) } - static loadByUrl (url: string, transaction?: Transaction) { + static loadByUrl (url: string, transaction?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -1460,7 +1490,7 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query) } - static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction) { + static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Bluebird { const query: FindOptions = { where: { url @@ -1472,11 +1502,12 @@ export class VideoModel extends Model { ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES, ScopeNames.WITH_STREAMING_PLAYLISTS, - ScopeNames.WITH_THUMBNAILS + ScopeNames.WITH_THUMBNAILS, + ScopeNames.WITH_BLACKLISTED ]).findOne(query) } - static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number) { + static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Transaction, userId?: number): Bluebird { const where = buildWhereIdOrUUID(id) const options = { @@ -1508,7 +1539,7 @@ export class VideoModel extends Model { id: number | string, t?: Transaction, userId?: number - }) { + }): Bluebird { const { id, t, userId } = parameters const where = buildWhereIdOrUUID(id) @@ -1586,7 +1617,7 @@ export class VideoModel extends Model { .then(results => results.length === 1) } - static bulkUpdateSupportField (videoChannel: VideoChannelModel, t: Transaction) { + static bulkUpdateSupportField (videoChannel: MChannel, t: Transaction) { const options = { where: { channelId: videoChannel.id @@ -1597,7 +1628,7 @@ export class VideoModel extends Model { return VideoModel.update({ support: videoChannel.support }, options) } - static getAllIdsFromChannel (videoChannel: VideoChannelModel) { + static getAllIdsFromChannel (videoChannel: MChannelId): Bluebird { const query = { attributes: [ 'id' ], where: { @@ -1756,20 +1787,20 @@ export class VideoModel extends Model { this.VideoChannel.Account.isBlocked() } - getOriginalFile () { + getOriginalFile (this: T) { if (Array.isArray(this.VideoFiles) === false) return undefined // The original file is the file that have the higher resolution return maxBy(this.VideoFiles, file => file.resolution) } - getFile (resolution: number) { + getFile (this: T, resolution: number) { if (Array.isArray(this.VideoFiles) === false) return undefined return this.VideoFiles.find(f => f.resolution === resolution) } - async addAndSaveThumbnail (thumbnail: ThumbnailModel, transaction: Transaction) { + async addAndSaveThumbnail (thumbnail: MThumbnail, transaction: Transaction) { thumbnail.videoId = this.id const savedThumbnail = await thumbnail.save({ transaction }) @@ -1782,7 +1813,7 @@ export class VideoModel extends Model { this.Thumbnails.push(savedThumbnail) } - getVideoFilename (videoFile: VideoFileModel) { + getVideoFilename (videoFile: MVideoFile) { return this.uuid + '-' + videoFile.resolution + videoFile.extname } @@ -1806,7 +1837,7 @@ export class VideoModel extends Model { return this.Thumbnails.find(t => t.type === ThumbnailType.PREVIEW) } - getTorrentFileName (videoFile: VideoFileModel) { + getTorrentFileName (videoFile: MVideoFile) { const extension = '.torrent' return this.uuid + '-' + videoFile.resolution + extension } @@ -1815,15 +1846,15 @@ export class VideoModel extends Model { return this.remote === false } - getTorrentFilePath (videoFile: VideoFileModel) { + getTorrentFilePath (videoFile: MVideoFile) { return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) } - getVideoFilePath (videoFile: VideoFileModel) { + getVideoFilePath (videoFile: MVideoFile) { return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) } - async createTorrentAndSetInfoHash (videoFile: VideoFileModel) { + async createTorrentAndSetInfoHash (videoFile: MVideoFile) { const options = { // Keep the extname, it's used by the client to stream the file inside a web browser name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`, @@ -1869,11 +1900,11 @@ export class VideoModel extends Model { return join(LAZY_STATIC_PATHS.PREVIEWS, preview.filename) } - toFormattedJSON (options?: VideoFormattingJSONOptions): Video { + toFormattedJSON (this: MVideoFormattable, options?: VideoFormattingJSONOptions): Video { return videoModelToFormattedJSON(this, options) } - toFormattedDetailsJSON (): VideoDetails { + toFormattedDetailsJSON (this: MVideoFormattableDetails): VideoDetails { return videoModelToFormattedDetailsJSON(this) } @@ -1881,7 +1912,7 @@ export class VideoModel extends Model { return videoFilesModelToFormattedJSON(this, this.VideoFiles) } - toActivityPubObject (): VideoTorrentObject { + toActivityPubObject (this: MVideoAP): VideoTorrentObject { return videoModelToActivityPubObject(this) } @@ -1908,7 +1939,7 @@ export class VideoModel extends Model { return this.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) } - removeFile (videoFile: VideoFileModel, isRedundancy = false) { + removeFile (videoFile: MVideoFile, isRedundancy = false) { const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR const filePath = join(baseDir, this.getVideoFilename(videoFile)) @@ -1916,7 +1947,7 @@ export class VideoModel extends Model { .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) } - removeTorrent (videoFile: VideoFileModel) { + removeTorrent (videoFile: MVideoFile) { const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) return remove(torrentPath) .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) @@ -1957,7 +1988,7 @@ export class VideoModel extends Model { return { baseUrlHttp, baseUrlWs } } - generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) { + generateMagnetUri (videoFile: MVideoFileRedundanciesOpt, baseUrlHttp: string, baseUrlWs: string) { const xs = this.getTorrentUrl(videoFile, baseUrlHttp) const announce = this.getTrackerUrls(baseUrlHttp, baseUrlWs) let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ] @@ -1980,27 +2011,27 @@ export class VideoModel extends Model { return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ] } - getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) } - getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile) } - getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) } - getVideoRedundancyUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoRedundancyUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getVideoFilename(videoFile) } - getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) { + getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) { return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile) } - getBandwidthBits (videoFile: VideoFileModel) { + getBandwidthBits (videoFile: MVideoFile) { return Math.ceil((videoFile.size * 8) / this.duration) } }