1 import { ActivityLike } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountModel } from '../../../models/account/account'
5 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
6 import { VideoModel } from '../../../models/video/video'
7 import { getOrCreateAccountAndServer } from '../account'
8 import { forwardActivity } from '../send/misc'
10 async function processLikeActivity (activity: ActivityLike) {
11 const account = await getOrCreateAccountAndServer(activity.actor)
13 return processLikeVideo(account, activity)
16 // ---------------------------------------------------------------------------
22 // ---------------------------------------------------------------------------
24 async function processLikeVideo (byAccount: AccountModel, activity: ActivityLike) {
26 arguments: [ byAccount, activity ],
27 errorMessage: 'Cannot like the video with many retries.'
30 return retryTransactionWrapper(createVideoLike, options)
33 function createVideoLike (byAccount: AccountModel, activity: ActivityLike) {
34 const videoUrl = activity.object
36 return sequelizeTypescript.transaction(async t => {
37 const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl)
39 if (!video) throw new Error('Unknown video ' + videoUrl)
42 type: 'like' as 'like',
44 accountId: byAccount.id
46 const [ , created ] = await AccountVideoRateModel.findOrCreate({
51 if (created === true) await video.increment('likes', { transaction: t })
53 if (video.isOwned() && created === true) {
54 // Don't resend the activity to the sender
55 const exceptions = [ byAccount ]
56 await forwardActivity(activity, t, exceptions)