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