]>
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' |
90d4bb81 | 7 | import { forwardVideoRelatedActivity } from '../send/utils' |
1297eb5d | 8 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
0032ebe9 C |
9 | |
10 | async function processLikeActivity (activity: ActivityLike) { | |
50d6de9c | 11 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
0032ebe9 | 12 | |
90d4bb81 | 13 | return retryTransactionWrapper(processLikeVideo, actor, activity) |
0032ebe9 C |
14 | } |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | ||
18 | export { | |
19 | processLikeActivity | |
20 | } | |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
90d4bb81 | 24 | async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) { |
63c93323 C |
25 | const videoUrl = activity.object |
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 | ||
1297eb5d | 30 | const { video } = await getOrCreateVideoAndAccountAndChannel(videoUrl) |
0032ebe9 | 31 | |
2ccaeeb3 | 32 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
33 | const rate = { |
34 | type: 'like' as 'like', | |
35 | videoId: video.id, | |
36 | accountId: byAccount.id | |
37 | } | |
3fd3ab2d | 38 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 39 | where: rate, |
63c93323 C |
40 | defaults: rate, |
41 | transaction: t | |
0032ebe9 | 42 | }) |
f00984c0 | 43 | if (created === true) await video.increment('likes', { transaction: t }) |
0032ebe9 | 44 | |
63c93323 C |
45 | if (video.isOwned() && created === true) { |
46 | // Don't resend the activity to the sender | |
50d6de9c | 47 | const exceptions = [ byActor ] |
9588d4f4 C |
48 | |
49 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 50 | } |
0032ebe9 C |
51 | }) |
52 | } |