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