]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-rates.ts
Fix control bar alignment
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
CommitLineData
2ccaeeb3 1import { Transaction } from 'sequelize'
1e7eb25f 2import { sendLike, sendUndoDislike, sendUndoLike } from './send'
1297eb5d
C
3import { VideoRateType } from '../../../shared/models/videos'
4import * as Bluebird from 'bluebird'
5import { getOrCreateActorAndServerAndModel } from './actor'
6import { AccountVideoRateModel } from '../../models/account/account-video-rate'
7import { logger } from '../../helpers/logger'
74dc3bca 8import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
5c6d985f 9import { doRequest } from '../../helpers/requests'
848f499d 10import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
de94ac86 11import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
1e7eb25f 12import { sendDislike } from './send/send-dislike'
26d6bf65 13import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
1297eb5d 14
453e83ea 15async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
5c6d985f 16 await Bluebird.map(ratesUrl, async rateUrl => {
1297eb5d 17 try {
5c6d985f 18 // Fetch url
366caf8b 19 const { body } = await doRequest<any>({
5c6d985f
C
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
848f499d 26 const actorUrl = getAPId(body.actor)
5c6d985f
C
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
1297eb5d 35 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
5c6d985f 36
2ba92871
C
37 const entry = {
38 videoId: video.id,
39 accountId: actor.Account.id,
40 type: rate,
41 url: body.id
42 }
43
b49f22d8
C
44 // Video "likes"/"dislikes" will be updated by the caller
45 await AccountVideoRateModel.upsert(entry)
1297eb5d 46 } catch (err) {
5c6d985f 47 logger.warn('Cannot add rate %s.', rateUrl, { err })
1297eb5d
C
48 }
49 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
1297eb5d 50}
2ccaeeb3 51
453e83ea
C
52async function sendVideoRateChange (
53 account: MAccountActor,
54 video: MVideoAccountLight,
55 likes: number,
56 dislikes: number,
57 t: Transaction
58) {
2ccaeeb3
C
59 const actor = account.Actor
60
61 // Keep the order: first we undo and then we create
62
63 // Undo Like
07197db4 64 if (likes < 0) await sendUndoLike(actor, video, t)
2ccaeeb3 65 // Undo Dislike
07197db4 66 if (dislikes < 0) await sendUndoDislike(actor, video, t)
2ccaeeb3
C
67
68 // Like
07197db4 69 if (likes > 0) await sendLike(actor, video, t)
2ccaeeb3 70 // Dislike
1e7eb25f 71 if (dislikes > 0) await sendDislike(actor, video, t)
2ccaeeb3
C
72}
73
de94ac86 74function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
453e83ea 75 return rateType === 'like'
de94ac86
C
76 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
77 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
5c6d985f
C
78}
79
2ccaeeb3 80export {
de94ac86 81 getLocalRateUrl,
1297eb5d 82 createRates,
07197db4 83 sendVideoRateChange
2ccaeeb3 84}