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