]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Remove unnecessary actor existance check
[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'
90d4bb81 6import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
0032ebe9 8
e587e0ec
C
9async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
10 return retryTransactionWrapper(processLikeVideo, byActor, activity)
0032ebe9
C
11}
12
13// ---------------------------------------------------------------------------
14
15export {
16 processLikeActivity
17}
18
19// ---------------------------------------------------------------------------
20
90d4bb81 21async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
63c93323
C
22 const videoUrl = activity.object
23
50d6de9c
C
24 const byAccount = byActor.Account
25 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
26
4157cdb1 27 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
0032ebe9 28
2ccaeeb3 29 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
30 const rate = {
31 type: 'like' as 'like',
32 videoId: video.id,
33 accountId: byAccount.id
34 }
3fd3ab2d 35 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 36 where: rate,
63c93323
C
37 defaults: rate,
38 transaction: t
0032ebe9 39 })
f00984c0 40 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 41
63c93323
C
42 if (video.isOwned() && created === true) {
43 // Don't resend the activity to the sender
50d6de9c 44 const exceptions = [ byActor ]
9588d4f4
C
45
46 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 47 }
0032ebe9
C
48 })
49}