diff options
Diffstat (limited to 'server/lib')
3 files changed, 54 insertions, 5 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 @@ | |||
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/activity' |
4 | import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' | ||
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | 6 | import { logger } from '../../../helpers/logger' |
6 | import { database as db } from '../../../initializers' | 7 | import { database as db } from '../../../initializers' |
7 | import { AccountInstance } from '../../../models/account/account-interface' | 8 | import { AccountInstance } from '../../../models/account/account-interface' |
8 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' | 9 | import { VideoChannelInstance } from '../../../models/video/video-channel-interface' |
10 | import { VideoInstance } from '../../../models/video/video-interface' | ||
9 | import { getOrCreateAccountAndServer } from '../account' | 11 | import { getOrCreateAccountAndServer } from '../account' |
10 | import { getOrCreateVideoChannel } from '../video-channels' | 12 | import { getOrCreateVideoChannel } from '../video-channels' |
11 | import { generateThumbnailFromUrl } from '../videos' | 13 | import { generateThumbnailFromUrl } from '../videos' |
@@ -35,13 +37,29 @@ export { | |||
35 | 37 | ||
36 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
37 | 39 | ||
38 | function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) { | 40 | async 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 | ||
47 | function addRemoteVideo (account: AccountInstance, | 65 | function addRemoteVideo (account: AccountInstance, |
@@ -86,3 +104,30 @@ function addRemoteVideo (account: AccountInstance, | |||
86 | return videoCreated | 104 | return videoCreated |
87 | }) | 105 | }) |
88 | } | 106 | } |
107 | |||
108 | async 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 | ||
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 bda319592..9adceab84 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 | |||
@@ -49,7 +49,7 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | function onError (err: Error, jobId: number) { | 51 | function onError (err: Error, jobId: number) { |
52 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | 52 | logger.error('Error when fetcher ActivityPub request in job %d.', jobId, err) |
53 | return Promise.resolve() | 53 | return Promise.resolve() |
54 | } | 54 | } |
55 | 55 | ||