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