]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Fetch video likes/dislikes too
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
CommitLineData
0032ebe9
C
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}