X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo.ts;h=8c316e00c78a283f6e312b4c6d7450269b65292f;hb=e024fd6a7494b37251da1d59470324305cdb4129;hp=b4c7da655bf190dbc0c772f2bfd8813ea030779a;hpb=74d249bc1346c7cfaac7ee49bebbebcf2a01f82a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video.ts b/server/models/video/video.ts index b4c7da655..8c316e00c 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -24,7 +24,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { v4 as uuidv4 } from 'uuid' +import { setAsUpdated } from '@server/helpers/database-utils' import { buildNSFWFilter } from '@server/helpers/express-utils' import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' import { LiveManager } from '@server/lib/live-manager' @@ -100,10 +100,10 @@ import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../types/models import { VideoAbuseModel } from '../abuse/video-abuse' import { AccountModel } from '../account/account' import { AccountVideoRateModel } from '../account/account-video-rate' +import { ActorImageModel } from '../account/actor-image' import { UserModel } from '../account/user' import { UserVideoHistoryModel } from '../account/user-video-history' import { ActorModel } from '../activitypub/actor' -import { AvatarModel } from '../avatar/avatar' import { VideoRedundancyModel } from '../redundancy/video-redundancy' import { ServerModel } from '../server/server' import { TrackerModel } from '../server/tracker' @@ -286,7 +286,8 @@ export type AvailableForListIDsOptions = { required: false }, { - model: AvatarModel.unscoped(), + model: ActorImageModel.unscoped(), + as: 'Avatar', required: false } ] @@ -308,7 +309,8 @@ export type AvailableForListIDsOptions = { required: false }, { - model: AvatarModel.unscoped(), + model: ActorImageModel.unscoped(), + as: 'Avatar', required: false } ] @@ -426,7 +428,12 @@ export type AvailableForListIDsOptions = { ] }, { fields: [ 'duration' ] }, - { fields: [ 'views' ] }, + { + fields: [ + { name: 'views', order: 'DESC' }, + { name: 'id', order: 'ASC' } + ] + }, { fields: [ 'channelId' ] }, { fields: [ 'originallyPublishedAt' ], @@ -778,21 +785,20 @@ export class VideoModel extends Model { @BeforeDestroy static async sendDelete (instance: MVideoAccountLight, options) { - if (instance.isOwned()) { - if (!instance.VideoChannel) { - instance.VideoChannel = await instance.$get('VideoChannel', { - include: [ - ActorModel, - AccountModel - ], - transaction: options.transaction - }) as MChannelAccountDefault - } + if (!instance.isOwned()) return undefined - return sendDeleteVideo(instance, options.transaction) + // Lazy load channels + if (!instance.VideoChannel) { + instance.VideoChannel = await instance.$get('VideoChannel', { + include: [ + ActorModel, + AccountModel + ], + transaction: options.transaction + }) as MChannelAccountDefault } - return undefined + return sendDeleteVideo(instance, options.transaction) } @BeforeDestroy @@ -857,6 +863,7 @@ export class VideoModel extends Model { logger.info('Saving video abuses details of video %s.', instance.url) + if (!instance.Trackers) instance.Trackers = await instance.$get('Trackers', { transaction: options.transaction }) const details = instance.toFormattedDetailsJSON() for (const abuse of instance.VideoAbuses) { @@ -911,7 +918,7 @@ export class VideoModel extends Model { }, include: [ { - attributes: [ 'language', 'fileUrl' ], + attributes: [ 'filename', 'language', 'fileUrl' ], model: VideoCaptionModel.unscoped(), required: false }, @@ -1015,14 +1022,28 @@ export class VideoModel extends Model { start: number count: number sort: string + isLive?: boolean search?: string }) { - const { accountId, start, count, sort, search } = options + const { accountId, start, count, sort, search, isLive } = options function buildBaseQuery (): FindOptions { - let baseQuery = { + const where: WhereOptions = {} + + if (search) { + where.name = { + [Op.iLike]: '%' + search + '%' + } + } + + if (isLive) { + where.isLive = isLive + } + + const baseQuery = { offset: start, limit: count, + where, order: getVideoSort(sort), include: [ { @@ -1041,16 +1062,6 @@ export class VideoModel extends Model { ] } - if (search) { - baseQuery = Object.assign(baseQuery, { - where: { - name: { - [Op.iLike]: '%' + search + '%' - } - } - }) - } - return baseQuery } @@ -1078,23 +1089,34 @@ export class VideoModel extends Model { start: number count: number sort: string + nsfw: boolean + filter?: VideoFilter + isLive?: boolean + includeLocalVideos: boolean withFiles: boolean + categoryOneOf?: number[] licenceOneOf?: number[] languageOneOf?: string[] tagsOneOf?: string[] tagsAllOf?: string[] - filter?: VideoFilter + accountId?: number videoChannelId?: number + followerActorId?: number + videoPlaylistId?: number + trendingDays?: number + user?: MUserAccountId historyOfUser?: MUserId + countVideos?: boolean + search?: string }) { if ((options.filter === 'all-local' || options.filter === 'all') && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) { @@ -1122,6 +1144,7 @@ export class VideoModel extends Model { followerActorId, serverAccountId: serverActor.Account.id, nsfw: options.nsfw, + isLive: options.isLive, categoryOneOf: options.categoryOneOf, licenceOneOf: options.licenceOneOf, languageOneOf: options.languageOneOf, @@ -1154,6 +1177,7 @@ export class VideoModel extends Model { originallyPublishedStartDate?: string originallyPublishedEndDate?: string nsfw?: boolean + isLive?: boolean categoryOneOf?: number[] licenceOneOf?: number[] languageOneOf?: string[] @@ -1165,23 +1189,32 @@ export class VideoModel extends Model { filter?: VideoFilter }) { const serverActor = await getServerActor() + const queryOptions = { followerActorId: serverActor.id, serverAccountId: serverActor.Account.id, + includeLocalVideos: options.includeLocalVideos, nsfw: options.nsfw, + isLive: options.isLive, + categoryOneOf: options.categoryOneOf, licenceOneOf: options.licenceOneOf, languageOneOf: options.languageOneOf, + tagsOneOf: options.tagsOneOf, tagsAllOf: options.tagsAllOf, + user: options.user, filter: options.filter, + start: options.start, count: options.count, sort: options.sort, + startDate: options.startDate, endDate: options.endDate, + originallyPublishedStartDate: options.originallyPublishedStartDate, originallyPublishedEndDate: options.originallyPublishedEndDate, @@ -1698,7 +1731,7 @@ export class VideoModel extends Model { function buildActor (rowActor: any) { const avatarModel = rowActor.Avatar.id !== null - ? new AvatarModel(pick(rowActor.Avatar, avatarKeys), buildOpts) + ? new ActorImageModel(pick(rowActor.Avatar, avatarKeys), buildOpts) : null const serverModel = rowActor.Server.id !== null @@ -1864,20 +1897,12 @@ export class VideoModel extends Model { this.Thumbnails.push(savedThumbnail) } - generateThumbnailName () { - return uuidv4() + '.jpg' - } - getMiniature () { if (Array.isArray(this.Thumbnails) === false) return undefined return this.Thumbnails.find(t => t.type === ThumbnailType.MINIATURE) } - generatePreviewName () { - return uuidv4() + '.jpg' - } - hasPreview () { return !!this.getPreview() } @@ -2029,9 +2054,7 @@ export class VideoModel extends Model { } setAsRefreshed () { - this.changed('updatedAt', true) - - return this.save() + return setAsUpdated('video', this.id) } requiresAuth () {