]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-like.ts
Optimize video view AP processing
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
1 import { ActivityLike } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { getOrCreateActorAndServerAndModel } from '../actor'
7 import { forwardVideoRelatedActivity } from '../send/utils'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9
10 async function processLikeActivity (activity: ActivityLike) {
11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
12
13 return retryTransactionWrapper(processLikeVideo, actor, activity)
14 }
15
16 // ---------------------------------------------------------------------------
17
18 export {
19 processLikeActivity
20 }
21
22 // ---------------------------------------------------------------------------
23
24 async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
25 const videoUrl = activity.object
26
27 const byAccount = byActor.Account
28 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
29
30 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
31
32 return sequelizeTypescript.transaction(async t => {
33 const rate = {
34 type: 'like' as 'like',
35 videoId: video.id,
36 accountId: byAccount.id
37 }
38 const [ , created ] = await AccountVideoRateModel.findOrCreate({
39 where: rate,
40 defaults: rate,
41 transaction: t
42 })
43 if (created === true) await video.increment('likes', { transaction: t })
44
45 if (video.isOwned() && created === true) {
46 // Don't resend the activity to the sender
47 const exceptions = [ byActor ]
48
49 await forwardVideoRelatedActivity(activity, t, exceptions, video)
50 }
51 })
52 }