]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Support broadcast messages
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
848f499d 1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
6d852470 2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
80fdaf06 5import { sequelizeTypescript } from '../../../initializers/database'
6b9c966f 6import { resolveThread } from '../video-comments'
1297eb5d 7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
cfaf819c 8import { forwardVideoRelatedActivity } from '../send/utils'
b88a4596 9import { createOrUpdateCacheFile } from '../cache-file'
cef534ed 10import { Notifier } from '../../notifier'
418d092a
C
11import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
12import { createOrUpdateVideoPlaylist } from '../playlist'
1198edf4 13import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
0283eaac 14import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models'
8c9e7875 15import { isRedundancyAccepted } from '@server/lib/redundancy'
e4f97bab 16
1198edf4
C
17async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
18 const { activity, byActor } = options
19
20 // Only notify if it is not from a fetcher job
21 const notify = options.fromFetch !== true
e4f97bab
C
22 const activityObject = activity.object
23 const activityType = activityObject.type
24
848f499d 25 if (activityType === 'Video') {
1198edf4 26 return processCreateVideo(activity, notify)
848f499d
C
27 }
28
29 if (activityType === 'Note') {
1198edf4 30 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
848f499d
C
31 }
32
33 if (activityType === 'CacheFile') {
418d092a
C
34 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
35 }
36
37 if (activityType === 'Playlist') {
38 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
39 }
40
41 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 42 return Promise.resolve(undefined)
e4f97bab
C
43}
44
45// ---------------------------------------------------------------------------
46
47export {
48 processCreateActivity
49}
50
51// ---------------------------------------------------------------------------
52
1198edf4 53async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
50d6de9c
C
54 const videoToCreateData = activity.object as VideoTorrentObject
55
5b77537c 56 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
cef534ed 57
1198edf4 58 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 59
50d6de9c
C
60 return video
61}
62
453e83ea 63async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
8c9e7875
C
64 if (await isRedundancyAccepted(activity, byActor) !== true) return
65
c48e82b5
C
66 const cacheFile = activity.object as CacheFileObject
67
4157cdb1 68 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
c48e82b5 69
e5565833 70 await sequelizeTypescript.transaction(async t => {
b88a4596 71 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 72 })
c48e82b5
C
73
74 if (video.isOwned()) {
75 // Don't resend the activity to the sender
76 const exceptions = [ byActor ]
e5565833 77 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
78 }
79}
80
453e83ea 81async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
83e6519b 82 const commentObject = activity.object as VideoCommentObject
6d852470
C
83 const byAccount = byActor.Account
84
85 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
86
0283eaac 87 let video: MVideoAccountLightBlacklistAllFiles
6b9c966f 88 let created: boolean
453e83ea 89 let comment: MCommentOwnerVideo
ee79b60e 90 try {
6b9c966f 91 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
ee79b60e 92 video = resolveThreadResult.video
6b9c966f
C
93 created = resolveThreadResult.commentCreated
94 comment = resolveThreadResult.comment
ee79b60e
C
95 } catch (err) {
96 logger.debug(
97 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
98 commentObject.inReplyTo,
99 { err }
100 )
7d14d4d2 101 return
ee79b60e 102 }
2ccaeeb3 103
83e6519b
C
104 if (video.isOwned() && created === true) {
105 // Don't resend the activity to the sender
106 const exceptions = [ byActor ]
93ef8a9d 107
83e6519b
C
108 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
109 }
cef534ed 110
1198edf4 111 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
6d852470 112}
418d092a 113
453e83ea 114async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
418d092a
C
115 const playlistObject = activity.object as PlaylistObject
116 const byAccount = byActor.Account
117
118 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
119
120 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
121}