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