]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Support broadcast messages
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers/database'
6import { resolveThread } from '../video-comments'
7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8import { forwardVideoRelatedActivity } from '../send/utils'
9import { createOrUpdateCacheFile } from '../cache-file'
10import { Notifier } from '../../notifier'
11import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
12import { createOrUpdateVideoPlaylist } from '../playlist'
13import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
14import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models'
15import { isRedundancyAccepted } from '@server/lib/redundancy'
16
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
22 const activityObject = activity.object
23 const activityType = activityObject.type
24
25 if (activityType === 'Video') {
26 return processCreateVideo(activity, notify)
27 }
28
29 if (activityType === 'Note') {
30 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
31 }
32
33 if (activityType === 'CacheFile') {
34 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
35 }
36
37 if (activityType === 'Playlist') {
38 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
39 }
40
41 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
42 return Promise.resolve(undefined)
43}
44
45// ---------------------------------------------------------------------------
46
47export {
48 processCreateActivity
49}
50
51// ---------------------------------------------------------------------------
52
53async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
54 const videoToCreateData = activity.object as VideoTorrentObject
55
56 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
57
58 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
59
60 return video
61}
62
63async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
64 if (await isRedundancyAccepted(activity, byActor) !== true) return
65
66 const cacheFile = activity.object as CacheFileObject
67
68 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
69
70 await sequelizeTypescript.transaction(async t => {
71 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
72 })
73
74 if (video.isOwned()) {
75 // Don't resend the activity to the sender
76 const exceptions = [ byActor ]
77 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
78 }
79}
80
81async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
82 const commentObject = activity.object as VideoCommentObject
83 const byAccount = byActor.Account
84
85 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
86
87 let video: MVideoAccountLightBlacklistAllFiles
88 let created: boolean
89 let comment: MCommentOwnerVideo
90 try {
91 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
92 video = resolveThreadResult.video
93 created = resolveThreadResult.commentCreated
94 comment = resolveThreadResult.comment
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 )
101 return
102 }
103
104 if (video.isOwned() && created === true) {
105 // Don't resend the activity to the sender
106 const exceptions = [ byActor ]
107
108 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
109 }
110
111 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
112}
113
114async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
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}