]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-rates.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
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'
11 import { doRequest } from '../../helpers/requests'
12 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
13 import { ActorModel } from '../../models/activitypub/actor'
14 import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url'
15
16 async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) {
17 let rateCounts = 0
18
19 await Bluebird.map(ratesUrl, async rateUrl => {
20 try {
21 // Fetch url
22 const { body } = await doRequest({
23 uri: rateUrl,
24 json: true,
25 activityPub: true
26 })
27 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
28
29 const actorUrl = getAPId(body.actor)
30 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
31 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
32 }
33
34 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
35 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
36 }
37
38 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
39
40 const [ , created ] = await AccountVideoRateModel
41 .findOrCreate({
42 where: {
43 videoId: video.id,
44 accountId: actor.Account.id
45 },
46 defaults: {
47 videoId: video.id,
48 accountId: actor.Account.id,
49 type: rate,
50 url: body.id
51 }
52 })
53
54 if (created) rateCounts += 1
55 } catch (err) {
56 logger.warn('Cannot add rate %s.', rateUrl, { err })
57 }
58 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
59
60 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
61
62 // This is "likes" and "dislikes"
63 if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts })
64
65 return
66 }
67
68 async function sendVideoRateChange (account: AccountModel,
69 video: VideoModel,
70 likes: number,
71 dislikes: number,
72 t: Transaction) {
73 const actor = account.Actor
74
75 // Keep the order: first we undo and then we create
76
77 // Undo Like
78 if (likes < 0) await sendUndoLike(actor, video, t)
79 // Undo Dislike
80 if (dislikes < 0) await sendUndoDislike(actor, video, t)
81
82 // Like
83 if (likes > 0) await sendLike(actor, video, t)
84 // Dislike
85 if (dislikes > 0) await sendCreateDislike(actor, video, t)
86 }
87
88 function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) {
89 return rateType === 'like' ? getVideoLikeActivityPubUrl(actor, video) : getVideoDislikeActivityPubUrl(actor, video)
90 }
91
92 export {
93 getRateUrl,
94 createRates,
95 sendVideoRateChange
96 }