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'
8 async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor)
11 return processLikeVideo(account, activity)
14 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
22 async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) {
24 arguments: [ byAccount, activity ],
25 errorMessage: 'Cannot like the video with many retries.'
28 return retryTransactionWrapper(createVideoLike, options)
31 function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
32 const videoUrl = activity.object
34 return db.sequelize.transaction(async t => {
35 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl)
37 if (!video) throw new Error('Unknown video ' + videoUrl)
40 type: 'like' as 'like',
42 accountId: byAccount.id
44 const [ , created ] = await db.AccountVideoRate.findOrCreate({
49 await video.increment('likes', { transaction: t })
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)