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 { getVideoLikeActivityPubUrl } from '../url'
9 import { getAPId } from '../../../helpers/activitypub'
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 = getAPId(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 => {
32 const url = getVideoLikeActivityPubUrl(byActor, video)
34 const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url)
35 if (existingRate && existingRate.type === 'like') return
37 if (existingRate && existingRate.type === 'dislike') {
38 await video.decrement('dislikes', { transaction: t })
41 await video.increment('likes', { transaction: t })
43 const rate = existingRate || new AccountVideoRateModel()
45 rate.videoId = video.id
46 rate.accountId = byAccount.id
49 await rate.save({ transaction: t })
51 if (video.isOwned()) {
52 // Don't resend the activity to the sender
53 const exceptions = [ byActor ]
55 await forwardVideoRelatedActivity(activity, t, exceptions, video)