]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-like.ts
Don't store remote rates of remote videos
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
... / ...
CommitLineData
1import { VideoModel } from '@server/models/video/video'
2import { ActivityLike } from '../../../../shared/models/activitypub'
3import { getAPId } from '../../../helpers/activitypub'
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { sequelizeTypescript } from '../../../initializers/database'
6import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8import { MActorSignature } from '../../../types/models'
9import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
10
11async function processLikeActivity (options: APProcessorOptions<ActivityLike>) {
12 const { activity, byActor } = options
13
14 return retryTransactionWrapper(processLikeVideo, byActor, activity)
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processLikeActivity
21}
22
23// ---------------------------------------------------------------------------
24
25async function processLikeVideo (byActor: MActorSignature, activity: ActivityLike) {
26 const videoUrl = getAPId(activity.object)
27
28 const byAccount = byActor.Account
29 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
30
31 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: videoUrl, fetchType: 'only-video' })
32
33 // We don't care about likes of remote videos
34 if (!onlyVideo.isOwned()) return
35
36 return sequelizeTypescript.transaction(async t => {
37 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t)
38
39 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t)
40 if (existingRate && existingRate.type === 'like') return
41
42 if (existingRate && existingRate.type === 'dislike') {
43 await video.decrement('dislikes', { transaction: t })
44 video.dislikes--
45 }
46
47 await video.increment('likes', { transaction: t })
48 video.likes++
49
50 const rate = existingRate || new AccountVideoRateModel()
51 rate.type = 'like'
52 rate.videoId = video.id
53 rate.accountId = byAccount.id
54 rate.url = activity.id
55
56 await rate.save({ transaction: t })
57
58 await federateVideoIfNeeded(video, false, t)
59 })
60}