1 import { Transaction } from 'sequelize'
2 import { AccountModel } from '../../models/account/account'
3 import { VideoModel } from '../../models/video/video'
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'
12 async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
15 await Bluebird.map(actorUrls, async actorUrl => {
17 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
18 const [ , created ] = await AccountVideoRateModel
22 accountId: actor.Account.id
26 accountId: actor.Account.id,
31 if (created) rateCounts += 1
33 logger.warn('Cannot add rate %s for actor %s.', rate, actorUrl, { err })
35 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
37 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
39 // This is "likes" and "dislikes"
40 if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts })
45 async function sendVideoRateChange (account: AccountModel,
50 const actor = account.Actor
52 // Keep the order: first we undo and then we create
55 if (likes < 0) await sendUndoLike(actor, video, t)
57 if (dislikes < 0) await sendUndoDislike(actor, video, t)
60 if (likes > 0) await sendLike(actor, video, t)
62 if (dislikes > 0) await sendCreateDislike(actor, video, t)