aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/process/process-add.ts51
-rw-r--r--server/lib/activitypub/process/process.ts6
2 files changed, 53 insertions, 4 deletions
diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts
index 332c18cc0..433e68eb6 100644
--- a/server/lib/activitypub/process/process-add.ts
+++ b/server/lib/activitypub/process/process-add.ts
@@ -1,11 +1,13 @@
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/activity'
4import { VideoRateType } from '../../../../shared/models/videos/video-rate.type'
4import { retryTransactionWrapper } from '../../../helpers/database-utils' 5import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger' 6import { logger } from '../../../helpers/logger'
6import { database as db } from '../../../initializers' 7import { database as db } from '../../../initializers'
7import { AccountInstance } from '../../../models/account/account-interface' 8import { AccountInstance } from '../../../models/account/account-interface'
8import { VideoChannelInstance } from '../../../models/video/video-channel-interface' 9import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
10import { VideoInstance } from '../../../models/video/video-interface'
9import { getOrCreateAccountAndServer } from '../account' 11import { getOrCreateAccountAndServer } from '../account'
10import { getOrCreateVideoChannel } from '../video-channels' 12import { getOrCreateVideoChannel } from '../video-channels'
11import { generateThumbnailFromUrl } from '../videos' 13import { generateThumbnailFromUrl } from '../videos'
@@ -35,13 +37,29 @@ export {
35 37
36// --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
37 39
38function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) { 40async function processAddVideo (
41 account: AccountInstance,
42 activity: ActivityAdd,
43 videoChannel: VideoChannelInstance,
44 videoToCreateData: VideoTorrentObject
45) {
39 const options = { 46 const options = {
40 arguments: [ account, activity, videoChannel, video ], 47 arguments: [ account, activity, videoChannel, videoToCreateData ],
41 errorMessage: 'Cannot insert the remote video with many retries.' 48 errorMessage: 'Cannot insert the remote video with many retries.'
42 } 49 }
43 50
44 return retryTransactionWrapper(addRemoteVideo, options) 51 const video = await retryTransactionWrapper(addRemoteVideo, options)
52
53 // Process outside the transaction because we could fetch remote data
54 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
55 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
56 }
57
58 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
59 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
60 }
61
62 return video
45} 63}
46 64
47function addRemoteVideo (account: AccountInstance, 65function addRemoteVideo (account: AccountInstance,
@@ -86,3 +104,30 @@ function addRemoteVideo (account: AccountInstance,
86 return videoCreated 104 return videoCreated
87 }) 105 })
88} 106}
107
108async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
109 let rateCounts = 0
110 const tasks: Bluebird<any>[] = []
111
112 for (const accountUrl of accountUrls) {
113 const account = await getOrCreateAccountAndServer(accountUrl)
114 const p = db.AccountVideoRate
115 .create({
116 videoId: video.id,
117 accountId: account.id,
118 type: rate
119 })
120 .then(() => rateCounts += 1)
121
122 tasks.push(p)
123 }
124
125 await Promise.all(tasks)
126
127 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
128
129 // This is "likes" and "dislikes"
130 await video.increment(rate + 's', { by: rateCounts })
131
132 return
133}
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts
index 942bce0e6..40f19c701 100644
--- a/server/lib/activitypub/process/process.ts
+++ b/server/lib/activitypub/process/process.ts
@@ -31,7 +31,11 @@ async function processActivities (activities: Activity[], inboxAccount?: Account
31 continue 31 continue
32 } 32 }
33 33
34 await activityProcessor(activity, inboxAccount) 34 try {
35 await activityProcessor(activity, inboxAccount)
36 } catch (err) {
37 logger.warn('Cannot process activity %s.', activity.type, err)
38 }
35 } 39 }
36} 40}
37 41