]>
Commit | Line | Data |
---|---|---|
57e4e1c1 | 1 | import { VideoModel } from '@server/models/video/video' |
3fd3ab2d | 2 | import { ActivityLike } from '../../../../shared/models/activitypub' |
da854ddd | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
80fdaf06 | 4 | import { sequelizeTypescript } from '../../../initializers/database' |
7e98a7df | 5 | import { getAPId } from '../../../lib/activitypub/activity' |
3fd3ab2d | 6 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
26d6bf65 C |
7 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
8 | import { MActorSignature } from '../../../types/models' | |
57e4e1c1 | 9 | import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos' |
0032ebe9 | 10 | |
1198edf4 C |
11 | async function processLikeActivity (options: APProcessorOptions<ActivityLike>) { |
12 | const { activity, byActor } = options | |
57e4e1c1 | 13 | |
e587e0ec | 14 | return retryTransactionWrapper(processLikeVideo, byActor, activity) |
0032ebe9 C |
15 | } |
16 | ||
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | export { | |
20 | processLikeActivity | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
453e83ea | 25 | async function processLikeVideo (byActor: MActorSignature, activity: ActivityLike) { |
848f499d | 26 | const videoUrl = getAPId(activity.object) |
63c93323 | 27 | |
50d6de9c C |
28 | const byAccount = byActor.Account |
29 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) | |
30 | ||
57e4e1c1 C |
31 | const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: videoUrl, fetchType: 'only-video' }) |
32 | ||
33 | // We don't care about likes of remote videos | |
34 | if (!onlyVideo.isOwned()) return | |
0032ebe9 | 35 | |
2ccaeeb3 | 36 | return sequelizeTypescript.transaction(async t => { |
57e4e1c1 C |
37 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t) |
38 | ||
b49f22d8 | 39 | const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t) |
970ceac0 C |
40 | if (existingRate && existingRate.type === 'like') return |
41 | ||
29d4e137 C |
42 | if (existingRate && existingRate.type === 'dislike') { |
43 | await video.decrement('dislikes', { transaction: t }) | |
57e4e1c1 | 44 | video.dislikes-- |
29d4e137 C |
45 | } |
46 | ||
a21e25ff | 47 | await video.increment('likes', { transaction: t }) |
57e4e1c1 | 48 | video.likes++ |
a21e25ff C |
49 | |
50 | const rate = existingRate || new AccountVideoRateModel() | |
51 | rate.type = 'like' | |
52 | rate.videoId = video.id | |
53 | rate.accountId = byAccount.id | |
de94ac86 | 54 | rate.url = activity.id |
a21e25ff C |
55 | |
56 | await rate.save({ transaction: t }) | |
57 | ||
57e4e1c1 | 58 | await federateVideoIfNeeded(video, false, t) |
0032ebe9 C |
59 | }) |
60 | } |