diff options
Diffstat (limited to 'server/lib/activitypub/process/process-add.ts')
-rw-r--r-- | server/lib/activitypub/process/process-add.ts | 42 |
1 files changed, 22 insertions, 20 deletions
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 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { VideoTorrentObject } from '../../../../shared' | 2 | import { VideoTorrentObject } from '../../../../shared' |
3 | import { ActivityAdd } from '../../../../shared/models/activitypub/activity' | 3 | import { ActivityAdd } from '../../../../shared/models/activitypub' |
4 | import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' | 4 | import { VideoRateType } from '../../../../shared/models/videos' |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 5 | import { logger, retryTransactionWrapper } from '../../../helpers' |
6 | import { logger } from '../../../helpers/logger' | 6 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { database as db } from '../../../initializers' | 7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountInstance } from '../../../models/account/account-interface' | 8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 9 | import { TagModel } from '../../../models/video/tag' |
10 | import { VideoInstance } from '../../../models/video/video-interface' | 10 | import { VideoModel } from '../../../models/video/video' |
11 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
12 | import { VideoFileModel } from '../../../models/video/video-file' | ||
11 | import { getOrCreateAccountAndServer } from '../account' | 13 | import { getOrCreateAccountAndServer } from '../account' |
12 | import { getOrCreateVideoChannel } from '../video-channels' | 14 | import { getOrCreateVideoChannel } from '../video-channels' |
13 | import { generateThumbnailFromUrl } from '../videos' | 15 | import { generateThumbnailFromUrl } from '../videos' |
@@ -37,9 +39,9 @@ export { | |||
37 | 39 | ||
38 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
39 | 41 | ||
40 | async function processAddVideo (account: AccountInstance, | 42 | async 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 | ||
67 | function addRemoteVideo (account: AccountInstance, | 69 | function 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 | ||
110 | async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { | 112 | async 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, |