]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Fix conflict rate serializations
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
CommitLineData
0032ebe9 1import { ActivityLike } from '../../../../shared/models/activitypub/activity'
63c93323 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
0032ebe9
C
3import { database as db } from '../../../initializers'
4import { AccountInstance } from '../../../models/account/account-interface'
5import { getOrCreateAccountAndServer } from '../account'
63c93323 6import { forwardActivity } from '../send/misc'
0032ebe9
C
7
8async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor)
10
63c93323 11 return processLikeVideo(account, activity)
0032ebe9
C
12}
13
14// ---------------------------------------------------------------------------
15
16export {
17 processLikeActivity
18}
19
20// ---------------------------------------------------------------------------
21
63c93323 22async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) {
0032ebe9 23 const options = {
63c93323 24 arguments: [ byAccount, activity ],
0032ebe9
C
25 errorMessage: 'Cannot like the video with many retries.'
26 }
27
28 return retryTransactionWrapper(createVideoLike, options)
29}
30
63c93323
C
31function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
32 const videoUrl = activity.object
33
0032ebe9
C
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,
63c93323
C
46 defaults: rate,
47 transaction: t
0032ebe9 48 })
f00984c0 49 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 50
63c93323
C
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 }
0032ebe9
C
56 })
57}