]>
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' |
3fd3ab2d | 6 | import { VideoModel } from '../../../models/video/video' |
50d6de9c | 7 | import { getOrCreateActorAndServerAndModel } from '../actor' |
63c93323 | 8 | import { forwardActivity } from '../send/misc' |
0032ebe9 C |
9 | |
10 | async function processLikeActivity (activity: ActivityLike) { | |
50d6de9c | 11 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
0032ebe9 | 12 | |
50d6de9c | 13 | return processLikeVideo(actor, activity) |
0032ebe9 C |
14 | } |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | ||
18 | export { | |
19 | processLikeActivity | |
20 | } | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
50d6de9c | 24 | async function processLikeVideo (actor: ActorModel, activity: ActivityLike) { |
0032ebe9 | 25 | const options = { |
50d6de9c | 26 | arguments: [ actor, activity ], |
0032ebe9 C |
27 | errorMessage: 'Cannot like the video with many retries.' |
28 | } | |
29 | ||
30 | return retryTransactionWrapper(createVideoLike, options) | |
31 | } | |
32 | ||
50d6de9c | 33 | function createVideoLike (byActor: ActorModel, activity: ActivityLike) { |
63c93323 C |
34 | const videoUrl = activity.object |
35 | ||
50d6de9c C |
36 | const byAccount = byActor.Account |
37 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) | |
38 | ||
3fd3ab2d C |
39 | return sequelizeTypescript.transaction(async t => { |
40 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl) | |
0032ebe9 C |
41 | |
42 | if (!video) throw new Error('Unknown video ' + videoUrl) | |
43 | ||
44 | const rate = { | |
45 | type: 'like' as 'like', | |
46 | videoId: video.id, | |
47 | accountId: byAccount.id | |
48 | } | |
3fd3ab2d | 49 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 50 | where: rate, |
63c93323 C |
51 | defaults: rate, |
52 | transaction: t | |
0032ebe9 | 53 | }) |
f00984c0 | 54 | if (created === true) await video.increment('likes', { transaction: t }) |
0032ebe9 | 55 | |
63c93323 C |
56 | if (video.isOwned() && created === true) { |
57 | // Don't resend the activity to the sender | |
50d6de9c | 58 | const exceptions = [ byActor ] |
63c93323 C |
59 | await forwardActivity(activity, t, exceptions) |
60 | } | |
0032ebe9 C |
61 | }) |
62 | } |