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