1 import { ActivityLike } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { forwardVideoRelatedActivity } from '../send/utils'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9 async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
10 return retryTransactionWrapper(processLikeVideo, byActor, activity)
13 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
22 const videoUrl = activity.object
24 const byAccount = byActor.Account
25 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
27 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
29 return sequelizeTypescript.transaction(async t => {
31 type: 'like' as 'like',
33 accountId: byAccount.id
35 const [ , created ] = await AccountVideoRateModel.findOrCreate({
40 if (created === true) await video.increment('likes', { transaction: t })
42 if (video.isOwned() && created === true) {
43 // Don't resend the activity to the sender
44 const exceptions = [ byActor ]
46 await forwardVideoRelatedActivity(activity, t, exceptions, video)