]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Reduce video import TTL
[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'
cfaf819c 12import { 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
6b616860 93 await Redis.Instance.addVideoView(video.id)
40ff5707 94
63c93323
C
95 if (video.isOwned()) {
96 // Don't resend the activity to the sender
6d852470 97 const exceptions = [ byActor ]
e5565833 98 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
63c93323 99 }
40ff5707
C
100}
101
c48e82b5
C
102async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
103 const cacheFile = activity.object as CacheFileObject
104
4157cdb1 105 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 106
e5565833
C
107 await sequelizeTypescript.transaction(async t => {
108 return createCacheFile(cacheFile, video, byActor, t)
109 })
c48e82b5
C
110
111 if (video.isOwned()) {
112 // Don't resend the activity to the sender
113 const exceptions = [ byActor ]
e5565833 114 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
115 }
116}
117
e587e0ec 118async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
8e13fa7d
C
119 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
120
e587e0ec
C
121 const account = byActor.Account
122 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
50d6de9c 123
4157cdb1 124 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
8e13fa7d 125
2ccaeeb3 126 return sequelizeTypescript.transaction(async t => {
8e13fa7d
C
127 const videoAbuseData = {
128 reporterAccountId: account.id,
129 reason: videoAbuseToCreateData.content,
268eebed
C
130 videoId: video.id,
131 state: VideoAbuseState.PENDING
8e13fa7d
C
132 }
133
c48e82b5 134 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
8e13fa7d
C
135
136 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
137 })
138}
6d852470 139
90d4bb81 140async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
83e6519b 141 const commentObject = activity.object as VideoCommentObject
6d852470
C
142 const byAccount = byActor.Account
143
144 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
145
83e6519b 146 const { video } = await resolveThread(commentObject.inReplyTo)
2ccaeeb3 147
83e6519b 148 const { created } = await addVideoComment(video, commentObject.id)
93ef8a9d 149
83e6519b
C
150 if (video.isOwned() && created === true) {
151 // Don't resend the activity to the sender
152 const exceptions = [ byActor ]
93ef8a9d 153
83e6519b
C
154 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
155 }
6d852470 156}