1 import * as Bluebird from 'bluebird'
2 import { Transaction } from 'sequelize'
3 import { doJSONRequest } from '@server/helpers/requests'
4 import { VideoRateType } from '../../../shared/models/videos'
5 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
6 import { logger, loggerTagsFactory } from '../../helpers/logger'
7 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
8 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
9 import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
10 import { getOrCreateAPActor } from './actors'
11 import { sendLike, sendUndoDislike, sendUndoLike } from './send'
12 import { sendDislike } from './send/send-dislike'
13 import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
15 const lTags = loggerTagsFactory('ap', 'video-rate', 'create')
17 async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
18 await Bluebird.map(ratesUrl, async rateUrl => {
20 await createRate(rateUrl, video, rate)
22 logger.info('Cannot add rate %s.', rateUrl, { err, ...lTags(rateUrl, video.uuid, video.url) })
24 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
27 async function sendVideoRateChange (
28 account: MAccountActor,
29 video: MVideoAccountLight,
34 const actor = account.Actor
36 // Keep the order: first we undo and then we create
39 if (likes < 0) await sendUndoLike(actor, video, t)
41 if (dislikes < 0) await sendUndoDislike(actor, video, t)
44 if (likes > 0) await sendLike(actor, video, t)
46 if (dislikes > 0) await sendDislike(actor, video, t)
49 function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
50 return rateType === 'like'
51 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
52 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
55 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) {
67 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true })
68 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
70 const actorUrl = getAPId(body.actor)
71 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
72 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
75 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
76 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
79 const actor = await getOrCreateAPActor(actorUrl)
83 accountId: actor.Account.id,
88 // Video "likes"/"dislikes" will be updated by the caller
89 await AccountVideoRateModel.upsert(entry)