Cleanup models

This commit is contained in:
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 * as Sequelize from 'sequelize'
import { VideoChannelCreate } from '../../shared/models' import { VideoChannelCreate } from '../../shared/models'
import { logger } from '../helpers'
import { database as db } from '../initializers' import { database as db } from '../initializers'
import { AccountInstance } from '../models' import { AccountInstance } from '../models'
import { getVideoChannelActivityPubUrl } from './activitypub/url' import { getVideoChannelActivityPubUrl } from './activitypub/url'
@ -27,21 +26,8 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account
return videoChannelCreated 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 { export {
createVideoChannel, createVideoChannel
fetchVideoChannelByHostAndUUID
} }

View File

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

View File

@ -20,14 +20,12 @@ import { addMethodsToModel } from '../utils'
import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface' import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
let Account: Sequelize.Model<AccountInstance, AccountAttributes> let Account: Sequelize.Model<AccountInstance, AccountAttributes>
let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
let load: AccountMethods.Load let load: AccountMethods.Load
let loadApplication: AccountMethods.LoadApplication let loadApplication: AccountMethods.LoadApplication
let loadByUUID: AccountMethods.LoadByUUID let loadByUUID: AccountMethods.LoadByUUID
let loadByUrl: AccountMethods.LoadByUrl let loadByUrl: AccountMethods.LoadByUrl
let loadLocalByName: AccountMethods.LoadLocalByName let loadLocalByName: AccountMethods.LoadLocalByName
let loadByNameAndHost: AccountMethods.LoadByNameAndHost let loadByNameAndHost: AccountMethods.LoadByNameAndHost
let listOwned: AccountMethods.ListOwned
let isOwned: AccountMethods.IsOwned let isOwned: AccountMethods.IsOwned
let toActivityPubObject: AccountMethods.ToActivityPubObject let toActivityPubObject: AccountMethods.ToActivityPubObject
let toFormattedJSON: AccountMethods.ToFormattedJSON let toFormattedJSON: AccountMethods.ToFormattedJSON
@ -185,14 +183,12 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
const classMethods = [ const classMethods = [
associate, associate,
loadAccountByServerAndUUID,
loadApplication, loadApplication,
load, load,
loadByUUID, loadByUUID,
loadByUrl, loadByUrl,
loadLocalByName, loadLocalByName,
loadByNameAndHost, loadByNameAndHost
listOwned
] ]
const instanceMethods = [ const instanceMethods = [
isOwned, isOwned,
@ -355,16 +351,6 @@ getPublicKeyUrl = function (this: AccountInstance) {
// ------------------------------ STATICS ------------------------------ // ------------------------------ STATICS ------------------------------
listOwned = function () {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
serverId: null
}
}
return Account.findAll(query)
}
loadApplication = function () { loadApplication = function () {
return Account.findOne({ return Account.findOne({
include: [ include: [
@ -441,15 +427,3 @@ loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
return Account.findOne(query) 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 Promise from 'bluebird'
import * as Sequelize from 'sequelize'
import { UserModel } from '../account/user-interface' import { UserModel } from '../account/user-interface'
@ -18,15 +18,12 @@ export namespace OAuthTokenMethods {
export type GetByRefreshTokenAndPopulateClient = (refreshToken: string) => Promise<OAuthTokenInfo> export type GetByRefreshTokenAndPopulateClient = (refreshToken: string) => Promise<OAuthTokenInfo>
export type GetByTokenAndPopulateUser = (bearerToken: string) => Promise<OAuthTokenInstance> export type GetByTokenAndPopulateUser = (bearerToken: string) => Promise<OAuthTokenInstance>
export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Promise<OAuthTokenInstance> export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Promise<OAuthTokenInstance>
export type RemoveByUserId = (userId) => Promise<number>
} }
export interface OAuthTokenClass { export interface OAuthTokenClass {
getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
removeByUserId: OAuthTokenMethods.RemoveByUserId
} }
export interface OAuthTokenAttributes { export interface OAuthTokenAttributes {

View File

@ -3,19 +3,12 @@ import * as Sequelize from 'sequelize'
import { logger } from '../../helpers' import { logger } from '../../helpers'
import { addMethodsToModel } from '../utils' import { addMethodsToModel } from '../utils'
import { import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface'
OAuthTokenInstance,
OAuthTokenAttributes,
OAuthTokenMethods,
OAuthTokenInfo
} from './oauth-token-interface'
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes> let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
let removeByUserId: OAuthTokenMethods.RemoveByUserId
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken', OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
@ -62,8 +55,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
getByRefreshTokenAndPopulateClient, getByRefreshTokenAndPopulateClient,
getByTokenAndPopulateUser, getByTokenAndPopulateUser,
getByRefreshTokenAndPopulateUser, getByRefreshTokenAndPopulateUser
removeByUserId
] ]
addMethodsToModel(OAuthToken, classMethods) addMethodsToModel(OAuthToken, classMethods)
@ -170,13 +162,3 @@ getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
return token 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' import * as Promise from 'bluebird'
import * as Sequelize from 'sequelize'
// Don't use barrel, import just what we need
import { ResultList } from '../../../shared/models/result-list.model'
export namespace ServerMethods { 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 ListBadServers = () => Promise<ServerInstance[]>
export type UpdateServersScoreAndRemoveBadOnes = (goodServers: number[], badServers: number[]) => void
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 interface ServerClass { export interface ServerClass {
countAll: ServerMethods.CountAll updateServersScoreAndRemoveBadOnes: ServerMethods.UpdateServersScoreAndRemoveBadOnes
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
} }
export interface ServerAttributes { export interface ServerAttributes {

View File

@ -1,29 +1,11 @@
import { map } from 'lodash'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { isHostValid, logger } from '../../helpers'
import { FRIEND_SCORE, SERVERS_SCORE } from '../../initializers' import { FRIEND_SCORE, SERVERS_SCORE } from '../../initializers'
import { logger, isHostValid } from '../../helpers' import { addMethodsToModel } from '../utils'
import { ServerAttributes, ServerInstance, ServerMethods } from './server-interface'
import { addMethodsToModel, getSort } from '../utils'
import {
ServerInstance,
ServerAttributes,
ServerMethods
} from './server-interface'
let Server: Sequelize.Model<ServerInstance, ServerAttributes> let Server: Sequelize.Model<ServerInstance, ServerAttributes>
let countAll: ServerMethods.CountAll let updateServersScoreAndRemoveBadOnes: ServerMethods.UpdateServersScoreAndRemoveBadOnes
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
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
Server = sequelize.define<ServerInstance, ServerAttributes>('Server', Server = sequelize.define<ServerInstance, ServerAttributes>('Server',
@ -62,17 +44,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
) )
const classMethods = [ const classMethods = [
countAll, listBadServers
incrementScores,
list,
listForApi,
listAllIds,
listRandomServerIdsWithRequest,
listBadServers,
load,
loadByHost,
updateServersScore,
removeAll
] ]
addMethodsToModel(Server, classMethods) addMethodsToModel(Server, classMethods)
@ -81,118 +53,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
// ------------------------------ Statics ------------------------------ // ------------------------------ Statics ------------------------------
countAll = function () { updateServersScoreAndRemoveBadOnes = function (goodServers: number[], badServers: number[]) {
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[]) {
logger.info('Updating %d good servers and %d bad servers scores.', goodServers.length, badServers.length) logger.info('Updating %d good servers and %d bad servers scores.', goodServers.length, badServers.length)
if (goodServers.length !== 0) { if (goodServers.length !== 0) {
@ -231,3 +92,33 @@ async function removeBadServers () {
logger.error('Cannot remove bad servers.', err) 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 namespace BlacklistedVideoMethods {
export type ToFormattedJSON = (this: BlacklistedVideoInstance) => FormattedBlacklistedVideo 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 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 type LoadByVideoId = (id: number) => Promise<BlacklistedVideoInstance>
} }
export interface BlacklistedVideoClass { export interface BlacklistedVideoClass {
toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
countTotal: BlacklistedVideoMethods.CountTotal
list: BlacklistedVideoMethods.List
listForApi: BlacklistedVideoMethods.ListForApi listForApi: BlacklistedVideoMethods.ListForApi
loadById: BlacklistedVideoMethods.LoadById
loadByVideoId: BlacklistedVideoMethods.LoadByVideoId loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
} }

View File

@ -12,10 +12,7 @@ import {
let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes> let BlacklistedVideo: Sequelize.Model<BlacklistedVideoInstance, BlacklistedVideoAttributes>
let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON
let countTotal: BlacklistedVideoMethods.CountTotal
let list: BlacklistedVideoMethods.List
let listForApi: BlacklistedVideoMethods.ListForApi let listForApi: BlacklistedVideoMethods.ListForApi
let loadById: BlacklistedVideoMethods.LoadById
let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
@ -34,10 +31,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
const classMethods = [ const classMethods = [
associate, associate,
countTotal,
list,
listForApi, listForApi,
loadById,
loadByVideoId loadByVideoId
] ]
const instanceMethods = [ 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) { listForApi = function (start: number, count: number, sort: SortType) {
const query = { const query = {
offset: start, 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) { loadByVideoId = function (id: number) {
const query = { const query = {
where: { where: {

View File

@ -1,13 +1,11 @@
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import * as Sequelize from 'sequelize'
import { ResultList } from '../../../shared' 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 { 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 namespace VideoChannelMethods {
export type ToFormattedJSON = (this: VideoChannelInstance) => FormattedVideoChannel export type ToFormattedJSON = (this: VideoChannelInstance) => FormattedVideoChannel
@ -15,7 +13,6 @@ export namespace VideoChannelMethods {
export type IsOwned = (this: VideoChannelInstance) => boolean export type IsOwned = (this: VideoChannelInstance) => boolean
export type CountByAccount = (accountId: number) => Promise<number> 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 ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<VideoChannelInstance> >
export type LoadByIdAndAccount = (id: number, accountId: number) => Promise<VideoChannelInstance> export type LoadByIdAndAccount = (id: number, accountId: number) => Promise<VideoChannelInstance>
export type ListByAccount = (accountId: number) => Promise< ResultList<VideoChannelInstance> > export type ListByAccount = (accountId: number) => Promise< ResultList<VideoChannelInstance> >
@ -32,10 +29,7 @@ export interface VideoChannelClass {
countByAccount: VideoChannelMethods.CountByAccount countByAccount: VideoChannelMethods.CountByAccount
listForApi: VideoChannelMethods.ListForApi listForApi: VideoChannelMethods.ListForApi
listByAccount: VideoChannelMethods.ListByAccount listByAccount: VideoChannelMethods.ListByAccount
listOwned: VideoChannelMethods.ListOwned
loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
loadByUUID: VideoChannelMethods.LoadByUUID
loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos

View File

@ -12,7 +12,6 @@ let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
let isOwned: VideoChannelMethods.IsOwned let isOwned: VideoChannelMethods.IsOwned
let countByAccount: VideoChannelMethods.CountByAccount let countByAccount: VideoChannelMethods.CountByAccount
let listOwned: VideoChannelMethods.ListOwned
let listForApi: VideoChannelMethods.ListForApi let listForApi: VideoChannelMethods.ListForApi
let listByAccount: VideoChannelMethods.ListByAccount let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
@ -88,7 +87,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
listForApi, listForApi,
listByAccount, listByAccount,
listOwned,
loadByIdAndAccount, loadByIdAndAccount,
loadAndPopulateAccount, loadAndPopulateAccount,
loadByUUIDAndPopulateAccount, loadByUUIDAndPopulateAccount,
@ -192,17 +190,6 @@ countByAccount = function (accountId: number) {
return VideoChannel.count(query) 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) { listForApi = function (start: number, count: number, sort: string) {
const query = { const query = {
offset: start, offset: start,

View File

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

View File

@ -1,8 +1,8 @@
import * as Bluebird from 'bluebird'
import { map, maxBy, truncate } from 'lodash' import { map, maxBy, truncate } from 'lodash'
import * as magnetUtil from 'magnet-uri' import * as magnetUtil from 'magnet-uri'
import * as parseTorrent from 'parse-torrent' import * as parseTorrent from 'parse-torrent'
import { join } from 'path' import { join } from 'path'
import * as safeBuffer from 'safe-buffer'
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { VideoPrivacy, VideoResolution } from '../../../shared' import { VideoPrivacy, VideoResolution } from '../../../shared'
import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object' import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object'
@ -25,6 +25,7 @@ import {
unlinkPromise, unlinkPromise,
writeFilePromise writeFilePromise
} from '../../helpers' } from '../../helpers'
import { activityPubCollection } from '../../helpers/activitypub'
import { isVideoUrlValid } from '../../helpers/custom-validators/videos' import { isVideoUrlValid } from '../../helpers/custom-validators/videos'
import { import {
API_VERSION, API_VERSION,
@ -39,17 +40,13 @@ import {
VIDEO_LICENCES, VIDEO_LICENCES,
VIDEO_PRIVACIES VIDEO_PRIVACIES
} from '../../initializers' } from '../../initializers'
import { sendDeleteVideo } from '../../lib/index'
import { addMethodsToModel, getSort } from '../utils' import { addMethodsToModel, getSort } from '../utils'
import { TagInstance } from './tag-interface' import { TagInstance } from './tag-interface'
import { VideoFileInstance, VideoFileModel } from './video-file-interface' import { VideoFileInstance, VideoFileModel } from './video-file-interface'
import { VideoAttributes, VideoInstance, VideoMethods } from './video-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 Video: Sequelize.Model<VideoInstance, VideoAttributes>
let getOriginalFile: VideoMethods.GetOriginalFile let getOriginalFile: VideoMethods.GetOriginalFile
@ -77,20 +74,14 @@ let getCategoryLabel: VideoMethods.GetCategoryLabel
let getLicenceLabel: VideoMethods.GetLicenceLabel let getLicenceLabel: VideoMethods.GetLicenceLabel
let getLanguageLabel: VideoMethods.GetLanguageLabel let getLanguageLabel: VideoMethods.GetLanguageLabel
let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData
let list: VideoMethods.List let list: VideoMethods.List
let listForApi: VideoMethods.ListForApi let listForApi: VideoMethods.ListForApi
let listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox let listAllAndSharedByAccountForOutbox: VideoMethods.ListAllAndSharedByAccountForOutbox
let listUserVideosForApi: VideoMethods.ListUserVideosForApi let listUserVideosForApi: VideoMethods.ListUserVideosForApi
let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID
let listOwnedAndPopulateAccountAndTags: VideoMethods.ListOwnedAndPopulateAccountAndTags
let listOwnedByAccount: VideoMethods.ListOwnedByAccount
let load: VideoMethods.Load let load: VideoMethods.Load
let loadByUrlAndPopulateAccount: VideoMethods.LoadByUrlAndPopulateAccount let loadByUrlAndPopulateAccount: VideoMethods.LoadByUrlAndPopulateAccount
let loadByUUID: VideoMethods.LoadByUUID let loadByUUID: VideoMethods.LoadByUUID
let loadByUUIDOrURL: VideoMethods.LoadByUUIDOrURL let loadByUUIDOrURL: VideoMethods.LoadByUUIDOrURL
let loadLocalVideoByUUID: VideoMethods.LoadLocalVideoByUUID
let loadAndPopulateAccount: VideoMethods.LoadAndPopulateAccount
let loadAndPopulateAccountAndServerAndTags: VideoMethods.LoadAndPopulateAccountAndServerAndTags let loadAndPopulateAccountAndServerAndTags: VideoMethods.LoadAndPopulateAccountAndServerAndTags
let loadByUUIDAndPopulateAccountAndServerAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndServerAndTags let loadByUUIDAndPopulateAccountAndServerAndTags: VideoMethods.LoadByUUIDAndPopulateAccountAndServerAndTags
let searchAndPopulateAccountAndServerAndTags: VideoMethods.SearchAndPopulateAccountAndServerAndTags let searchAndPopulateAccountAndServerAndTags: VideoMethods.SearchAndPopulateAccountAndServerAndTags
@ -267,21 +258,15 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
const classMethods = [ const classMethods = [
associate, associate,
generateThumbnailFromData,
list, list,
listAllAndSharedByAccountForOutbox, listAllAndSharedByAccountForOutbox,
listForApi, listForApi,
listUserVideosForApi, listUserVideosForApi,
listOwnedAndPopulateAccountAndTags,
listOwnedByAccount,
load, load,
loadByUrlAndPopulateAccount, loadByUrlAndPopulateAccount,
loadAndPopulateAccount,
loadAndPopulateAccountAndServerAndTags, loadAndPopulateAccountAndServerAndTags,
loadByHostAndUUID,
loadByUUIDOrURL, loadByUUIDOrURL,
loadByUUID, loadByUUID,
loadLocalVideoByUUID,
loadByUUIDAndPopulateAccountAndServerAndTags, loadByUUIDAndPopulateAccountAndServerAndTags,
searchAndPopulateAccountAndServerAndTags searchAndPopulateAccountAndServerAndTags
] ]
@ -803,16 +788,6 @@ removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) {
// ------------------------------ STATICS ------------------------------ // ------------------------------ 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 () { list = function () {
const query = { const query = {
include: [ Video['sequelize'].models.VideoFile ] 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) { load = function (id: number) {
return Video.findById(id) return Video.findById(id)
} }
@ -1100,34 +997,6 @@ loadByUUIDOrURL = function (uuid: string, url: string, t?: Sequelize.Transaction
return Video.findOne(query) 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) { loadAndPopulateAccountAndServerAndTags = function (id: number) {
const options = { const options = {
include: [ include: [