diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-23 16:55:13 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:53 +0100 |
commit | 16b90975941b78d01d7202d441bf731a10048c16 (patch) | |
tree | 11709f51ec79785f32282dfbdd33277af483b2e6 /server/lib/activitypub/process/process-add.ts | |
parent | d8553faa4939889fa7b7ef7329aa474a81cbbdb9 (diff) | |
download | PeerTube-16b90975941b78d01d7202d441bf731a10048c16.tar.gz PeerTube-16b90975941b78d01d7202d441bf731a10048c16.tar.zst PeerTube-16b90975941b78d01d7202d441bf731a10048c16.zip |
Fetch video likes/dislikes too
Diffstat (limited to 'server/lib/activitypub/process/process-add.ts')
-rw-r--r-- | server/lib/activitypub/process/process-add.ts | 51 |
1 files changed, 48 insertions, 3 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 | } | ||