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