]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-create.ts
Remove activitypub helper
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
CommitLineData
49af5ac8 1import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
696d83fd 2import { isRedundancyAccepted } from '@server/lib/redundancy'
d17c7b4e 3import { ActivityCreate, CacheFileObject, PlaylistObject, VideoCommentObject, VideoObject } from '@shared/models'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
80fdaf06 6import { sequelizeTypescript } from '../../../initializers/database'
26d6bf65
C
7import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
696d83fd
C
9import { Notifier } from '../../notifier'
10import { createOrUpdateCacheFile } from '../cache-file'
49af5ac8 11import { createOrUpdateVideoPlaylist } from '../playlists'
57e4e1c1 12import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
696d83fd 13import { resolveThread } from '../video-comments'
304a84d5 14import { getOrCreateAPVideo } from '../videos'
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') {
a2f99b54
C
29 // Comments will be fetched from videos
30 if (options.fromFetch) return
31
1198edf4 32 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
848f499d
C
33 }
34
35 if (activityType === 'CacheFile') {
418d092a
C
36 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
37 }
38
39 if (activityType === 'Playlist') {
40 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
e4f97bab
C
41 }
42
43 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
0d0e8dd0 44 return Promise.resolve(undefined)
e4f97bab
C
45}
46
47// ---------------------------------------------------------------------------
48
49export {
50 processCreateActivity
51}
52
53// ---------------------------------------------------------------------------
54
1198edf4 55async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
de6310b2 56 const videoToCreateData = activity.object as VideoObject
50d6de9c 57
57e4e1c1 58 const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false }
304a84d5 59 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
cef534ed 60
1198edf4 61 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
50d6de9c 62
50d6de9c
C
63 return video
64}
65
453e83ea 66async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
8c9e7875
C
67 if (await isRedundancyAccepted(activity, byActor) !== true) return
68
c48e82b5
C
69 const cacheFile = activity.object as CacheFileObject
70
304a84d5 71 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
c48e82b5 72
e5565833 73 await sequelizeTypescript.transaction(async t => {
b88a4596 74 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
e5565833 75 })
c48e82b5
C
76
77 if (video.isOwned()) {
78 // Don't resend the activity to the sender
79 const exceptions = [ byActor ]
e5565833 80 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
c48e82b5
C
81 }
82}
83
453e83ea 84async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
83e6519b 85 const commentObject = activity.object as VideoCommentObject
6d852470
C
86 const byAccount = byActor.Account
87
88 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
89
0283eaac 90 let video: MVideoAccountLightBlacklistAllFiles
6b9c966f 91 let created: boolean
453e83ea 92 let comment: MCommentOwnerVideo
ee79b60e 93 try {
6b9c966f 94 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
403c69c5 95
ee79b60e 96 video = resolveThreadResult.video
6b9c966f
C
97 created = resolveThreadResult.commentCreated
98 comment = resolveThreadResult.comment
ee79b60e
C
99 } catch (err) {
100 logger.debug(
101 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
102 commentObject.inReplyTo,
103 { err }
104 )
7d14d4d2 105 return
ee79b60e 106 }
2ccaeeb3 107
696d83fd 108 // Try to not forward unwanted commments on our videos
403c69c5
C
109 if (video.isOwned()) {
110 if (await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) {
111 logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url)
112 return
113 }
696d83fd 114
403c69c5
C
115 if (created === true) {
116 // Don't resend the activity to the sender
117 const exceptions = [ byActor ]
93ef8a9d 118
403c69c5
C
119 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
120 }
83e6519b 121 }
cef534ed 122
1198edf4 123 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
6d852470 124}
418d092a 125
453e83ea 126async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
418d092a
C
127 const playlistObject = activity.object as PlaylistObject
128 const byAccount = byActor.Account
129
130 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
131
37a44fc9 132 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
418d092a 133}