]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-like.ts
Fix conflict rate serializations
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
1 import { ActivityLike } from '../../../../shared/models/activitypub/activity'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { database as db } from '../../../initializers'
4 import { AccountInstance } from '../../../models/account/account-interface'
5 import { getOrCreateAccountAndServer } from '../account'
6 import { forwardActivity } from '../send/misc'
7
8 async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor)
10
11 return processLikeVideo(account, activity)
12 }
13
14 // ---------------------------------------------------------------------------
15
16 export {
17 processLikeActivity
18 }
19
20 // ---------------------------------------------------------------------------
21
22 async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) {
23 const options = {
24 arguments: [ byAccount, activity ],
25 errorMessage: 'Cannot like the video with many retries.'
26 }
27
28 return retryTransactionWrapper(createVideoLike, options)
29 }
30
31 function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
32 const videoUrl = activity.object
33
34 return db.sequelize.transaction(async t => {
35 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl)
36
37 if (!video) throw new Error('Unknown video ' + videoUrl)
38
39 const rate = {
40 type: 'like' as 'like',
41 videoId: video.id,
42 accountId: byAccount.id
43 }
44 const [ , created ] = await db.AccountVideoRate.findOrCreate({
45 where: rate,
46 defaults: rate,
47 transaction: t
48 })
49 if (created === true) await video.increment('likes', { transaction: t })
50
51 if (video.isOwned() && created === true) {
52 // Don't resend the activity to the sender
53 const exceptions = [ byAccount ]
54 await forwardActivity(activity, t, exceptions)
55 }
56 })
57 }