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