Cleanup models

pull/128/head
Chocobozzz 2017-11-23 17:36:15 +01:00
parent 16b9097594
commit 39445ead45
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
13 changed files with 52 additions and 452 deletions

View File

@ -1,6 +1,5 @@
import * as Sequelize from 'sequelize'
import { VideoChannelCreate } from '../../shared/models'
import { logger } from '../helpers'
import { database as db } from '../initializers'
import { AccountInstance } from '../models'
import { getVideoChannelActivityPubUrl } from './activitypub/url'
@ -27,21 +26,8 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account
return videoChannelCreated
}
async function fetchVideoChannelByHostAndUUID (serverHost: string, uuid: string, t: Sequelize.Transaction) {
try {
const videoChannel = await db.VideoChannel.loadByHostAndUUID(serverHost, uuid, t)
if (!videoChannel) throw new Error('Video channel not found')
return videoChannel
} catch (err) {
logger.error('Cannot load video channel from host and uuid.', { error: err.stack, serverHost, uuid })
throw err
}
}
// ---------------------------------------------------------------------------
export {
createVideoChannel,
fetchVideoChannelByHostAndUUID
createVideoChannel
}

View File

@ -10,10 +10,8 @@ export namespace AccountMethods {
export type Load = (id: number) => Bluebird<AccountInstance>
export type LoadByUUID = (uuid: string) => Bluebird<AccountInstance>
export type LoadByUrl = (url: string, transaction?: Sequelize.Transaction) => Bluebird<AccountInstance>
export type LoadAccountByServerAndUUID = (uuid: string, serverId: number, transaction: Sequelize.Transaction) => Bluebird<AccountInstance>
export type LoadLocalByName = (name: string) => Bluebird<AccountInstance>
export type LoadByNameAndHost = (name: string, host: string) => Bluebird<AccountInstance>
export type ListOwned = () => Bluebird<AccountInstance[]>
export type ToActivityPubObject = (this: AccountInstance) => ActivityPubActor
export type ToFormattedJSON = (this: AccountInstance) => FormattedAccount
@ -26,13 +24,11 @@ export namespace AccountMethods {
export interface AccountClass {
loadApplication: AccountMethods.LoadApplication
loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
load: AccountMethods.Load
loadByUUID: AccountMethods.LoadByUUID
loadByUrl: AccountMethods.LoadByUrl
loadLocalByName: AccountMethods.LoadLocalByName
loadByNameAndHost: AccountMethods.LoadByNameAndHost
listOwned: AccountMethods.ListOwned
}
export interface AccountAttributes {

View File

@ -20,14 +20,12 @@ import { addMethodsToModel } from '../utils'
import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
let Account: Sequelize.Model<AccountInstance, AccountAttributes>
let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
let load: AccountMethods.Load
let loadApplication: AccountMethods.LoadApplication
let loadByUUID: AccountMethods.LoadByUUID
let loadByUrl: AccountMethods.LoadByUrl
let loadLocalByName: AccountMethods.LoadLocalByName
let loadByNameAndHost: AccountMethods.LoadByNameAndHost
let listOwned: AccountMethods.ListOwned
let isOwned: AccountMethods.IsOwned
let toActivityPubObject: AccountMethods.ToActivityPubObject
let toFormattedJSON: AccountMethods.ToFormattedJSON
@ -185,14 +183,12 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
const classMethods = [
associate,
loadAccountByServerAndUUID,
loadApplication,
load,
loadByUUID,
loadByUrl,
loadLocalByName,
loadByNameAndHost,
listOwned
loadByNameAndHost
]
const instanceMethods = [
isOwned,
@ -355,16 +351,6 @@ getPublicKeyUrl = function (this: AccountInstance) {
// ------------------------------ STATICS ------------------------------
listOwned = function () {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
serverId: null
}
}
return Account.findAll(query)
}
loadApplication = function () {
return Account.findOne({
include: [
@ -441,15 +427,3 @@ loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
return Account.findOne(query)
}
loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
serverId,
uuid
},
transaction
}
return Account.find(query)
}

View File

@ -1,5 +1,5 @@
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
import * as Sequelize from 'sequelize'
import { UserModel } from '../account/user-interface'
@ -18,15 +18,12 @@ export namespace OAuthTokenMethods {
export type GetByRefreshTokenAndPopulateClient = (refreshToken: string) => Promise<OAuthTokenInfo>
export type GetByTokenAndPopulateUser = (bearerToken: string) => Promise<OAuthTokenInstance>
export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Promise<OAuthTokenInstance>
export type RemoveByUserId = (userId) => Promise<number>
}
export interface OAuthTokenClass {
getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
removeByUserId: OAuthTokenMethods.RemoveByUserId
}
export interface OAuthTokenAttributes {

View File

@ -3,19 +3,12 @@ import * as Sequelize from 'sequelize'
import { logger } from '../../helpers'
import { addMethodsToModel } from '../utils'
import {
OAuthTokenInstance,
OAuthTokenAttributes,
OAuthTokenMethods,
OAuthTokenInfo
} from './oauth-token-interface'
import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface'
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
let removeByUserId: OAuthTokenMethods.RemoveByUserId
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
@ -62,8 +55,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
getByRefreshTokenAndPopulateClient,
getByTokenAndPopulateUser,
getByRefreshTokenAndPopulateUser,
removeByUserId
getByRefreshTokenAndPopulateUser
]
addMethodsToModel(OAuthToken, classMethods)
@ -170,13 +162,3 @@ getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
return token
})
}
removeByUserId = function (userId: number) {
const query = {
where: {
userId: userId
}
}
return OAuthToken.destroy(query)
}

