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