]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-like.ts
Prepare Dislike/Flag/View fixes
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
CommitLineData
3fd3ab2d 1import { ActivityLike } from '../../../../shared/models/activitypub'
da854ddd 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3fd3ab2d 3import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 4import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c 5import { ActorModel } from '../../../models/activitypub/actor'
90d4bb81 6import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
a8f378e0 8import { getVideoLikeActivityPubUrl } from '../url'
848f499d 9import { getAPId } from '../../../helpers/activitypub'
0032ebe9 10
e587e0ec
C
11async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
12 return retryTransactionWrapper(processLikeVideo, byActor, activity)
0032ebe9
C
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 processLikeActivity
19}
20
21// ---------------------------------------------------------------------------
22
90d4bb81 23async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
848f499d 24 const videoUrl = getAPId(activity.object)
63c93323 25
50d6de9c
C
26 const byAccount = byActor.Account
27 if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url)
28
4157cdb1 29 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoUrl })
0032ebe9 30
2ccaeeb3 31 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
32 const rate = {
33 type: 'like' as 'like',
34 videoId: video.id,
35 accountId: byAccount.id
36 }
3fd3ab2d 37 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 38 where: rate,
a8f378e0 39 defaults: Object.assign({}, rate, { url: getVideoLikeActivityPubUrl(byActor, video) }),
63c93323 40 transaction: t
0032ebe9 41 })
f00984c0 42 if (created === true) await video.increment('likes', { transaction: t })
0032ebe9 43
63c93323
C
44 if (video.isOwned() && created === true) {
45 // Don't resend the activity to the sender
50d6de9c 46 const exceptions = [ byActor ]
9588d4f4
C
47
48 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 49 }
0032ebe9
C
50 })
51}