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