]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-rates.ts
Fix user subscription follows count
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
CommitLineData
41fb13c3 1import { map } 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'
5e08989e 6import { logger, loggerTagsFactory } 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'
136d7efd 10import { getOrCreateAPActor } from './actors'
db4b15f2
C
11import { sendLike, sendUndoDislike, sendUndoLike } from './send'
12import { sendDislike } from './send/send-dislike'
13import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
1297eb5d 14
5e08989e
C
15const lTags = loggerTagsFactory('ap', 'video-rate', 'create')
16
453e83ea 17async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
41fb13c3 18 await map(ratesUrl, async rateUrl => {
1297eb5d 19 try {
49af5ac8 20 await createRate(rateUrl, video, rate)
1297eb5d 21 } catch (err) {
7ffa92d6 22 logger.info('Cannot add rate %s.', rateUrl, { err, ...lTags(rateUrl, video.uuid, video.url) })
1297eb5d
C
23 }
24 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
1297eb5d 25}
2ccaeeb3 26
453e83ea
C
27async function sendVideoRateChange (
28 account: MAccountActor,
29 video: MVideoAccountLight,
30 likes: number,
31 dislikes: number,
32 t: Transaction
33) {
2ccaeeb3
C
34 const actor = account.Actor
35
36 // Keep the order: first we undo and then we create
37
38 // Undo Like
07197db4 39 if (likes < 0) await sendUndoLike(actor, video, t)
2ccaeeb3 40 // Undo Dislike
07197db4 41 if (dislikes < 0) await sendUndoDislike(actor, video, t)
2ccaeeb3
C
42
43 // Like
07197db4 44 if (likes > 0) await sendLike(actor, video, t)
2ccaeeb3 45 // Dislike
1e7eb25f 46 if (dislikes > 0) await sendDislike(actor, video, t)
2ccaeeb3
C
47}
48
de94ac86 49function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
453e83ea 50 return rateType === 'like'
de94ac86
C
51 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
52 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
5c6d985f
C
53}
54
49af5ac8
C
55// ---------------------------------------------------------------------------
56
2ccaeeb3 57export {
de94ac86 58 getLocalRateUrl,
1297eb5d 59 createRates,
07197db4 60 sendVideoRateChange
2ccaeeb3 61}
49af5ac8
C
62
63// ---------------------------------------------------------------------------
64
65async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) {
66 // Fetch url
67 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true })
68 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
69
70 const actorUrl = getAPId(body.actor)
71 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
72 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
73 }
74
75 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
76 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
77 }
78
136d7efd 79 const actor = await getOrCreateAPActor(actorUrl)
49af5ac8
C
80
81 const entry = {
82 videoId: video.id,
83 accountId: actor.Account.id,
84 type: rate,
85 url: body.id
86 }
87
88 // Video "likes"/"dislikes" will be updated by the caller
89 await AccountVideoRateModel.upsert(entry)
90}