Table,
UpdatedAt
} from 'sequelize-typescript'
-import { VideoPrivacy, VideoResolution, VideoState } from '../../../shared'
+import { ActivityUrlObject, VideoPrivacy, VideoResolution, 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 { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { isBooleanValid } from '../../helpers/custom-validators/misc'
+import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc'
import {
isVideoCategoryValid,
isVideoDescriptionValid,
import { VideoBlacklistModel } from './video-blacklist'
import { copy, remove, rename, stat, writeFile } from 'fs-extra'
import { VideoViewModel } from './video-views'
+import { VideoRedundancyModel } from '../redundancy/video-redundancy'
// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
const indexes: Sequelize.DefineIndexesOptions[] = [
},
[ ScopeNames.AVAILABLE_FOR_LIST_IDS ]: (options: AvailableForListIDsOptions) => {
const query: IFindOptions<VideoModel> = {
+ raw: true,
attributes: [ 'id' ],
where: {
id: {
}
if (options.trendingDays) {
- query.include.push({
- attributes: [],
- model: VideoViewModel,
- required: false,
- where: {
- startDate: {
- [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays)
- }
- }
- })
+ query.include.push(VideoModel.buildTrendingQuery(options.trendingDays))
query.subQuery = false
}
include: [
{
model: () => VideoFileModel.unscoped(),
- required: false
+ required: false,
+ include: [
+ {
+ model: () => VideoRedundancyModel.unscoped(),
+ required: false
+ }
+ ]
}
]
},
name: 'videoId',
allowNull: false
},
+ hooks: true,
onDelete: 'cascade'
})
VideoFiles: VideoFileModel[]
videoChannelId?: number,
actorId?: number
trendingDays?: number
- }) {
+ }, countVideos = true) {
const query: IFindOptions<VideoModel> = {
offset: options.start,
limit: options.count,
trendingDays
}
- return VideoModel.getAvailableForApi(query, queryOptions)
+ return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
}
static async searchAndPopulateAccountAndServer (options: {
}
static load (id: number, t?: Sequelize.Transaction) {
- const options = t ? { transaction: t } : undefined
+ return VideoModel.findById(id, { transaction: t })
+ }
- return VideoModel.findById(id, options)
+ static loadWithFile (id: number, t?: Sequelize.Transaction, logging?: boolean) {
+ return VideoModel.scope(ScopeNames.WITH_FILES)
+ .findById(id, { transaction: t, logging })
}
static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
}
// threshold corresponds to how many video the field should have to be returned
- static getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
+ static async getRandomFieldSamples (field: 'category' | 'channelId', threshold: number, count: number) {
+ const actorId = (await getServerActor()).id
+
+ const scopeOptions = {
+ actorId,
+ includeLocalVideos: true
+ }
+
const query: IFindOptions<VideoModel> = {
attributes: [ field ],
limit: count,
having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col(field)), {
[ Sequelize.Op.gte ]: threshold
}) as any, // FIXME: typings
- where: {
- [ field ]: {
- [ Sequelize.Op.not ]: null
- },
- privacy: VideoPrivacy.PUBLIC,
- state: VideoState.PUBLISHED
- },
order: [ this.sequelize.random() ]
}
- return VideoModel.findAll(query)
+ return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST_IDS, scopeOptions ] })
+ .findAll(query)
.then(rows => rows.map(r => r[ field ]))
}
+ static buildTrendingQuery (trendingDays: number) {
+ return {
+ attributes: [],
+ subQuery: false,
+ model: VideoViewModel,
+ required: false,
+ where: {
+ startDate: {
+ [ Sequelize.Op.gte ]: new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
+ }
+ }
+ }
+ }
+
private static buildActorWhereWithFilter (filter?: VideoFilter) {
if (filter && filter === 'local') {
return {
return {}
}
- private static async getAvailableForApi (query: IFindOptions<VideoModel>, options: AvailableForListIDsOptions) {
+ private static async getAvailableForApi (query: IFindOptions<VideoModel>, options: AvailableForListIDsOptions, countVideos = true) {
const idsScope = {
method: [
ScopeNames.AVAILABLE_FOR_LIST_IDS, options
}
const [ count, rowsId ] = await Promise.all([
- VideoModel.scope(countScope).count(countQuery),
+ countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve(undefined),
VideoModel.scope(idsScope).findAll(query)
])
const ids = rowsId.map(r => r.id)
[ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
[ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
],
- urlList: [
- CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
- ]
+ urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
}
const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
}
}
- const url = []
+ const url: ActivityUrlObject[] = []
for (const file of this.VideoFiles) {
url.push({
type: 'Link',
- mimeType: VIDEO_EXT_MIMETYPE[ file.extname ],
+ mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
href: this.getVideoFileUrl(file, baseUrlHttp),
height: file.resolution,
size: file.size,
url.push({
type: 'Link',
- mimeType: 'application/x-bittorrent',
+ mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
href: this.getTorrentUrl(file, baseUrlHttp),
height: file.resolution
})
url.push({
type: 'Link',
- mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
+ mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
height: file.resolution
})
(now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
}
- private getBaseUrls () {
+ getBaseUrls () {
let baseUrlHttp
let baseUrlWs
return { baseUrlHttp, baseUrlWs }
}
- private getThumbnailUrl (baseUrlHttp: string) {
+ generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
+ const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
+ const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
+ let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
+
+ const redundancies = videoFile.RedundancyVideos
+ if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
+
+ const magnetHash = {
+ xs,
+ announce,
+ urlList,
+ infoHash: videoFile.infoHash,
+ name: this.name
+ }
+
+ return magnetUtil.encode(magnetHash)
+ }
+
+ getThumbnailUrl (baseUrlHttp: string) {
return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
}
- private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+ getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
}
- private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+ getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
}
- private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+ getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
}
- private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+ getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
}
-
- private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
- const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
- const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
- const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
-
- const magnetHash = {
- xs,
- announce,
- urlList,
- infoHash: videoFile.infoHash,
- name: this.name
- }
-
- return magnetUtil.encode(magnetHash)
- }
}