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