]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Update README.md (#378)
[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'
63c93323 7import { forwardActivity } from '../send/misc'
2ccaeeb3 8import { getOrCreateAccountAndVideoAndChannel } from '../videos'
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
2ccaeeb3 33async function 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
2ccaeeb3 39 const { video } = await getOrCreateAccountAndVideoAndChannel(videoUrl)
0032ebe9 40
2ccaeeb3 41 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
42 const rate = {
43 type: 'like' as 'like',
44 videoId: video.id,
45 accountId: byAccount.id
46 }
3fd3ab2d 47 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 48 where: rate,
63c93323
C
49 defaults: rate,
50 transaction: t
0032ebe9 51 })
f00984c0 52 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 53
63c93323
C
54 if (video.isOwned() && created === true) {
55 // Don't resend the activity to the sender
50d6de9c 56 const exceptions = [ byActor ]
63c93323
C
57 await forwardActivity(activity, t, exceptions)
58 }
0032ebe9
C
59 })
60}