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, forwardVideoRelatedActivity } from '../send/utils'
8 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
9 import { getActorsInvolvedInVideo } from '../audience'
11 async function processLikeActivity (activity: ActivityLike) {
12 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
14 return processLikeVideo(actor, activity)
17 // ---------------------------------------------------------------------------
23 // ---------------------------------------------------------------------------
25 async function processLikeVideo (actor: ActorModel, activity: ActivityLike) {
27 arguments: [ actor, activity ],
28 errorMessage: 'Cannot like the video with many retries.'
31 return retryTransactionWrapper(createVideoLike, options)
34 async function createVideoLike (byActor: ActorModel, activity: ActivityLike) {
35 const videoUrl = activity.object
37 const byAccount = byActor.Account
38 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
40 const { video } = await getOrCreateAccountAndVideoAndChannel(videoUrl)
42 return sequelizeTypescript.transaction(async t => {
44 type: 'like' as 'like',
46 accountId: byAccount.id
48 const [ , created ] = await AccountVideoRateModel.findOrCreate({
53 if (created === true) await video.increment('likes', { transaction: t })
55 if (video.isOwned() && created === true) {
56 // Don't resend the activity to the sender
57 const exceptions = [ byActor ]
59 await forwardVideoRelatedActivity(activity, t, exceptions, video)