]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Optimize video view AP processing
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
CommitLineData
3fd3ab2d 1import { ActivityLike } from '../../../../shared/models/activitypub'
da854ddd 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3fd3ab2d 3import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 4import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c 5import { ActorModel } from '../../../models/activitypub/actor'
50d6de9c 6import { getOrCreateActorAndServerAndModel } from '../actor'
90d4bb81 7import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 8import { getOrCreateVideoAndAccountAndChannel } from '../videos'
0032ebe9
C
9
10async function processLikeActivity (activity: ActivityLike) {
50d6de9c 11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
0032ebe9 12
90d4bb81 13 return retryTransactionWrapper(processLikeVideo, actor, activity)
0032ebe9
C
14}
15
16// ---------------------------------------------------------------------------
17
18export {
19 processLikeActivity
20}
21
22// ---------------------------------------------------------------------------
23
90d4bb81 24async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
63c93323
C
25 const videoUrl = activity.object
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
4157cdb1 30 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
0032ebe9 31
2ccaeeb3 32 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
33 const rate = {
34 type: 'like' as 'like',
35 videoId: video.id,
36 accountId: byAccount.id
37 }
3fd3ab2d 38 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 39 where: rate,
63c93323
C
40 defaults: rate,
41 transaction: t
0032ebe9 42 })
f00984c0 43 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 44
63c93323
C
45 if (video.isOwned() && created === true) {
46 // Don't resend the activity to the sender
50d6de9c 47 const exceptions = [ byActor ]
9588d4f4
C
48
49 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 50 }
0032ebe9
C
51 })
52}