From 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Dec 2017 17:53:50 +0100 Subject: Move models to typescript-sequelize --- server/lib/activitypub/process/misc.ts | 38 ++++++++--------- server/lib/activitypub/process/process-accept.ts | 14 +++---- server/lib/activitypub/process/process-add.ts | 42 ++++++++++--------- server/lib/activitypub/process/process-announce.ts | 35 ++++++++-------- server/lib/activitypub/process/process-create.ts | 48 +++++++++++----------- server/lib/activitypub/process/process-delete.ts | 35 ++++++++-------- server/lib/activitypub/process/process-follow.ts | 22 +++++----- server/lib/activitypub/process/process-like.ts | 20 +++++---- server/lib/activitypub/process/process-undo.ts | 37 +++++++++-------- server/lib/activitypub/process/process-update.ts | 39 +++++++++--------- server/lib/activitypub/process/process.ts | 10 ++--- 11 files changed, 174 insertions(+), 166 deletions(-) (limited to 'server/lib/activitypub/process') diff --git a/server/lib/activitypub/process/misc.ts b/server/lib/activitypub/process/misc.ts index 0baa22c26..a775c858a 100644 --- a/server/lib/activitypub/process/misc.ts +++ b/server/lib/activitypub/process/misc.ts @@ -1,18 +1,18 @@ import * as magnetUtil from 'magnet-uri' import { VideoTorrentObject } from '../../../../shared' -import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object' -import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' +import { VideoChannelObject } from '../../../../shared/models/activitypub/objects' +import { VideoPrivacy } from '../../../../shared/models/videos' +import { doRequest } from '../../../helpers' import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' -import { doRequest } from '../../../helpers/requests' -import { database as db } from '../../../initializers' -import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -import { VideoFileAttributes } from '../../../models/video/video-file-interface' -import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface' +import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' +import { VideoChannelShareModel } from '../../../models/video/video-channel-share' +import { VideoShareModel } from '../../../models/video/video-share' import { getOrCreateAccountAndServer } from '../account' -function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) { +function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountModel) { return { name: videoChannelObject.name, description: videoChannelObject.content, @@ -26,7 +26,7 @@ function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChan } async function videoActivityObjectToDBAttributes ( - videoChannel: VideoChannelInstance, + videoChannel: VideoChannelModel, videoObject: VideoTorrentObject, to: string[] = [], cc: string[] = [] @@ -56,7 +56,7 @@ async function videoActivityObjectToDBAttributes ( description = videoObject.content } - const videoData: VideoAttributes = { + return { name: videoObject.name, uuid: videoObject.uuid, url: videoObject.id, @@ -76,11 +76,9 @@ async function videoActivityObjectToDBAttributes ( remote: true, privacy } - - return videoData } -function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { +function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) { const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT) const fileUrls = videoObject.url.filter(u => { return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/') @@ -90,7 +88,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO throw new Error('Cannot find video files for ' + videoCreated.url) } - const attributes: VideoFileAttributes[] = [] + const attributes = [] for (const fileUrl of fileUrls) { // Fetch associated magnet uri const magnet = videoObject.url.find(u => { @@ -115,7 +113,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO return attributes } -async function addVideoShares (instance: VideoInstance, shares: string[]) { +async function addVideoShares (instance: VideoModel, shares: string[]) { for (const share of shares) { // Fetch url const json = await doRequest({ @@ -132,14 +130,14 @@ async function addVideoShares (instance: VideoInstance, shares: string[]) { videoId: instance.id } - await db.VideoShare.findOrCreate({ + await VideoShareModel.findOrCreate({ where: entry, defaults: entry }) } } -async function addVideoChannelShares (instance: VideoChannelInstance, shares: string[]) { +async function addVideoChannelShares (instance: VideoChannelModel, shares: string[]) { for (const share of shares) { // Fetch url const json = await doRequest({ @@ -156,7 +154,7 @@ async function addVideoChannelShares (instance: VideoChannelInstance, shares: st videoChannelId: instance.id } - await db.VideoChannelShare.findOrCreate({ + await VideoChannelShareModel.findOrCreate({ where: entry, defaults: entry }) diff --git a/server/lib/activitypub/process/process-accept.ts b/server/lib/activitypub/process/process-accept.ts index 73c6cb279..5b321f771 100644 --- a/server/lib/activitypub/process/process-accept.ts +++ b/server/lib/activitypub/process/process-accept.ts @@ -1,12 +1,12 @@ -import { ActivityAccept } from '../../../../shared/models/activitypub/activity' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' +import { ActivityAccept } from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' import { addFetchOutboxJob } from '../fetch' -async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountInstance) { +async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountModel) { if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.') - const targetAccount = await db.Account.loadByUrl(activity.actor) + const targetAccount = await AccountModel.loadByUrl(activity.actor) return processAccept(inboxAccount, targetAccount) } @@ -19,8 +19,8 @@ export { // --------------------------------------------------------------------------- -async function processAccept (account: AccountInstance, targetAccount: AccountInstance) { - const follow = await db.AccountFollow.loadByAccountAndTarget(account.id, targetAccount.id) +async function processAccept (account: AccountModel, targetAccount: AccountModel) { + const follow = await AccountFollowModel.loadByAccountAndTarget(account.id, targetAccount.id) if (!follow) throw new Error('Cannot find associated follow.') follow.set('state', 'accepted') diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts index e6bf63eb2..550593eab 100644 --- a/server/lib/activitypub/process/process-add.ts +++ b/server/lib/activitypub/process/process-add.ts @@ -1,13 +1,15 @@ import * as Bluebird from 'bluebird' import { VideoTorrentObject } from '../../../../shared' -import { ActivityAdd } from '../../../../shared/models/activitypub/activity' -import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -import { VideoInstance } from '../../../models/video/video-interface' +import { ActivityAdd } from '../../../../shared/models/activitypub' +import { VideoRateType } from '../../../../shared/models/videos' +import { logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { TagModel } from '../../../models/video/tag' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' +import { VideoFileModel } from '../../../models/video/video-file' import { getOrCreateAccountAndServer } from '../account' import { getOrCreateVideoChannel } from '../video-channels' import { generateThumbnailFromUrl } from '../videos' @@ -37,9 +39,9 @@ export { // --------------------------------------------------------------------------- -async function processAddVideo (account: AccountInstance, +async function processAddVideo (account: AccountModel, activity: ActivityAdd, - videoChannel: VideoChannelInstance, + videoChannel: VideoChannelModel, videoToCreateData: VideoTorrentObject) { const options = { arguments: [ account, activity, videoChannel, videoToCreateData ], @@ -64,24 +66,24 @@ async function processAddVideo (account: AccountInstance, return video } -function addRemoteVideo (account: AccountInstance, +function addRemoteVideo (account: AccountModel, activity: ActivityAdd, - videoChannel: VideoChannelInstance, + videoChannel: VideoChannelModel, videoToCreateData: VideoTorrentObject) { logger.debug('Adding remote video %s.', videoToCreateData.id) - return db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') - const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) + const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) if (videoFromDatabase) return videoFromDatabase const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) - const video = db.Video.build(videoData) + const video = VideoModel.build(videoData) // Don't block on request generateThumbnailFromUrl(video, videoToCreateData.icon) @@ -94,12 +96,12 @@ function addRemoteVideo (account: AccountInstance, throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) } - const tasks: Bluebird[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) + const tasks: Bluebird[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) await Promise.all(tasks) const tags = videoToCreateData.tag.map(t => t.name) - const tagInstances = await db.Tag.findOrCreateTags(tags, t) - await videoCreated.setTags(tagInstances, sequelizeOptions) + const tagInstances = await TagModel.findOrCreateTags(tags, t) + await videoCreated.$set('Tags', tagInstances, sequelizeOptions) logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) @@ -107,13 +109,13 @@ function addRemoteVideo (account: AccountInstance, }) } -async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { +async function createRates (accountUrls: string[], video: VideoModel, rate: VideoRateType) { let rateCounts = 0 const tasks: Bluebird[] = [] for (const accountUrl of accountUrls) { const account = await getOrCreateAccountAndServer(accountUrl) - const p = db.AccountVideoRate + const p = AccountVideoRateModel .create({ videoId: video.id, accountId: account.id, diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 2aa9ad5c7..ff2c6d708 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -1,10 +1,11 @@ -import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers/index' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoInstance } from '../../../models/index' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' +import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' +import { logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' +import { VideoChannelShareModel } from '../../../models/video/video-channel-share' +import { VideoShareModel } from '../../../models/video/video-share' import { getOrCreateAccountAndServer } from '../account' import { forwardActivity } from '../send/misc' import { processAddActivity } from './process-add' @@ -36,7 +37,7 @@ export { // --------------------------------------------------------------------------- -function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { +function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { const options = { arguments: [ accountAnnouncer, activity ], errorMessage: 'Cannot share the video channel with many retries.' @@ -45,18 +46,18 @@ function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: return retryTransactionWrapper(shareVideoChannel, options) } -async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { +async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { const announcedActivity = activity.object as ActivityCreate - return db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { // Add share entry - const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity) + const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity) const share = { accountId: accountAnnouncer.id, videoChannelId: videoChannel.id } - const [ , created ] = await db.VideoChannelShare.findOrCreate({ + const [ , created ] = await VideoChannelShareModel.findOrCreate({ where: share, defaults: share, transaction: t @@ -72,7 +73,7 @@ async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: A }) } -function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { +function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { const options = { arguments: [ accountAnnouncer, activity ], errorMessage: 'Cannot share the video with many retries.' @@ -81,19 +82,19 @@ function processVideoShare (accountAnnouncer: AccountInstance, activity: Activit return retryTransactionWrapper(shareVideo, options) } -function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { +function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { const announcedActivity = activity.object as ActivityAdd - return db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { // Add share entry - const video: VideoInstance = await processAddActivity(announcedActivity) + const video: VideoModel = await processAddActivity(announcedActivity) const share = { accountId: accountAnnouncer.id, videoId: video.id } - const [ , created ] = await db.VideoShare.findOrCreate({ + const [ , created ] = await VideoShareModel.findOrCreate({ where: share, defaults: share, transaction: t diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 4740dc432..c1eb2a8ab 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,10 +1,12 @@ import { ActivityCreate, VideoChannelObject } from '../../../../shared' -import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' -import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' -import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' +import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' import { logger, retryTransactionWrapper } from '../../../helpers' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { VideoModel } from '../../../models/video/video' +import { VideoAbuseModel } from '../../../models/video/video-abuse' +import { VideoChannelModel } from '../../../models/video/video-channel' import { getOrCreateAccountAndServer } from '../account' import { forwardActivity } from '../send/misc' import { getVideoChannelActivityPubUrl } from '../url' @@ -37,7 +39,7 @@ export { // --------------------------------------------------------------------------- -async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) { +async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) { const options = { arguments: [ byAccount, activity ], errorMessage: 'Cannot dislike the video with many retries.' @@ -46,11 +48,11 @@ async function processCreateDislike (byAccount: AccountInstance, activity: Activ return retryTransactionWrapper(createVideoDislike, options) } -function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) { +function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) { const dislike = activity.object as DislikeObject - return db.sequelize.transaction(async t => { - const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) + return sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) if (!video) throw new Error('Unknown video ' + dislike.object) const rate = { @@ -58,7 +60,7 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat videoId: video.id, accountId: byAccount.id } - const [ , created ] = await db.AccountVideoRate.findOrCreate({ + const [ , created ] = await AccountVideoRateModel.findOrCreate({ where: rate, defaults: rate, transaction: t @@ -73,14 +75,14 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat }) } -async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) { +async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) { const view = activity.object as ViewObject - const video = await db.Video.loadByUrlAndPopulateAccount(view.object) + const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) if (!video) throw new Error('Unknown video ' + view.object) - const account = await db.Account.loadByUrl(view.actor) + const account = await AccountModel.loadByUrl(view.actor) if (!account) throw new Error('Unknown account ' + view.actor) await video.increment('views') @@ -92,7 +94,7 @@ async function processCreateView (byAccount: AccountInstance, activity: Activity } } -async function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { +async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { const options = { arguments: [ account, videoChannelToCreateData ], errorMessage: 'Cannot insert the remote video channel with many retries.' @@ -107,15 +109,15 @@ async function processCreateVideoChannel (account: AccountInstance, videoChannel return videoChannel } -function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { +function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) { logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) - return db.sequelize.transaction(async t => { - let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) + return sequelizeTypescript.transaction(async t => { + let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) if (videoChannel) return videoChannel const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) - videoChannel = db.VideoChannel.build(videoChannelData) + videoChannel = new VideoChannelModel(videoChannelData) videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) videoChannel = await videoChannel.save({ transaction: t }) @@ -125,7 +127,7 @@ function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateDa }) } -function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { +function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { const options = { arguments: [ account, videoAbuseToCreateData ], errorMessage: 'Cannot insert the remote video abuse with many retries.' @@ -134,11 +136,11 @@ function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateDa return retryTransactionWrapper(addRemoteVideoAbuse, options) } -function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { +function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) { logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) - return db.sequelize.transaction(async t => { - const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) + return sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) if (!video) { logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) return undefined @@ -150,7 +152,7 @@ function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: videoId: video.id } - await db.VideoAbuse.create(videoAbuseData) + await VideoAbuseModel.create(videoAbuseData) logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) }) diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 41cdc236d..8f280d37f 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts @@ -1,10 +1,9 @@ -import { ActivityDelete } from '../../../../shared/models/activitypub/activity' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -import { VideoInstance } from '../../../models/video/video-interface' +import { ActivityDelete } from '../../../../shared/models/activitypub' +import { logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' import { getOrCreateAccountAndServer } from '../account' async function processDeleteActivity (activity: ActivityDelete) { @@ -15,14 +14,14 @@ async function processDeleteActivity (activity: ActivityDelete) { } { - let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id) + let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id) if (videoObject !== undefined) { return processDeleteVideo(account, videoObject) } } { - let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id) + let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id) if (videoChannelObject !== undefined) { return processDeleteVideoChannel(account, videoChannelObject) } @@ -39,7 +38,7 @@ export { // --------------------------------------------------------------------------- -async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) { +async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) { const options = { arguments: [ account, videoToDelete ], errorMessage: 'Cannot remove the remote video with many retries.' @@ -48,10 +47,10 @@ async function processDeleteVideo (account: AccountInstance, videoToDelete: Vide await retryTransactionWrapper(deleteRemoteVideo, options) } -async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) { +async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) { logger.debug('Removing remote video "%s".', videoToDelete.uuid) - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { if (videoToDelete.VideoChannel.Account.id !== account.id) { throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) } @@ -62,7 +61,7 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) } -async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { +async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { const options = { arguments: [ account, videoChannelToRemove ], errorMessage: 'Cannot remove the remote video channel with many retries.' @@ -71,10 +70,10 @@ async function processDeleteVideoChannel (account: AccountInstance, videoChannel await retryTransactionWrapper(deleteRemoteVideoChannel, options) } -async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { +async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { if (videoChannelToRemove.Account.id !== account.id) { throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) } @@ -85,7 +84,7 @@ async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelT logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid) } -async function processDeleteAccount (accountToRemove: AccountInstance) { +async function processDeleteAccount (accountToRemove: AccountModel) { const options = { arguments: [ accountToRemove ], errorMessage: 'Cannot remove the remote account with many retries.' @@ -94,10 +93,10 @@ async function processDeleteAccount (accountToRemove: AccountInstance) { await retryTransactionWrapper(deleteRemoteAccount, options) } -async function deleteRemoteAccount (accountToRemove: AccountInstance) { +async function deleteRemoteAccount (accountToRemove: AccountModel) { logger.debug('Removing remote account "%s".', accountToRemove.uuid) - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { await accountToRemove.destroy({ transaction: t }) }) diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 320dc1138..ccaee43a6 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -1,10 +1,10 @@ -import { ActivityFollow } from '../../../../shared/models/activitypub/activity' -import { retryTransactionWrapper } from '../../../helpers' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { logger } from '../../../helpers/logger' -import { sendAccept } from '../send/send-accept' +import { ActivityFollow } from '../../../../shared/models/activitypub' +import { logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' import { getOrCreateAccountAndServer } from '../account' +import { sendAccept } from '../send' async function processFollowActivity (activity: ActivityFollow) { const activityObject = activity.object @@ -21,7 +21,7 @@ export { // --------------------------------------------------------------------------- -function processFollow (account: AccountInstance, targetAccountURL: string) { +function processFollow (account: AccountModel, targetAccountURL: string) { const options = { arguments: [ account, targetAccountURL ], errorMessage: 'Cannot follow with many retries.' @@ -30,14 +30,14 @@ function processFollow (account: AccountInstance, targetAccountURL: string) { return retryTransactionWrapper(follow, options) } -async function follow (account: AccountInstance, targetAccountURL: string) { - await db.sequelize.transaction(async t => { - const targetAccount = await db.Account.loadByUrl(targetAccountURL, t) +async function follow (account: AccountModel, targetAccountURL: string) { + await sequelizeTypescript.transaction(async t => { + const targetAccount = await AccountModel.loadByUrl(targetAccountURL, t) if (!targetAccount) throw new Error('Unknown account') if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') - const [ accountFollow ] = await db.AccountFollow.findOrCreate({ + const [ accountFollow ] = await AccountFollowModel.findOrCreate({ where: { accountId: account.id, targetAccountId: targetAccount.id diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index 5f2ffe7ea..a6e391f1e 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts @@ -1,7 +1,9 @@ -import { ActivityLike } from '../../../../shared/models/activitypub/activity' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' +import { ActivityLike } from '../../../../shared/models/activitypub' +import { retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { VideoModel } from '../../../models/video/video' import { getOrCreateAccountAndServer } from '../account' import { forwardActivity } from '../send/misc' @@ -19,7 +21,7 @@ export { // --------------------------------------------------------------------------- -async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) { +async function processLikeVideo (byAccount: AccountModel, activity: ActivityLike) { const options = { arguments: [ byAccount, activity ], errorMessage: 'Cannot like the video with many retries.' @@ -28,11 +30,11 @@ async function processLikeVideo (byAccount: AccountInstance, activity: ActivityL return retryTransactionWrapper(createVideoLike, options) } -function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) { +function createVideoLike (byAccount: AccountModel, activity: ActivityLike) { const videoUrl = activity.object - return db.sequelize.transaction(async t => { - const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl) + return sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl) if (!video) throw new Error('Unknown video ' + videoUrl) @@ -41,7 +43,7 @@ function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) { videoId: video.id, accountId: byAccount.id } - const [ , created ] = await db.AccountVideoRate.findOrCreate({ + const [ , created ] = await AccountVideoRateModel.findOrCreate({ where: rate, defaults: rate, transaction: t diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index cc221045f..efa63122b 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,8 +1,11 @@ -import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' -import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers' +import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' +import { DislikeObject } from '../../../../shared/models/activitypub/objects' +import { logger, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' +import { AccountVideoRateModel } from '../../../models/account/account-video-rate' +import { VideoModel } from '../../../models/video/video' import { forwardActivity } from '../send/misc' async function processUndoActivity (activity: ActivityUndo) { @@ -41,14 +44,14 @@ function processUndoLike (actor: string, activity: ActivityUndo) { function undoLike (actor: string, activity: ActivityUndo) { const likeActivity = activity.object as ActivityLike - return db.sequelize.transaction(async t => { - const byAccount = await db.Account.loadByUrl(actor, t) + return sequelizeTypescript.transaction(async t => { + const byAccount = await AccountModel.loadByUrl(actor, t) if (!byAccount) throw new Error('Unknown account ' + actor) - const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t) + const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) if (!video) throw new Error('Unknown video ' + likeActivity.actor) - const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) + const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) await rate.destroy({ transaction: t }) @@ -74,14 +77,14 @@ function processUndoDislike (actor: string, activity: ActivityUndo) { function undoDislike (actor: string, activity: ActivityUndo) { const dislike = activity.object.object as DislikeObject - return db.sequelize.transaction(async t => { - const byAccount = await db.Account.loadByUrl(actor, t) + return sequelizeTypescript.transaction(async t => { + const byAccount = await AccountModel.loadByUrl(actor, t) if (!byAccount) throw new Error('Unknown account ' + actor) - const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) + const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) if (!video) throw new Error('Unknown video ' + dislike.actor) - const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) + const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) await rate.destroy({ transaction: t }) @@ -105,10 +108,10 @@ function processUndoFollow (actor: string, followActivity: ActivityFollow) { } function undoFollow (actor: string, followActivity: ActivityFollow) { - return db.sequelize.transaction(async t => { - const follower = await db.Account.loadByUrl(actor, t) - const following = await db.Account.loadByUrl(followActivity.object, t) - const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t) + return sequelizeTypescript.transaction(async t => { + const follower = await AccountModel.loadByUrl(actor, t) + const following = await AccountModel.loadByUrl(followActivity.object, t) + const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t) if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 11c6de8f5..771021f0c 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts @@ -1,12 +1,13 @@ import * as Bluebird from 'bluebird' import { VideoChannelObject, VideoTorrentObject } from '../../../../shared' -import { ActivityUpdate } from '../../../../shared/models/activitypub/activity' -import { retryTransactionWrapper } from '../../../helpers/database-utils' -import { logger } from '../../../helpers/logger' -import { resetSequelizeInstance } from '../../../helpers/utils' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoInstance } from '../../../models/video/video-interface' +import { ActivityUpdate } from '../../../../shared/models/activitypub' +import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' +import { sequelizeTypescript } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { TagModel } from '../../../models/video/tag' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' +import { VideoFileModel } from '../../../models/video/video-file' import { getOrCreateAccountAndServer } from '../account' import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' @@ -30,7 +31,7 @@ export { // --------------------------------------------------------------------------- -function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject) { +function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) { const options = { arguments: [ account, video ], errorMessage: 'Cannot update the remote video with many retries' @@ -39,18 +40,18 @@ function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject return retryTransactionWrapper(updateRemoteVideo, options) } -async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpdate: VideoTorrentObject) { +async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) { logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) - let videoInstance: VideoInstance + let videoInstance: VideoModel let videoFieldsSave: object try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } - const videoInstance = await db.Video.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) + const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') if (videoInstance.VideoChannel.Account.id !== account.id) { @@ -81,12 +82,12 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd await Promise.all(videoFileDestroyTasks) const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) - const tasks: Bluebird[] = videoFileAttributes.map(f => db.VideoFile.create(f)) + const tasks: Bluebird[] = videoFileAttributes.map(f => VideoFileModel.create(f)) await Promise.all(tasks) const tags = videoAttributesToUpdate.tag.map(t => t.name) - const tagInstances = await db.Tag.findOrCreateTags(tags, t) - await videoInstance.setTags(tagInstances, sequelizeOptions) + const tagInstances = await TagModel.findOrCreateTags(tags, t) + await videoInstance.$set('Tags', tagInstances, sequelizeOptions) }) logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) @@ -101,7 +102,7 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd } } -async function processUpdateVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { +async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { const options = { arguments: [ account, videoChannel ], errorMessage: 'Cannot update the remote video channel with many retries.' @@ -110,13 +111,13 @@ async function processUpdateVideoChannel (account: AccountInstance, videoChannel await retryTransactionWrapper(updateRemoteVideoChannel, options) } -async function updateRemoteVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { +async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) { logger.debug('Updating remote video channel "%s".', videoChannel.uuid) - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } - const videoChannelInstance = await db.VideoChannel.loadByUrl(videoChannel.id) + const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id) if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.') if (videoChannelInstance.Account.id !== account.id) { diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 54981c289..bfbf8053c 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts @@ -1,6 +1,6 @@ -import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' -import { logger } from '../../../helpers/logger' -import { AccountInstance } from '../../../models/account/account-interface' +import { Activity, ActivityType } from '../../../../shared/models/activitypub' +import { logger } from '../../../helpers' +import { AccountModel } from '../../../models/account/account' import { processAcceptActivity } from './process-accept' import { processAddActivity } from './process-add' import { processAnnounceActivity } from './process-announce' @@ -11,7 +11,7 @@ import { processLikeActivity } from './process-like' import { processUndoActivity } from './process-undo' import { processUpdateActivity } from './process-update' -const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise } = { +const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountModel) => Promise } = { Create: processCreateActivity, Add: processAddActivity, Update: processUpdateActivity, @@ -23,7 +23,7 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccoun Like: processLikeActivity } -async function processActivities (activities: Activity[], signatureAccount?: AccountInstance, inboxAccount?: AccountInstance) { +async function processActivities (activities: Activity[], signatureAccount?: AccountModel, inboxAccount?: AccountModel) { for (const activity of activities) { // When we fetch remote data, we don't have signature if (signatureAccount && activity.actor !== signatureAccount.url) { -- cgit v1.2.3