]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-dislike.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-dislike.ts
CommitLineData
848f499d
C
1import { ActivityCreate, ActivityDislike } from '../../../../shared'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { sequelizeTypescript } from '../../../initializers'
5import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
848f499d
C
6import { getOrCreateVideoAndAccountAndChannel } from '../videos'
7import { forwardVideoRelatedActivity } from '../send/utils'
8import { getVideoDislikeActivityPubUrl } from '../url'
1198edf4 9import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
453e83ea 10import { MActorSignature } from '../../../typings/models'
848f499d 11
1198edf4
C
12async function processDislikeActivity (options: APProcessorOptions<ActivityCreate | ActivityDislike>) {
13 const { activity, byActor } = options
848f499d
C
14 return retryTransactionWrapper(processDislike, activity, byActor)
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processDislikeActivity
21}
22
23// ---------------------------------------------------------------------------
24
453e83ea 25async function processDislike (activity: ActivityCreate | ActivityDislike, byActor: MActorSignature) {
848f499d
C
26 const dislikeObject = activity.type === 'Dislike' ? activity.object : (activity.object as DislikeObject).object
27 const byAccount = byActor.Account
28
29 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
30
31 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislikeObject })
32
33 return sequelizeTypescript.transaction(async t => {
970ceac0
C
34 const url = getVideoDislikeActivityPubUrl(byActor, video)
35
36 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url)
37 if (existingRate && existingRate.type === 'dislike') return
38
970ceac0 39 await video.increment('dislikes', { transaction: t })
848f499d 40
29d4e137
C
41 if (existingRate && existingRate.type === 'like') {
42 await video.decrement('likes', { transaction: t })
43 }
44
a21e25ff
C
45 const rate = existingRate || new AccountVideoRateModel()
46 rate.type = 'dislike'
47 rate.videoId = video.id
48 rate.accountId = byAccount.id
49 rate.url = url
50
51 await rate.save({ transaction: t })
52
970ceac0 53 if (video.isOwned()) {
848f499d
C
54 // Don't resend the activity to the sender
55 const exceptions = [ byActor ]
56
57 await forwardVideoRelatedActivity(activity, t, exceptions, video)
58 }
59 })
60}