]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
57e4e1c1 1import { VideoModel } from '@server/models/video/video'
d17c7b4e 2import { ActivityCreate, ActivityDislike, DislikeObject } from '@shared/models'
848f499d 3import { retryTransactionWrapper } from '../../../helpers/database-utils'
80fdaf06 4import { sequelizeTypescript } from '../../../initializers/database'
848f499d 5import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
26d6bf65
C
6import { APProcessorOptions } from '../../../types/activitypub-processor.model'
7import { MActorSignature } from '../../../types/models'
57e4e1c1 8import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
848f499d 9
1198edf4
C
10async function processDislikeActivity (options: APProcessorOptions<ActivityCreate | ActivityDislike>) {
11 const { activity, byActor } = options
848f499d
C
12 return retryTransactionWrapper(processDislike, activity, byActor)
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 processDislikeActivity
19}
20
21// ---------------------------------------------------------------------------
22
453e83ea 23async function processDislike (activity: ActivityCreate | ActivityDislike, byActor: MActorSignature) {
de94ac86
C
24 const dislikeObject = activity.type === 'Dislike'
25 ? activity.object
26 : (activity.object as DislikeObject).object
27
848f499d
C
28 const byAccount = byActor.Account
29
30 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
31
57e4e1c1
C
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
848f499d
C
36
37 return sequelizeTypescript.transaction(async t => {
4fae2b1f 38 const video = await VideoModel.loadFull(onlyVideo.id, t)
57e4e1c1 39
b49f22d8 40 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t)
970ceac0
C
41 if (existingRate && existingRate.type === 'dislike') return
42
970ceac0 43 await video.increment('dislikes', { transaction: t })
57e4e1c1 44 video.dislikes++
848f499d 45
29d4e137
C
46 if (existingRate && existingRate.type === 'like') {
47 await video.decrement('likes', { transaction: t })
57e4e1c1 48 video.likes--
29d4e137
C
49 }
50
a21e25ff
C
51 const rate = existingRate || new AccountVideoRateModel()
52 rate.type = 'dislike'
53 rate.videoId = video.id
54 rate.accountId = byAccount.id
de94ac86 55 rate.url = activity.id
a21e25ff
C
56
57 await rate.save({ transaction: t })
58
57e4e1c1 59 await federateVideoIfNeeded(video, false, t)
848f499d
C
60 })
61}