]>
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' |
e251f170 | 7 | import { forwardActivity } from '../send/utils' |
2ccaeeb3 | 8 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' |
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 | ||
2ccaeeb3 | 33 | async 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 | ||
2ccaeeb3 | 39 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoUrl) |
0032ebe9 | 40 | |
2ccaeeb3 | 41 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
42 | const rate = { |
43 | type: 'like' as 'like', | |
44 | videoId: video.id, | |
45 | accountId: byAccount.id | |
46 | } | |
3fd3ab2d | 47 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 48 | where: rate, |
63c93323 C |
49 | defaults: rate, |
50 | transaction: t | |
0032ebe9 | 51 | }) |
f00984c0 | 52 | if (created === true) await video.increment('likes', { transaction: t }) |
0032ebe9 | 53 | |
63c93323 C |
54 | if (video.isOwned() && created === true) { |
55 | // Don't resend the activity to the sender | |
50d6de9c | 56 | const exceptions = [ byActor ] |
63c93323 C |
57 | await forwardActivity(activity, t, exceptions) |
58 | } | |
0032ebe9 C |
59 | }) |
60 | } |