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