]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/video-rates.ts
Support short uuid for GET video/playlist
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-rates.ts
... / ...
CommitLineData
1import * as Bluebird from 'bluebird'
2import { Transaction } from 'sequelize'
3import { doJSONRequest } from '@server/helpers/requests'
4import { VideoRateType } from '../../../shared/models/videos'
5import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
6import { logger, loggerTagsFactory } from '../../helpers/logger'
7import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
8import { AccountVideoRateModel } from '../../models/account/account-video-rate'
9import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
10import { getOrCreateAPActor } from './actors'
11import { sendLike, sendUndoDislike, sendUndoLike } from './send'
12import { sendDislike } from './send/send-dislike'
13import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
14
15const lTags = loggerTagsFactory('ap', 'video-rate', 'create')
16
17async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
18 await Bluebird.map(ratesUrl, async rateUrl => {
19 try {
20 await createRate(rateUrl, video, rate)
21 } catch (err) {
22 logger.info('Cannot add rate %s.', rateUrl, { err, ...lTags(rateUrl, video.uuid, video.url) })
23 }
24 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
25}
26
27async function sendVideoRateChange (
28 account: MAccountActor,
29 video: MVideoAccountLight,
30 likes: number,
31 dislikes: number,
32 t: Transaction
33) {
34 const actor = account.Actor
35
36 // Keep the order: first we undo and then we create
37
38 // Undo Like
39 if (likes < 0) await sendUndoLike(actor, video, t)
40 // Undo Dislike
41 if (dislikes < 0) await sendUndoDislike(actor, video, t)
42
43 // Like
44 if (likes > 0) await sendLike(actor, video, t)
45 // Dislike
46 if (dislikes > 0) await sendDislike(actor, video, t)
47}
48
49function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
50 return rateType === 'like'
51 ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
52 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
53}
54
55// ---------------------------------------------------------------------------
56
57export {
58 getLocalRateUrl,
59 createRates,
60 sendVideoRateChange
61}
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
79 const actor = await getOrCreateAPActor(actorUrl)
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}