]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-rates.ts
Don't inject untrusted input
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
1 import { Transaction } from 'sequelize'
2 import { VideoRateType } from '../../../shared/models/videos'
3 import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models'
4 import { sendLike, sendUndoDislike, sendUndoLike } from './send'
5 import { sendDislike } from './send/send-dislike'
6 import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
7 import { federateVideoIfNeeded } from './videos'
8
9 async function sendVideoRateChange (
10 account: MAccountActor,
11 video: MVideoFullLight,
12 likes: number,
13 dislikes: number,
14 t: Transaction
15 ) {
16 if (video.isOwned()) return federateVideoIfNeeded(video, false, t)
17
18 return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t)
19 }
20
21 function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
22 return rateType === 'like'
23 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
24 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
25 }
26
27 // ---------------------------------------------------------------------------
28
29 export {
30 getLocalRateUrl,
31 sendVideoRateChange
32 }
33
34 // ---------------------------------------------------------------------------
35
36 async function sendVideoRateChangeToOrigin (
37 account: MAccountActor,
38 video: MVideoAccountLight,
39 likes: number,
40 dislikes: number,
41 t: Transaction
42 ) {
43 // Local video, we don't need to send like
44 if (video.isOwned()) return
45
46 const actor = account.Actor
47
48 // Keep the order: first we undo and then we create
49
50 // Undo Like
51 if (likes < 0) await sendUndoLike(actor, video, t)
52 // Undo Dislike
53 if (dislikes < 0) await sendUndoDislike(actor, video, t)
54
55 // Like
56 if (likes > 0) await sendLike(actor, video, t)
57 // Dislike
58 if (dislikes > 0) await sendDislike(actor, video, t)
59 }