]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-rates.ts
Fix checkbox margins
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
CommitLineData
2ccaeeb3
C
1import { Transaction } from 'sequelize'
2import { AccountModel } from '../../models/account/account'
3import { VideoModel } from '../../models/video/video'
07197db4 4import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send'
1297eb5d
C
5import { VideoRateType } from '../../../shared/models/videos'
6import * as Bluebird from 'bluebird'
7import { getOrCreateActorAndServerAndModel } from './actor'
8import { AccountVideoRateModel } from '../../models/account/account-video-rate'
9import { logger } from '../../helpers/logger'
10import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
5c6d985f 11import { doRequest } from '../../helpers/requests'
361805c4 12import { checkUrlsSameHost, getAPUrl } from '../../helpers/activitypub'
5c6d985f
C
13import { ActorModel } from '../../models/activitypub/actor'
14import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url'
1297eb5d 15
5c6d985f 16async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) {
1297eb5d
C
17 let rateCounts = 0
18
5c6d985f 19 await Bluebird.map(ratesUrl, async rateUrl => {
1297eb5d 20 try {
5c6d985f
C
21 // Fetch url
22 const { body } = await doRequest({
23 uri: rateUrl,
24 json: true,
25 activityPub: true
26 })
27 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
28
361805c4 29 const actorUrl = getAPUrl(body.actor)
5c6d985f
C
30 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
31 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
32 }
33
34 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
35 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
36 }
37
1297eb5d 38 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
5c6d985f 39
1297eb5d
C
40 const [ , created ] = await AccountVideoRateModel
41 .findOrCreate({
42 where: {
43 videoId: video.id,
44 accountId: actor.Account.id
45 },
46 defaults: {
47 videoId: video.id,
48 accountId: actor.Account.id,
5c6d985f
C
49 type: rate,
50 url: body.id
1297eb5d
C
51 }
52 })
53
54 if (created) rateCounts += 1
55 } catch (err) {
5c6d985f 56 logger.warn('Cannot add rate %s.', rateUrl, { err })
1297eb5d
C
57 }
58 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
59
60 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
61
62 // This is "likes" and "dislikes"
63 if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts })
64
65 return
66}
2ccaeeb3 67
07197db4
C
68async function sendVideoRateChange (account: AccountModel,
69 video: VideoModel,
70 likes: number,
71 dislikes: number,
72 t: Transaction) {
2ccaeeb3
C
73 const actor = account.Actor
74
75 // Keep the order: first we undo and then we create
76
77 // Undo Like
07197db4 78 if (likes < 0) await sendUndoLike(actor, video, t)
2ccaeeb3 79 // Undo Dislike
07197db4 80 if (dislikes < 0) await sendUndoDislike(actor, video, t)
2ccaeeb3
C
81
82 // Like
07197db4 83 if (likes > 0) await sendLike(actor, video, t)
2ccaeeb3 84 // Dislike
07197db4 85 if (dislikes > 0) await sendCreateDislike(actor, video, t)
2ccaeeb3
C
86}
87
5c6d985f
C
88function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) {
89 return rateType === 'like' ? getVideoLikeActivityPubUrl(actor, video) : getVideoDislikeActivityPubUrl(actor, video)
90}
91
2ccaeeb3 92export {
5c6d985f 93 getRateUrl,
1297eb5d 94 createRates,
07197db4 95 sendVideoRateChange
2ccaeeb3 96}