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'
8 import { immutableAssign } from '../../../tests/utils'
9 import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
11 async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
12 return retryTransactionWrapper(processLikeVideo, byActor, activity)
15 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
23 async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
24 const videoUrl = activity.object
26 const byAccount = byActor.Account
27 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
29 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
31 return sequelizeTypescript.transaction(async t => {
33 type: 'like' as 'like',
35 accountId: byAccount.id
37 const [ , created ] = await AccountVideoRateModel.findOrCreate({
39 defaults: immutableAssign(rate, { url: getVideoLikeActivityPubUrl(byActor, video) }),
42 if (created === true) await video.increment('likes', { transaction: t })
44 if (video.isOwned() && created === true) {
45 // Don't resend the activity to the sender
46 const exceptions = [ byActor ]
48 await forwardVideoRelatedActivity(activity, t, exceptions, video)