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/account.ts | 28 +++++------ server/lib/activitypub/fetch.ts | 6 +-- 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 ++-- server/lib/activitypub/send/misc.ts | 56 +++++++++++----------- server/lib/activitypub/send/send-accept.ts | 12 ++--- server/lib/activitypub/send/send-add.ts | 20 ++++---- server/lib/activitypub/send/send-announce.ts | 35 ++++++-------- server/lib/activitypub/send/send-create.ts | 46 +++++++++--------- server/lib/activitypub/send/send-delete.ts | 25 +++++----- server/lib/activitypub/send/send-follow.ts | 14 +++--- server/lib/activitypub/send/send-like.ts | 19 ++++---- server/lib/activitypub/send/send-undo.ts | 26 +++++----- server/lib/activitypub/send/send-update.ts | 23 ++++----- server/lib/activitypub/share.ts | 19 ++++---- server/lib/activitypub/url.ts | 30 ++++++------ server/lib/activitypub/video-channels.ts | 21 ++++---- server/lib/activitypub/videos.ts | 31 ++++++------ server/lib/cache/videos-preview-cache.ts | 13 +++-- .../activitypub-http-broadcast-handler.ts | 3 +- .../activitypub-http-fetcher-handler.ts | 7 ++- .../activitypub-http-job-scheduler.ts | 9 ++-- .../activitypub-http-unicast-handler.ts | 3 +- server/lib/jobs/job-scheduler.ts | 20 ++++---- .../transcoding-job-scheduler.ts | 5 +- .../video-file-optimizer-handler.ts | 16 +++---- .../video-file-transcoder-handler.ts | 11 ++--- server/lib/oauth-model.ts | 19 ++++---- server/lib/user.ts | 17 ++++--- server/lib/video-channel.ts | 10 ++-- 39 files changed, 438 insertions(+), 446 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/account.ts b/server/lib/activitypub/account.ts index 906c8ff29..45690b88d 100644 --- a/server/lib/activitypub/account.ts +++ b/server/lib/activitypub/account.ts @@ -1,17 +1,15 @@ import * as Bluebird from 'bluebird' -import * as url from 'url' -import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor' -import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account' -import { retryTransactionWrapper } from '../../helpers/database-utils' -import { logger } from '../../helpers/logger' -import { doRequest } from '../../helpers/requests' -import { ACTIVITY_PUB } from '../../initializers/constants' -import { database as db } from '../../initializers/database' -import { AccountInstance } from '../../models/account/account-interface' import { Transaction } from 'sequelize' +import * as url from 'url' +import { ActivityPubActor } from '../../../shared/models/activitypub' +import { doRequest, logger, retryTransactionWrapper } from '../../helpers' +import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub' +import { ACTIVITY_PUB, sequelizeTypescript } from '../../initializers' +import { AccountModel } from '../../models/account/account' +import { ServerModel } from '../../models/server/server' async function getOrCreateAccountAndServer (accountUrl: string) { - let account = await db.Account.loadByUrl(accountUrl) + let account = await AccountModel.loadByUrl(accountUrl) // We don't have this account in our database, fetch it on remote if (!account) { @@ -28,11 +26,11 @@ async function getOrCreateAccountAndServer (accountUrl: string) { return account } -function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird | Promise { +function saveAccountAndServerIfNotExist (account: AccountModel, t?: Transaction): Bluebird | Promise { if (t !== undefined) { return save(t) } else { - return db.sequelize.transaction(t => { + return sequelizeTypescript.transaction(t => { return save(t) }) } @@ -49,7 +47,7 @@ function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transacti }, transaction: t } - const [ server ] = await db.Server.findOrCreate(serverOptions) + const [ server ] = await ServerModel.findOrCreate(serverOptions) // Save our new account in database account.set('serverId', server.id) @@ -87,7 +85,7 @@ async function fetchRemoteAccount (accountUrl: string) { const followersCount = await fetchAccountCount(accountJSON.followers) const followingCount = await fetchAccountCount(accountJSON.following) - const account = db.Account.build({ + return new AccountModel({ uuid: accountJSON.uuid, name: accountJSON.preferredUsername, url: accountJSON.url, @@ -101,8 +99,6 @@ async function fetchRemoteAccount (accountUrl: string) { followersUrl: accountJSON.followers, followingUrl: accountJSON.following }) - - return account } export { diff --git a/server/lib/activitypub/fetch.ts b/server/lib/activitypub/fetch.ts index fd2af0761..aa4dea8e0 100644 --- a/server/lib/activitypub/fetch.ts +++ b/server/lib/activitypub/fetch.ts @@ -1,8 +1,8 @@ import { Transaction } from 'sequelize' -import { AccountInstance } from '../../models/account/account-interface' -import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' +import { AccountModel } from '../../models/account/account' +import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler' -async function addFetchOutboxJob (account: AccountInstance, t: Transaction) { +async function addFetchOutboxJob (account: AccountModel, t: Transaction) { const jobPayload: ActivityPubHttpPayload = { uris: [ account.outboxUrl ] } 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) { diff --git a/server/lib/activitypub/send/misc.ts b/server/lib/activitypub/send/misc.ts index 999def701..ffc221477 100644 --- a/server/lib/activitypub/send/misc.ts +++ b/server/lib/activitypub/send/misc.ts @@ -1,19 +1,19 @@ import { Transaction } from 'sequelize' -import { Activity } from '../../../../shared/models/activitypub/activity' -import { logger } from '../../../helpers/logger' -import { ACTIVITY_PUB, database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoChannelInstance } from '../../../models/index' -import { VideoInstance } from '../../../models/video/video-interface' -import { - activitypubHttpJobScheduler, - ActivityPubHttpPayload -} from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' +import { Activity } from '../../../../shared/models/activitypub' +import { logger } from '../../../helpers' +import { ACTIVITY_PUB } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' +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 { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler' async function forwardActivity ( activity: Activity, t: Transaction, - followersException: AccountInstance[] = [] + followersException: AccountModel[] = [] ) { const to = activity.to || [] const cc = activity.cc || [] @@ -25,7 +25,7 @@ async function forwardActivity ( } } - const toAccountFollowers = await db.Account.listByFollowersUrls(followersUrls, t) + const toAccountFollowers = await AccountModel.listByFollowersUrls(followersUrls, t) const uris = await computeFollowerUris(toAccountFollowers, followersException, t) if (uris.length === 0) { @@ -45,10 +45,10 @@ async function forwardActivity ( async function broadcastToFollowers ( data: any, - byAccount: AccountInstance, - toAccountFollowers: AccountInstance[], + byAccount: AccountModel, + toAccountFollowers: AccountModel[], t: Transaction, - followersException: AccountInstance[] = [] + followersException: AccountModel[] = [] ) { const uris = await computeFollowerUris(toAccountFollowers, followersException, t) if (uris.length === 0) { @@ -67,7 +67,7 @@ async function broadcastToFollowers ( return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload) } -async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) { +async function unicastTo (data: any, byAccount: AccountModel, toAccountUrl: string, t: Transaction) { logger.debug('Creating unicast job.', { uri: toAccountUrl }) const jobPayload: ActivityPubHttpPayload = { @@ -79,42 +79,42 @@ async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: s return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) } -function getOriginVideoAudience (video: VideoInstance, accountsInvolvedInVideo: AccountInstance[]) { +function getOriginVideoAudience (video: VideoModel, accountsInvolvedInVideo: AccountModel[]) { return { to: [ video.VideoChannel.Account.url ], cc: accountsInvolvedInVideo.map(a => a.followersUrl) } } -function getOriginVideoChannelAudience (videoChannel: VideoChannelInstance, accountsInvolved: AccountInstance[]) { +function getOriginVideoChannelAudience (videoChannel: VideoChannelModel, accountsInvolved: AccountModel[]) { return { to: [ videoChannel.Account.url ], cc: accountsInvolved.map(a => a.followersUrl) } } -function getObjectFollowersAudience (accountsInvolvedInObject: AccountInstance[]) { +function getObjectFollowersAudience (accountsInvolvedInObject: AccountModel[]) { return { to: accountsInvolvedInObject.map(a => a.followersUrl), cc: [] } } -async function getAccountsInvolvedInVideo (video: VideoInstance, t: Transaction) { - const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id, t) +async function getAccountsInvolvedInVideo (video: VideoModel, t: Transaction) { + const accountsToForwardView = await VideoShareModel.loadAccountsByShare(video.id, t) accountsToForwardView.push(video.VideoChannel.Account) return accountsToForwardView } -async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { - const accountsToForwardView = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) +async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelModel, t: Transaction) { + const accountsToForwardView = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t) accountsToForwardView.push(videoChannel.Account) return accountsToForwardView } -async function getAudience (accountSender: AccountInstance, t: Transaction, isPublic = true) { +async function getAudience (accountSender: AccountModel, t: Transaction, isPublic = true) { const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls(t) // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 @@ -132,14 +132,12 @@ async function getAudience (accountSender: AccountInstance, t: Transaction, isPu return { to, cc } } -async function computeFollowerUris (toAccountFollower: AccountInstance[], followersException: AccountInstance[], t: Transaction) { +async function computeFollowerUris (toAccountFollower: AccountModel[], followersException: AccountModel[], t: Transaction) { const toAccountFollowerIds = toAccountFollower.map(a => a.id) - const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, t) + const result = await AccountFollowModel.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, t) const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) - const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) - - return uris + return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) } // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/send/send-accept.ts b/server/lib/activitypub/send/send-accept.ts index d3f8fbe38..f160af3c9 100644 --- a/server/lib/activitypub/send/send-accept.ts +++ b/server/lib/activitypub/send/send-accept.ts @@ -1,11 +1,11 @@ import { Transaction } from 'sequelize' -import { ActivityAccept } from '../../../../shared/models/activitypub/activity' -import { AccountInstance } from '../../../models' -import { AccountFollowInstance } from '../../../models/account/account-follow-interface' -import { unicastTo } from './misc' +import { ActivityAccept } from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' import { getAccountFollowAcceptActivityPubUrl } from '../url' +import { unicastTo } from './misc' -async function sendAccept (accountFollow: AccountFollowInstance, t: Transaction) { +async function sendAccept (accountFollow: AccountFollowModel, t: Transaction) { const follower = accountFollow.AccountFollower const me = accountFollow.AccountFollowing @@ -23,7 +23,7 @@ export { // --------------------------------------------------------------------------- -function acceptActivityData (url: string, byAccount: AccountInstance) { +function acceptActivityData (url: string, byAccount: AccountModel) { const activity: ActivityAccept = { type: 'Accept', id: url, diff --git a/server/lib/activitypub/send/send-add.ts b/server/lib/activitypub/send/send-add.ts index d8ac2853e..fd614db75 100644 --- a/server/lib/activitypub/send/send-add.ts +++ b/server/lib/activitypub/send/send-add.ts @@ -1,10 +1,11 @@ import { Transaction } from 'sequelize' -import { ActivityAdd } from '../../../../shared/models/activitypub/activity' -import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' -import { AccountInstance, VideoInstance } from '../../../models' +import { ActivityAdd } from '../../../../shared/models/activitypub' +import { VideoPrivacy } from '../../../../shared/models/videos' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' import { broadcastToFollowers, getAudience } from './misc' -async function sendAddVideo (video: VideoInstance, t: Transaction) { +async function sendAddVideo (video: VideoModel, t: Transaction) { const byAccount = video.VideoChannel.Account const videoObject = video.toActivityPubObject() @@ -15,16 +16,17 @@ async function sendAddVideo (video: VideoInstance, t: Transaction) { async function addActivityData ( url: string, - byAccount: AccountInstance, - video: VideoInstance, + byAccount: AccountModel, + video: VideoModel, target: string, object: any, t: Transaction -) { +): Promise { const videoPublic = video.privacy === VideoPrivacy.PUBLIC const { to, cc } = await getAudience(byAccount, t, videoPublic) - const activity: ActivityAdd = { + + return { type: 'Add', id: url, actor: byAccount.url, @@ -33,8 +35,6 @@ async function addActivityData ( object, target } - - return activity } // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/send/send-announce.ts b/server/lib/activitypub/send/send-announce.ts index 3acf604cd..e685323e8 100644 --- a/server/lib/activitypub/send/send-announce.ts +++ b/server/lib/activitypub/send/send-announce.ts @@ -1,8 +1,9 @@ import { Transaction } from 'sequelize' import { ActivityAdd } from '../../../../shared/index' -import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity' -import { AccountInstance, VideoInstance } from '../../../models' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' +import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoChannelModel } from '../../../models/video/video-channel' import { getAnnounceActivityPubUrl } from '../url' import { broadcastToFollowers, @@ -17,7 +18,7 @@ import { import { addActivityData } from './send-add' import { createActivityData } from './send-create' -async function buildVideoAnnounceToFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function buildVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getAnnounceActivityPubUrl(video.url, byAccount) const videoChannel = video.VideoChannel @@ -25,18 +26,16 @@ async function buildVideoAnnounceToFollowers (byAccount: AccountInstance, video: const accountsToForwardView = await getAccountsInvolvedInVideo(video, t) const audience = getObjectFollowersAudience(accountsToForwardView) - const data = await announceActivityData(url, byAccount, announcedActivity, t, audience) - - return data + return announceActivityData(url, byAccount, announcedActivity, t, audience) } -async function sendVideoAnnounceToFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const data = await buildVideoAnnounceToFollowers(byAccount, video, t) return broadcastToFollowers(data, byAccount, [ byAccount ], t) } -async function sendVideoAnnounceToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendVideoAnnounceToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getAnnounceActivityPubUrl(video.url, byAccount) const videoChannel = video.VideoChannel @@ -49,24 +48,22 @@ async function sendVideoAnnounceToOrigin (byAccount: AccountInstance, video: Vid return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t) } -async function buildVideoChannelAnnounceToFollowers (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { +async function buildVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) { const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount) const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t) const accountsToForwardView = await getAccountsInvolvedInVideoChannel(videoChannel, t) const audience = getObjectFollowersAudience(accountsToForwardView) - const data = await announceActivityData(url, byAccount, announcedActivity, t, audience) - - return data + return announceActivityData(url, byAccount, announcedActivity, t, audience) } -async function sendVideoChannelAnnounceToFollowers (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { +async function sendVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) { const data = await buildVideoChannelAnnounceToFollowers(byAccount, videoChannel, t) return broadcastToFollowers(data, byAccount, [ byAccount ], t) } -async function sendVideoChannelAnnounceToOrigin (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { +async function sendVideoChannelAnnounceToOrigin (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) { const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount) const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t) @@ -79,16 +76,16 @@ async function sendVideoChannelAnnounceToOrigin (byAccount: AccountInstance, vid async function announceActivityData ( url: string, - byAccount: AccountInstance, + byAccount: AccountModel, object: ActivityCreate | ActivityAdd, t: Transaction, audience?: ActivityAudience -) { +): Promise { if (!audience) { audience = await getAudience(byAccount, t) } - const activity: ActivityAnnounce = { + return { type: 'Announce', to: audience.to, cc: audience.cc, @@ -96,8 +93,6 @@ async function announceActivityData ( actor: byAccount.url, object } - - return activity } // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index a34d3776c..9fbaa8196 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -1,8 +1,10 @@ import { Transaction } from 'sequelize' -import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity' -import { getServerAccount } from '../../../helpers/utils' -import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' -import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface' +import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' +import { getServerAccount } from '../../../helpers' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoAbuseModel } from '../../../models/video/video-abuse' +import { VideoChannelModel } from '../../../models/video/video-channel' import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' import { broadcastToFollowers, @@ -13,7 +15,7 @@ import { unicastTo } from './misc' -async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { +async function sendCreateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) { const byAccount = videoChannel.Account const videoChannelObject = videoChannel.toActivityPubObject() @@ -22,7 +24,7 @@ async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Tr return broadcastToFollowers(data, byAccount, [ byAccount ], t) } -async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) { +async function sendVideoAbuse (byAccount: AccountModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) { const url = getVideoAbuseActivityPubUrl(videoAbuse) const audience = { to: [ video.VideoChannel.Account.url ], cc: [] } @@ -31,7 +33,7 @@ async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbus return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendCreateViewToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoViewActivityPubUrl(byAccount, video) const viewActivity = createViewActivityData(byAccount, video) @@ -42,7 +44,7 @@ async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoI return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendCreateViewToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoViewActivityPubUrl(byAccount, video) const viewActivity = createViewActivityData(byAccount, video) @@ -56,7 +58,7 @@ async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException) } -async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendCreateDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoDislikeActivityPubUrl(byAccount, video) const dislikeActivity = createDislikeActivityData(byAccount, video) @@ -67,7 +69,7 @@ async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: Vid return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendCreateDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoDislikeActivityPubUrl(byAccount, video) const dislikeActivity = createDislikeActivityData(byAccount, video) @@ -79,12 +81,18 @@ async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, vi return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException) } -async function createActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction, audience?: ActivityAudience) { +async function createActivityData ( + url: string, + byAccount: AccountModel, + object: any, + t: Transaction, + audience?: ActivityAudience +): Promise { if (!audience) { audience = await getAudience(byAccount, t) } - const activity: ActivityCreate = { + return { type: 'Create', id: url, actor: byAccount.url, @@ -92,18 +100,14 @@ async function createActivityData (url: string, byAccount: AccountInstance, obje cc: audience.cc, object } - - return activity } -function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) { - const obj = { +function createDislikeActivityData (byAccount: AccountModel, video: VideoModel) { + return { type: 'Dislike', actor: byAccount.url, object: video.url } - - return obj } // --------------------------------------------------------------------------- @@ -121,12 +125,10 @@ export { // --------------------------------------------------------------------------- -function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) { - const obj = { +function createViewActivityData (byAccount: AccountModel, video: VideoModel) { + return { type: 'View', actor: byAccount.url, object: video.url } - - return obj } diff --git a/server/lib/activitypub/send/send-delete.ts b/server/lib/activitypub/send/send-delete.ts index 8193790b3..0a45ea10f 100644 --- a/server/lib/activitypub/send/send-delete.ts +++ b/server/lib/activitypub/send/send-delete.ts @@ -1,32 +1,35 @@ import { Transaction } from 'sequelize' -import { ActivityDelete } from '../../../../shared/models/activitypub/activity' -import { database as db } from '../../../initializers' -import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' +import { ActivityDelete } from '../../../../shared/models/activitypub' +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 { broadcastToFollowers } from './misc' -async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { +async function sendDeleteVideoChannel (videoChannel: VideoChannelModel, t: Transaction) { const byAccount = videoChannel.Account const data = deleteActivityData(videoChannel.url, byAccount) - const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) + const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t) accountsInvolved.push(byAccount) return broadcastToFollowers(data, byAccount, accountsInvolved, t) } -async function sendDeleteVideo (video: VideoInstance, t: Transaction) { +async function sendDeleteVideo (video: VideoModel, t: Transaction) { const byAccount = video.VideoChannel.Account const data = deleteActivityData(video.url, byAccount) - const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id, t) + const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t) accountsInvolved.push(byAccount) return broadcastToFollowers(data, byAccount, accountsInvolved, t) } -async function sendDeleteAccount (account: AccountInstance, t: Transaction) { +async function sendDeleteAccount (account: AccountModel, t: Transaction) { const data = deleteActivityData(account.url, account) return broadcastToFollowers(data, account, [ account ], t) @@ -42,12 +45,10 @@ export { // --------------------------------------------------------------------------- -function deleteActivityData (url: string, byAccount: AccountInstance) { - const activity: ActivityDelete = { +function deleteActivityData (url: string, byAccount: AccountModel): ActivityDelete { + return { type: 'Delete', id: url, actor: byAccount.url } - - return activity } diff --git a/server/lib/activitypub/send/send-follow.ts b/server/lib/activitypub/send/send-follow.ts index 8fba1b6b5..51735ddfd 100644 --- a/server/lib/activitypub/send/send-follow.ts +++ b/server/lib/activitypub/send/send-follow.ts @@ -1,11 +1,11 @@ import { Transaction } from 'sequelize' -import { ActivityFollow } from '../../../../shared/models/activitypub/activity' -import { AccountInstance } from '../../../models' -import { AccountFollowInstance } from '../../../models/account/account-follow-interface' +import { ActivityFollow } from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' import { getAccountFollowActivityPubUrl } from '../url' import { unicastTo } from './misc' -function sendFollow (accountFollow: AccountFollowInstance, t: Transaction) { +function sendFollow (accountFollow: AccountFollowModel, t: Transaction) { const me = accountFollow.AccountFollower const following = accountFollow.AccountFollowing @@ -15,15 +15,13 @@ function sendFollow (accountFollow: AccountFollowInstance, t: Transaction) { return unicastTo(data, me, following.inboxUrl, t) } -function followActivityData (url: string, byAccount: AccountInstance, targetAccount: AccountInstance) { - const activity: ActivityFollow = { +function followActivityData (url: string, byAccount: AccountModel, targetAccount: AccountModel): ActivityFollow { + return { type: 'Follow', id: url, actor: byAccount.url, object: targetAccount.url } - - return activity } // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/send/send-like.ts b/server/lib/activitypub/send/send-like.ts index 0c464b2d3..1a35d0db0 100644 --- a/server/lib/activitypub/send/send-like.ts +++ b/server/lib/activitypub/send/send-like.ts @@ -1,6 +1,7 @@ import { Transaction } from 'sequelize' -import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub/activity' -import { AccountInstance, VideoInstance } from '../../../models' +import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' import { getVideoLikeActivityPubUrl } from '../url' import { broadcastToFollowers, @@ -11,7 +12,7 @@ import { unicastTo } from './misc' -async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoLikeActivityPubUrl(byAccount, video) const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t) @@ -21,7 +22,7 @@ async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstanc return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const url = getVideoLikeActivityPubUrl(byAccount, video) const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t) @@ -34,16 +35,16 @@ async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: Vide async function likeActivityData ( url: string, - byAccount: AccountInstance, - video: VideoInstance, + byAccount: AccountModel, + video: VideoModel, t: Transaction, audience?: ActivityAudience -) { +): Promise { if (!audience) { audience = await getAudience(byAccount, t) } - const activity: ActivityLike = { + return { type: 'Like', id: url, actor: byAccount.url, @@ -51,8 +52,6 @@ async function likeActivityData ( cc: audience.cc, object: video.url } - - return activity } // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts index 015f02b35..699f920f0 100644 --- a/server/lib/activitypub/send/send-undo.ts +++ b/server/lib/activitypub/send/send-undo.ts @@ -5,10 +5,10 @@ import { ActivityFollow, ActivityLike, ActivityUndo -} from '../../../../shared/models/activitypub/activity' -import { AccountInstance } from '../../../models' -import { AccountFollowInstance } from '../../../models/account/account-follow-interface' -import { VideoInstance } from '../../../models/video/video-interface' +} from '../../../../shared/models/activitypub' +import { AccountModel } from '../../../models/account/account' +import { AccountFollowModel } from '../../../models/account/account-follow' +import { VideoModel } from '../../../models/video/video' import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' import { broadcastToFollowers, @@ -22,7 +22,7 @@ import { createActivityData, createDislikeActivityData } from './send-create' import { followActivityData } from './send-follow' import { likeActivityData } from './send-like' -async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transaction) { +async function sendUndoFollow (accountFollow: AccountFollowModel, t: Transaction) { const me = accountFollow.AccountFollower const following = accountFollow.AccountFollowing @@ -35,7 +35,7 @@ async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transact return unicastTo(data, me, following.inboxUrl, t) } -async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendUndoLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) const undoUrl = getUndoActivityPubUrl(likeUrl) @@ -47,7 +47,7 @@ async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoIns return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendUndoLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) const undoUrl = getUndoActivityPubUrl(likeUrl) @@ -60,7 +60,7 @@ async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException) } -async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendUndoDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) { const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video) const undoUrl = getUndoActivityPubUrl(dislikeUrl) @@ -74,7 +74,7 @@ async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: Video return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) } -async function sendUndoDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { +async function sendUndoDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) { const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video) const undoUrl = getUndoActivityPubUrl(dislikeUrl) @@ -103,16 +103,16 @@ export { async function undoActivityData ( url: string, - byAccount: AccountInstance, + byAccount: AccountModel, object: ActivityFollow | ActivityLike | ActivityCreate, t: Transaction, audience?: ActivityAudience -) { +): Promise { if (!audience) { audience = await getAudience(byAccount, t) } - const activity: ActivityUndo = { + return { type: 'Undo', id: url, actor: byAccount.url, @@ -120,6 +120,4 @@ async function undoActivityData ( cc: audience.cc, object } - - return activity } diff --git a/server/lib/activitypub/send/send-update.ts b/server/lib/activitypub/send/send-update.ts index 59524e523..9baf13a87 100644 --- a/server/lib/activitypub/send/send-update.ts +++ b/server/lib/activitypub/send/send-update.ts @@ -1,31 +1,34 @@ import { Transaction } from 'sequelize' -import { ActivityUpdate } from '../../../../shared/models/activitypub/activity' -import { database as db } from '../../../initializers' -import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' +import { ActivityUpdate } from '../../../../shared/models/activitypub' +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 { getUpdateActivityPubUrl } from '../url' import { broadcastToFollowers, getAudience } from './misc' -async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { +async function sendUpdateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) { const byAccount = videoChannel.Account const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString()) const videoChannelObject = videoChannel.toActivityPubObject() const data = await updateActivityData(url, byAccount, videoChannelObject, t) - const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) + const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t) accountsInvolved.push(byAccount) return broadcastToFollowers(data, byAccount, accountsInvolved, t) } -async function sendUpdateVideo (video: VideoInstance, t: Transaction) { +async function sendUpdateVideo (video: VideoModel, t: Transaction) { const byAccount = video.VideoChannel.Account const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString()) const videoObject = video.toActivityPubObject() const data = await updateActivityData(url, byAccount, videoObject, t) - const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id, t) + const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t) accountsInvolved.push(byAccount) return broadcastToFollowers(data, byAccount, accountsInvolved, t) @@ -40,9 +43,9 @@ export { // --------------------------------------------------------------------------- -async function updateActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction) { +async function updateActivityData (url: string, byAccount: AccountModel, object: any, t: Transaction): Promise { const { to, cc } = await getAudience(byAccount, t) - const activity: ActivityUpdate = { + return { type: 'Update', id: url, actor: byAccount.url, @@ -50,6 +53,4 @@ async function updateActivityData (url: string, byAccount: AccountInstance, obje cc, object } - - return activity } diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts index e14b0f50c..5bec61c05 100644 --- a/server/lib/activitypub/share.ts +++ b/server/lib/activitypub/share.ts @@ -1,14 +1,15 @@ import { Transaction } from 'sequelize' -import { getServerAccount } from '../../helpers/utils' -import { database as db } from '../../initializers' -import { VideoChannelInstance } from '../../models/index' -import { VideoInstance } from '../../models/video/video-interface' -import { sendVideoAnnounceToFollowers, sendVideoChannelAnnounceToFollowers } from './send/send-announce' +import { getServerAccount } from '../../helpers' +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 { sendVideoAnnounceToFollowers, sendVideoChannelAnnounceToFollowers } from './send' -async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t: Transaction) { +async function shareVideoChannelByServer (videoChannel: VideoChannelModel, t: Transaction) { const serverAccount = await getServerAccount() - await db.VideoChannelShare.create({ + await VideoChannelShareModel.create({ accountId: serverAccount.id, videoChannelId: videoChannel.id }, { transaction: t }) @@ -16,10 +17,10 @@ async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t: return sendVideoChannelAnnounceToFollowers(serverAccount, videoChannel, t) } -async function shareVideoByServer (video: VideoInstance, t: Transaction) { +async function shareVideoByServer (video: VideoModel, t: Transaction) { const serverAccount = await getServerAccount() - await db.VideoShare.create({ + await VideoShareModel.create({ accountId: serverAccount.id, videoId: video.id }, { transaction: t }) diff --git a/server/lib/activitypub/url.ts b/server/lib/activitypub/url.ts index 6475c4218..00b4e8852 100644 --- a/server/lib/activitypub/url.ts +++ b/server/lib/activitypub/url.ts @@ -1,15 +1,15 @@ -import { CONFIG } from '../../initializers/constants' -import { VideoInstance } from '../../models/video/video-interface' -import { VideoChannelInstance } from '../../models/video/video-channel-interface' -import { VideoAbuseInstance } from '../../models/video/video-abuse-interface' -import { AccountFollowInstance } from '../../models/account/account-follow-interface' -import { AccountInstance } from '../../models/account/account-interface' +import { CONFIG } from '../../initializers' +import { AccountModel } from '../../models/account/account' +import { AccountFollowModel } from '../../models/account/account-follow' +import { VideoModel } from '../../models/video/video' +import { VideoAbuseModel } from '../../models/video/video-abuse' +import { VideoChannelModel } from '../../models/video/video-channel' -function getVideoActivityPubUrl (video: VideoInstance) { +function getVideoActivityPubUrl (video: VideoModel) { return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid } -function getVideoChannelActivityPubUrl (videoChannel: VideoChannelInstance) { +function getVideoChannelActivityPubUrl (videoChannel: VideoChannelModel) { return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid } @@ -17,37 +17,37 @@ function getAccountActivityPubUrl (accountName: string) { return CONFIG.WEBSERVER.URL + '/account/' + accountName } -function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseInstance) { +function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseModel) { return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id } -function getVideoViewActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { +function getVideoViewActivityPubUrl (byAccount: AccountModel, video: VideoModel) { return video.url + '/views/' + byAccount.uuid + '/' + new Date().toISOString() } -function getVideoLikeActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { +function getVideoLikeActivityPubUrl (byAccount: AccountModel, video: VideoModel) { return byAccount.url + '/likes/' + video.id } -function getVideoDislikeActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { +function getVideoDislikeActivityPubUrl (byAccount: AccountModel, video: VideoModel) { return byAccount.url + '/dislikes/' + video.id } -function getAccountFollowActivityPubUrl (accountFollow: AccountFollowInstance) { +function getAccountFollowActivityPubUrl (accountFollow: AccountFollowModel) { const me = accountFollow.AccountFollower const following = accountFollow.AccountFollowing return me.url + '/follows/' + following.id } -function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowInstance) { +function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowModel) { const follower = accountFollow.AccountFollower const me = accountFollow.AccountFollowing return follower.url + '/accepts/follows/' + me.id } -function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountInstance) { +function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountModel) { return originalUrl + '/announces/' + byAccount.id } diff --git a/server/lib/activitypub/video-channels.ts b/server/lib/activitypub/video-channels.ts index 7339d79f9..c05a46f95 100644 --- a/server/lib/activitypub/video-channels.ts +++ b/server/lib/activitypub/video-channels.ts @@ -1,14 +1,13 @@ -import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object' -import { isVideoChannelObjectValid } from '../../helpers/custom-validators/activitypub/video-channels' -import { logger } from '../../helpers/logger' -import { doRequest } from '../../helpers/requests' -import { database as db } from '../../initializers' -import { ACTIVITY_PUB } from '../../initializers/constants' -import { AccountInstance } from '../../models/account/account-interface' +import { VideoChannelObject } from '../../../shared/models/activitypub/objects' +import { doRequest, logger } from '../../helpers' +import { isVideoChannelObjectValid } from '../../helpers/custom-validators/activitypub' +import { ACTIVITY_PUB } from '../../initializers' +import { AccountModel } from '../../models/account/account' +import { VideoChannelModel } from '../../models/video/video-channel' import { videoChannelActivityObjectToDBAttributes } from './process/misc' -async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) { - let videoChannel = await db.VideoChannel.loadByUrl(videoChannelUrl) +async function getOrCreateVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) { + let videoChannel = await VideoChannelModel.loadByUrl(videoChannelUrl) // We don't have this account in our database, fetch it on remote if (!videoChannel) { @@ -22,7 +21,7 @@ async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChan return videoChannel } -async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) { +async function fetchRemoteVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) { const options = { uri: videoChannelUrl, method: 'GET', @@ -48,7 +47,7 @@ async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChan } const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount) - const videoChannel = db.VideoChannel.build(videoChannelAttributes) + const videoChannel = new VideoChannelModel(videoChannelAttributes) videoChannel.Account = ownerAccount return videoChannel diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 6b82f12d5..14c07fec0 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -2,21 +2,22 @@ import { join } from 'path' import * as request from 'request' import { Transaction } from 'sequelize' import { ActivityIconObject } from '../../../shared/index' -import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' -import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers/constants' -import { AccountInstance } from '../../models/account/account-interface' -import { VideoInstance } from '../../models/video/video-interface' -import { sendLikeToOrigin } from './index' -import { sendCreateDislikeToOrigin, sendCreateDislikeToVideoFollowers } from './send/send-create' -import { sendLikeToVideoFollowers } from './send/send-like' +import { doRequest, doRequestAndSaveToFile } from '../../helpers' +import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers' +import { AccountModel } from '../../models/account/account' +import { VideoModel } from '../../models/video/video' import { + sendCreateDislikeToOrigin, + sendCreateDislikeToVideoFollowers, + sendLikeToOrigin, + sendLikeToVideoFollowers, sendUndoDislikeToOrigin, sendUndoDislikeToVideoFollowers, sendUndoLikeToOrigin, sendUndoLikeToVideoFollowers -} from './send/send-undo' +} from './send' -function fetchRemoteVideoPreview (video: VideoInstance) { +function fetchRemoteVideoPreview (video: VideoModel) { // FIXME: use url const host = video.VideoChannel.Account.Server.host const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) @@ -24,7 +25,7 @@ function fetchRemoteVideoPreview (video: VideoInstance) { return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) } -async function fetchRemoteVideoDescription (video: VideoInstance) { +async function fetchRemoteVideoDescription (video: VideoModel) { // FIXME: use url const host = video.VideoChannel.Account.Server.host const path = video.getDescriptionPath() @@ -37,7 +38,7 @@ async function fetchRemoteVideoDescription (video: VideoInstance) { return body.description ? body.description : '' } -function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { +function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) { const thumbnailName = video.getThumbnailName() const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) @@ -49,8 +50,8 @@ function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObjec } async function sendVideoRateChangeToFollowers ( - account: AccountInstance, - video: VideoInstance, + account: AccountModel, + video: VideoModel, likes: number, dislikes: number, t: Transaction @@ -69,8 +70,8 @@ async function sendVideoRateChangeToFollowers ( } async function sendVideoRateChangeToOrigin ( - account: AccountInstance, - video: VideoInstance, + account: AccountModel, + video: VideoModel, likes: number, dislikes: number, t: Transaction diff --git a/server/lib/cache/videos-preview-cache.ts b/server/lib/cache/videos-preview-cache.ts index 7f352f361..c5bda8dd8 100644 --- a/server/lib/cache/videos-preview-cache.ts +++ b/server/lib/cache/videos-preview-cache.ts @@ -1,11 +1,10 @@ import * as asyncLRU from 'async-lru' -import { join } from 'path' import { createWriteStream } from 'fs' - -import { database as db, CONFIG, CACHE } from '../../initializers' +import { join } from 'path' import { logger, unlinkPromise } from '../../helpers' -import { VideoInstance } from '../../models' -import { fetchRemoteVideoPreview } from '../activitypub/videos' +import { CACHE, CONFIG } from '../../initializers' +import { VideoModel } from '../../models/video/video' +import { fetchRemoteVideoPreview } from '../activitypub' class VideosPreviewCache { @@ -43,7 +42,7 @@ class VideosPreviewCache { } private async loadPreviews (key: string) { - const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(key) + const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key) if (!video) return undefined if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) @@ -53,7 +52,7 @@ class VideosPreviewCache { return res } - private saveRemotePreviewAndReturnPath (video: VideoInstance) { + private saveRemotePreviewAndReturnPath (video: VideoModel) { const req = fetchRemoteVideoPreview(video) return new Promise((res, rej) => { diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts index 49d4bf5c6..8040dde2a 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts @@ -1,5 +1,4 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' +import { doRequest, logger } from '../../../helpers' import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts index 9adceab84..638150202 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts @@ -1,7 +1,6 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' -import { ACTIVITY_PUB } from '../../../initializers/constants' -import { processActivities } from '../../activitypub/process/process' +import { doRequest, logger } from '../../../helpers' +import { ACTIVITY_PUB } from '../../../initializers' +import { processActivities } from '../../activitypub/process' import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts index fcc81eb16..76da5b724 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts @@ -1,8 +1,7 @@ import { JobCategory } from '../../../../shared' -import { buildSignedActivity } from '../../../helpers/activitypub' -import { logger } from '../../../helpers/logger' -import { ACTIVITY_PUB } from '../../../initializers/constants' -import { database as db } from '../../../initializers/database' +import { buildSignedActivity, logger } from '../../../helpers' +import { ACTIVITY_PUB } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' import { JobHandler, JobScheduler } from '../job-scheduler' import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' @@ -46,7 +45,7 @@ async function computeBody (payload: ActivityPubHttpPayload) { let body = payload.body if (payload.signatureAccountId) { - const accountSignature = await db.Account.load(payload.signatureAccountId) + const accountSignature = await AccountModel.load(payload.signatureAccountId) if (!accountSignature) throw new Error('Unknown signature account id.') body = await buildSignedActivity(accountSignature, payload.body) } diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts index 4c95197c4..f16cfcec3 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts @@ -1,5 +1,4 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' +import { doRequest, logger } from '../../../helpers' import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts index 62ce6927e..88fe8a4a3 100644 --- a/server/lib/jobs/job-scheduler.ts +++ b/server/lib/jobs/job-scheduler.ts @@ -2,8 +2,8 @@ import { AsyncQueue, forever, queue } from 'async' import * as Sequelize from 'sequelize' import { JobCategory } from '../../../shared' import { logger } from '../../helpers' -import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' -import { JobInstance } from '../../models' +import { JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' +import { JobModel } from '../../models/job/job' export interface JobHandler { process (data: object, jobId: number): Promise @@ -24,12 +24,12 @@ class JobScheduler { logger.info('Jobs scheduler %s activated.', this.jobCategory) - const jobsQueue = queue(this.processJob.bind(this)) + const jobsQueue = queue(this.processJob.bind(this)) // Finish processing jobs from a previous start const state = JOB_STATES.PROCESSING try { - const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) + const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory) this.enqueueJobs(jobsQueue, jobs) } catch (err) { @@ -45,7 +45,7 @@ class JobScheduler { const state = JOB_STATES.PENDING try { - const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) + const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory) this.enqueueJobs(jobsQueue, jobs) } catch (err) { @@ -70,14 +70,14 @@ class JobScheduler { const options = { transaction } - return db.Job.create(createQuery, options) + return JobModel.create(createQuery, options) } - private enqueueJobs (jobsQueue: AsyncQueue, jobs: JobInstance[]) { + private enqueueJobs (jobsQueue: AsyncQueue, jobs: JobModel[]) { jobs.forEach(job => jobsQueue.push(job)) } - private async processJob (job: JobInstance, callback: (err: Error) => void) { + private async processJob (job: JobModel, callback: (err: Error) => void) { const jobHandler = this.jobHandlers[job.handlerName] if (jobHandler === undefined) { const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id @@ -110,7 +110,7 @@ class JobScheduler { return callback(null) } - private async onJobError (jobHandler: JobHandler, job: JobInstance, err: Error) { + private async onJobError (jobHandler: JobHandler, job: JobModel, err: Error) { job.state = JOB_STATES.ERROR try { @@ -121,7 +121,7 @@ class JobScheduler { } } - private async onJobSuccess (jobHandler: JobHandler, job: JobInstance, jobResult: T) { + private async onJobSuccess (jobHandler: JobHandler, job: JobModel, jobResult: T) { job.state = JOB_STATES.SUCCESS try { diff --git a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts index c5efe8eeb..e5530a73c 100644 --- a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts @@ -1,14 +1,15 @@ import { JobCategory } from '../../../../shared' +import { VideoModel } from '../../../models/video/video' import { JobHandler, JobScheduler } from '../job-scheduler' + import * as videoFileOptimizer from './video-file-optimizer-handler' import * as videoFileTranscoder from './video-file-transcoder-handler' -import { VideoInstance } from '../../../models/video/video-interface' type TranscodingJobPayload = { videoUUID: string resolution?: number } -const jobHandlers: { [ handlerName: string ]: JobHandler } = { +const jobHandlers: { [ handlerName: string ]: JobHandler } = { videoFileOptimizer, videoFileTranscoder } diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts index e65ab3ee1..1786ce971 100644 --- a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts @@ -1,14 +1,14 @@ import * as Bluebird from 'bluebird' import { computeResolutionsToTranscode, logger } from '../../../helpers' -import { database as db } from '../../../initializers/database' -import { VideoInstance } from '../../../models' -import { sendAddVideo } from '../../activitypub/send/send-add' +import { sequelizeTypescript } from '../../../initializers' +import { VideoModel } from '../../../models/video/video' +import { shareVideoByServer } from '../../activitypub' +import { sendAddVideo } from '../../activitypub/send' import { JobScheduler } from '../job-scheduler' import { TranscodingJobPayload } from './transcoding-job-scheduler' -import { shareVideoByServer } from '../../activitypub/share' async function process (data: TranscodingJobPayload, jobId: number) { - const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) + const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) // No video, maybe deleted? if (!video) { logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) @@ -25,13 +25,13 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: JobScheduler) { +async function onSuccess (jobId: number, video: VideoModel, jobScheduler: JobScheduler) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) // Maybe the video changed in database, refresh it - const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) + const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) // Video does not exist anymore if (!videoDatabase) return undefined @@ -50,7 +50,7 @@ async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: Job if (resolutionsEnabled.length !== 0) { try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const tasks: Bluebird[] = [] for (const resolution of resolutionsEnabled) { diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts index 867580200..8957b4565 100644 --- a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts @@ -1,11 +1,10 @@ import { VideoResolution } from '../../../../shared' import { logger } from '../../../helpers' -import { database as db } from '../../../initializers/database' -import { VideoInstance } from '../../../models' -import { sendUpdateVideo } from '../../activitypub/send/send-update' +import { VideoModel } from '../../../models/video/video' +import { sendUpdateVideo } from '../../activitypub/send' async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { - const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) + const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) // No video, maybe deleted? if (!video) { logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) @@ -22,13 +21,13 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -async function onSuccess (jobId: number, video: VideoInstance) { +async function onSuccess (jobId: number, video: VideoModel) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) // Maybe the video changed in database, refresh it - const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) + const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) // Video does not exist anymore if (!videoDatabase) return undefined diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts index d91b00c55..dce71e83b 100644 --- a/server/lib/oauth-model.ts +++ b/server/lib/oauth-model.ts @@ -1,6 +1,7 @@ -import { OAuthClientInstance, UserInstance } from '../models' -import { database as db } from '../initializers/database' import { logger } from '../helpers' +import { UserModel } from '../models/account/user' +import { OAuthClientModel } from '../models/oauth/oauth-client' +import { OAuthTokenModel } from '../models/oauth/oauth-token' type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } @@ -9,25 +10,25 @@ type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpires function getAccessToken (bearerToken: string) { logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') - return db.OAuthToken.getByTokenAndPopulateUser(bearerToken) + return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) } function getClient (clientId: string, clientSecret: string) { logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') - return db.OAuthClient.getByIdAndSecret(clientId, clientSecret) + return OAuthClientModel.getByIdAndSecret(clientId, clientSecret) } function getRefreshToken (refreshToken: string) { logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') - return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) + return OAuthTokenModel.getByRefreshTokenAndPopulateClient(refreshToken) } async function getUser (username: string, password: string) { logger.debug('Getting User (username: ' + username + ', password: ******).') - const user = await db.User.getByUsername(username) + const user = await UserModel.getByUsername(username) if (!user) return null const passwordMatch = await user.isPasswordMatch(password) @@ -37,7 +38,7 @@ async function getUser (username: string, password: string) { } async function revokeToken (tokenInfo: TokenInfo) { - const token = await db.OAuthToken.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) + const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) if (token) token.destroy() /* @@ -53,7 +54,7 @@ async function revokeToken (tokenInfo: TokenInfo) { return expiredToken } -async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: UserInstance) { +async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) { logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') const tokenToCreate = { @@ -65,7 +66,7 @@ async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: U userId: user.id } - const tokenCreated = await db.OAuthToken.create(tokenToCreate) + const tokenCreated = await OAuthTokenModel.create(tokenToCreate) const tokenToReturn = Object.assign(tokenCreated, { client, user }) return tokenToReturn diff --git a/server/lib/user.ts b/server/lib/user.ts index 5653d8e65..c4722fae2 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -1,14 +1,13 @@ import * as Sequelize from 'sequelize' -import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto' -import { database as db } from '../initializers' -import { CONFIG } from '../initializers/constants' -import { UserInstance } from '../models' +import { createPrivateAndPublicKeys, logger } from '../helpers' +import { CONFIG, sequelizeTypescript } from '../initializers' +import { AccountModel } from '../models/account/account' +import { UserModel } from '../models/account/user' +import { getAccountActivityPubUrl } from './activitypub' import { createVideoChannel } from './video-channel' -import { logger } from '../helpers/logger' -import { getAccountActivityPubUrl } from './activitypub/url' -async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { - const { account, videoChannel } = await db.sequelize.transaction(async t => { +async function createUserAccountAndChannel (user: UserModel, validateUser = true) { + const { account, videoChannel } = await sequelizeTypescript.transaction(async t => { const userOptions = { transaction: t, validate: validateUser @@ -38,7 +37,7 @@ async function createUserAccountAndChannel (user: UserInstance, validateUser = t async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { const url = getAccountActivityPubUrl(name) - const accountInstance = db.Account.build({ + const accountInstance = new AccountModel({ name, url, publicKey: null, diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index beb01da9b..97924aa9e 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts @@ -1,10 +1,10 @@ import * as Sequelize from 'sequelize' import { VideoChannelCreate } from '../../shared/models' -import { database as db } from '../initializers' -import { AccountInstance } from '../models' -import { getVideoChannelActivityPubUrl } from './activitypub/url' +import { AccountModel } from '../models/account/account' +import { VideoChannelModel } from '../models/video/video-channel' +import { getVideoChannelActivityPubUrl } from './activitypub' -async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) { +async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountModel, t: Sequelize.Transaction) { const videoChannelData = { name: videoChannelInfo.name, description: videoChannelInfo.description, @@ -12,7 +12,7 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account accountId: account.id } - const videoChannel = db.VideoChannel.build(videoChannelData) + const videoChannel = VideoChannelModel.build(videoChannelData) videoChannel.set('url', getVideoChannelActivityPubUrl(videoChannel)) const options = { transaction: t } -- cgit v1.2.3