Split types and typings
[oweals/peertube.git] / server / lib / activitypub / video-rates.ts
1 import { Transaction } from 'sequelize'
2 import { sendLike, sendUndoDislike, sendUndoLike } from './send'
3 import { VideoRateType } from '../../../shared/models/videos'
4 import * as Bluebird from 'bluebird'
5 import { getOrCreateActorAndServerAndModel } from './actor'
6 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
7 import { logger } from '../../helpers/logger'
8 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
9 import { doRequest } from '../../helpers/requests'
10 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
11 import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url'
12 import { sendDislike } from './send/send-dislike'
13 import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
14
15 async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
16   let rateCounts = 0
17
18   await Bluebird.map(ratesUrl, async rateUrl => {
19     try {
20       // Fetch url
21       const { body } = await doRequest<any>({
22         uri: rateUrl,
23         json: true,
24         activityPub: true
25       })
26       if (!body || !body.actor) throw new Error('Body or body actor is invalid')
27
28       const actorUrl = getAPId(body.actor)
29       if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
30         throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
31       }
32
33       if (checkUrlsSameHost(body.id, rateUrl) !== true) {
34         throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
35       }
36
37       const actor = await getOrCreateActorAndServerAndModel(actorUrl)
38
39       const entry = {
40         videoId: video.id,
41         accountId: actor.Account.id,
42         type: rate,
43         url: body.id
44       }
45
46       const created = await AccountVideoRateModel.upsert(entry)
47
48       if (created) rateCounts += 1
49     } catch (err) {
50       logger.warn('Cannot add rate %s.', rateUrl, { err })
51     }
52   }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
53
54   logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
55
56   // This is "likes" and "dislikes"
57   if (rateCounts !== 0) {
58     const field = rate === 'like' ? 'likes' : 'dislikes'
59     await video.increment(field, { by: rateCounts })
60   }
61 }
62
63 async function sendVideoRateChange (
64   account: MAccountActor,
65   video: MVideoAccountLight,
66   likes: number,
67   dislikes: number,
68   t: Transaction
69 ) {
70   const actor = account.Actor
71
72   // Keep the order: first we undo and then we create
73
74   // Undo Like
75   if (likes < 0) await sendUndoLike(actor, video, t)
76   // Undo Dislike
77   if (dislikes < 0) await sendUndoDislike(actor, video, t)
78
79   // Like
80   if (likes > 0) await sendLike(actor, video, t)
81   // Dislike
82   if (dislikes > 0) await sendDislike(actor, video, t)
83 }
84
85 function getRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
86   return rateType === 'like'
87     ? getVideoLikeActivityPubUrl(actor, video)
88     : getVideoDislikeActivityPubUrl(actor, video)
89 }
90
91 export {
92   getRateUrl,
93   createRates,
94   sendVideoRateChange
95 }