]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-like.ts
Merge branch 'hotfix/docker' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
... / ...
CommitLineData
1import { ActivityLike } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { sequelizeTypescript } from '../../../initializers'
4import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
5import { ActorModel } from '../../../models/activitypub/actor'
6import { forwardVideoRelatedActivity } from '../send/utils'
7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8import { getVideoLikeActivityPubUrl } from '../url'
9
10async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
11 return retryTransactionWrapper(processLikeVideo, byActor, activity)
12}
13
14// ---------------------------------------------------------------------------
15
16export {
17 processLikeActivity
18}
19
20// ---------------------------------------------------------------------------
21
22async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
23 const videoUrl = activity.object
24
25 const byAccount = byActor.Account
26 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
27
28 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
29
30 return sequelizeTypescript.transaction(async t => {
31 const rate = {
32 type: 'like' as 'like',
33 videoId: video.id,
34 accountId: byAccount.id
35 }
36 const [ , created ] = await AccountVideoRateModel.findOrCreate({
37 where: rate,
38 defaults: Object.assign({}, rate, { url: getVideoLikeActivityPubUrl(byActor, video) }),
39 transaction: t
40 })
41 if (created === true) await video.increment('likes', { transaction: t })
42
43 if (video.isOwned() && created === true) {
44 // Don't resend the activity to the sender
45 const exceptions = [ byActor ]
46
47 await forwardVideoRelatedActivity(activity, t, exceptions, video)
48 }
49 })
50}