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