X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fredundancy%2Fvideo-redundancy.ts;h=ca56a57dc67bb1cba0c8ce2afc23401fd914a09c;hb=5ec3cbdf22fc88ebe57f370fc0bc0e3df7453458;hp=d3b839cfe932542dcc8a37d810c1acf6429307b1;hpb=7448551fe5c111fcc7b7448ad864fb15a5c8d87f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index d3b839cfe..ca56a57dc 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -1,5 +1,5 @@ import { sample } from 'lodash' -import { col, FindOptions, fn, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' +import { literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -16,6 +16,7 @@ import { } from 'sequelize-typescript' import { getServerActor } from '@server/models/application/application' import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models' +import { AttributesOnly } from '@shared/core-utils' import { VideoRedundanciesTarget } from '@shared/models/redundancy/video-redundancies-filters.model' import { FileRedundancyInformation, @@ -29,14 +30,14 @@ import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validato import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' -import { ActorModel } from '../activitypub/actor' +import { ActorModel } from '../actor/actor' import { ServerModel } from '../server/server' import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' +import { ScheduleVideoUpdateModel } from '../video/schedule-video-update' import { VideoModel } from '../video/video' import { VideoChannelModel } from '../video/video-channel' import { VideoFileModel } from '../video/video-file' import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' -import { forEachSeries } from 'async' export enum ScopeNames { WITH_VIDEO = 'WITH_VIDEO' @@ -84,7 +85,7 @@ export enum ScopeNames { } ] }) -export class VideoRedundancyModel extends Model { +export class VideoRedundancyModel extends Model>> { @CreatedAt createdAt: Date @@ -186,6 +187,57 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) } + static async listLocalByVideoId (videoId: number): Promise { + const actor = await getServerActor() + + const queryStreamingPlaylist = { + where: { + actorId: actor.id + }, + include: [ + { + model: VideoStreamingPlaylistModel.unscoped(), + required: true, + include: [ + { + model: VideoModel.unscoped(), + required: true, + where: { + id: videoId + } + } + ] + } + ] + } + + const queryFiles = { + where: { + actorId: actor.id + }, + include: [ + { + model: VideoFileModel, + required: true, + include: [ + { + model: VideoModel, + required: true, + where: { + id: videoId + } + } + ] + } + ] + } + + return Promise.all([ + VideoRedundancyModel.findAll(queryStreamingPlaylist), + VideoRedundancyModel.findAll(queryFiles) + ]).then(([ r1, r2 ]) => r1.concat(r2)) + } + static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise { const actor = await getServerActor() @@ -324,7 +376,13 @@ export class VideoRedundancyModel extends Model { ...this.buildVideoIdsForDuplication(peertubeActor) }, include: [ - VideoRedundancyModel.buildServerRedundancyInclude() + VideoRedundancyModel.buildServerRedundancyInclude(), + + // Required by publishedAt sort + { + model: ScheduleVideoUpdateModel.unscoped(), + required: false + } ] } @@ -350,50 +408,6 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query) } - static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { - const actor = await getServerActor() - const redundancyInclude = { - attributes: [], - model: VideoRedundancyModel, - required: true, - where: { - actorId: actor.id, - strategy - } - } - - const queryFiles: FindOptions = { - include: [ redundancyInclude ] - } - - const queryStreamingPlaylists: FindOptions = { - include: [ - { - attributes: [], - model: VideoModel.unscoped(), - required: true, - include: [ - { - required: true, - attributes: [], - model: VideoStreamingPlaylistModel.unscoped(), - include: [ - redundancyInclude - ] - } - ] - } - ] - } - - return Promise.all([ - VideoFileModel.aggregate('size', 'SUM', queryFiles), - VideoFileModel.aggregate('size', 'SUM', queryStreamingPlaylists) - ]).then(([ r1, r2 ]) => { - return parseAggregateResult(r1) + parseAggregateResult(r2) - }) - } - static async listLocalExpired () { const actor = await getServerActor() @@ -659,9 +673,9 @@ export class VideoRedundancyModel extends Model { } getVideo () { - if (this.VideoFile) return this.VideoFile.Video + if (this.VideoFile?.Video) return this.VideoFile.Video - if (this.VideoStreamingPlaylist.Video) return this.VideoStreamingPlaylist.Video + if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video return undefined }