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