]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-rates.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
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 } 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 { getOrCreateActorAndServerAndModel } from './actor'
11 import { sendLike, sendUndoDislike, sendUndoLike } from './send'
12 import { sendDislike } from './send/send-dislike'
13 import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
14
15 async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
16 await Bluebird.map(ratesUrl, async rateUrl => {
17 try {
18 // Fetch url
19 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true })
20 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
21
22 const actorUrl = getAPId(body.actor)
23 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
24 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
25 }
26
27 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
28 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
29 }
30
31 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
32
33 const entry = {
34 videoId: video.id,
35 accountId: actor.Account.id,
36 type: rate,
37 url: body.id
38 }
39
40 // Video "likes"/"dislikes" will be updated by the caller
41 await AccountVideoRateModel.upsert(entry)
42 } catch (err) {
43 logger.warn('Cannot add rate %s.', rateUrl, { err })
44 }
45 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
46 }
47
48 async function sendVideoRateChange (
49 account: MAccountActor,
50 video: MVideoAccountLight,
51 likes: number,
52 dislikes: number,
53 t: Transaction
54 ) {
55 const actor = account.Actor
56
57 // Keep the order: first we undo and then we create
58
59 // Undo Like
60 if (likes < 0) await sendUndoLike(actor, video, t)
61 // Undo Dislike
62 if (dislikes < 0) await sendUndoDislike(actor, video, t)
63
64 // Like
65 if (likes > 0) await sendLike(actor, video, t)
66 // Dislike
67 if (dislikes > 0) await sendDislike(actor, video, t)
68 }
69
70 function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
71 return rateType === 'like'
72 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
73 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
74 }
75
76 export {
77 getLocalRateUrl,
78 createRates,
79 sendVideoRateChange
80 }