aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-like.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/process/process-like.ts')
-rw-r--r--server/lib/activitypub/process/process-like.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts
new file mode 100644
index 000000000..d77b30f24
--- /dev/null
+++ b/server/lib/activitypub/process/process-like.ts
@@ -0,0 +1,50 @@
1import { ActivityLike } from '../../../../shared/models/activitypub/activity'
2import { database as db } from '../../../initializers'
3import { AccountInstance } from '../../../models/account/account-interface'
4import { getOrCreateAccountAndServer } from '../account'
5import { sendLikeToVideoFollowers } from '../send/send-like'
6import { retryTransactionWrapper } from '../../../helpers/database-utils'
7
8async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor)
10
11 return processLikeVideo(account, activity.object)
12}
13
14// ---------------------------------------------------------------------------
15
16export {
17 processLikeActivity
18}
19
20// ---------------------------------------------------------------------------
21
22async 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
31function 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}