]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityCreate, ActivityDislike, DislikeObject } from '@shared/models' | |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
3 | import { sequelizeTypescript } from '../../../initializers/database' | |
4 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
5 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | |
6 | import { MActorSignature } from '../../../types/models' | |
7 | import { forwardVideoRelatedActivity } from '../send/utils' | |
8 | import { getOrCreateAPVideo } from '../videos' | |
9 | ||
10 | async function processDislikeActivity (options: APProcessorOptions<ActivityCreate | ActivityDislike>) { | |
11 | const { activity, byActor } = options | |
12 | return retryTransactionWrapper(processDislike, activity, byActor) | |
13 | } | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | ||
17 | export { | |
18 | processDislikeActivity | |
19 | } | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
23 | async function processDislike (activity: ActivityCreate | ActivityDislike, byActor: MActorSignature) { | |
24 | const dislikeObject = activity.type === 'Dislike' | |
25 | ? activity.object | |
26 | : (activity.object as DislikeObject).object | |
27 | ||
28 | const byAccount = byActor.Account | |
29 | ||
30 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
31 | ||
32 | const { video } = await getOrCreateAPVideo({ videoObject: dislikeObject }) | |
33 | ||
34 | return sequelizeTypescript.transaction(async t => { | |
35 | const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t) | |
36 | if (existingRate && existingRate.type === 'dislike') return | |
37 | ||
38 | await video.increment('dislikes', { transaction: t }) | |
39 | ||
40 | if (existingRate && existingRate.type === 'like') { | |
41 | await video.decrement('likes', { transaction: t }) | |
42 | } | |
43 | ||
44 | const rate = existingRate || new AccountVideoRateModel() | |
45 | rate.type = 'dislike' | |
46 | rate.videoId = video.id | |
47 | rate.accountId = byAccount.id | |
48 | rate.url = activity.id | |
49 | ||
50 | await rate.save({ transaction: t }) | |
51 | ||
52 | if (video.isOwned()) { | |
53 | // Don't resend the activity to the sender | |
54 | const exceptions = [ byActor ] | |
55 | ||
56 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
57 | } | |
58 | }) | |
59 | } |