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