]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-create.ts
Remove activitypub helper
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-create.ts
... / ...
CommitLineData
1import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
2import { isRedundancyAccepted } from '@server/lib/redundancy'
3import { ActivityCreate, CacheFileObject, PlaylistObject, VideoCommentObject, VideoObject } from '@shared/models'
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
6import { sequelizeTypescript } from '../../../initializers/database'
7import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
9import { Notifier } from '../../notifier'
10import { createOrUpdateCacheFile } from '../cache-file'
11import { createOrUpdateVideoPlaylist } from '../playlists'
12import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
13import { resolveThread } from '../video-comments'
14import { getOrCreateAPVideo } from '../videos'
15
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
21 const activityObject = activity.object
22 const activityType = activityObject.type
23
24 if (activityType === 'Video') {
25 return processCreateVideo(activity, notify)
26 }
27
28 if (activityType === 'Note') {
29 // Comments will be fetched from videos
30 if (options.fromFetch) return
31
32 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
33 }
34
35 if (activityType === 'CacheFile') {
36 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
37 }
38
39 if (activityType === 'Playlist') {
40 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
41 }
42
43 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
44 return Promise.resolve(undefined)
45}
46
47// ---------------------------------------------------------------------------
48
49export {
50 processCreateActivity
51}
52
53// ---------------------------------------------------------------------------
54
55async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
56 const videoToCreateData = activity.object as VideoObject
57
58 const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false }
59 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
60
61 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
62
63 return video
64}
65
66async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
67 if (await isRedundancyAccepted(activity, byActor) !== true) return
68
69 const cacheFile = activity.object as CacheFileObject
70
71 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
72
73 await sequelizeTypescript.transaction(async t => {
74 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
75 })
76
77 if (video.isOwned()) {
78 // Don't resend the activity to the sender
79 const exceptions = [ byActor ]
80 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
81 }
82}
83
84async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
85 const commentObject = activity.object as VideoCommentObject
86 const byAccount = byActor.Account
87
88 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
89
90 let video: MVideoAccountLightBlacklistAllFiles
91 let created: boolean
92 let comment: MCommentOwnerVideo
93 try {
94 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
95
96 video = resolveThreadResult.video
97 created = resolveThreadResult.commentCreated
98 comment = resolveThreadResult.comment
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 )
105 return
106 }
107
108 // Try to not forward unwanted commments on our videos
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 }
114
115 if (created === true) {
116 // Don't resend the activity to the sender
117 const exceptions = [ byActor ]
118
119 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
120 }
121 }
122
123 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
124}
125
126async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
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
132 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
133}