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