From: Chocobozzz Date: Thu, 23 Nov 2017 16:36:15 +0000 (+0100) Subject: Cleanup models X-Git-Tag: v0.0.1-alpha~184 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=39445ead45aaaea801ec09991b8dd2464f722e47;p=oweals%2Fpeertube.git Cleanup models --- diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index a939a23d5..beb01da9b 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts @@ -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 } diff --git a/server/models/account/account-interface.ts b/server/models/account/account-interface.ts index e30260f76..2e4b0382d 100644 --- a/server/models/account/account-interface.ts +++ b/server/models/account/account-interface.ts @@ -10,10 +10,8 @@ export namespace AccountMethods { export type Load = (id: number) => Bluebird export type LoadByUUID = (uuid: string) => Bluebird export type LoadByUrl = (url: string, transaction?: Sequelize.Transaction) => Bluebird - export type LoadAccountByServerAndUUID = (uuid: string, serverId: number, transaction: Sequelize.Transaction) => Bluebird export type LoadLocalByName = (name: string) => Bluebird export type LoadByNameAndHost = (name: string, host: string) => Bluebird - export type ListOwned = () => Bluebird 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 { diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 9a2921501..f2bd325f3 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -20,14 +20,12 @@ import { addMethodsToModel } from '../utils' import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface' let Account: Sequelize.Model -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 = { - 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 = { - where: { - serverId, - uuid - }, - transaction - } - - return Account.find(query) -} diff --git a/server/models/oauth/oauth-token-interface.ts b/server/models/oauth/oauth-token-interface.ts index ef97893c4..47d95d5fc 100644 --- a/server/models/oauth/oauth-token-interface.ts +++ b/server/models/oauth/oauth-token-interface.ts @@ -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 export type GetByTokenAndPopulateUser = (bearerToken: string) => Promise export type GetByRefreshTokenAndPopulateUser = (refreshToken: string) => Promise - - export type RemoveByUserId = (userId) => Promise } export interface OAuthTokenClass { getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser - removeByUserId: OAuthTokenMethods.RemoveByUserId } export interface OAuthTokenAttributes { diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index c7afcc38c..a82bff130 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts @@ -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 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('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) -} diff --git a/server/models/server/server-interface.ts b/server/models/server/server-interface.ts index 806d052cb..be1a4917e 100644 --- a/server/models/server/server-interface.ts +++ b/server/models/server/server-interface.ts @@ -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 - - export type IncrementScores = (ids: number[], value: number) => Promise<[ number, ServerInstance[] ]> - - export type List = () => Promise - - export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList > - - export type ListAllIds = (transaction: Sequelize.Transaction) => Promise - - export type ListRandomServerIdsWithRequest = (limit: number, tableWithServers: string, tableWithServersJoins: string) => Promise - export type ListBadServers = () => Promise - - export type Load = (id: number) => Promise - - export type LoadByHost = (host: string) => Promise - - export type RemoveAll = () => Promise - - 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 { diff --git a/server/models/server/server.ts b/server/models/server/server.ts index 26fa87550..75cd5f929 100644 --- a/server/models/server/server.ts +++ b/server/models/server/server.ts @@ -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 -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('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) +} diff --git a/server/models/video/video-blacklist-interface.ts b/server/models/video/video-blacklist-interface.ts index 9d167c037..be2483d4c 100644 --- a/server/models/video/video-blacklist-interface.ts +++ b/server/models/video/video-blacklist-interface.ts @@ -10,24 +10,13 @@ import { BlacklistedVideo as FormattedBlacklistedVideo } from '../../../shared/m export namespace BlacklistedVideoMethods { export type ToFormattedJSON = (this: BlacklistedVideoInstance) => FormattedBlacklistedVideo - - export type CountTotal = () => Promise - - export type List = () => Promise - export type ListForApi = (start: number, count: number, sort: SortType) => Promise< ResultList > - - export type LoadById = (id: number) => Promise - export type LoadByVideoId = (id: number) => Promise } export interface BlacklistedVideoClass { toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON - countTotal: BlacklistedVideoMethods.CountTotal - list: BlacklistedVideoMethods.List listForApi: BlacklistedVideoMethods.ListForApi - loadById: BlacklistedVideoMethods.LoadById loadByVideoId: BlacklistedVideoMethods.LoadByVideoId } diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 1c279b1ba..ae8286285 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -12,10 +12,7 @@ import { let BlacklistedVideo: Sequelize.Model 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: { diff --git a/server/models/video/video-channel-interface.ts b/server/models/video/video-channel-interface.ts index 8ad3e5cb7..b409d1db3 100644 --- a/server/models/video/video-channel-interface.ts +++ b/server/models/video/video-channel-interface.ts @@ -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 { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object' 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 { 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 - export type ListOwned = () => Promise export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList > export type LoadByIdAndAccount = (id: number, accountId: number) => Promise export type ListByAccount = (accountId: number) => Promise< ResultList > @@ -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 diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 93566a5c6..64130310d 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -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, diff --git a/server/models/video/video-interface.ts b/server/models/video/video-interface.ts index 89e528acf..be140de86 100644 --- a/server/models/video/video-interface.ts +++ b/server/models/video/video-interface.ts @@ -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 - export type List = () => Bluebird - export type ListOwnedAndPopulateAccountAndTags = () => Bluebird - export type ListOwnedByAccount = (account: string) => Bluebird export type ListAllAndSharedByAccountForOutbox = ( accountId: number, @@ -65,9 +59,6 @@ export namespace VideoMethods { export type Load = (id: number) => Bluebird export type LoadByUUID = (uuid: string, t?: Sequelize.Transaction) => Bluebird export type LoadByUrlAndPopulateAccount = (url: string, t?: Sequelize.Transaction) => Bluebird - export type LoadLocalVideoByUUID = (uuid: string, t?: Sequelize.Transaction) => Bluebird - export type LoadByHostAndUUID = (fromHost: string, uuid: string, t?: Sequelize.Transaction) => Bluebird - export type LoadAndPopulateAccount = (id: number) => Bluebird export type LoadAndPopulateAccountAndServerAndTags = (id: number) => Bluebird export type LoadByUUIDAndPopulateAccountAndServerAndTags = (uuid: string) => Bluebird export type LoadByUUIDOrURL = (uuid: string, url: string, t?: Sequelize.Transaction) => Bluebird @@ -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 } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 592fc2d59..e5fd92549 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -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 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 = { - 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 = { - 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: [