diff options
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r-- | server/lib/activitypub/video-rates.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index 19011b4ab..1619251c3 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts | |||
@@ -2,6 +2,45 @@ import { Transaction } from 'sequelize' | |||
2 | import { AccountModel } from '../../models/account/account' | 2 | import { AccountModel } from '../../models/account/account' |
3 | import { VideoModel } from '../../models/video/video' | 3 | import { VideoModel } from '../../models/video/video' |
4 | import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send' | 4 | import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send' |
5 | import { VideoRateType } from '../../../shared/models/videos' | ||
6 | import * as Bluebird from 'bluebird' | ||
7 | import { getOrCreateActorAndServerAndModel } from './actor' | ||
8 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | ||
9 | import { logger } from '../../helpers/logger' | ||
10 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers' | ||
11 | |||
12 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | ||
13 | let rateCounts = 0 | ||
14 | |||
15 | await Bluebird.map(actorUrls, async actorUrl => { | ||
16 | try { | ||
17 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
18 | const [ , created ] = await AccountVideoRateModel | ||
19 | .findOrCreate({ | ||
20 | where: { | ||
21 | videoId: video.id, | ||
22 | accountId: actor.Account.id | ||
23 | }, | ||
24 | defaults: { | ||
25 | videoId: video.id, | ||
26 | accountId: actor.Account.id, | ||
27 | type: rate | ||
28 | } | ||
29 | }) | ||
30 | |||
31 | if (created) rateCounts += 1 | ||
32 | } catch (err) { | ||
33 | logger.warn('Cannot add rate %s for actor %s.', rate, actorUrl, { err }) | ||
34 | } | ||
35 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | ||
36 | |||
37 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | ||
38 | |||
39 | // This is "likes" and "dislikes" | ||
40 | if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts }) | ||
41 | |||
42 | return | ||
43 | } | ||
5 | 44 | ||
6 | async function sendVideoRateChange (account: AccountModel, | 45 | async function sendVideoRateChange (account: AccountModel, |
7 | video: VideoModel, | 46 | video: VideoModel, |
@@ -24,5 +63,6 @@ async function sendVideoRateChange (account: AccountModel, | |||
24 | } | 63 | } |
25 | 64 | ||
26 | export { | 65 | export { |
66 | createRates, | ||
27 | sendVideoRateChange | 67 | sendVideoRateChange |
28 | } | 68 | } |