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 { forwardActivity } from '../send/misc'
8 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
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 async 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 const { video } = await getOrCreateAccountAndVideoAndChannel(videoUrl)
41 return sequelizeTypescript.transaction(async t => {
43 type: 'like' as 'like',
45 accountId: byAccount.id
47 const [ , created ] = await AccountVideoRateModel.findOrCreate({
52 if (created === true) await video.increment('likes', { transaction: t })
54 if (video.isOwned() && created === true) {
55 // Don't resend the activity to the sender
56 const exceptions = [ byActor ]
57 await forwardActivity(activity, t, exceptions)