1 import { ActivityCreate, ActivityDislike } from '../../../../shared'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { forwardVideoRelatedActivity } from '../send/utils'
9 import { getVideoDislikeActivityPubUrl } from '../url'
11 async function processDislikeActivity (activity: ActivityCreate | ActivityDislike, byActor: ActorModel) {
12 return retryTransactionWrapper(processDislike, activity, byActor)
15 // ---------------------------------------------------------------------------
18 processDislikeActivity
21 // ---------------------------------------------------------------------------
23 async function processDislike (activity: ActivityCreate | ActivityDislike, byActor: ActorModel) {
24 const dislikeObject = activity.type === 'Dislike' ? activity.object : (activity.object as DislikeObject).object
25 const byAccount = byActor.Account
27 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
29 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislikeObject })
31 return sequelizeTypescript.transaction(async t => {
33 type: 'dislike' as 'dislike',
35 accountId: byAccount.id
38 const [ , created ] = await AccountVideoRateModel.findOrCreate({
40 defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }),
43 if (created === true) await video.increment('dislikes', { 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)