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