aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/activitypub/account.ts28
-rw-r--r--server/lib/activitypub/fetch.ts6
-rw-r--r--server/lib/activitypub/process/misc.ts38
-rw-r--r--server/lib/activitypub/process/process-accept.ts14
-rw-r--r--server/lib/activitypub/process/process-add.ts42
-rw-r--r--server/lib/activitypub/process/process-announce.ts35
-rw-r--r--server/lib/activitypub/process/process-create.ts48
-rw-r--r--server/lib/activitypub/process/process-delete.ts35
-rw-r--r--server/lib/activitypub/process/process-follow.ts22
-rw-r--r--server/lib/activitypub/process/process-like.ts20
-rw-r--r--server/lib/activitypub/process/process-undo.ts37
-rw-r--r--server/lib/activitypub/process/process-update.ts39
-rw-r--r--server/lib/activitypub/process/process.ts10
-rw-r--r--server/lib/activitypub/send/misc.ts56
-rw-r--r--server/lib/activitypub/send/send-accept.ts12
-rw-r--r--server/lib/activitypub/send/send-add.ts20
-rw-r--r--server/lib/activitypub/send/send-announce.ts35
-rw-r--r--server/lib/activitypub/send/send-create.ts46
-rw-r--r--server/lib/activitypub/send/send-delete.ts25
-rw-r--r--server/lib/activitypub/send/send-follow.ts14
-rw-r--r--server/lib/activitypub/send/send-like.ts19
-rw-r--r--server/lib/activitypub/send/send-undo.ts26
-rw-r--r--server/lib/activitypub/send/send-update.ts23
-rw-r--r--server/lib/activitypub/share.ts19
-rw-r--r--server/lib/activitypub/url.ts30
-rw-r--r--server/lib/activitypub/video-channels.ts21
-rw-r--r--server/lib/activitypub/videos.ts31
-rw-r--r--server/lib/cache/videos-preview-cache.ts13
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts3
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts7
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts9
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts3
-rw-r--r--server/lib/jobs/job-scheduler.ts20
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts5
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts16
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts11
-rw-r--r--server/lib/oauth-model.ts19
-rw-r--r--server/lib/user.ts17
-rw-r--r--server/lib/video-channel.ts10
39 files changed, 438 insertions, 446 deletions
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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import * as url from 'url'
3import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor'
4import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account'
5import { retryTransactionWrapper } from '../../helpers/database-utils'
6import { logger } from '../../helpers/logger'
7import { doRequest } from '../../helpers/requests'
8import { ACTIVITY_PUB } from '../../initializers/constants'
9import { database as db } from '../../initializers/database'
10import { AccountInstance } from '../../models/account/account-interface'
11import { Transaction } from 'sequelize' 2import { Transaction } from 'sequelize'
3import * as url from 'url'
4import { ActivityPubActor } from '../../../shared/models/activitypub'
5import { doRequest, logger, retryTransactionWrapper } from '../../helpers'
6import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub'
7import { ACTIVITY_PUB, sequelizeTypescript } from '../../initializers'
8import { AccountModel } from '../../models/account/account'
9import { ServerModel } from '../../models/server/server'
12 10
13async function getOrCreateAccountAndServer (accountUrl: string) { 11async function getOrCreateAccountAndServer (accountUrl: string) {
14 let account = await db.Account.loadByUrl(accountUrl) 12 let account = await AccountModel.loadByUrl(accountUrl)
15 13
16 // We don't have this account in our database, fetch it on remote 14 // We don't have this account in our database, fetch it on remote
17 if (!account) { 15 if (!account) {
@@ -28,11 +26,11 @@ async function getOrCreateAccountAndServer (accountUrl: string) {
28 return account 26 return account
29} 27}
30 28
31function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird<AccountInstance> | Promise<AccountInstance> { 29function saveAccountAndServerIfNotExist (account: AccountModel, t?: Transaction): Bluebird<AccountModel> | Promise<AccountModel> {
32 if (t !== undefined) { 30 if (t !== undefined) {
33 return save(t) 31 return save(t)
34 } else { 32 } else {
35 return db.sequelize.transaction(t => { 33 return sequelizeTypescript.transaction(t => {
36 return save(t) 34 return save(t)
37 }) 35 })
38 } 36 }
@@ -49,7 +47,7 @@ function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transacti
49 }, 47 },
50 transaction: t 48 transaction: t
51 } 49 }
52 const [ server ] = await db.Server.findOrCreate(serverOptions) 50 const [ server ] = await ServerModel.findOrCreate(serverOptions)
53 51
54 // Save our new account in database 52 // Save our new account in database
55 account.set('serverId', server.id) 53 account.set('serverId', server.id)
@@ -87,7 +85,7 @@ async function fetchRemoteAccount (accountUrl: string) {
87 const followersCount = await fetchAccountCount(accountJSON.followers) 85 const followersCount = await fetchAccountCount(accountJSON.followers)
88 const followingCount = await fetchAccountCount(accountJSON.following) 86 const followingCount = await fetchAccountCount(accountJSON.following)
89 87
90 const account = db.Account.build({ 88 return new AccountModel({
91 uuid: accountJSON.uuid, 89 uuid: accountJSON.uuid,
92 name: accountJSON.preferredUsername, 90 name: accountJSON.preferredUsername,
93 url: accountJSON.url, 91 url: accountJSON.url,
@@ -101,8 +99,6 @@ async function fetchRemoteAccount (accountUrl: string) {
101 followersUrl: accountJSON.followers, 99 followersUrl: accountJSON.followers,
102 followingUrl: accountJSON.following 100 followingUrl: accountJSON.following
103 }) 101 })
104
105 return account
106} 102}
107 103
108export { 104export {
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { AccountInstance } from '../../models/account/account-interface' 2import { AccountModel } from '../../models/account/account'
3import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' 3import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler'
4 4
5async function addFetchOutboxJob (account: AccountInstance, t: Transaction) { 5async function addFetchOutboxJob (account: AccountModel, t: Transaction) {
6 const jobPayload: ActivityPubHttpPayload = { 6 const jobPayload: ActivityPubHttpPayload = {
7 uris: [ account.outboxUrl ] 7 uris: [ account.outboxUrl ]
8 } 8 }
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 @@
1import * as magnetUtil from 'magnet-uri' 1import * as magnetUtil from 'magnet-uri'
2import { VideoTorrentObject } from '../../../../shared' 2import { VideoTorrentObject } from '../../../../shared'
3import { VideoChannelObject } from '../../../../shared/models/activitypub/objects/video-channel-object' 3import { VideoChannelObject } from '../../../../shared/models/activitypub/objects'
4import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' 4import { VideoPrivacy } from '../../../../shared/models/videos'
5import { doRequest } from '../../../helpers'
5import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' 6import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
6import { doRequest } from '../../../helpers/requests' 7import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
7import { database as db } from '../../../initializers' 8import { AccountModel } from '../../../models/account/account'
8import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers/constants' 9import { VideoModel } from '../../../models/video/video'
9import { AccountInstance } from '../../../models/account/account-interface' 10import { VideoChannelModel } from '../../../models/video/video-channel'
10import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 11import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
11import { VideoFileAttributes } from '../../../models/video/video-file-interface' 12import { VideoShareModel } from '../../../models/video/video-share'
12import { VideoAttributes, VideoInstance } from '../../../models/video/video-interface'
13import { getOrCreateAccountAndServer } from '../account' 13import { getOrCreateAccountAndServer } from '../account'
14 14
15function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountInstance) { 15function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChannelObject, account: AccountModel) {
16 return { 16 return {
17 name: videoChannelObject.name, 17 name: videoChannelObject.name,
18 description: videoChannelObject.content, 18 description: videoChannelObject.content,
@@ -26,7 +26,7 @@ function videoChannelActivityObjectToDBAttributes (videoChannelObject: VideoChan
26} 26}
27 27
28async function videoActivityObjectToDBAttributes ( 28async function videoActivityObjectToDBAttributes (
29 videoChannel: VideoChannelInstance, 29 videoChannel: VideoChannelModel,
30 videoObject: VideoTorrentObject, 30 videoObject: VideoTorrentObject,
31 to: string[] = [], 31 to: string[] = [],
32 cc: string[] = [] 32 cc: string[] = []
@@ -56,7 +56,7 @@ async function videoActivityObjectToDBAttributes (
56 description = videoObject.content 56 description = videoObject.content
57 } 57 }
58 58
59 const videoData: VideoAttributes = { 59 return {
60 name: videoObject.name, 60 name: videoObject.name,
61 uuid: videoObject.uuid, 61 uuid: videoObject.uuid,
62 url: videoObject.id, 62 url: videoObject.id,
@@ -76,11 +76,9 @@ async function videoActivityObjectToDBAttributes (
76 remote: true, 76 remote: true,
77 privacy 77 privacy
78 } 78 }
79
80 return videoData
81} 79}
82 80
83function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { 81function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
84 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT) 82 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
85 const fileUrls = videoObject.url.filter(u => { 83 const fileUrls = videoObject.url.filter(u => {
86 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/') 84 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
@@ -90,7 +88,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO
90 throw new Error('Cannot find video files for ' + videoCreated.url) 88 throw new Error('Cannot find video files for ' + videoCreated.url)
91 } 89 }
92 90
93 const attributes: VideoFileAttributes[] = [] 91 const attributes = []
94 for (const fileUrl of fileUrls) { 92 for (const fileUrl of fileUrls) {
95 // Fetch associated magnet uri 93 // Fetch associated magnet uri
96 const magnet = videoObject.url.find(u => { 94 const magnet = videoObject.url.find(u => {
@@ -115,7 +113,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoO
115 return attributes 113 return attributes
116} 114}
117 115
118async function addVideoShares (instance: VideoInstance, shares: string[]) { 116async function addVideoShares (instance: VideoModel, shares: string[]) {
119 for (const share of shares) { 117 for (const share of shares) {
120 // Fetch url 118 // Fetch url
121 const json = await doRequest({ 119 const json = await doRequest({
@@ -132,14 +130,14 @@ async function addVideoShares (instance: VideoInstance, shares: string[]) {
132 videoId: instance.id 130 videoId: instance.id
133 } 131 }
134 132
135 await db.VideoShare.findOrCreate({ 133 await VideoShareModel.findOrCreate({
136 where: entry, 134 where: entry,
137 defaults: entry 135 defaults: entry
138 }) 136 })
139 } 137 }
140} 138}
141 139
142async function addVideoChannelShares (instance: VideoChannelInstance, shares: string[]) { 140async function addVideoChannelShares (instance: VideoChannelModel, shares: string[]) {
143 for (const share of shares) { 141 for (const share of shares) {
144 // Fetch url 142 // Fetch url
145 const json = await doRequest({ 143 const json = await doRequest({
@@ -156,7 +154,7 @@ async function addVideoChannelShares (instance: VideoChannelInstance, shares: st
156 videoChannelId: instance.id 154 videoChannelId: instance.id
157 } 155 }
158 156
159 await db.VideoChannelShare.findOrCreate({ 157 await VideoChannelShareModel.findOrCreate({
160 where: entry, 158 where: entry,
161 defaults: entry 159 defaults: entry
162 }) 160 })
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 @@
1import { ActivityAccept } from '../../../../shared/models/activitypub/activity' 1import { ActivityAccept } from '../../../../shared/models/activitypub'
2import { database as db } from '../../../initializers' 2import { AccountModel } from '../../../models/account/account'
3import { AccountInstance } from '../../../models/account/account-interface' 3import { AccountFollowModel } from '../../../models/account/account-follow'
4import { addFetchOutboxJob } from '../fetch' 4import { addFetchOutboxJob } from '../fetch'
5 5
6async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountInstance) { 6async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountModel) {
7 if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.') 7 if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.')
8 8
9 const targetAccount = await db.Account.loadByUrl(activity.actor) 9 const targetAccount = await AccountModel.loadByUrl(activity.actor)
10 10
11 return processAccept(inboxAccount, targetAccount) 11 return processAccept(inboxAccount, targetAccount)
12} 12}
@@ -19,8 +19,8 @@ export {
19 19
20// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
21 21
22async function processAccept (account: AccountInstance, targetAccount: AccountInstance) { 22async function processAccept (account: AccountModel, targetAccount: AccountModel) {
23 const follow = await db.AccountFollow.loadByAccountAndTarget(account.id, targetAccount.id) 23 const follow = await AccountFollowModel.loadByAccountAndTarget(account.id, targetAccount.id)
24 if (!follow) throw new Error('Cannot find associated follow.') 24 if (!follow) throw new Error('Cannot find associated follow.')
25 25
26 follow.set('state', 'accepted') 26 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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { VideoTorrentObject } from '../../../../shared' 2import { VideoTorrentObject } from '../../../../shared'
3import { ActivityAdd } from '../../../../shared/models/activitypub/activity' 3import { ActivityAdd } from '../../../../shared/models/activitypub'
4import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' 4import { VideoRateType } from '../../../../shared/models/videos'
5import { retryTransactionWrapper } from '../../../helpers/database-utils' 5import { logger, retryTransactionWrapper } from '../../../helpers'
6import { logger } from '../../../helpers/logger' 6import { sequelizeTypescript } from '../../../initializers'
7import { database as db } from '../../../initializers' 7import { AccountModel } from '../../../models/account/account'
8import { AccountInstance } from '../../../models/account/account-interface' 8import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 9import { TagModel } from '../../../models/video/tag'
10import { VideoInstance } from '../../../models/video/video-interface' 10import { VideoModel } from '../../../models/video/video'
11import { VideoChannelModel } from '../../../models/video/video-channel'
12import { VideoFileModel } from '../../../models/video/video-file'
11import { getOrCreateAccountAndServer } from '../account' 13import { getOrCreateAccountAndServer } from '../account'
12import { getOrCreateVideoChannel } from '../video-channels' 14import { getOrCreateVideoChannel } from '../video-channels'
13import { generateThumbnailFromUrl } from '../videos' 15import { generateThumbnailFromUrl } from '../videos'
@@ -37,9 +39,9 @@ export {
37 39
38// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
39 41
40async function processAddVideo (account: AccountInstance, 42async function processAddVideo (account: AccountModel,
41 activity: ActivityAdd, 43 activity: ActivityAdd,
42 videoChannel: VideoChannelInstance, 44 videoChannel: VideoChannelModel,
43 videoToCreateData: VideoTorrentObject) { 45 videoToCreateData: VideoTorrentObject) {
44 const options = { 46 const options = {
45 arguments: [ account, activity, videoChannel, videoToCreateData ], 47 arguments: [ account, activity, videoChannel, videoToCreateData ],
@@ -64,24 +66,24 @@ async function processAddVideo (account: AccountInstance,
64 return video 66 return video
65} 67}
66 68
67function addRemoteVideo (account: AccountInstance, 69function addRemoteVideo (account: AccountModel,
68 activity: ActivityAdd, 70 activity: ActivityAdd,
69 videoChannel: VideoChannelInstance, 71 videoChannel: VideoChannelModel,
70 videoToCreateData: VideoTorrentObject) { 72 videoToCreateData: VideoTorrentObject) {
71 logger.debug('Adding remote video %s.', videoToCreateData.id) 73 logger.debug('Adding remote video %s.', videoToCreateData.id)
72 74
73 return db.sequelize.transaction(async t => { 75 return sequelizeTypescript.transaction(async t => {
74 const sequelizeOptions = { 76 const sequelizeOptions = {
75 transaction: t 77 transaction: t
76 } 78 }
77 79
78 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') 80 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
79 81
80 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) 82 const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
81 if (videoFromDatabase) return videoFromDatabase 83 if (videoFromDatabase) return videoFromDatabase
82 84
83 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) 85 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
84 const video = db.Video.build(videoData) 86 const video = VideoModel.build(videoData)
85 87
86 // Don't block on request 88 // Don't block on request
87 generateThumbnailFromUrl(video, videoToCreateData.icon) 89 generateThumbnailFromUrl(video, videoToCreateData.icon)
@@ -94,12 +96,12 @@ function addRemoteVideo (account: AccountInstance,
94 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) 96 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
95 } 97 }
96 98
97 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f, { transaction: t })) 99 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
98 await Promise.all(tasks) 100 await Promise.all(tasks)
99 101
100 const tags = videoToCreateData.tag.map(t => t.name) 102 const tags = videoToCreateData.tag.map(t => t.name)
101 const tagInstances = await db.Tag.findOrCreateTags(tags, t) 103 const tagInstances = await TagModel.findOrCreateTags(tags, t)
102 await videoCreated.setTags(tagInstances, sequelizeOptions) 104 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
103 105
104 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) 106 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
105 107
@@ -107,13 +109,13 @@ function addRemoteVideo (account: AccountInstance,
107 }) 109 })
108} 110}
109 111
110async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { 112async function createRates (accountUrls: string[], video: VideoModel, rate: VideoRateType) {
111 let rateCounts = 0 113 let rateCounts = 0
112 const tasks: Bluebird<any>[] = [] 114 const tasks: Bluebird<any>[] = []
113 115
114 for (const accountUrl of accountUrls) { 116 for (const accountUrl of accountUrls) {
115 const account = await getOrCreateAccountAndServer(accountUrl) 117 const account = await getOrCreateAccountAndServer(accountUrl)
116 const p = db.AccountVideoRate 118 const p = AccountVideoRateModel
117 .create({ 119 .create({
118 videoId: video.id, 120 videoId: video.id,
119 accountId: account.id, 121 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 @@
1import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity' 1import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils' 2import { logger, retryTransactionWrapper } from '../../../helpers'
3import { logger } from '../../../helpers/logger' 3import { sequelizeTypescript } from '../../../initializers'
4import { database as db } from '../../../initializers/index' 4import { AccountModel } from '../../../models/account/account'
5import { AccountInstance } from '../../../models/account/account-interface' 5import { VideoModel } from '../../../models/video/video'
6import { VideoInstance } from '../../../models/index' 6import { VideoChannelModel } from '../../../models/video/video-channel'
7import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 7import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
8import { VideoShareModel } from '../../../models/video/video-share'
8import { getOrCreateAccountAndServer } from '../account' 9import { getOrCreateAccountAndServer } from '../account'
9import { forwardActivity } from '../send/misc' 10import { forwardActivity } from '../send/misc'
10import { processAddActivity } from './process-add' 11import { processAddActivity } from './process-add'
@@ -36,7 +37,7 @@ export {
36 37
37// --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
38 39
39function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { 40function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
40 const options = { 41 const options = {
41 arguments: [ accountAnnouncer, activity ], 42 arguments: [ accountAnnouncer, activity ],
42 errorMessage: 'Cannot share the video channel with many retries.' 43 errorMessage: 'Cannot share the video channel with many retries.'
@@ -45,18 +46,18 @@ function processVideoChannelShare (accountAnnouncer: AccountInstance, activity:
45 return retryTransactionWrapper(shareVideoChannel, options) 46 return retryTransactionWrapper(shareVideoChannel, options)
46} 47}
47 48
48async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { 49async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
49 const announcedActivity = activity.object as ActivityCreate 50 const announcedActivity = activity.object as ActivityCreate
50 51
51 return db.sequelize.transaction(async t => { 52 return sequelizeTypescript.transaction(async t => {
52 // Add share entry 53 // Add share entry
53 const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity) 54 const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
54 const share = { 55 const share = {
55 accountId: accountAnnouncer.id, 56 accountId: accountAnnouncer.id,
56 videoChannelId: videoChannel.id 57 videoChannelId: videoChannel.id
57 } 58 }
58 59
59 const [ , created ] = await db.VideoChannelShare.findOrCreate({ 60 const [ , created ] = await VideoChannelShareModel.findOrCreate({
60 where: share, 61 where: share,
61 defaults: share, 62 defaults: share,
62 transaction: t 63 transaction: t
@@ -72,7 +73,7 @@ async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: A
72 }) 73 })
73} 74}
74 75
75function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { 76function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
76 const options = { 77 const options = {
77 arguments: [ accountAnnouncer, activity ], 78 arguments: [ accountAnnouncer, activity ],
78 errorMessage: 'Cannot share the video with many retries.' 79 errorMessage: 'Cannot share the video with many retries.'
@@ -81,19 +82,19 @@ function processVideoShare (accountAnnouncer: AccountInstance, activity: Activit
81 return retryTransactionWrapper(shareVideo, options) 82 return retryTransactionWrapper(shareVideo, options)
82} 83}
83 84
84function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { 85function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
85 const announcedActivity = activity.object as ActivityAdd 86 const announcedActivity = activity.object as ActivityAdd
86 87
87 return db.sequelize.transaction(async t => { 88 return sequelizeTypescript.transaction(async t => {
88 // Add share entry 89 // Add share entry
89 const video: VideoInstance = await processAddActivity(announcedActivity) 90 const video: VideoModel = await processAddActivity(announcedActivity)
90 91
91 const share = { 92 const share = {
92 accountId: accountAnnouncer.id, 93 accountId: accountAnnouncer.id,
93 videoId: video.id 94 videoId: video.id
94 } 95 }
95 96
96 const [ , created ] = await db.VideoShare.findOrCreate({ 97 const [ , created ] = await VideoShareModel.findOrCreate({
97 where: share, 98 where: share,
98 defaults: share, 99 defaults: share,
99 transaction: t 100 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 @@
1import { ActivityCreate, VideoChannelObject } from '../../../../shared' 1import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' 2import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
4import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object'
5import { logger, retryTransactionWrapper } from '../../../helpers' 3import { logger, retryTransactionWrapper } from '../../../helpers'
6import { database as db } from '../../../initializers' 4import { sequelizeTypescript } from '../../../initializers'
7import { AccountInstance } from '../../../models/account/account-interface' 5import { AccountModel } from '../../../models/account/account'
6import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7import { VideoModel } from '../../../models/video/video'
8import { VideoAbuseModel } from '../../../models/video/video-abuse'
9import { VideoChannelModel } from '../../../models/video/video-channel'
8import { getOrCreateAccountAndServer } from '../account' 10import { getOrCreateAccountAndServer } from '../account'
9import { forwardActivity } from '../send/misc' 11import { forwardActivity } from '../send/misc'
10import { getVideoChannelActivityPubUrl } from '../url' 12import { getVideoChannelActivityPubUrl } from '../url'
@@ -37,7 +39,7 @@ export {
37 39
38// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
39 41
40async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) { 42async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) {
41 const options = { 43 const options = {
42 arguments: [ byAccount, activity ], 44 arguments: [ byAccount, activity ],
43 errorMessage: 'Cannot dislike the video with many retries.' 45 errorMessage: 'Cannot dislike the video with many retries.'
@@ -46,11 +48,11 @@ async function processCreateDislike (byAccount: AccountInstance, activity: Activ
46 return retryTransactionWrapper(createVideoDislike, options) 48 return retryTransactionWrapper(createVideoDislike, options)
47} 49}
48 50
49function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) { 51function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) {
50 const dislike = activity.object as DislikeObject 52 const dislike = activity.object as DislikeObject
51 53
52 return db.sequelize.transaction(async t => { 54 return sequelizeTypescript.transaction(async t => {
53 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) 55 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
54 if (!video) throw new Error('Unknown video ' + dislike.object) 56 if (!video) throw new Error('Unknown video ' + dislike.object)
55 57
56 const rate = { 58 const rate = {
@@ -58,7 +60,7 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat
58 videoId: video.id, 60 videoId: video.id,
59 accountId: byAccount.id 61 accountId: byAccount.id
60 } 62 }
61 const [ , created ] = await db.AccountVideoRate.findOrCreate({ 63 const [ , created ] = await AccountVideoRateModel.findOrCreate({
62 where: rate, 64 where: rate,
63 defaults: rate, 65 defaults: rate,
64 transaction: t 66 transaction: t
@@ -73,14 +75,14 @@ function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreat
73 }) 75 })
74} 76}
75 77
76async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) { 78async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) {
77 const view = activity.object as ViewObject 79 const view = activity.object as ViewObject
78 80
79 const video = await db.Video.loadByUrlAndPopulateAccount(view.object) 81 const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
80 82
81 if (!video) throw new Error('Unknown video ' + view.object) 83 if (!video) throw new Error('Unknown video ' + view.object)
82 84
83 const account = await db.Account.loadByUrl(view.actor) 85 const account = await AccountModel.loadByUrl(view.actor)
84 if (!account) throw new Error('Unknown account ' + view.actor) 86 if (!account) throw new Error('Unknown account ' + view.actor)
85 87
86 await video.increment('views') 88 await video.increment('views')
@@ -92,7 +94,7 @@ async function processCreateView (byAccount: AccountInstance, activity: Activity
92 } 94 }
93} 95}
94 96
95async function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { 97async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
96 const options = { 98 const options = {
97 arguments: [ account, videoChannelToCreateData ], 99 arguments: [ account, videoChannelToCreateData ],
98 errorMessage: 'Cannot insert the remote video channel with many retries.' 100 errorMessage: 'Cannot insert the remote video channel with many retries.'
@@ -107,15 +109,15 @@ async function processCreateVideoChannel (account: AccountInstance, videoChannel
107 return videoChannel 109 return videoChannel
108} 110}
109 111
110function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { 112function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
111 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) 113 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
112 114
113 return db.sequelize.transaction(async t => { 115 return sequelizeTypescript.transaction(async t => {
114 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) 116 let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
115 if (videoChannel) return videoChannel 117 if (videoChannel) return videoChannel
116 118
117 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) 119 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
118 videoChannel = db.VideoChannel.build(videoChannelData) 120 videoChannel = new VideoChannelModel(videoChannelData)
119 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) 121 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
120 122
121 videoChannel = await videoChannel.save({ transaction: t }) 123 videoChannel = await videoChannel.save({ transaction: t })
@@ -125,7 +127,7 @@ function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateDa
125 }) 127 })
126} 128}
127 129
128function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { 130function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
129 const options = { 131 const options = {
130 arguments: [ account, videoAbuseToCreateData ], 132 arguments: [ account, videoAbuseToCreateData ],
131 errorMessage: 'Cannot insert the remote video abuse with many retries.' 133 errorMessage: 'Cannot insert the remote video abuse with many retries.'
@@ -134,11 +136,11 @@ function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateDa
134 return retryTransactionWrapper(addRemoteVideoAbuse, options) 136 return retryTransactionWrapper(addRemoteVideoAbuse, options)
135} 137}
136 138
137function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { 139function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
138 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) 140 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
139 141
140 return db.sequelize.transaction(async t => { 142 return sequelizeTypescript.transaction(async t => {
141 const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) 143 const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
142 if (!video) { 144 if (!video) {
143 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) 145 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
144 return undefined 146 return undefined
@@ -150,7 +152,7 @@ function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData:
150 videoId: video.id 152 videoId: video.id
151 } 153 }
152 154
153 await db.VideoAbuse.create(videoAbuseData) 155 await VideoAbuseModel.create(videoAbuseData)
154 156
155 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) 157 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
156 }) 158 })
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 @@
1import { ActivityDelete } from '../../../../shared/models/activitypub/activity' 1import { ActivityDelete } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils' 2import { logger, retryTransactionWrapper } from '../../../helpers'
3import { logger } from '../../../helpers/logger' 3import { sequelizeTypescript } from '../../../initializers'
4import { database as db } from '../../../initializers' 4import { AccountModel } from '../../../models/account/account'
5import { AccountInstance } from '../../../models/account/account-interface' 5import { VideoModel } from '../../../models/video/video'
6import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 6import { VideoChannelModel } from '../../../models/video/video-channel'
7import { VideoInstance } from '../../../models/video/video-interface'
8import { getOrCreateAccountAndServer } from '../account' 7import { getOrCreateAccountAndServer } from '../account'
9 8
10async function processDeleteActivity (activity: ActivityDelete) { 9async function processDeleteActivity (activity: ActivityDelete) {
@@ -15,14 +14,14 @@ async function processDeleteActivity (activity: ActivityDelete) {
15 } 14 }
16 15
17 { 16 {
18 let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id) 17 let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
19 if (videoObject !== undefined) { 18 if (videoObject !== undefined) {
20 return processDeleteVideo(account, videoObject) 19 return processDeleteVideo(account, videoObject)
21 } 20 }
22 } 21 }
23 22
24 { 23 {
25 let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id) 24 let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id)
26 if (videoChannelObject !== undefined) { 25 if (videoChannelObject !== undefined) {
27 return processDeleteVideoChannel(account, videoChannelObject) 26 return processDeleteVideoChannel(account, videoChannelObject)
28 } 27 }
@@ -39,7 +38,7 @@ export {
39 38
40// --------------------------------------------------------------------------- 39// ---------------------------------------------------------------------------
41 40
42async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) { 41async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) {
43 const options = { 42 const options = {
44 arguments: [ account, videoToDelete ], 43 arguments: [ account, videoToDelete ],
45 errorMessage: 'Cannot remove the remote video with many retries.' 44 errorMessage: 'Cannot remove the remote video with many retries.'
@@ -48,10 +47,10 @@ async function processDeleteVideo (account: AccountInstance, videoToDelete: Vide
48 await retryTransactionWrapper(deleteRemoteVideo, options) 47 await retryTransactionWrapper(deleteRemoteVideo, options)
49} 48}
50 49
51async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) { 50async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) {
52 logger.debug('Removing remote video "%s".', videoToDelete.uuid) 51 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
53 52
54 await db.sequelize.transaction(async t => { 53 await sequelizeTypescript.transaction(async t => {
55 if (videoToDelete.VideoChannel.Account.id !== account.id) { 54 if (videoToDelete.VideoChannel.Account.id !== account.id) {
56 throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) 55 throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
57 } 56 }
@@ -62,7 +61,7 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video
62 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) 61 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
63} 62}
64 63
65async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { 64async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
66 const options = { 65 const options = {
67 arguments: [ account, videoChannelToRemove ], 66 arguments: [ account, videoChannelToRemove ],
68 errorMessage: 'Cannot remove the remote video channel with many retries.' 67 errorMessage: 'Cannot remove the remote video channel with many retries.'
@@ -71,10 +70,10 @@ async function processDeleteVideoChannel (account: AccountInstance, videoChannel
71 await retryTransactionWrapper(deleteRemoteVideoChannel, options) 70 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
72} 71}
73 72
74async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { 73async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
75 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) 74 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
76 75
77 await db.sequelize.transaction(async t => { 76 await sequelizeTypescript.transaction(async t => {
78 if (videoChannelToRemove.Account.id !== account.id) { 77 if (videoChannelToRemove.Account.id !== account.id) {
79 throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) 78 throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
80 } 79 }
@@ -85,7 +84,7 @@ async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelT
85 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid) 84 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
86} 85}
87 86
88async function processDeleteAccount (accountToRemove: AccountInstance) { 87async function processDeleteAccount (accountToRemove: AccountModel) {
89 const options = { 88 const options = {
90 arguments: [ accountToRemove ], 89 arguments: [ accountToRemove ],
91 errorMessage: 'Cannot remove the remote account with many retries.' 90 errorMessage: 'Cannot remove the remote account with many retries.'
@@ -94,10 +93,10 @@ async function processDeleteAccount (accountToRemove: AccountInstance) {
94 await retryTransactionWrapper(deleteRemoteAccount, options) 93 await retryTransactionWrapper(deleteRemoteAccount, options)
95} 94}
96 95
97async function deleteRemoteAccount (accountToRemove: AccountInstance) { 96async function deleteRemoteAccount (accountToRemove: AccountModel) {
98 logger.debug('Removing remote account "%s".', accountToRemove.uuid) 97 logger.debug('Removing remote account "%s".', accountToRemove.uuid)
99 98
100 await db.sequelize.transaction(async t => { 99 await sequelizeTypescript.transaction(async t => {
101 await accountToRemove.destroy({ transaction: t }) 100 await accountToRemove.destroy({ transaction: t })
102 }) 101 })
103 102
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 @@
1import { ActivityFollow } from '../../../../shared/models/activitypub/activity' 1import { ActivityFollow } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers' 2import { logger, retryTransactionWrapper } from '../../../helpers'
3import { database as db } from '../../../initializers' 3import { sequelizeTypescript } from '../../../initializers'
4import { AccountInstance } from '../../../models/account/account-interface' 4import { AccountModel } from '../../../models/account/account'
5import { logger } from '../../../helpers/logger' 5import { AccountFollowModel } from '../../../models/account/account-follow'
6import { sendAccept } from '../send/send-accept'
7import { getOrCreateAccountAndServer } from '../account' 6import { getOrCreateAccountAndServer } from '../account'
7import { sendAccept } from '../send'
8 8
9async function processFollowActivity (activity: ActivityFollow) { 9async function processFollowActivity (activity: ActivityFollow) {
10 const activityObject = activity.object 10 const activityObject = activity.object
@@ -21,7 +21,7 @@ export {
21 21
22// --------------------------------------------------------------------------- 22// ---------------------------------------------------------------------------
23 23
24function processFollow (account: AccountInstance, targetAccountURL: string) { 24function processFollow (account: AccountModel, targetAccountURL: string) {
25 const options = { 25 const options = {
26 arguments: [ account, targetAccountURL ], 26 arguments: [ account, targetAccountURL ],
27 errorMessage: 'Cannot follow with many retries.' 27 errorMessage: 'Cannot follow with many retries.'
@@ -30,14 +30,14 @@ function processFollow (account: AccountInstance, targetAccountURL: string) {
30 return retryTransactionWrapper(follow, options) 30 return retryTransactionWrapper(follow, options)
31} 31}
32 32
33async function follow (account: AccountInstance, targetAccountURL: string) { 33async function follow (account: AccountModel, targetAccountURL: string) {
34 await db.sequelize.transaction(async t => { 34 await sequelizeTypescript.transaction(async t => {
35 const targetAccount = await db.Account.loadByUrl(targetAccountURL, t) 35 const targetAccount = await AccountModel.loadByUrl(targetAccountURL, t)
36 36
37 if (!targetAccount) throw new Error('Unknown account') 37 if (!targetAccount) throw new Error('Unknown account')
38 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.') 38 if (targetAccount.isOwned() === false) throw new Error('This is not a local account.')
39 39
40 const [ accountFollow ] = await db.AccountFollow.findOrCreate({ 40 const [ accountFollow ] = await AccountFollowModel.findOrCreate({
41 where: { 41 where: {
42 accountId: account.id, 42 accountId: account.id,
43 targetAccountId: targetAccount.id 43 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 @@
1import { ActivityLike } from '../../../../shared/models/activitypub/activity' 1import { ActivityLike } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils' 2import { retryTransactionWrapper } from '../../../helpers'
3import { database as db } from '../../../initializers' 3import { sequelizeTypescript } from '../../../initializers'
4import { AccountInstance } from '../../../models/account/account-interface' 4import { AccountModel } from '../../../models/account/account'
5import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
6import { VideoModel } from '../../../models/video/video'
5import { getOrCreateAccountAndServer } from '../account' 7import { getOrCreateAccountAndServer } from '../account'
6import { forwardActivity } from '../send/misc' 8import { forwardActivity } from '../send/misc'
7 9
@@ -19,7 +21,7 @@ export {
19 21
20// --------------------------------------------------------------------------- 22// ---------------------------------------------------------------------------
21 23
22async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) { 24async function processLikeVideo (byAccount: AccountModel, activity: ActivityLike) {
23 const options = { 25 const options = {
24 arguments: [ byAccount, activity ], 26 arguments: [ byAccount, activity ],
25 errorMessage: 'Cannot like the video with many retries.' 27 errorMessage: 'Cannot like the video with many retries.'
@@ -28,11 +30,11 @@ async function processLikeVideo (byAccount: AccountInstance, activity: ActivityL
28 return retryTransactionWrapper(createVideoLike, options) 30 return retryTransactionWrapper(createVideoLike, options)
29} 31}
30 32
31function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) { 33function createVideoLike (byAccount: AccountModel, activity: ActivityLike) {
32 const videoUrl = activity.object 34 const videoUrl = activity.object
33 35
34 return db.sequelize.transaction(async t => { 36 return sequelizeTypescript.transaction(async t => {
35 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl) 37 const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl)
36 38
37 if (!video) throw new Error('Unknown video ' + videoUrl) 39 if (!video) throw new Error('Unknown video ' + videoUrl)
38 40
@@ -41,7 +43,7 @@ function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
41 videoId: video.id, 43 videoId: video.id,
42 accountId: byAccount.id 44 accountId: byAccount.id
43 } 45 }
44 const [ , created ] = await db.AccountVideoRate.findOrCreate({ 46 const [ , created ] = await AccountVideoRateModel.findOrCreate({
45 where: rate, 47 where: rate,
46 defaults: rate, 48 defaults: rate,
47 transaction: t 49 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 @@
1import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' 1import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' 2import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3import { retryTransactionWrapper } from '../../../helpers/database-utils' 3import { logger, retryTransactionWrapper } from '../../../helpers'
4import { logger } from '../../../helpers/logger' 4import { sequelizeTypescript } from '../../../initializers'
5import { database as db } from '../../../initializers' 5import { AccountModel } from '../../../models/account/account'
6import { AccountFollowModel } from '../../../models/account/account-follow'
7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8import { VideoModel } from '../../../models/video/video'
6import { forwardActivity } from '../send/misc' 9import { forwardActivity } from '../send/misc'
7 10
8async function processUndoActivity (activity: ActivityUndo) { 11async function processUndoActivity (activity: ActivityUndo) {
@@ -41,14 +44,14 @@ function processUndoLike (actor: string, activity: ActivityUndo) {
41function undoLike (actor: string, activity: ActivityUndo) { 44function undoLike (actor: string, activity: ActivityUndo) {
42 const likeActivity = activity.object as ActivityLike 45 const likeActivity = activity.object as ActivityLike
43 46
44 return db.sequelize.transaction(async t => { 47 return sequelizeTypescript.transaction(async t => {
45 const byAccount = await db.Account.loadByUrl(actor, t) 48 const byAccount = await AccountModel.loadByUrl(actor, t)
46 if (!byAccount) throw new Error('Unknown account ' + actor) 49 if (!byAccount) throw new Error('Unknown account ' + actor)
47 50
48 const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t) 51 const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
49 if (!video) throw new Error('Unknown video ' + likeActivity.actor) 52 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
50 53
51 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) 54 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
52 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) 55 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
53 56
54 await rate.destroy({ transaction: t }) 57 await rate.destroy({ transaction: t })
@@ -74,14 +77,14 @@ function processUndoDislike (actor: string, activity: ActivityUndo) {
74function undoDislike (actor: string, activity: ActivityUndo) { 77function undoDislike (actor: string, activity: ActivityUndo) {
75 const dislike = activity.object.object as DislikeObject 78 const dislike = activity.object.object as DislikeObject
76 79
77 return db.sequelize.transaction(async t => { 80 return sequelizeTypescript.transaction(async t => {
78 const byAccount = await db.Account.loadByUrl(actor, t) 81 const byAccount = await AccountModel.loadByUrl(actor, t)
79 if (!byAccount) throw new Error('Unknown account ' + actor) 82 if (!byAccount) throw new Error('Unknown account ' + actor)
80 83
81 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) 84 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
82 if (!video) throw new Error('Unknown video ' + dislike.actor) 85 if (!video) throw new Error('Unknown video ' + dislike.actor)
83 86
84 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) 87 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
85 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) 88 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
86 89
87 await rate.destroy({ transaction: t }) 90 await rate.destroy({ transaction: t })
@@ -105,10 +108,10 @@ function processUndoFollow (actor: string, followActivity: ActivityFollow) {
105} 108}
106 109
107function undoFollow (actor: string, followActivity: ActivityFollow) { 110function undoFollow (actor: string, followActivity: ActivityFollow) {
108 return db.sequelize.transaction(async t => { 111 return sequelizeTypescript.transaction(async t => {
109 const follower = await db.Account.loadByUrl(actor, t) 112 const follower = await AccountModel.loadByUrl(actor, t)
110 const following = await db.Account.loadByUrl(followActivity.object, t) 113 const following = await AccountModel.loadByUrl(followActivity.object, t)
111 const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t) 114 const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t)
112 115
113 if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) 116 if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`)
114 117
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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { VideoChannelObject, VideoTorrentObject } from '../../../../shared' 2import { VideoChannelObject, VideoTorrentObject } from '../../../../shared'
3import { ActivityUpdate } from '../../../../shared/models/activitypub/activity' 3import { ActivityUpdate } from '../../../../shared/models/activitypub'
4import { retryTransactionWrapper } from '../../../helpers/database-utils' 4import { logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers'
5import { logger } from '../../../helpers/logger' 5import { sequelizeTypescript } from '../../../initializers'
6import { resetSequelizeInstance } from '../../../helpers/utils' 6import { AccountModel } from '../../../models/account/account'
7import { database as db } from '../../../initializers' 7import { TagModel } from '../../../models/video/tag'
8import { AccountInstance } from '../../../models/account/account-interface' 8import { VideoModel } from '../../../models/video/video'
9import { VideoInstance } from '../../../models/video/video-interface' 9import { VideoChannelModel } from '../../../models/video/video-channel'
10import { VideoFileModel } from '../../../models/video/video-file'
10import { getOrCreateAccountAndServer } from '../account' 11import { getOrCreateAccountAndServer } from '../account'
11import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' 12import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
12 13
@@ -30,7 +31,7 @@ export {
30 31
31// --------------------------------------------------------------------------- 32// ---------------------------------------------------------------------------
32 33
33function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject) { 34function processUpdateVideo (account: AccountModel, video: VideoTorrentObject) {
34 const options = { 35 const options = {
35 arguments: [ account, video ], 36 arguments: [ account, video ],
36 errorMessage: 'Cannot update the remote video with many retries' 37 errorMessage: 'Cannot update the remote video with many retries'
@@ -39,18 +40,18 @@ function processUpdateVideo (account: AccountInstance, video: VideoTorrentObject
39 return retryTransactionWrapper(updateRemoteVideo, options) 40 return retryTransactionWrapper(updateRemoteVideo, options)
40} 41}
41 42
42async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpdate: VideoTorrentObject) { 43async function updateRemoteVideo (account: AccountModel, videoAttributesToUpdate: VideoTorrentObject) {
43 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) 44 logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
44 let videoInstance: VideoInstance 45 let videoInstance: VideoModel
45 let videoFieldsSave: object 46 let videoFieldsSave: object
46 47
47 try { 48 try {
48 await db.sequelize.transaction(async t => { 49 await sequelizeTypescript.transaction(async t => {
49 const sequelizeOptions = { 50 const sequelizeOptions = {
50 transaction: t 51 transaction: t
51 } 52 }
52 53
53 const videoInstance = await db.Video.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) 54 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
54 if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') 55 if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.')
55 56
56 if (videoInstance.VideoChannel.Account.id !== account.id) { 57 if (videoInstance.VideoChannel.Account.id !== account.id) {
@@ -81,12 +82,12 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd
81 await Promise.all(videoFileDestroyTasks) 82 await Promise.all(videoFileDestroyTasks)
82 83
83 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate) 84 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoAttributesToUpdate)
84 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => db.VideoFile.create(f)) 85 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f))
85 await Promise.all(tasks) 86 await Promise.all(tasks)
86 87
87 const tags = videoAttributesToUpdate.tag.map(t => t.name) 88 const tags = videoAttributesToUpdate.tag.map(t => t.name)
88 const tagInstances = await db.Tag.findOrCreateTags(tags, t) 89 const tagInstances = await TagModel.findOrCreateTags(tags, t)
89 await videoInstance.setTags(tagInstances, sequelizeOptions) 90 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
90 }) 91 })
91 92
92 logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid) 93 logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
@@ -101,7 +102,7 @@ async function updateRemoteVideo (account: AccountInstance, videoAttributesToUpd
101 } 102 }
102} 103}
103 104
104async function processUpdateVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { 105async function processUpdateVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
105 const options = { 106 const options = {
106 arguments: [ account, videoChannel ], 107 arguments: [ account, videoChannel ],
107 errorMessage: 'Cannot update the remote video channel with many retries.' 108 errorMessage: 'Cannot update the remote video channel with many retries.'
@@ -110,13 +111,13 @@ async function processUpdateVideoChannel (account: AccountInstance, videoChannel
110 await retryTransactionWrapper(updateRemoteVideoChannel, options) 111 await retryTransactionWrapper(updateRemoteVideoChannel, options)
111} 112}
112 113
113async function updateRemoteVideoChannel (account: AccountInstance, videoChannel: VideoChannelObject) { 114async function updateRemoteVideoChannel (account: AccountModel, videoChannel: VideoChannelObject) {
114 logger.debug('Updating remote video channel "%s".', videoChannel.uuid) 115 logger.debug('Updating remote video channel "%s".', videoChannel.uuid)
115 116
116 await db.sequelize.transaction(async t => { 117 await sequelizeTypescript.transaction(async t => {
117 const sequelizeOptions = { transaction: t } 118 const sequelizeOptions = { transaction: t }
118 119
119 const videoChannelInstance = await db.VideoChannel.loadByUrl(videoChannel.id) 120 const videoChannelInstance = await VideoChannelModel.loadByUrl(videoChannel.id)
120 if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.') 121 if (!videoChannelInstance) throw new Error('Video ' + videoChannel.id + ' not found.')
121 122
122 if (videoChannelInstance.Account.id !== account.id) { 123 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 @@
1import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' 1import { Activity, ActivityType } from '../../../../shared/models/activitypub'
2import { logger } from '../../../helpers/logger' 2import { logger } from '../../../helpers'
3import { AccountInstance } from '../../../models/account/account-interface' 3import { AccountModel } from '../../../models/account/account'
4import { processAcceptActivity } from './process-accept' 4import { processAcceptActivity } from './process-accept'
5import { processAddActivity } from './process-add' 5import { processAddActivity } from './process-add'
6import { processAnnounceActivity } from './process-announce' 6import { processAnnounceActivity } from './process-announce'
@@ -11,7 +11,7 @@ import { processLikeActivity } from './process-like'
11import { processUndoActivity } from './process-undo' 11import { processUndoActivity } from './process-undo'
12import { processUpdateActivity } from './process-update' 12import { processUpdateActivity } from './process-update'
13 13
14const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { 14const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountModel) => Promise<any> } = {
15 Create: processCreateActivity, 15 Create: processCreateActivity,
16 Add: processAddActivity, 16 Add: processAddActivity,
17 Update: processUpdateActivity, 17 Update: processUpdateActivity,
@@ -23,7 +23,7 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccoun
23 Like: processLikeActivity 23 Like: processLikeActivity
24} 24}
25 25
26async function processActivities (activities: Activity[], signatureAccount?: AccountInstance, inboxAccount?: AccountInstance) { 26async function processActivities (activities: Activity[], signatureAccount?: AccountModel, inboxAccount?: AccountModel) {
27 for (const activity of activities) { 27 for (const activity of activities) {
28 // When we fetch remote data, we don't have signature 28 // When we fetch remote data, we don't have signature
29 if (signatureAccount && activity.actor !== signatureAccount.url) { 29 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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { Activity } from '../../../../shared/models/activitypub/activity' 2import { Activity } from '../../../../shared/models/activitypub'
3import { logger } from '../../../helpers/logger' 3import { logger } from '../../../helpers'
4import { ACTIVITY_PUB, database as db } from '../../../initializers' 4import { ACTIVITY_PUB } from '../../../initializers'
5import { AccountInstance } from '../../../models/account/account-interface' 5import { AccountModel } from '../../../models/account/account'
6import { VideoChannelInstance } from '../../../models/index' 6import { AccountFollowModel } from '../../../models/account/account-follow'
7import { VideoInstance } from '../../../models/video/video-interface' 7import { VideoModel } from '../../../models/video/video'
8import { 8import { VideoChannelModel } from '../../../models/video/video-channel'
9 activitypubHttpJobScheduler, 9import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
10 ActivityPubHttpPayload 10import { VideoShareModel } from '../../../models/video/video-share'
11} from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' 11import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
12 12
13async function forwardActivity ( 13async function forwardActivity (
14 activity: Activity, 14 activity: Activity,
15 t: Transaction, 15 t: Transaction,
16 followersException: AccountInstance[] = [] 16 followersException: AccountModel[] = []
17) { 17) {
18 const to = activity.to || [] 18 const to = activity.to || []
19 const cc = activity.cc || [] 19 const cc = activity.cc || []
@@ -25,7 +25,7 @@ async function forwardActivity (
25 } 25 }
26 } 26 }
27 27
28 const toAccountFollowers = await db.Account.listByFollowersUrls(followersUrls, t) 28 const toAccountFollowers = await AccountModel.listByFollowersUrls(followersUrls, t)
29 const uris = await computeFollowerUris(toAccountFollowers, followersException, t) 29 const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
30 30
31 if (uris.length === 0) { 31 if (uris.length === 0) {
@@ -45,10 +45,10 @@ async function forwardActivity (
45 45
46async function broadcastToFollowers ( 46async function broadcastToFollowers (
47 data: any, 47 data: any,
48 byAccount: AccountInstance, 48 byAccount: AccountModel,
49 toAccountFollowers: AccountInstance[], 49 toAccountFollowers: AccountModel[],
50 t: Transaction, 50 t: Transaction,
51 followersException: AccountInstance[] = [] 51 followersException: AccountModel[] = []
52) { 52) {
53 const uris = await computeFollowerUris(toAccountFollowers, followersException, t) 53 const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
54 if (uris.length === 0) { 54 if (uris.length === 0) {
@@ -67,7 +67,7 @@ async function broadcastToFollowers (
67 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload) 67 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
68} 68}
69 69
70async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) { 70async function unicastTo (data: any, byAccount: AccountModel, toAccountUrl: string, t: Transaction) {
71 logger.debug('Creating unicast job.', { uri: toAccountUrl }) 71 logger.debug('Creating unicast job.', { uri: toAccountUrl })
72 72
73 const jobPayload: ActivityPubHttpPayload = { 73 const jobPayload: ActivityPubHttpPayload = {
@@ -79,42 +79,42 @@ async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: s
79 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) 79 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
80} 80}
81 81
82function getOriginVideoAudience (video: VideoInstance, accountsInvolvedInVideo: AccountInstance[]) { 82function getOriginVideoAudience (video: VideoModel, accountsInvolvedInVideo: AccountModel[]) {
83 return { 83 return {
84 to: [ video.VideoChannel.Account.url ], 84 to: [ video.VideoChannel.Account.url ],
85 cc: accountsInvolvedInVideo.map(a => a.followersUrl) 85 cc: accountsInvolvedInVideo.map(a => a.followersUrl)
86 } 86 }
87} 87}
88 88
89function getOriginVideoChannelAudience (videoChannel: VideoChannelInstance, accountsInvolved: AccountInstance[]) { 89function getOriginVideoChannelAudience (videoChannel: VideoChannelModel, accountsInvolved: AccountModel[]) {
90 return { 90 return {
91 to: [ videoChannel.Account.url ], 91 to: [ videoChannel.Account.url ],
92 cc: accountsInvolved.map(a => a.followersUrl) 92 cc: accountsInvolved.map(a => a.followersUrl)
93 } 93 }
94} 94}
95 95
96function getObjectFollowersAudience (accountsInvolvedInObject: AccountInstance[]) { 96function getObjectFollowersAudience (accountsInvolvedInObject: AccountModel[]) {
97 return { 97 return {
98 to: accountsInvolvedInObject.map(a => a.followersUrl), 98 to: accountsInvolvedInObject.map(a => a.followersUrl),
99 cc: [] 99 cc: []
100 } 100 }
101} 101}
102 102
103async function getAccountsInvolvedInVideo (video: VideoInstance, t: Transaction) { 103async function getAccountsInvolvedInVideo (video: VideoModel, t: Transaction) {
104 const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id, t) 104 const accountsToForwardView = await VideoShareModel.loadAccountsByShare(video.id, t)
105 accountsToForwardView.push(video.VideoChannel.Account) 105 accountsToForwardView.push(video.VideoChannel.Account)
106 106
107 return accountsToForwardView 107 return accountsToForwardView
108} 108}
109 109
110async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { 110async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
111 const accountsToForwardView = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) 111 const accountsToForwardView = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
112 accountsToForwardView.push(videoChannel.Account) 112 accountsToForwardView.push(videoChannel.Account)
113 113
114 return accountsToForwardView 114 return accountsToForwardView
115} 115}
116 116
117async function getAudience (accountSender: AccountInstance, t: Transaction, isPublic = true) { 117async function getAudience (accountSender: AccountModel, t: Transaction, isPublic = true) {
118 const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls(t) 118 const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls(t)
119 119
120 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 120 // 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
132 return { to, cc } 132 return { to, cc }
133} 133}
134 134
135async function computeFollowerUris (toAccountFollower: AccountInstance[], followersException: AccountInstance[], t: Transaction) { 135async function computeFollowerUris (toAccountFollower: AccountModel[], followersException: AccountModel[], t: Transaction) {
136 const toAccountFollowerIds = toAccountFollower.map(a => a.id) 136 const toAccountFollowerIds = toAccountFollower.map(a => a.id)
137 137
138 const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, t) 138 const result = await AccountFollowModel.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, t)
139 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) 139 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
140 const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) 140 return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
141
142 return uris
143} 141}
144 142
145// --------------------------------------------------------------------------- 143// ---------------------------------------------------------------------------
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAccept } from '../../../../shared/models/activitypub/activity' 2import { ActivityAccept } from '../../../../shared/models/activitypub'
3import { AccountInstance } from '../../../models' 3import { AccountModel } from '../../../models/account/account'
4import { AccountFollowInstance } from '../../../models/account/account-follow-interface' 4import { AccountFollowModel } from '../../../models/account/account-follow'
5import { unicastTo } from './misc'
6import { getAccountFollowAcceptActivityPubUrl } from '../url' 5import { getAccountFollowAcceptActivityPubUrl } from '../url'
6import { unicastTo } from './misc'
7 7
8async function sendAccept (accountFollow: AccountFollowInstance, t: Transaction) { 8async function sendAccept (accountFollow: AccountFollowModel, t: Transaction) {
9 const follower = accountFollow.AccountFollower 9 const follower = accountFollow.AccountFollower
10 const me = accountFollow.AccountFollowing 10 const me = accountFollow.AccountFollowing
11 11
@@ -23,7 +23,7 @@ export {
23 23
24// --------------------------------------------------------------------------- 24// ---------------------------------------------------------------------------
25 25
26function acceptActivityData (url: string, byAccount: AccountInstance) { 26function acceptActivityData (url: string, byAccount: AccountModel) {
27 const activity: ActivityAccept = { 27 const activity: ActivityAccept = {
28 type: 'Accept', 28 type: 'Accept',
29 id: url, 29 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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAdd } from '../../../../shared/models/activitypub/activity' 2import { ActivityAdd } from '../../../../shared/models/activitypub'
3import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' 3import { VideoPrivacy } from '../../../../shared/models/videos'
4import { AccountInstance, VideoInstance } from '../../../models' 4import { AccountModel } from '../../../models/account/account'
5import { VideoModel } from '../../../models/video/video'
5import { broadcastToFollowers, getAudience } from './misc' 6import { broadcastToFollowers, getAudience } from './misc'
6 7
7async function sendAddVideo (video: VideoInstance, t: Transaction) { 8async function sendAddVideo (video: VideoModel, t: Transaction) {
8 const byAccount = video.VideoChannel.Account 9 const byAccount = video.VideoChannel.Account
9 10
10 const videoObject = video.toActivityPubObject() 11 const videoObject = video.toActivityPubObject()
@@ -15,16 +16,17 @@ async function sendAddVideo (video: VideoInstance, t: Transaction) {
15 16
16async function addActivityData ( 17async function addActivityData (
17 url: string, 18 url: string,
18 byAccount: AccountInstance, 19 byAccount: AccountModel,
19 video: VideoInstance, 20 video: VideoModel,
20 target: string, 21 target: string,
21 object: any, 22 object: any,
22 t: Transaction 23 t: Transaction
23) { 24): Promise<ActivityAdd> {
24 const videoPublic = video.privacy === VideoPrivacy.PUBLIC 25 const videoPublic = video.privacy === VideoPrivacy.PUBLIC
25 26
26 const { to, cc } = await getAudience(byAccount, t, videoPublic) 27 const { to, cc } = await getAudience(byAccount, t, videoPublic)
27 const activity: ActivityAdd = { 28
29 return {
28 type: 'Add', 30 type: 'Add',
29 id: url, 31 id: url,
30 actor: byAccount.url, 32 actor: byAccount.url,
@@ -33,8 +35,6 @@ async function addActivityData (
33 object, 35 object,
34 target 36 target
35 } 37 }
36
37 return activity
38} 38}
39 39
40// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAdd } from '../../../../shared/index' 2import { ActivityAdd } from '../../../../shared/index'
3import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity' 3import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
4import { AccountInstance, VideoInstance } from '../../../models' 4import { AccountModel } from '../../../models/account/account'
5import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 5import { VideoModel } from '../../../models/video/video'
6import { VideoChannelModel } from '../../../models/video/video-channel'
6import { getAnnounceActivityPubUrl } from '../url' 7import { getAnnounceActivityPubUrl } from '../url'
7import { 8import {
8 broadcastToFollowers, 9 broadcastToFollowers,
@@ -17,7 +18,7 @@ import {
17import { addActivityData } from './send-add' 18import { addActivityData } from './send-add'
18import { createActivityData } from './send-create' 19import { createActivityData } from './send-create'
19 20
20async function buildVideoAnnounceToFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 21async function buildVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
21 const url = getAnnounceActivityPubUrl(video.url, byAccount) 22 const url = getAnnounceActivityPubUrl(video.url, byAccount)
22 23
23 const videoChannel = video.VideoChannel 24 const videoChannel = video.VideoChannel
@@ -25,18 +26,16 @@ async function buildVideoAnnounceToFollowers (byAccount: AccountInstance, video:
25 26
26 const accountsToForwardView = await getAccountsInvolvedInVideo(video, t) 27 const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
27 const audience = getObjectFollowersAudience(accountsToForwardView) 28 const audience = getObjectFollowersAudience(accountsToForwardView)
28 const data = await announceActivityData(url, byAccount, announcedActivity, t, audience) 29 return announceActivityData(url, byAccount, announcedActivity, t, audience)
29
30 return data
31} 30}
32 31
33async function sendVideoAnnounceToFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 32async function sendVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
34 const data = await buildVideoAnnounceToFollowers(byAccount, video, t) 33 const data = await buildVideoAnnounceToFollowers(byAccount, video, t)
35 34
36 return broadcastToFollowers(data, byAccount, [ byAccount ], t) 35 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
37} 36}
38 37
39async function sendVideoAnnounceToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 38async function sendVideoAnnounceToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
40 const url = getAnnounceActivityPubUrl(video.url, byAccount) 39 const url = getAnnounceActivityPubUrl(video.url, byAccount)
41 40
42 const videoChannel = video.VideoChannel 41 const videoChannel = video.VideoChannel
@@ -49,24 +48,22 @@ async function sendVideoAnnounceToOrigin (byAccount: AccountInstance, video: Vid
49 return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t) 48 return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t)
50} 49}
51 50
52async function buildVideoChannelAnnounceToFollowers (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { 51async function buildVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
53 const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount) 52 const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
54 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t) 53 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)
55 54
56 const accountsToForwardView = await getAccountsInvolvedInVideoChannel(videoChannel, t) 55 const accountsToForwardView = await getAccountsInvolvedInVideoChannel(videoChannel, t)
57 const audience = getObjectFollowersAudience(accountsToForwardView) 56 const audience = getObjectFollowersAudience(accountsToForwardView)
58 const data = await announceActivityData(url, byAccount, announcedActivity, t, audience) 57 return announceActivityData(url, byAccount, announcedActivity, t, audience)
59
60 return data
61} 58}
62 59
63async function sendVideoChannelAnnounceToFollowers (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { 60async function sendVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
64 const data = await buildVideoChannelAnnounceToFollowers(byAccount, videoChannel, t) 61 const data = await buildVideoChannelAnnounceToFollowers(byAccount, videoChannel, t)
65 62
66 return broadcastToFollowers(data, byAccount, [ byAccount ], t) 63 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
67} 64}
68 65
69async function sendVideoChannelAnnounceToOrigin (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) { 66async function sendVideoChannelAnnounceToOrigin (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
70 const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount) 67 const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
71 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t) 68 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)
72 69
@@ -79,16 +76,16 @@ async function sendVideoChannelAnnounceToOrigin (byAccount: AccountInstance, vid
79 76
80async function announceActivityData ( 77async function announceActivityData (
81 url: string, 78 url: string,
82 byAccount: AccountInstance, 79 byAccount: AccountModel,
83 object: ActivityCreate | ActivityAdd, 80 object: ActivityCreate | ActivityAdd,
84 t: Transaction, 81 t: Transaction,
85 audience?: ActivityAudience 82 audience?: ActivityAudience
86) { 83): Promise<ActivityAnnounce> {
87 if (!audience) { 84 if (!audience) {
88 audience = await getAudience(byAccount, t) 85 audience = await getAudience(byAccount, t)
89 } 86 }
90 87
91 const activity: ActivityAnnounce = { 88 return {
92 type: 'Announce', 89 type: 'Announce',
93 to: audience.to, 90 to: audience.to,
94 cc: audience.cc, 91 cc: audience.cc,
@@ -96,8 +93,6 @@ async function announceActivityData (
96 actor: byAccount.url, 93 actor: byAccount.url,
97 object 94 object
98 } 95 }
99
100 return activity
101} 96}
102 97
103// --------------------------------------------------------------------------- 98// ---------------------------------------------------------------------------
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity' 2import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3import { getServerAccount } from '../../../helpers/utils' 3import { getServerAccount } from '../../../helpers'
4import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' 4import { AccountModel } from '../../../models/account/account'
5import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface' 5import { VideoModel } from '../../../models/video/video'
6import { VideoAbuseModel } from '../../../models/video/video-abuse'
7import { VideoChannelModel } from '../../../models/video/video-channel'
6import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' 8import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7import { 9import {
8 broadcastToFollowers, 10 broadcastToFollowers,
@@ -13,7 +15,7 @@ import {
13 unicastTo 15 unicastTo
14} from './misc' 16} from './misc'
15 17
16async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { 18async function sendCreateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
17 const byAccount = videoChannel.Account 19 const byAccount = videoChannel.Account
18 20
19 const videoChannelObject = videoChannel.toActivityPubObject() 21 const videoChannelObject = videoChannel.toActivityPubObject()
@@ -22,7 +24,7 @@ async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Tr
22 return broadcastToFollowers(data, byAccount, [ byAccount ], t) 24 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
23} 25}
24 26
25async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) { 27async function sendVideoAbuse (byAccount: AccountModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
26 const url = getVideoAbuseActivityPubUrl(videoAbuse) 28 const url = getVideoAbuseActivityPubUrl(videoAbuse)
27 29
28 const audience = { to: [ video.VideoChannel.Account.url ], cc: [] } 30 const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
@@ -31,7 +33,7 @@ async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbus
31 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 33 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
32} 34}
33 35
34async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 36async function sendCreateViewToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
35 const url = getVideoViewActivityPubUrl(byAccount, video) 37 const url = getVideoViewActivityPubUrl(byAccount, video)
36 const viewActivity = createViewActivityData(byAccount, video) 38 const viewActivity = createViewActivityData(byAccount, video)
37 39
@@ -42,7 +44,7 @@ async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoI
42 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 44 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
43} 45}
44 46
45async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 47async function sendCreateViewToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
46 const url = getVideoViewActivityPubUrl(byAccount, video) 48 const url = getVideoViewActivityPubUrl(byAccount, video)
47 const viewActivity = createViewActivityData(byAccount, video) 49 const viewActivity = createViewActivityData(byAccount, video)
48 50
@@ -56,7 +58,7 @@ async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video
56 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException) 58 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
57} 59}
58 60
59async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 61async function sendCreateDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
60 const url = getVideoDislikeActivityPubUrl(byAccount, video) 62 const url = getVideoDislikeActivityPubUrl(byAccount, video)
61 const dislikeActivity = createDislikeActivityData(byAccount, video) 63 const dislikeActivity = createDislikeActivityData(byAccount, video)
62 64
@@ -67,7 +69,7 @@ async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: Vid
67 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 69 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
68} 70}
69 71
70async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 72async function sendCreateDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
71 const url = getVideoDislikeActivityPubUrl(byAccount, video) 73 const url = getVideoDislikeActivityPubUrl(byAccount, video)
72 const dislikeActivity = createDislikeActivityData(byAccount, video) 74 const dislikeActivity = createDislikeActivityData(byAccount, video)
73 75
@@ -79,12 +81,18 @@ async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, vi
79 return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException) 81 return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException)
80} 82}
81 83
82async function createActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction, audience?: ActivityAudience) { 84async function createActivityData (
85 url: string,
86 byAccount: AccountModel,
87 object: any,
88 t: Transaction,
89 audience?: ActivityAudience
90): Promise<ActivityCreate> {
83 if (!audience) { 91 if (!audience) {
84 audience = await getAudience(byAccount, t) 92 audience = await getAudience(byAccount, t)
85 } 93 }
86 94
87 const activity: ActivityCreate = { 95 return {
88 type: 'Create', 96 type: 'Create',
89 id: url, 97 id: url,
90 actor: byAccount.url, 98 actor: byAccount.url,
@@ -92,18 +100,14 @@ async function createActivityData (url: string, byAccount: AccountInstance, obje
92 cc: audience.cc, 100 cc: audience.cc,
93 object 101 object
94 } 102 }
95
96 return activity
97} 103}
98 104
99function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) { 105function createDislikeActivityData (byAccount: AccountModel, video: VideoModel) {
100 const obj = { 106 return {
101 type: 'Dislike', 107 type: 'Dislike',
102 actor: byAccount.url, 108 actor: byAccount.url,
103 object: video.url 109 object: video.url
104 } 110 }
105
106 return obj
107} 111}
108 112
109// --------------------------------------------------------------------------- 113// ---------------------------------------------------------------------------
@@ -121,12 +125,10 @@ export {
121 125
122// --------------------------------------------------------------------------- 126// ---------------------------------------------------------------------------
123 127
124function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) { 128function createViewActivityData (byAccount: AccountModel, video: VideoModel) {
125 const obj = { 129 return {
126 type: 'View', 130 type: 'View',
127 actor: byAccount.url, 131 actor: byAccount.url,
128 object: video.url 132 object: video.url
129 } 133 }
130
131 return obj
132} 134}
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityDelete } from '../../../../shared/models/activitypub/activity' 2import { ActivityDelete } from '../../../../shared/models/activitypub'
3import { database as db } from '../../../initializers' 3import { AccountModel } from '../../../models/account/account'
4import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' 4import { VideoModel } from '../../../models/video/video'
5import { VideoChannelModel } from '../../../models/video/video-channel'
6import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
7import { VideoShareModel } from '../../../models/video/video-share'
5import { broadcastToFollowers } from './misc' 8import { broadcastToFollowers } from './misc'
6 9
7async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { 10async function sendDeleteVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
8 const byAccount = videoChannel.Account 11 const byAccount = videoChannel.Account
9 12
10 const data = deleteActivityData(videoChannel.url, byAccount) 13 const data = deleteActivityData(videoChannel.url, byAccount)
11 14
12 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) 15 const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
13 accountsInvolved.push(byAccount) 16 accountsInvolved.push(byAccount)
14 17
15 return broadcastToFollowers(data, byAccount, accountsInvolved, t) 18 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
16} 19}
17 20
18async function sendDeleteVideo (video: VideoInstance, t: Transaction) { 21async function sendDeleteVideo (video: VideoModel, t: Transaction) {
19 const byAccount = video.VideoChannel.Account 22 const byAccount = video.VideoChannel.Account
20 23
21 const data = deleteActivityData(video.url, byAccount) 24 const data = deleteActivityData(video.url, byAccount)
22 25
23 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id, t) 26 const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t)
24 accountsInvolved.push(byAccount) 27 accountsInvolved.push(byAccount)
25 28
26 return broadcastToFollowers(data, byAccount, accountsInvolved, t) 29 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
27} 30}
28 31
29async function sendDeleteAccount (account: AccountInstance, t: Transaction) { 32async function sendDeleteAccount (account: AccountModel, t: Transaction) {
30 const data = deleteActivityData(account.url, account) 33 const data = deleteActivityData(account.url, account)
31 34
32 return broadcastToFollowers(data, account, [ account ], t) 35 return broadcastToFollowers(data, account, [ account ], t)
@@ -42,12 +45,10 @@ export {
42 45
43// --------------------------------------------------------------------------- 46// ---------------------------------------------------------------------------
44 47
45function deleteActivityData (url: string, byAccount: AccountInstance) { 48function deleteActivityData (url: string, byAccount: AccountModel): ActivityDelete {
46 const activity: ActivityDelete = { 49 return {
47 type: 'Delete', 50 type: 'Delete',
48 id: url, 51 id: url,
49 actor: byAccount.url 52 actor: byAccount.url
50 } 53 }
51
52 return activity
53} 54}
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityFollow } from '../../../../shared/models/activitypub/activity' 2import { ActivityFollow } from '../../../../shared/models/activitypub'
3import { AccountInstance } from '../../../models' 3import { AccountModel } from '../../../models/account/account'
4import { AccountFollowInstance } from '../../../models/account/account-follow-interface' 4import { AccountFollowModel } from '../../../models/account/account-follow'
5import { getAccountFollowActivityPubUrl } from '../url' 5import { getAccountFollowActivityPubUrl } from '../url'
6import { unicastTo } from './misc' 6import { unicastTo } from './misc'
7 7
8function sendFollow (accountFollow: AccountFollowInstance, t: Transaction) { 8function sendFollow (accountFollow: AccountFollowModel, t: Transaction) {
9 const me = accountFollow.AccountFollower 9 const me = accountFollow.AccountFollower
10 const following = accountFollow.AccountFollowing 10 const following = accountFollow.AccountFollowing
11 11
@@ -15,15 +15,13 @@ function sendFollow (accountFollow: AccountFollowInstance, t: Transaction) {
15 return unicastTo(data, me, following.inboxUrl, t) 15 return unicastTo(data, me, following.inboxUrl, t)
16} 16}
17 17
18function followActivityData (url: string, byAccount: AccountInstance, targetAccount: AccountInstance) { 18function followActivityData (url: string, byAccount: AccountModel, targetAccount: AccountModel): ActivityFollow {
19 const activity: ActivityFollow = { 19 return {
20 type: 'Follow', 20 type: 'Follow',
21 id: url, 21 id: url,
22 actor: byAccount.url, 22 actor: byAccount.url,
23 object: targetAccount.url 23 object: targetAccount.url
24 } 24 }
25
26 return activity
27} 25}
28 26
29// --------------------------------------------------------------------------- 27// ---------------------------------------------------------------------------
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub/activity' 2import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
3import { AccountInstance, VideoInstance } from '../../../models' 3import { AccountModel } from '../../../models/account/account'
4import { VideoModel } from '../../../models/video/video'
4import { getVideoLikeActivityPubUrl } from '../url' 5import { getVideoLikeActivityPubUrl } from '../url'
5import { 6import {
6 broadcastToFollowers, 7 broadcastToFollowers,
@@ -11,7 +12,7 @@ import {
11 unicastTo 12 unicastTo
12} from './misc' 13} from './misc'
13 14
14async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 15async function sendLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
15 const url = getVideoLikeActivityPubUrl(byAccount, video) 16 const url = getVideoLikeActivityPubUrl(byAccount, video)
16 17
17 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t) 18 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
@@ -21,7 +22,7 @@ async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstanc
21 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 22 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
22} 23}
23 24
24async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 25async function sendLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
25 const url = getVideoLikeActivityPubUrl(byAccount, video) 26 const url = getVideoLikeActivityPubUrl(byAccount, video)
26 27
27 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t) 28 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
@@ -34,16 +35,16 @@ async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: Vide
34 35
35async function likeActivityData ( 36async function likeActivityData (
36 url: string, 37 url: string,
37 byAccount: AccountInstance, 38 byAccount: AccountModel,
38 video: VideoInstance, 39 video: VideoModel,
39 t: Transaction, 40 t: Transaction,
40 audience?: ActivityAudience 41 audience?: ActivityAudience
41) { 42): Promise<ActivityLike> {
42 if (!audience) { 43 if (!audience) {
43 audience = await getAudience(byAccount, t) 44 audience = await getAudience(byAccount, t)
44 } 45 }
45 46
46 const activity: ActivityLike = { 47 return {
47 type: 'Like', 48 type: 'Like',
48 id: url, 49 id: url,
49 actor: byAccount.url, 50 actor: byAccount.url,
@@ -51,8 +52,6 @@ async function likeActivityData (
51 cc: audience.cc, 52 cc: audience.cc,
52 object: video.url 53 object: video.url
53 } 54 }
54
55 return activity
56} 55}
57 56
58// --------------------------------------------------------------------------- 57// ---------------------------------------------------------------------------
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 {
5 ActivityFollow, 5 ActivityFollow,
6 ActivityLike, 6 ActivityLike,
7 ActivityUndo 7 ActivityUndo
8} from '../../../../shared/models/activitypub/activity' 8} from '../../../../shared/models/activitypub'
9import { AccountInstance } from '../../../models' 9import { AccountModel } from '../../../models/account/account'
10import { AccountFollowInstance } from '../../../models/account/account-follow-interface' 10import { AccountFollowModel } from '../../../models/account/account-follow'
11import { VideoInstance } from '../../../models/video/video-interface' 11import { VideoModel } from '../../../models/video/video'
12import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' 12import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
13import { 13import {
14 broadcastToFollowers, 14 broadcastToFollowers,
@@ -22,7 +22,7 @@ import { createActivityData, createDislikeActivityData } from './send-create'
22import { followActivityData } from './send-follow' 22import { followActivityData } from './send-follow'
23import { likeActivityData } from './send-like' 23import { likeActivityData } from './send-like'
24 24
25async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transaction) { 25async function sendUndoFollow (accountFollow: AccountFollowModel, t: Transaction) {
26 const me = accountFollow.AccountFollower 26 const me = accountFollow.AccountFollower
27 const following = accountFollow.AccountFollowing 27 const following = accountFollow.AccountFollowing
28 28
@@ -35,7 +35,7 @@ async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transact
35 return unicastTo(data, me, following.inboxUrl, t) 35 return unicastTo(data, me, following.inboxUrl, t)
36} 36}
37 37
38async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 38async function sendUndoLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
39 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) 39 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
40 const undoUrl = getUndoActivityPubUrl(likeUrl) 40 const undoUrl = getUndoActivityPubUrl(likeUrl)
41 41
@@ -47,7 +47,7 @@ async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoIns
47 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 47 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
48} 48}
49 49
50async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 50async function sendUndoLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
51 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) 51 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
52 const undoUrl = getUndoActivityPubUrl(likeUrl) 52 const undoUrl = getUndoActivityPubUrl(likeUrl)
53 53
@@ -60,7 +60,7 @@ async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video:
60 return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException) 60 return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
61} 61}
62 62
63async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 63async function sendUndoDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
64 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video) 64 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
65 const undoUrl = getUndoActivityPubUrl(dislikeUrl) 65 const undoUrl = getUndoActivityPubUrl(dislikeUrl)
66 66
@@ -74,7 +74,7 @@ async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: Video
74 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) 74 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
75} 75}
76 76
77async function sendUndoDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { 77async function sendUndoDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
78 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video) 78 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
79 const undoUrl = getUndoActivityPubUrl(dislikeUrl) 79 const undoUrl = getUndoActivityPubUrl(dislikeUrl)
80 80
@@ -103,16 +103,16 @@ export {
103 103
104async function undoActivityData ( 104async function undoActivityData (
105 url: string, 105 url: string,
106 byAccount: AccountInstance, 106 byAccount: AccountModel,
107 object: ActivityFollow | ActivityLike | ActivityCreate, 107 object: ActivityFollow | ActivityLike | ActivityCreate,
108 t: Transaction, 108 t: Transaction,
109 audience?: ActivityAudience 109 audience?: ActivityAudience
110) { 110): Promise<ActivityUndo> {
111 if (!audience) { 111 if (!audience) {
112 audience = await getAudience(byAccount, t) 112 audience = await getAudience(byAccount, t)
113 } 113 }
114 114
115 const activity: ActivityUndo = { 115 return {
116 type: 'Undo', 116 type: 'Undo',
117 id: url, 117 id: url,
118 actor: byAccount.url, 118 actor: byAccount.url,
@@ -120,6 +120,4 @@ async function undoActivityData (
120 cc: audience.cc, 120 cc: audience.cc,
121 object 121 object
122 } 122 }
123
124 return activity
125} 123}
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityUpdate } from '../../../../shared/models/activitypub/activity' 2import { ActivityUpdate } from '../../../../shared/models/activitypub'
3import { database as db } from '../../../initializers' 3import { AccountModel } from '../../../models/account/account'
4import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models' 4import { VideoModel } from '../../../models/video/video'
5import { VideoChannelModel } from '../../../models/video/video-channel'
6import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
7import { VideoShareModel } from '../../../models/video/video-share'
5import { getUpdateActivityPubUrl } from '../url' 8import { getUpdateActivityPubUrl } from '../url'
6import { broadcastToFollowers, getAudience } from './misc' 9import { broadcastToFollowers, getAudience } from './misc'
7 10
8async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) { 11async function sendUpdateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
9 const byAccount = videoChannel.Account 12 const byAccount = videoChannel.Account
10 13
11 const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString()) 14 const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString())
12 const videoChannelObject = videoChannel.toActivityPubObject() 15 const videoChannelObject = videoChannel.toActivityPubObject()
13 const data = await updateActivityData(url, byAccount, videoChannelObject, t) 16 const data = await updateActivityData(url, byAccount, videoChannelObject, t)
14 17
15 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id, t) 18 const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
16 accountsInvolved.push(byAccount) 19 accountsInvolved.push(byAccount)
17 20
18 return broadcastToFollowers(data, byAccount, accountsInvolved, t) 21 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
19} 22}
20 23
21async function sendUpdateVideo (video: VideoInstance, t: Transaction) { 24async function sendUpdateVideo (video: VideoModel, t: Transaction) {
22 const byAccount = video.VideoChannel.Account 25 const byAccount = video.VideoChannel.Account
23 26
24 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString()) 27 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
25 const videoObject = video.toActivityPubObject() 28 const videoObject = video.toActivityPubObject()
26 const data = await updateActivityData(url, byAccount, videoObject, t) 29 const data = await updateActivityData(url, byAccount, videoObject, t)
27 30
28 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id, t) 31 const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t)
29 accountsInvolved.push(byAccount) 32 accountsInvolved.push(byAccount)
30 33
31 return broadcastToFollowers(data, byAccount, accountsInvolved, t) 34 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
@@ -40,9 +43,9 @@ export {
40 43
41// --------------------------------------------------------------------------- 44// ---------------------------------------------------------------------------
42 45
43async function updateActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction) { 46async function updateActivityData (url: string, byAccount: AccountModel, object: any, t: Transaction): Promise<ActivityUpdate> {
44 const { to, cc } = await getAudience(byAccount, t) 47 const { to, cc } = await getAudience(byAccount, t)
45 const activity: ActivityUpdate = { 48 return {
46 type: 'Update', 49 type: 'Update',
47 id: url, 50 id: url,
48 actor: byAccount.url, 51 actor: byAccount.url,
@@ -50,6 +53,4 @@ async function updateActivityData (url: string, byAccount: AccountInstance, obje
50 cc, 53 cc,
51 object 54 object
52 } 55 }
53
54 return activity
55} 56}
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 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { getServerAccount } from '../../helpers/utils' 2import { getServerAccount } from '../../helpers'
3import { database as db } from '../../initializers' 3import { VideoModel } from '../../models/video/video'
4import { VideoChannelInstance } from '../../models/index' 4import { VideoChannelModel } from '../../models/video/video-channel'
5import { VideoInstance } from '../../models/video/video-interface' 5import { VideoChannelShareModel } from '../../models/video/video-channel-share'
6import { sendVideoAnnounceToFollowers, sendVideoChannelAnnounceToFollowers } from './send/send-announce' 6import { VideoShareModel } from '../../models/video/video-share'
7import { sendVideoAnnounceToFollowers, sendVideoChannelAnnounceToFollowers } from './send'
7 8
8async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t: Transaction) { 9async function shareVideoChannelByServer (videoChannel: VideoChannelModel, t: Transaction) {
9 const serverAccount = await getServerAccount() 10 const serverAccount = await getServerAccount()
10 11
11 await db.VideoChannelShare.create({ 12 await VideoChannelShareModel.create({
12 accountId: serverAccount.id, 13 accountId: serverAccount.id,
13 videoChannelId: videoChannel.id 14 videoChannelId: videoChannel.id
14 }, { transaction: t }) 15 }, { transaction: t })
@@ -16,10 +17,10 @@ async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t:
16 return sendVideoChannelAnnounceToFollowers(serverAccount, videoChannel, t) 17 return sendVideoChannelAnnounceToFollowers(serverAccount, videoChannel, t)
17} 18}
18 19
19async function shareVideoByServer (video: VideoInstance, t: Transaction) { 20async function shareVideoByServer (video: VideoModel, t: Transaction) {
20 const serverAccount = await getServerAccount() 21 const serverAccount = await getServerAccount()
21 22
22 await db.VideoShare.create({ 23 await VideoShareModel.create({
23 accountId: serverAccount.id, 24 accountId: serverAccount.id,
24 videoId: video.id 25 videoId: video.id
25 }, { transaction: t }) 26 }, { 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 @@
1import { CONFIG } from '../../initializers/constants' 1import { CONFIG } from '../../initializers'
2import { VideoInstance } from '../../models/video/video-interface' 2import { AccountModel } from '../../models/account/account'
3import { VideoChannelInstance } from '../../models/video/video-channel-interface' 3import { AccountFollowModel } from '../../models/account/account-follow'
4import { VideoAbuseInstance } from '../../models/video/video-abuse-interface' 4import { VideoModel } from '../../models/video/video'
5import { AccountFollowInstance } from '../../models/account/account-follow-interface' 5import { VideoAbuseModel } from '../../models/video/video-abuse'
6import { AccountInstance } from '../../models/account/account-interface' 6import { VideoChannelModel } from '../../models/video/video-channel'
7 7
8function getVideoActivityPubUrl (video: VideoInstance) { 8function getVideoActivityPubUrl (video: VideoModel) {
9 return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid 9 return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
10} 10}
11 11
12function getVideoChannelActivityPubUrl (videoChannel: VideoChannelInstance) { 12function getVideoChannelActivityPubUrl (videoChannel: VideoChannelModel) {
13 return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid 13 return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid
14} 14}
15 15
@@ -17,37 +17,37 @@ function getAccountActivityPubUrl (accountName: string) {
17 return CONFIG.WEBSERVER.URL + '/account/' + accountName 17 return CONFIG.WEBSERVER.URL + '/account/' + accountName
18} 18}
19 19
20function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseInstance) { 20function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseModel) {
21 return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id 21 return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id
22} 22}
23 23
24function getVideoViewActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { 24function getVideoViewActivityPubUrl (byAccount: AccountModel, video: VideoModel) {
25 return video.url + '/views/' + byAccount.uuid + '/' + new Date().toISOString() 25 return video.url + '/views/' + byAccount.uuid + '/' + new Date().toISOString()
26} 26}
27 27
28function getVideoLikeActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { 28function getVideoLikeActivityPubUrl (byAccount: AccountModel, video: VideoModel) {
29 return byAccount.url + '/likes/' + video.id 29 return byAccount.url + '/likes/' + video.id
30} 30}
31 31
32function getVideoDislikeActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) { 32function getVideoDislikeActivityPubUrl (byAccount: AccountModel, video: VideoModel) {
33 return byAccount.url + '/dislikes/' + video.id 33 return byAccount.url + '/dislikes/' + video.id
34} 34}
35 35
36function getAccountFollowActivityPubUrl (accountFollow: AccountFollowInstance) { 36function getAccountFollowActivityPubUrl (accountFollow: AccountFollowModel) {
37 const me = accountFollow.AccountFollower 37 const me = accountFollow.AccountFollower
38 const following = accountFollow.AccountFollowing 38 const following = accountFollow.AccountFollowing
39 39
40 return me.url + '/follows/' + following.id 40 return me.url + '/follows/' + following.id
41} 41}
42 42
43function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowInstance) { 43function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowModel) {
44 const follower = accountFollow.AccountFollower 44 const follower = accountFollow.AccountFollower
45 const me = accountFollow.AccountFollowing 45 const me = accountFollow.AccountFollowing
46 46
47 return follower.url + '/accepts/follows/' + me.id 47 return follower.url + '/accepts/follows/' + me.id
48} 48}
49 49
50function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountInstance) { 50function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountModel) {
51 return originalUrl + '/announces/' + byAccount.id 51 return originalUrl + '/announces/' + byAccount.id
52} 52}
53 53
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 @@
1import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object' 1import { VideoChannelObject } from '../../../shared/models/activitypub/objects'
2import { isVideoChannelObjectValid } from '../../helpers/custom-validators/activitypub/video-channels' 2import { doRequest, logger } from '../../helpers'
3import { logger } from '../../helpers/logger' 3import { isVideoChannelObjectValid } from '../../helpers/custom-validators/activitypub'
4import { doRequest } from '../../helpers/requests' 4import { ACTIVITY_PUB } from '../../initializers'
5import { database as db } from '../../initializers' 5import { AccountModel } from '../../models/account/account'
6import { ACTIVITY_PUB } from '../../initializers/constants' 6import { VideoChannelModel } from '../../models/video/video-channel'
7import { AccountInstance } from '../../models/account/account-interface'
8import { videoChannelActivityObjectToDBAttributes } from './process/misc' 7import { videoChannelActivityObjectToDBAttributes } from './process/misc'
9 8
10async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) { 9async function getOrCreateVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) {
11 let videoChannel = await db.VideoChannel.loadByUrl(videoChannelUrl) 10 let videoChannel = await VideoChannelModel.loadByUrl(videoChannelUrl)
12 11
13 // We don't have this account in our database, fetch it on remote 12 // We don't have this account in our database, fetch it on remote
14 if (!videoChannel) { 13 if (!videoChannel) {
@@ -22,7 +21,7 @@ async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChan
22 return videoChannel 21 return videoChannel
23} 22}
24 23
25async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) { 24async function fetchRemoteVideoChannel (ownerAccount: AccountModel, videoChannelUrl: string) {
26 const options = { 25 const options = {
27 uri: videoChannelUrl, 26 uri: videoChannelUrl,
28 method: 'GET', 27 method: 'GET',
@@ -48,7 +47,7 @@ async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChan
48 } 47 }
49 48
50 const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount) 49 const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount)
51 const videoChannel = db.VideoChannel.build(videoChannelAttributes) 50 const videoChannel = new VideoChannelModel(videoChannelAttributes)
52 videoChannel.Account = ownerAccount 51 videoChannel.Account = ownerAccount
53 52
54 return videoChannel 53 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'
2import * as request from 'request' 2import * as request from 'request'
3import { Transaction } from 'sequelize' 3import { Transaction } from 'sequelize'
4import { ActivityIconObject } from '../../../shared/index' 4import { ActivityIconObject } from '../../../shared/index'
5import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' 5import { doRequest, doRequestAndSaveToFile } from '../../helpers'
6import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers/constants' 6import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers'
7import { AccountInstance } from '../../models/account/account-interface' 7import { AccountModel } from '../../models/account/account'
8import { VideoInstance } from '../../models/video/video-interface' 8import { VideoModel } from '../../models/video/video'
9import { sendLikeToOrigin } from './index'
10import { sendCreateDislikeToOrigin, sendCreateDislikeToVideoFollowers } from './send/send-create'
11import { sendLikeToVideoFollowers } from './send/send-like'
12import { 9import {
10 sendCreateDislikeToOrigin,
11 sendCreateDislikeToVideoFollowers,
12 sendLikeToOrigin,
13 sendLikeToVideoFollowers,
13 sendUndoDislikeToOrigin, 14 sendUndoDislikeToOrigin,
14 sendUndoDislikeToVideoFollowers, 15 sendUndoDislikeToVideoFollowers,
15 sendUndoLikeToOrigin, 16 sendUndoLikeToOrigin,
16 sendUndoLikeToVideoFollowers 17 sendUndoLikeToVideoFollowers
17} from './send/send-undo' 18} from './send'
18 19
19function fetchRemoteVideoPreview (video: VideoInstance) { 20function fetchRemoteVideoPreview (video: VideoModel) {
20 // FIXME: use url 21 // FIXME: use url
21 const host = video.VideoChannel.Account.Server.host 22 const host = video.VideoChannel.Account.Server.host
22 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) 23 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
@@ -24,7 +25,7 @@ function fetchRemoteVideoPreview (video: VideoInstance) {
24 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path) 25 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
25} 26}
26 27
27async function fetchRemoteVideoDescription (video: VideoInstance) { 28async function fetchRemoteVideoDescription (video: VideoModel) {
28 // FIXME: use url 29 // FIXME: use url
29 const host = video.VideoChannel.Account.Server.host 30 const host = video.VideoChannel.Account.Server.host
30 const path = video.getDescriptionPath() 31 const path = video.getDescriptionPath()
@@ -37,7 +38,7 @@ async function fetchRemoteVideoDescription (video: VideoInstance) {
37 return body.description ? body.description : '' 38 return body.description ? body.description : ''
38} 39}
39 40
40function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { 41function generateThumbnailFromUrl (video: VideoModel, icon: ActivityIconObject) {
41 const thumbnailName = video.getThumbnailName() 42 const thumbnailName = video.getThumbnailName()
42 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) 43 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
43 44
@@ -49,8 +50,8 @@ function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObjec
49} 50}
50 51
51async function sendVideoRateChangeToFollowers ( 52async function sendVideoRateChangeToFollowers (
52 account: AccountInstance, 53 account: AccountModel,
53 video: VideoInstance, 54 video: VideoModel,
54 likes: number, 55 likes: number,
55 dislikes: number, 56 dislikes: number,
56 t: Transaction 57 t: Transaction
@@ -69,8 +70,8 @@ async function sendVideoRateChangeToFollowers (
69} 70}
70 71
71async function sendVideoRateChangeToOrigin ( 72async function sendVideoRateChangeToOrigin (
72 account: AccountInstance, 73 account: AccountModel,
73 video: VideoInstance, 74 video: VideoModel,
74 likes: number, 75 likes: number,
75 dislikes: number, 76 dislikes: number,
76 t: Transaction 77 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 @@
1import * as asyncLRU from 'async-lru' 1import * as asyncLRU from 'async-lru'
2import { join } from 'path'
3import { createWriteStream } from 'fs' 2import { createWriteStream } from 'fs'
4 3import { join } from 'path'
5import { database as db, CONFIG, CACHE } from '../../initializers'
6import { logger, unlinkPromise } from '../../helpers' 4import { logger, unlinkPromise } from '../../helpers'
7import { VideoInstance } from '../../models' 5import { CACHE, CONFIG } from '../../initializers'
8import { fetchRemoteVideoPreview } from '../activitypub/videos' 6import { VideoModel } from '../../models/video/video'
7import { fetchRemoteVideoPreview } from '../activitypub'
9 8
10class VideosPreviewCache { 9class VideosPreviewCache {
11 10
@@ -43,7 +42,7 @@ class VideosPreviewCache {
43 } 42 }
44 43
45 private async loadPreviews (key: string) { 44 private async loadPreviews (key: string) {
46 const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(key) 45 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
47 if (!video) return undefined 46 if (!video) return undefined
48 47
49 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) 48 if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
@@ -53,7 +52,7 @@ class VideosPreviewCache {
53 return res 52 return res
54 } 53 }
55 54
56 private saveRemotePreviewAndReturnPath (video: VideoInstance) { 55 private saveRemotePreviewAndReturnPath (video: VideoModel) {
57 const req = fetchRemoteVideoPreview(video) 56 const req = fetchRemoteVideoPreview(video)
58 57
59 return new Promise<string>((res, rej) => { 58 return new Promise<string>((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 @@
1import { logger } from '../../../helpers' 1import { doRequest, logger } from '../../../helpers'
2import { doRequest } from '../../../helpers/requests'
3import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' 2import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
4 3
5async function process (payload: ActivityPubHttpPayload, jobId: number) { 4async 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 @@
1import { logger } from '../../../helpers' 1import { doRequest, logger } from '../../../helpers'
2import { doRequest } from '../../../helpers/requests' 2import { ACTIVITY_PUB } from '../../../initializers'
3import { ACTIVITY_PUB } from '../../../initializers/constants' 3import { processActivities } from '../../activitypub/process'
4import { processActivities } from '../../activitypub/process/process'
5import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' 4import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler'
6 5
7async function process (payload: ActivityPubHttpPayload, jobId: number) { 6async 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 @@
1import { JobCategory } from '../../../../shared' 1import { JobCategory } from '../../../../shared'
2import { buildSignedActivity } from '../../../helpers/activitypub' 2import { buildSignedActivity, logger } from '../../../helpers'
3import { logger } from '../../../helpers/logger' 3import { ACTIVITY_PUB } from '../../../initializers'
4import { ACTIVITY_PUB } from '../../../initializers/constants' 4import { AccountModel } from '../../../models/account/account'
5import { database as db } from '../../../initializers/database'
6import { JobHandler, JobScheduler } from '../job-scheduler' 5import { JobHandler, JobScheduler } from '../job-scheduler'
7 6
8import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' 7import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
@@ -46,7 +45,7 @@ async function computeBody (payload: ActivityPubHttpPayload) {
46 let body = payload.body 45 let body = payload.body
47 46
48 if (payload.signatureAccountId) { 47 if (payload.signatureAccountId) {
49 const accountSignature = await db.Account.load(payload.signatureAccountId) 48 const accountSignature = await AccountModel.load(payload.signatureAccountId)
50 if (!accountSignature) throw new Error('Unknown signature account id.') 49 if (!accountSignature) throw new Error('Unknown signature account id.')
51 body = await buildSignedActivity(accountSignature, payload.body) 50 body = await buildSignedActivity(accountSignature, payload.body)
52 } 51 }
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 @@
1import { logger } from '../../../helpers' 1import { doRequest, logger } from '../../../helpers'
2import { doRequest } from '../../../helpers/requests'
3import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' 2import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
4 3
5async function process (payload: ActivityPubHttpPayload, jobId: number) { 4async 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'
2import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
3import { JobCategory } from '../../../shared' 3import { JobCategory } from '../../../shared'
4import { logger } from '../../helpers' 4import { logger } from '../../helpers'
5import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' 5import { JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers'
6import { JobInstance } from '../../models' 6import { JobModel } from '../../models/job/job'
7 7
8export interface JobHandler<P, T> { 8export interface JobHandler<P, T> {
9 process (data: object, jobId: number): Promise<T> 9 process (data: object, jobId: number): Promise<T>
@@ -24,12 +24,12 @@ class JobScheduler<P, T> {
24 24
25 logger.info('Jobs scheduler %s activated.', this.jobCategory) 25 logger.info('Jobs scheduler %s activated.', this.jobCategory)
26 26
27 const jobsQueue = queue<JobInstance, JobQueueCallback>(this.processJob.bind(this)) 27 const jobsQueue = queue<JobModel, JobQueueCallback>(this.processJob.bind(this))
28 28
29 // Finish processing jobs from a previous start 29 // Finish processing jobs from a previous start
30 const state = JOB_STATES.PROCESSING 30 const state = JOB_STATES.PROCESSING
31 try { 31 try {
32 const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) 32 const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory)
33 33
34 this.enqueueJobs(jobsQueue, jobs) 34 this.enqueueJobs(jobsQueue, jobs)
35 } catch (err) { 35 } catch (err) {
@@ -45,7 +45,7 @@ class JobScheduler<P, T> {
45 45
46 const state = JOB_STATES.PENDING 46 const state = JOB_STATES.PENDING
47 try { 47 try {
48 const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) 48 const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory)
49 49
50 this.enqueueJobs(jobsQueue, jobs) 50 this.enqueueJobs(jobsQueue, jobs)
51 } catch (err) { 51 } catch (err) {
@@ -70,14 +70,14 @@ class JobScheduler<P, T> {
70 70
71 const options = { transaction } 71 const options = { transaction }
72 72
73 return db.Job.create(createQuery, options) 73 return JobModel.create(createQuery, options)
74 } 74 }
75 75
76 private enqueueJobs (jobsQueue: AsyncQueue<JobInstance>, jobs: JobInstance[]) { 76 private enqueueJobs (jobsQueue: AsyncQueue<JobModel>, jobs: JobModel[]) {
77 jobs.forEach(job => jobsQueue.push(job)) 77 jobs.forEach(job => jobsQueue.push(job))
78 } 78 }
79 79
80 private async processJob (job: JobInstance, callback: (err: Error) => void) { 80 private async processJob (job: JobModel, callback: (err: Error) => void) {
81 const jobHandler = this.jobHandlers[job.handlerName] 81 const jobHandler = this.jobHandlers[job.handlerName]
82 if (jobHandler === undefined) { 82 if (jobHandler === undefined) {
83 const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id 83 const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id
@@ -110,7 +110,7 @@ class JobScheduler<P, T> {
110 return callback(null) 110 return callback(null)
111 } 111 }
112 112
113 private async onJobError (jobHandler: JobHandler<P, T>, job: JobInstance, err: Error) { 113 private async onJobError (jobHandler: JobHandler<P, T>, job: JobModel, err: Error) {
114 job.state = JOB_STATES.ERROR 114 job.state = JOB_STATES.ERROR
115 115
116 try { 116 try {
@@ -121,7 +121,7 @@ class JobScheduler<P, T> {
121 } 121 }
122 } 122 }
123 123
124 private async onJobSuccess (jobHandler: JobHandler<P, T>, job: JobInstance, jobResult: T) { 124 private async onJobSuccess (jobHandler: JobHandler<P, T>, job: JobModel, jobResult: T) {
125 job.state = JOB_STATES.SUCCESS 125 job.state = JOB_STATES.SUCCESS
126 126
127 try { 127 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 @@
1import { JobCategory } from '../../../../shared' 1import { JobCategory } from '../../../../shared'
2import { VideoModel } from '../../../models/video/video'
2import { JobHandler, JobScheduler } from '../job-scheduler' 3import { JobHandler, JobScheduler } from '../job-scheduler'
4
3import * as videoFileOptimizer from './video-file-optimizer-handler' 5import * as videoFileOptimizer from './video-file-optimizer-handler'
4import * as videoFileTranscoder from './video-file-transcoder-handler' 6import * as videoFileTranscoder from './video-file-transcoder-handler'
5import { VideoInstance } from '../../../models/video/video-interface'
6 7
7type TranscodingJobPayload = { 8type TranscodingJobPayload = {
8 videoUUID: string 9 videoUUID: string
9 resolution?: number 10 resolution?: number
10} 11}
11const jobHandlers: { [ handlerName: string ]: JobHandler<TranscodingJobPayload, VideoInstance> } = { 12const jobHandlers: { [ handlerName: string ]: JobHandler<TranscodingJobPayload, VideoModel> } = {
12 videoFileOptimizer, 13 videoFileOptimizer,
13 videoFileTranscoder 14 videoFileTranscoder
14} 15}
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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { computeResolutionsToTranscode, logger } from '../../../helpers' 2import { computeResolutionsToTranscode, logger } from '../../../helpers'
3import { database as db } from '../../../initializers/database' 3import { sequelizeTypescript } from '../../../initializers'
4import { VideoInstance } from '../../../models' 4import { VideoModel } from '../../../models/video/video'
5import { sendAddVideo } from '../../activitypub/send/send-add' 5import { shareVideoByServer } from '../../activitypub'
6import { sendAddVideo } from '../../activitypub/send'
6import { JobScheduler } from '../job-scheduler' 7import { JobScheduler } from '../job-scheduler'
7import { TranscodingJobPayload } from './transcoding-job-scheduler' 8import { TranscodingJobPayload } from './transcoding-job-scheduler'
8import { shareVideoByServer } from '../../activitypub/share'
9 9
10async function process (data: TranscodingJobPayload, jobId: number) { 10async function process (data: TranscodingJobPayload, jobId: number) {
11 const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) 11 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
12 // No video, maybe deleted? 12 // No video, maybe deleted?
13 if (!video) { 13 if (!video) {
14 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) 14 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) {
25 return Promise.resolve() 25 return Promise.resolve()
26} 26}
27 27
28async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: JobScheduler<TranscodingJobPayload, VideoInstance>) { 28async function onSuccess (jobId: number, video: VideoModel, jobScheduler: JobScheduler<TranscodingJobPayload, VideoModel>) {
29 if (video === undefined) return undefined 29 if (video === undefined) return undefined
30 30
31 logger.info('Job %d is a success.', jobId) 31 logger.info('Job %d is a success.', jobId)
32 32
33 // Maybe the video changed in database, refresh it 33 // Maybe the video changed in database, refresh it
34 const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) 34 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
35 // Video does not exist anymore 35 // Video does not exist anymore
36 if (!videoDatabase) return undefined 36 if (!videoDatabase) return undefined
37 37
@@ -50,7 +50,7 @@ async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: Job
50 50
51 if (resolutionsEnabled.length !== 0) { 51 if (resolutionsEnabled.length !== 0) {
52 try { 52 try {
53 await db.sequelize.transaction(async t => { 53 await sequelizeTypescript.transaction(async t => {
54 const tasks: Bluebird<any>[] = [] 54 const tasks: Bluebird<any>[] = []
55 55
56 for (const resolution of resolutionsEnabled) { 56 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 @@
1import { VideoResolution } from '../../../../shared' 1import { VideoResolution } from '../../../../shared'
2import { logger } from '../../../helpers' 2import { logger } from '../../../helpers'
3import { database as db } from '../../../initializers/database' 3import { VideoModel } from '../../../models/video/video'
4import { VideoInstance } from '../../../models' 4import { sendUpdateVideo } from '../../activitypub/send'
5import { sendUpdateVideo } from '../../activitypub/send/send-update'
6 5
7async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { 6async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) {
8 const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) 7 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
9 // No video, maybe deleted? 8 // No video, maybe deleted?
10 if (!video) { 9 if (!video) {
11 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) 10 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) {
22 return Promise.resolve() 21 return Promise.resolve()
23} 22}
24 23
25async function onSuccess (jobId: number, video: VideoInstance) { 24async function onSuccess (jobId: number, video: VideoModel) {
26 if (video === undefined) return undefined 25 if (video === undefined) return undefined
27 26
28 logger.info('Job %d is a success.', jobId) 27 logger.info('Job %d is a success.', jobId)
29 28
30 // Maybe the video changed in database, refresh it 29 // Maybe the video changed in database, refresh it
31 const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) 30 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
32 // Video does not exist anymore 31 // Video does not exist anymore
33 if (!videoDatabase) return undefined 32 if (!videoDatabase) return undefined
34 33
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 @@
1import { OAuthClientInstance, UserInstance } from '../models'
2import { database as db } from '../initializers/database'
3import { logger } from '../helpers' 1import { logger } from '../helpers'
2import { UserModel } from '../models/account/user'
3import { OAuthClientModel } from '../models/oauth/oauth-client'
4import { OAuthTokenModel } from '../models/oauth/oauth-token'
4 5
5type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } 6type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
6 7
@@ -9,25 +10,25 @@ type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpires
9function getAccessToken (bearerToken: string) { 10function getAccessToken (bearerToken: string) {
10 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') 11 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
11 12
12 return db.OAuthToken.getByTokenAndPopulateUser(bearerToken) 13 return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken)
13} 14}
14 15
15function getClient (clientId: string, clientSecret: string) { 16function getClient (clientId: string, clientSecret: string) {
16 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') 17 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
17 18
18 return db.OAuthClient.getByIdAndSecret(clientId, clientSecret) 19 return OAuthClientModel.getByIdAndSecret(clientId, clientSecret)
19} 20}
20 21
21function getRefreshToken (refreshToken: string) { 22function getRefreshToken (refreshToken: string) {
22 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') 23 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
23 24
24 return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) 25 return OAuthTokenModel.getByRefreshTokenAndPopulateClient(refreshToken)
25} 26}
26 27
27async function getUser (username: string, password: string) { 28async function getUser (username: string, password: string) {
28 logger.debug('Getting User (username: ' + username + ', password: ******).') 29 logger.debug('Getting User (username: ' + username + ', password: ******).')
29 30
30 const user = await db.User.getByUsername(username) 31 const user = await UserModel.getByUsername(username)
31 if (!user) return null 32 if (!user) return null
32 33
33 const passwordMatch = await user.isPasswordMatch(password) 34 const passwordMatch = await user.isPasswordMatch(password)
@@ -37,7 +38,7 @@ async function getUser (username: string, password: string) {
37} 38}
38 39
39async function revokeToken (tokenInfo: TokenInfo) { 40async function revokeToken (tokenInfo: TokenInfo) {
40 const token = await db.OAuthToken.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) 41 const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken)
41 if (token) token.destroy() 42 if (token) token.destroy()
42 43
43 /* 44 /*
@@ -53,7 +54,7 @@ async function revokeToken (tokenInfo: TokenInfo) {
53 return expiredToken 54 return expiredToken
54} 55}
55 56
56async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: UserInstance) { 57async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) {
57 logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') 58 logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
58 59
59 const tokenToCreate = { 60 const tokenToCreate = {
@@ -65,7 +66,7 @@ async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: U
65 userId: user.id 66 userId: user.id
66 } 67 }
67 68
68 const tokenCreated = await db.OAuthToken.create(tokenToCreate) 69 const tokenCreated = await OAuthTokenModel.create(tokenToCreate)
69 const tokenToReturn = Object.assign(tokenCreated, { client, user }) 70 const tokenToReturn = Object.assign(tokenCreated, { client, user })
70 71
71 return tokenToReturn 72 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 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto' 2import { createPrivateAndPublicKeys, logger } from '../helpers'
3import { database as db } from '../initializers' 3import { CONFIG, sequelizeTypescript } from '../initializers'
4import { CONFIG } from '../initializers/constants' 4import { AccountModel } from '../models/account/account'
5import { UserInstance } from '../models' 5import { UserModel } from '../models/account/user'
6import { getAccountActivityPubUrl } from './activitypub'
6import { createVideoChannel } from './video-channel' 7import { createVideoChannel } from './video-channel'
7import { logger } from '../helpers/logger'
8import { getAccountActivityPubUrl } from './activitypub/url'
9 8
10async function createUserAccountAndChannel (user: UserInstance, validateUser = true) { 9async function createUserAccountAndChannel (user: UserModel, validateUser = true) {
11 const { account, videoChannel } = await db.sequelize.transaction(async t => { 10 const { account, videoChannel } = await sequelizeTypescript.transaction(async t => {
12 const userOptions = { 11 const userOptions = {
13 transaction: t, 12 transaction: t,
14 validate: validateUser 13 validate: validateUser
@@ -38,7 +37,7 @@ async function createUserAccountAndChannel (user: UserInstance, validateUser = t
38async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) { 37async function createLocalAccountWithoutKeys (name: string, userId: number, applicationId: number, t: Sequelize.Transaction) {
39 const url = getAccountActivityPubUrl(name) 38 const url = getAccountActivityPubUrl(name)
40 39
41 const accountInstance = db.Account.build({ 40 const accountInstance = new AccountModel({
42 name, 41 name,
43 url, 42 url,
44 publicKey: null, 43 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 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { VideoChannelCreate } from '../../shared/models' 2import { VideoChannelCreate } from '../../shared/models'
3import { database as db } from '../initializers' 3import { AccountModel } from '../models/account/account'
4import { AccountInstance } from '../models' 4import { VideoChannelModel } from '../models/video/video-channel'
5import { getVideoChannelActivityPubUrl } from './activitypub/url' 5import { getVideoChannelActivityPubUrl } from './activitypub'
6 6
7async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) { 7async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountModel, t: Sequelize.Transaction) {
8 const videoChannelData = { 8 const videoChannelData = {
9 name: videoChannelInfo.name, 9 name: videoChannelInfo.name,
10 description: videoChannelInfo.description, 10 description: videoChannelInfo.description,
@@ -12,7 +12,7 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account
12 accountId: account.id 12 accountId: account.id
13 } 13 }
14 14
15 const videoChannel = db.VideoChannel.build(videoChannelData) 15 const videoChannel = VideoChannelModel.build(videoChannelData)
16 videoChannel.set('url', getVideoChannelActivityPubUrl(videoChannel)) 16 videoChannel.set('url', getVideoChannelActivityPubUrl(videoChannel))
17 17
18 const options = { transaction: t } 18 const options = { transaction: t }