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