1 import { ActivityLike } from '../../../../shared/models/activitypub/activity'
2 import { database as db } from '../../../initializers'
3 import { AccountInstance } from '../../../models/account/account-interface'
4 import { getOrCreateAccountAndServer } from '../account'
5 import { sendLikeToVideoFollowers } from '../send/send-like'
6 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor)
11 return processLikeVideo(account, activity.object)
14 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
22 async function processLikeVideo (byAccount: AccountInstance, videoUrl: string) {
24 arguments: [ byAccount, videoUrl ],
25 errorMessage: 'Cannot like the video with many retries.'
28 return retryTransactionWrapper(createVideoLike, options)
31 function createVideoLike (byAccount: AccountInstance, videoUrl: string) {
32 return db.sequelize.transaction(async t => {
33 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl)
35 if (!video) throw new Error('Unknown video ' + videoUrl)
38 type: 'like' as 'like',
40 accountId: byAccount.id
42 const [ , created ] = await db.AccountVideoRate.findOrCreate({
46 await video.increment('likes')
48 if (video.isOwned() && created === true) await sendLikeToVideoFollowers(byAccount, video, undefined)