]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-dislike.ts
Also fill avatar width for channels
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-dislike.ts
CommitLineData
d17c7b4e 1import { ActivityCreate, ActivityDislike, DislikeObject } from '@shared/models'
848f499d 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
80fdaf06 3import { sequelizeTypescript } from '../../../initializers/database'
848f499d 4import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
26d6bf65
C
5import { APProcessorOptions } from '../../../types/activitypub-processor.model'
6import { MActorSignature } from '../../../types/models'
de94ac86 7import { forwardVideoRelatedActivity } from '../send/utils'
304a84d5 8import { 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
304a84d5 32 const { video } = await getOrCreateAPVideo({ videoObject: dislikeObject })
848f499d
C
33
34 return sequelizeTypescript.transaction(async t => {
b49f22d8 35 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t)
970ceac0
C
36 if (existingRate && existingRate.type === 'dislike') return
37
970ceac0 38 await video.increment('dislikes', { transaction: t })
848f499d 39
29d4e137
C
40 if (existingRate && existingRate.type === 'like') {
41 await video.decrement('likes', { transaction: t })
42 }
43
a21e25ff
C
44 const rate = existingRate || new AccountVideoRateModel()
45 rate.type = 'dislike'
46 rate.videoId = video.id
47 rate.accountId = byAccount.id
de94ac86 48 rate.url = activity.id
a21e25ff
C
49
50 await rate.save({ transaction: t })
51
970ceac0 52 if (video.isOwned()) {
848f499d
C
53 // Don't resend the activity to the sender
54 const exceptions = [ byActor ]
55
56 await forwardVideoRelatedActivity(activity, t, exceptions, video)
57 }
58 })
59}