]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-like.ts
Fix HLS transcoding
[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 { forwardVideoRelatedActivity } from '../send/utils'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { getVideoLikeActivityPubUrl } from '../url'
9 import { getAPId } from '../../../helpers/activitypub'
10
11 async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
12 return retryTransactionWrapper(processLikeVideo, byActor, activity)
13 }
14
15 // ---------------------------------------------------------------------------
16
17 export {
18 processLikeActivity
19 }
20
21 // ---------------------------------------------------------------------------
22
23 async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
24 const videoUrl = getAPId(activity.object)
25
26 const byAccount = byActor.Account
27 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
28
29 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
30
31 return sequelizeTypescript.transaction(async t => {
32 const url = getVideoLikeActivityPubUrl(byActor, video)
33
34 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url)
35 if (existingRate && existingRate.type === 'like') return
36
37 await AccountVideoRateModel.create({
38 type: 'like' as 'like',
39 videoId: video.id,
40 accountId: byAccount.id,
41 url
42 }, { transaction: t })
43
44 await video.increment('likes', { transaction: t })
45
46 if (existingRate && existingRate.type === 'dislike') {
47 await video.decrement('dislikes', { transaction: t })
48 }
49
50 if (video.isOwned()) {
51 // Don't resend the activity to the sender
52 const exceptions = [ byActor ]
53
54 await forwardVideoRelatedActivity(activity, t, exceptions, video)
55 }
56 })
57 }