]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { ActivityLike } from '../../../../shared/models/activitypub' |
de94ac86 | 2 | import { getAPId } from '../../../helpers/activitypub' |
da854ddd | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
80fdaf06 | 4 | import { sequelizeTypescript } from '../../../initializers/database' |
3fd3ab2d | 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 C |
8 | import { forwardVideoRelatedActivity } from '../send/utils' |
9 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
0032ebe9 | 10 | |
1198edf4 C |
11 | async function processLikeActivity (options: APProcessorOptions<ActivityLike>) { |
12 | const { activity, byActor } = options | |
e587e0ec | 13 | return retryTransactionWrapper(processLikeVideo, byActor, activity) |
0032ebe9 C |
14 | } |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | ||
18 | export { | |
19 | processLikeActivity | |
20 | } | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
453e83ea | 24 | async function processLikeVideo (byActor: MActorSignature, activity: ActivityLike) { |
848f499d | 25 | const videoUrl = getAPId(activity.object) |
63c93323 | 26 | |
50d6de9c C |
27 | const byAccount = byActor.Account |
28 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) | |
29 | ||
4157cdb1 | 30 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl }) |
0032ebe9 | 31 | |
2ccaeeb3 | 32 | return sequelizeTypescript.transaction(async t => { |
b49f22d8 | 33 | const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t) |
970ceac0 C |
34 | if (existingRate && existingRate.type === 'like') return |
35 | ||
29d4e137 C |
36 | if (existingRate && existingRate.type === 'dislike') { |
37 | await video.decrement('dislikes', { transaction: t }) | |
38 | } | |
39 | ||
a21e25ff C |
40 | await video.increment('likes', { transaction: t }) |
41 | ||
42 | const rate = existingRate || new AccountVideoRateModel() | |
43 | rate.type = 'like' | |
44 | rate.videoId = video.id | |
45 | rate.accountId = byAccount.id | |
de94ac86 | 46 | rate.url = activity.id |
a21e25ff C |
47 | |
48 | await rate.save({ transaction: t }) | |
49 | ||
970ceac0 | 50 | if (video.isOwned()) { |
63c93323 | 51 | // Don't resend the activity to the sender |
50d6de9c | 52 | const exceptions = [ byActor ] |
9588d4f4 C |
53 | |
54 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 55 | } |
0032ebe9 C |
56 | }) |
57 | } |