]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Optimize activity actor load in AP processors
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
c48e82b5 1import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
3fd3ab2d 2import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
6d852470 3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
3fd3ab2d 6import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c 8import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 9import { VideoAbuseModel } from '../../../models/video/video-abuse'
83e6519b 10import { addVideoComment, resolveThread } from '../video-comments'
1297eb5d 11import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9588d4f4 12import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
6b616860 13import { Redis } from '../../redis'
c48e82b5 14import { createCacheFile } from '../cache-file'
e4f97bab 15
e587e0ec 16async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
e4f97bab
C
17 const activityObject = activity.object
18 const activityType = activityObject.type
19
40ff5707 20 if (activityType === 'View') {
e587e0ec 21 return processCreateView(byActor, activity)
0032ebe9 22 } else if (activityType === 'Dislike') {
e587e0ec 23 return retryTransactionWrapper(processCreateDislike, byActor, activity)
50d6de9c 24 } else if (activityType === 'Video') {
f6eebcb3 25 return processCreateVideo(activity)
8e13fa7d 26 } else if (activityType === 'Flag') {
e587e0ec 27 return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject)
6d852470 28 } else if (activityType === 'Note') {
e587e0ec 29 return retryTransactionWrapper(processCreateVideoComment, byActor, activity)
c48e82b5 30 } else if (activityType === 'CacheFile') {
e587e0ec 31 return retryTransactionWrapper(processCacheFile, byActor, activity)
e4f97bab
C
32 }
33
34 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 35 return Promise.resolve(undefined)
e4f97bab
C
36}
37
38// ---------------------------------------------------------------------------
39
40export {
41 processCreateActivity
42}
43
44// ---------------------------------------------------------------------------
45
f6eebcb3 46async function processCreateVideo (activity: ActivityCreate) {
50d6de9c
C
47 const videoToCreateData = activity.object as VideoTorrentObject
48
4157cdb1 49 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
50d6de9c 50
50d6de9c
C
51 return video
52}
53
50d6de9c 54async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
63c93323 55 const dislike = activity.object as DislikeObject
50d6de9c
C
56 const byAccount = byActor.Account
57
58 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
0032ebe9 59
4157cdb1 60 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
0032ebe9 61
2ccaeeb3 62 return sequelizeTypescript.transaction(async t => {
0032ebe9
C
63 const rate = {
64 type: 'dislike' as 'dislike',
65 videoId: video.id,
66 accountId: byAccount.id
67 }
3fd3ab2d 68 const [ , created ] = await AccountVideoRateModel.findOrCreate({
0032ebe9 69 where: rate,
63c93323
C
70 defaults: rate,
71 transaction: t
0032ebe9 72 })
f00984c0 73 if (created === true) await video.increment('dislikes', { transaction: t })
0032ebe9 74
63c93323
C
75 if (video.isOwned() && created === true) {
76 // Don't resend the activity to the sender
50d6de9c 77 const exceptions = [ byActor ]
9588d4f4
C
78
79 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 80 }
0032ebe9
C
81 })
82}
83
6d852470 84async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
63c93323
C
85 const view = activity.object as ViewObject
86
d4defe07
C
87 const options = {
88 videoObject: view.object,
89 fetchType: 'only-video' as 'only-video'
90 }
91 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
40ff5707 92
d4defe07
C
93 const actorExists = await ActorModel.isActorUrlExist(view.actor)
94 if (actorExists === false) throw new Error('Unknown actor ' + view.actor)
40ff5707 95
6b616860 96 await Redis.Instance.addVideoView(video.id)
40ff5707 97
63c93323
C
98 if (video.isOwned()) {
99 // Don't resend the activity to the sender
6d852470 100 const exceptions = [ byActor ]
63c93323
C
101 await forwardActivity(activity, undefined, exceptions)
102 }
40ff5707
C
103}
104
c48e82b5
C
105async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
106 const cacheFile = activity.object as CacheFileObject
107
4157cdb1 108 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5
C
109
110 await createCacheFile(cacheFile, video, byActor)
111
112 if (video.isOwned()) {
113 // Don't resend the activity to the sender
114 const exceptions = [ byActor ]
115 await forwardActivity(activity, undefined, exceptions)
116 }
117}
118
e587e0ec 119async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
8e13fa7d
C
120 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
121
e587e0ec
C
122 const account = byActor.Account
123 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
50d6de9c 124
4157cdb1 125 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
8e13fa7d 126
2ccaeeb3 127 return sequelizeTypescript.transaction(async t => {
8e13fa7d
C
128 const videoAbuseData = {
129 reporterAccountId: account.id,
130 reason: videoAbuseToCreateData.content,
268eebed
C
131 videoId: video.id,
132 state: VideoAbuseState.PENDING
8e13fa7d
C
133 }
134
c48e82b5 135 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
8e13fa7d
C
136
137 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
138 })
139}
6d852470 140
90d4bb81 141async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
83e6519b 142 const commentObject = activity.object as VideoCommentObject
6d852470
C
143 const byAccount = byActor.Account
144
145 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
146
83e6519b 147 const { video } = await resolveThread(commentObject.inReplyTo)
2ccaeeb3 148
83e6519b 149 const { created } = await addVideoComment(video, commentObject.id)
93ef8a9d 150
83e6519b
C
151 if (video.isOwned() && created === true) {
152 // Don't resend the activity to the sender
153 const exceptions = [ byActor ]
93ef8a9d 154
83e6519b
C
155 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
156 }
6d852470 157}