]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Fix sharedInboxUrl list
[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'
3fd3ab2d 6import { VideoModel } from '../../../models/video/video'
50d6de9c 7import { getOrCreateActorAndServerAndModel } from '../actor'
63c93323 8import { forwardActivity } from '../send/misc'
0032ebe9
C
9
10async function processLikeActivity (activity: ActivityLike) {
50d6de9c 11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
0032ebe9 12
50d6de9c 13 return processLikeVideo(actor, activity)
0032ebe9
C
14}
15
16// ---------------------------------------------------------------------------
17
18export {
19 processLikeActivity
20}
21
22// ---------------------------------------------------------------------------
23
50d6de9c 24async function processLikeVideo (actor: ActorModel, activity: ActivityLike) {
0032ebe9 25 const options = {
50d6de9c 26 arguments: [ actor, activity ],
0032ebe9
C
27 errorMessage: 'Cannot like the video with many retries.'
28 }
29
30 return retryTransactionWrapper(createVideoLike, options)
31}
32
50d6de9c 33function createVideoLike (byActor: ActorModel, activity: ActivityLike) {
63c93323
C
34 const videoUrl = activity.object
35
50d6de9c
C
36 const byAccount = byActor.Account
37 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
38
3fd3ab2d
C
39 return sequelizeTypescript.transaction(async t => {
40 const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl)
0032ebe9
C
41
42 if (!video) throw new Error('Unknown video ' + videoUrl)
43
44 const rate = {
45 type: 'like' as 'like',
46 videoId: video.id,
47 accountId: byAccount.id
48 }
3fd3ab2d 49 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 50 where: rate,
63c93323
C
51 defaults: rate,
52 transaction: t
0032ebe9 53 })
f00984c0 54 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 55
63c93323
C
56 if (video.isOwned() && created === true) {
57 // Don't resend the activity to the sender
50d6de9c 58 const exceptions = [ byActor ]
63c93323
C
59 await forwardActivity(activity, t, exceptions)
60 }
0032ebe9
C
61 })
62}