View File

@ -1,45 +1,13 @@
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
// Don't use barrel, import just what we need
import { ResultList } from '../../../shared/models/result-list.model'
import * as Sequelize from 'sequelize'
export namespace ServerMethods {
export type CountAll = () => Promise<number>
export type IncrementScores = (ids: number[], value: number) => Promise<[ number, ServerInstance[] ]>
export type List = () => Promise<ServerInstance[]>
export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<ServerInstance> >
export type ListAllIds = (transaction: Sequelize.Transaction) => Promise<number[]>
export type ListRandomServerIdsWithRequest = (limit: number, tableWithServers: string, tableWithServersJoins: string) => Promise<number[]>
export type ListBadServers = () => Promise<ServerInstance[]>
export type Load = (id: number) => Promise<ServerInstance>
export type LoadByHost = (host: string) => Promise<ServerInstance>
export type RemoveAll = () => Promise<number>
export type UpdateServersScore = (goodServers: number[], badServers: number[]) => void
export type UpdateServersScoreAndRemoveBadOnes = (goodServers: number[], badServers: number[]) => void
}
export interface ServerClass {
countAll: ServerMethods.CountAll
incrementScores: ServerMethods.IncrementScores
list: ServerMethods.List
listForApi: ServerMethods.ListForApi
listAllIds: ServerMethods.ListAllIds
listRandomServerIdsWithRequest: ServerMethods.ListRandomServerIdsWithRequest
listBadServers: ServerMethods.ListBadServers
load: ServerMethods.Load
loadByHost: ServerMethods.LoadByHost
removeAll: ServerMethods.RemoveAll
updateServersScore: ServerMethods.UpdateServersScore
updateServersScoreAndRemoveBadOnes: ServerMethods.UpdateServersScoreAndRemoveBadOnes
}
export interface ServerAttributes {

View File

@ -1,29 +1,11 @@
import { map } from 'lodash'
import * as Sequelize from 'sequelize'
import { isHostValid, logger } from '../../helpers'
import { FRIEND_SCORE, SERVERS_SCORE } from '../../initializers'
import { logger, isHostValid } from '../../helpers'
import { addMethodsToModel, getSort } from '../utils'
import {
ServerInstance,
ServerAttributes,
ServerMethods
} from './server-interface'
import { addMethodsToModel } from '../utils'
import { ServerAttributes, ServerInstance, ServerMethods } from './server-interface'
let Server: Sequelize.Model<ServerInstance, ServerAttributes>
let countAll: ServerMethods.CountAll
let incrementScores: ServerMethods.IncrementScores
let list: ServerMethods.List
let listForApi: ServerMethods.ListForApi
let listAllIds: ServerMethods.ListAllIds
let listRandomServerIdsWithRequest: ServerMethods.ListRandomServerIdsWithRequest
let listBadServers: ServerMethods.ListBadServers
let load: ServerMethods.Load
let loadByHost: ServerMethods.LoadByHost
let removeAll: ServerMethods.RemoveAll
let updateServersScore: ServerMethods.UpdateServersScore
let updateServersScoreAndRemoveBadOnes: ServerMethods.UpdateServersScoreAndRemoveBadOnes
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
Server = sequelize.define<ServerInstance, ServerAttributes>('Server',
@ -62,17 +44,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
)
const classMethods = [
countAll,
incrementScores,
list,
listForApi,
listAllIds,
listRandomServerIdsWithRequest,
listBadServers,
load,
loadByHost,
updateServersScore,
removeAll
listBadServers
]
addMethodsToModel(Server, classMethods)
@ -81,118 +53,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
// ------------------------------ Statics ------------------------------
countAll = function () {
return Server.count()
}
incrementScores = function (ids: number[], value: number) {
const update = {
score: Sequelize.literal('score +' + value)
}
const options = {
where: {
id: {
[Sequelize.Op.in]: ids
}
},
// In this case score is a literal and not an integer so we do not validate it
validate: false
}
return Server.update(update, options)
}
list = function () {
return Server.findAll()
}
listForApi = function (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: [ getSort(sort) ]
}
return Server.findAndCountAll(query).then(({ rows, count }) => {
return {
data: rows,
total: count
}
})
}
listAllIds = function (transaction: Sequelize.Transaction) {
const query = {
attributes: [ 'id' ],
transaction
}
return Server.findAll(query).then(servers => {
return map(servers, 'id')
})
}
listRandomServerIdsWithRequest = function (limit: number, tableWithServers: string, tableWithServersJoins: string) {
return Server.count().then(count => {
// Optimization...
if (count === 0) return []
let start = Math.floor(Math.random() * count) - limit
if (start < 0) start = 0
const subQuery = `(SELECT DISTINCT "${tableWithServers}"."serverId" FROM "${tableWithServers}" ${tableWithServersJoins})`
const query = {
attributes: [ 'id' ],
order: [
[ 'id', 'ASC' ]
],
offset: start,
limit: limit,
where: {
id: {
[Sequelize.Op.in]: Sequelize.literal(subQuery)
}
}
}
return Server.findAll(query).then(servers => {
return map(servers, 'id')
})
})
}
listBadServers = function () {
const query = {
where: {
score: {
[Sequelize.Op.lte]: 0
}
}
}
return Server.findAll(query)
}
load = function (id: number) {
return Server.findById(id)
}
loadByHost = function (host: string) {
const query = {
where: {
host: host
}
}
return Server.findOne(query)
}
removeAll = function () {
return Server.destroy()
}
updateServersScore = function (goodServers: number[], badServers: number[]) {
updateServersScoreAndRemoveBadOnes = function (goodServers: number[], badServers: number[]) {
logger.info('Updating %d good servers and %d bad servers scores.', goodServers.length, badServers.length)
if (goodServers.length !== 0) {
@ -231,3 +92,33 @@ async function removeBadServers () {
logger.error('Cannot remove bad servers.', err)
}
}
function incrementScores (ids: number[], value: number) {
const update = {
score: Sequelize.literal('score +' + value)
}
const options = {
where: {
id: {
[Sequelize.Op.in]: ids
}
},
// In this case score is a literal and not an integer so we do not validate it
validate: false
}
return Server.update(update, options)
}
function listBadServers () {
const query = {
where: {
score: {
[Sequelize.Op.lte]: 0
}
}
}
return Server.findAll(query)
}

View File

@ -10,24 +10,13 @@ import { BlacklistedVideo as FormattedBlacklistedVideo } from '../../../shared/m
export namespace BlacklistedVideoMethods {
export type ToFormattedJSON = (this: BlacklistedVideoInstance) => FormattedBlacklistedVideo
export type CountTotal = () => Promise<number>
export type List = () => Promise<BlacklistedVideoInstance[]>
export type ListForApi = (start: number, count: number, sort: SortType) => Promise< ResultList<BlacklistedVideoInstance> >
export type LoadById = (id: number) => Promise<BlacklistedVideoInstance>
export type LoadByVideoId = (id: number) => Promise<BlacklistedVideoInstance>
}
export interface BlacklistedVideoClass {
toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
countTotal: BlacklistedVideoMethods.CountTotal
list: BlacklistedVideoMethods.List
listForApi: BlacklistedVideoMethods.ListForApi
loadById: BlacklistedVideoMethods.LoadById
loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
}

View File

@ -12,10 +12,7 @@ import {
let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
let countTotal: BlacklistedVideoMethods.CountTotal
let list: BlacklistedVideoMethods.List
let listForApi: BlacklistedVideoMethods.ListForApi
let loadById: BlacklistedVideoMethods.LoadById
let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
@ -34,10 +31,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
const classMethods = [
associate,
countTotal,
list,
listForApi,
loadById,
loadByVideoId
]
const instanceMethods = [
@ -83,14 +77,6 @@ function associate (models) {
})
}
countTotal = function () {
return BlacklistedVideo.count()
}
list = function () {
return BlacklistedVideo.findAll()
}
listForApi = function (start: number, count: number, sort: SortType) {
const query = {
offset: start,
@ -107,10 +93,6 @@ listForApi = function (start: number, count: number, sort: SortType) {
})
}
loadById = function (id: number) {
return BlacklistedVideo.findById(id)
}
loadByVideoId = function (id: number) {
const query = {
where: {

View File

@ -1,13 +1,11 @@
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
import * as Sequelize from 'sequelize'
import { ResultList } from '../../../shared'
// Don't use barrel, import just what we need
import { VideoChannel as FormattedVideoChannel } from '../../../shared/models/videos/video-channel.model'
import { VideoInstance } from './video-interface'
import { AccountInstance } from '../account/account-interface'
import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object'
import { VideoChannel as FormattedVideoChannel } from '../../../shared/models/videos/video-channel.model'
import { AccountInstance } from '../account/account-interface'
import { VideoInstance } from './video-interface'
export namespace VideoChannelMethods {
export type ToFormattedJSON = (this: VideoChannelInstance) => FormattedVideoChannel
@ -15,7 +13,6 @@ export namespace VideoChannelMethods {
export type IsOwned = (this: VideoChannelInstance) => boolean
export type CountByAccount = (accountId: number) => Promise<number>
export type ListOwned = () => Promise<VideoChannelInstance[]>
export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<VideoChannelInstance> >
export type LoadByIdAndAccount = (id: number, accountId: number) => Promise<VideoChannelInstance>
export type ListByAccount = (accountId: number) => Promise< ResultList<VideoChannelInstance> >
@ -32,10 +29,7 @@ export interface VideoChannelClass {
countByAccount: VideoChannelMethods.CountByAccount
listForApi: VideoChannelMethods.ListForApi
listByAccount: VideoChannelMethods.ListByAccount
listOwned: VideoChannelMethods.ListOwned
loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
loadByUUID: VideoChannelMethods.LoadByUUID
loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos

View File

@ -12,7 +12,6 @@ let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
let isOwned: VideoChannelMethods.IsOwned
let countByAccount: VideoChannelMethods.CountByAccount
let listOwned: VideoChannelMethods.ListOwned
let listForApi: VideoChannelMethods.ListForApi
let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
@ -88,7 +87,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
listForApi,
listByAccount,
listOwned,
loadByIdAndAccount,
loadAndPopulateAccount,
loadByUUIDAndPopulateAccount,
@ -192,17 +190,6 @@ countByAccount = function (accountId: number) {
return VideoChannel.count(query)
}
listOwned = function () {
const query = {
where: {
remote: false
},
include: [ VideoChannel['sequelize'].models.Account ]
}
return VideoChannel.findAll(query)
}
listForApi = function (start: number, count: number, sort: string) {
const query = {
offset: start,

View File

@ -3,13 +3,12 @@ import * as Sequelize from 'sequelize'
import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object'
import { ResultList } from '../../../shared/models/result-list.model'
import { Video as FormattedVideo, VideoDetails as FormattedDetailsVideo } from '../../../shared/models/videos/video.model'
import { AccountVideoRateInstance } from '../account/account-video-rate-interface'
import { TagAttributes, TagInstance } from './tag-interface'
import { VideoChannelInstance } from './video-channel-interface'
import { VideoFileAttributes, VideoFileInstance } from './video-file-interface'
import { VideoShareInstance } from './video-share-interface'
import { UserVideoRate } from '../../../shared/models/videos/user-video-rate.model'
import { AccountVideoRateInstance } from '../account/account-video-rate-interface'
export namespace VideoMethods {
export type GetThumbnailName = (this: VideoInstance) => string
@ -40,12 +39,7 @@ export namespace VideoMethods {
export type GetLicenceLabel = (this: VideoInstance) => string
export type GetLanguageLabel = (this: VideoInstance) => string
// Return thumbnail name
export type GenerateThumbnailFromData = (video: VideoInstance, thumbnailData: string) => Promise<string>
export type List = () => Bluebird<VideoInstance[]>
export type ListOwnedAndPopulateAccountAndTags = () => Bluebird<VideoInstance[]>
export type ListOwnedByAccount = (account: string) => Bluebird<VideoInstance[]>
export type ListAllAndSharedByAccountForOutbox = (
accountId: number,
@ -65,9 +59,6 @@ export namespace VideoMethods {
export type Load = (id: number) => Bluebird<VideoInstance>
export type LoadByUUID = (uuid: string, t?: Sequelize.Transaction) => Bluebird<VideoInstance>
export type LoadByUrlAndPopulateAccount = (url: string, t?: Sequelize.Transaction) => Bluebird<VideoInstance>
export type LoadLocalVideoByUUID = (uuid: string, t?: Sequelize.Transaction) => Bluebird<VideoInstance>
export type LoadByHostAndUUID = (fromHost: string, uuid: string, t?: Sequelize.Transaction) => Bluebird<VideoInstance>
export type LoadAndPopulateAccount = (id: number) => Bluebird<VideoInstance>
export type LoadAndPopulateAccountAndServerAndTags = (id: number) => Bluebird<VideoInstance>
export type LoadByUUIDAndPopulateAccountAndServerAndTags = (uuid: string) => Bluebird<VideoInstance>
export type LoadByUUIDOrURL = (uuid: string, url: string, t?: Sequelize.Transaction) => Bluebird<VideoInstance>
@ -79,21 +70,15 @@ export namespace VideoMethods {
}
export interface VideoClass {
generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
list: VideoMethods.List
listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox
listForApi: VideoMethods.ListForApi
listUserVideosForApi: VideoMethods.ListUserVideosForApi
listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags
listOwnedByAccount: VideoMethods.ListOwnedByAccount
load: VideoMethods.Load
loadAndPopulateAccount: VideoMethods.LoadAndPopulateAccount
loadAndPopulateAccountAndServerAndTags: VideoMethods.LoadAndPopulateAccountAndServerAndTags
loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
loadByUUID: VideoMethods.LoadByUUID
loadByUrlAndPopulateAccount: VideoMethods.LoadByUrlAndPopulateAccount
loadByUUIDOrURL: VideoMethods.LoadByUUIDOrURL
loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID
loadByUUIDAndPopulateAccountAndServerAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndServerAndTags
searchAndPopulateAccountAndServerAndTags: VideoMethods.SearchAndPopulateAccountAndServerAndTags
}

View File

@ -1,8 +1,8 @@
import * as Bluebird from 'bluebird'
import { map, maxBy, truncate } from 'lodash'
import * as magnetUtil from 'magnet-uri'
import * as parseTorrent from 'parse-torrent'
import { join } from 'path'
import * as safeBuffer from 'safe-buffer'
import * as Sequelize from 'sequelize'
import { VideoPrivacy, VideoResolution } from '../../../shared'
import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object'
@ -25,6 +25,7 @@ import {
unlinkPromise,
writeFilePromise
} from '../../helpers'
import { activityPubCollection } from '../../helpers/activitypub'
import { isVideoUrlValid } from '../../helpers/custom-validators/videos'
import {
API_VERSION,
@ -39,17 +40,13 @@ import {
VIDEO_LICENCES,
VIDEO_PRIVACIES
} from '../../initializers'
import { sendDeleteVideo } from '../../lib/index'
import { addMethodsToModel, getSort } from '../utils'
import { TagInstance } from './tag-interface'
import { VideoFileInstance, VideoFileModel } from './video-file-interface'
import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface'
import { sendDeleteVideo } from '../../lib/index'
import * as Bluebird from 'bluebird'
import { activityPubCollection } from '../../helpers/activitypub'
const Buffer = safeBuffer.Buffer
let Video: Sequelize.Model<VideoInstance, VideoAttributes>
let getOriginalFile: VideoMethods.GetOriginalFile
@ -77,20 +74,14 @@ let getCategoryLabel: VideoMethods.GetCategoryLabel
let getLicenceLabel: VideoMethods.GetLicenceLabel
let getLanguageLabel: VideoMethods.GetLanguageLabel
let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
let list: VideoMethods.List
let listForApi: VideoMethods.ListForApi
let listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox
let listUserVideosForApi: VideoMethods.ListUserVideosForApi
let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
let listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags
let listOwnedByAccount: VideoMethods.ListOwnedByAccount
let load: VideoMethods.Load
let loadByUrlAndPopulateAccount: VideoMethods.LoadByUrlAndPopulateAccount
let loadByUUID: VideoMethods.LoadByUUID
let loadByUUIDOrURL: VideoMethods.LoadByUUIDOrURL
let loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID
let loadAndPopulateAccount: VideoMethods.LoadAndPopulateAccount
let loadAndPopulateAccountAndServerAndTags: VideoMethods.LoadAndPopulateAccountAndServerAndTags
let loadByUUIDAndPopulateAccountAndServerAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndServerAndTags
let searchAndPopulateAccountAndServerAndTags: VideoMethods.SearchAndPopulateAccountAndServerAndTags
@ -267,21 +258,15 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
const classMethods = [
associate,
generateThumbnailFromData,
list,
listAllAndSharedByAccountForOutbox,
listForApi,
listUserVideosForApi,
listOwnedAndPopulateAccountAndTags,
listOwnedByAccount,
load,
loadByUrlAndPopulateAccount,
loadAndPopulateAccount,
loadAndPopulateAccountAndServerAndTags,
loadByHostAndUUID,
loadByUUIDOrURL,
loadByUUID,
loadLocalVideoByUUID,
loadByUUIDAndPopulateAccountAndServerAndTags,
searchAndPopulateAccountAndServerAndTags
]
@ -803,16 +788,6 @@ removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) {
// ------------------------------ STATICS ------------------------------
generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) {
// Creating the thumbnail for a remote video
const thumbnailName = video.getThumbnailName()
const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => {
return thumbnailName
})
}
list = function () {
const query = {
include: [ Video['sequelize'].models.VideoFile ]
@ -970,84 +945,6 @@ listForApi = function (start: number, count: number, sort: string) {
})
}
loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoAttributes> = {
where: {
uuid
},
include: [
{
model: Video['sequelize'].models.VideoFile
},
{
model: Video['sequelize'].models.VideoChannel,
include: [
{
model: Video['sequelize'].models.Account,
include: [
{
model: Video['sequelize'].models.Server,
required: true,
where: {
host: fromHost
}
}
]
}
]
}
]
}
if (t !== undefined) query.transaction = t
return Video.findOne(query)
}
listOwnedAndPopulateAccountAndTags = function () {
const query = {
where: {
remote: false
},
include: [
Video['sequelize'].models.VideoFile,
{
model: Video['sequelize'].models.VideoChannel,
include: [ Video['sequelize'].models.Account ]
},
Video['sequelize'].models.Tag
]
}
return Video.findAll(query)
}
listOwnedByAccount = function (account: string) {
const query = {
where: {
remote: false
},
include: [
{
model: Video['sequelize'].models.VideoFile
},
{
model: Video['sequelize'].models.VideoChannel,
include: [
{
model: Video['sequelize'].models.Account,
where: {
name: account
}
}
]
}
]
}
return Video.findAll(query)
}
load = function (id: number) {
return Video.findById(id)
}
@ -1100,34 +997,6 @@ loadByUUIDOrURL = function (uuid: string, url: string, t?: Sequelize.Transaction
return Video.findOne(query)
}
loadLocalVideoByUUID = function (uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoAttributes> = {
where: {
uuid,
remote: false
},
include: [ Video['sequelize'].models.VideoFile ]
}
if (t !== undefined) query.transaction = t
return Video.findOne(query)
}
loadAndPopulateAccount = function (id: number) {
const options = {
include: [
Video['sequelize'].models.VideoFile,
{
model: Video['sequelize'].models.VideoChannel,
include: [ Video['sequelize'].models.Account ]
}
]
}
return Video.findById(id, options)
}
loadAndPopulateAccountAndServerAndTags = function (id: number) {
const options = {
include: [