]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-dislike.ts
Fix broken follow notification
[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'
6import { ActorModel } from '../../../models/activitypub/actor'
7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8import { forwardVideoRelatedActivity } from '../send/utils'
9import { getVideoDislikeActivityPubUrl } from '../url'
10
11async function processDislikeActivity (activity: ActivityCreate | ActivityDislike, byActor: ActorModel) {
12 return retryTransactionWrapper(processDislike, activity, byActor)
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 processDislikeActivity
19}
20
21// ---------------------------------------------------------------------------
22
23async 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 => {
970ceac0
C
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
970ceac0 37 await video.increment('dislikes', { transaction: t })
848f499d 38
29d4e137
C
39 if (existingRate && existingRate.type === 'like') {
40 await video.decrement('likes', { transaction: t })
41 }
42
a21e25ff
C
43 const rate = existingRate || new AccountVideoRateModel()
44 rate.type = 'dislike'
45 rate.videoId = video.id
46 rate.accountId = byAccount.id
47 rate.url = url
48
49 await rate.save({ transaction: t })
50
970ceac0 51 if (video.isOwned()) {
848f499d
C
52 // Don't resend the activity to the sender
53 const exceptions = [ byActor ]
54
55 await forwardVideoRelatedActivity(activity, t, exceptions, video)
56 }
57 })
58}