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 { VideoModel } from '../../../models/video/video'
7 import { getOrCreateActorAndServerAndModel } from '../actor'
8 import { forwardActivity } from '../send/misc'
10 async function processLikeActivity (activity: ActivityLike) {
11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
13 return processLikeVideo(actor, activity)
16 // ---------------------------------------------------------------------------
22 // ---------------------------------------------------------------------------
24 async function processLikeVideo (actor: ActorModel, activity: ActivityLike) {
26 arguments: [ actor, activity ],
27 errorMessage: 'Cannot like the video with many retries.'
30 return retryTransactionWrapper(createVideoLike, options)
33 function createVideoLike (byActor: ActorModel, activity: ActivityLike) {
34 const videoUrl = activity.object
36 const byAccount = byActor.Account
37 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
39 return sequelizeTypescript.transaction(async t => {
40 const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl)
42 if (!video) throw new Error('Unknown video ' + videoUrl)
45 type: 'like' as 'like',
47 accountId: byAccount.id
49 const [ , created ] = await AccountVideoRateModel.findOrCreate({
54 if (created === true) await video.increment('likes', { transaction: t })
56 if (video.isOwned() && created === true) {
57 // Don't resend the activity to the sender
58 const exceptions = [ byActor ]
59 await forwardActivity(activity, t, exceptions)