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 { getOrCreateActorAndServerAndModel } from '../actor'
7 import { forwardVideoRelatedActivity } from '../send/utils'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
10 async function processLikeActivity (activity: ActivityLike) {
11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
13 return retryTransactionWrapper(processLikeVideo, actor, activity)
16 // ---------------------------------------------------------------------------
22 // ---------------------------------------------------------------------------
24 async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
25 const videoUrl = activity.object
27 const byAccount = byActor.Account
28 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
30 const { video } = await getOrCreateVideoAndAccountAndChannel(videoUrl)
32 return sequelizeTypescript.transaction(async t => {
34 type: 'like' as 'like',
36 accountId: byAccount.id
38 const [ , created ] = await AccountVideoRateModel.findOrCreate({
43 if (created === true) await video.increment('likes', { transaction: t })
45 if (video.isOwned() && created === true) {
46 // Don't resend the activity to the sender
47 const exceptions = [ byActor ]
49 await forwardVideoRelatedActivity(activity, t, exceptions, video)