aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-create.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r--server/lib/activitypub/process/process-create.ts170
1 files changed, 0 insertions, 170 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts
deleted file mode 100644
index 5f980de65..000000000
--- a/server/lib/activitypub/process/process-create.ts
+++ /dev/null
@@ -1,170 +0,0 @@
1import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
2import { isRedundancyAccepted } from '@server/lib/redundancy'
3import { VideoModel } from '@server/models/video/video'
4import {
5 AbuseObject,
6 ActivityCreate,
7 ActivityCreateObject,
8 ActivityObject,
9 CacheFileObject,
10 PlaylistObject,
11 VideoCommentObject,
12 VideoObject,
13 WatchActionObject
14} from '@shared/models'
15import { retryTransactionWrapper } from '../../../helpers/database-utils'
16import { logger } from '../../../helpers/logger'
17import { sequelizeTypescript } from '../../../initializers/database'
18import { APProcessorOptions } from '../../../types/activitypub-processor.model'
19import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
20import { Notifier } from '../../notifier'
21import { fetchAPObjectIfNeeded } from '../activity'
22import { createOrUpdateCacheFile } from '../cache-file'
23import { createOrUpdateLocalVideoViewer } from '../local-video-viewer'
24import { createOrUpdateVideoPlaylist } from '../playlists'
25import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
26import { resolveThread } from '../video-comments'
27import { getOrCreateAPVideo } from '../videos'
28
29async function processCreateActivity (options: APProcessorOptions<ActivityCreate<ActivityCreateObject>>) {
30 const { activity, byActor } = options
31
32 // Only notify if it is not from a fetcher job
33 const notify = options.fromFetch !== true
34 const activityObject = await fetchAPObjectIfNeeded<Exclude<ActivityObject, AbuseObject>>(activity.object)
35 const activityType = activityObject.type
36
37 if (activityType === 'Video') {
38 return processCreateVideo(activityObject, notify)
39 }
40
41 if (activityType === 'Note') {
42 // Comments will be fetched from videos
43 if (options.fromFetch) return
44
45 return retryTransactionWrapper(processCreateVideoComment, activity, activityObject, byActor, notify)
46 }
47
48 if (activityType === 'WatchAction') {
49 return retryTransactionWrapper(processCreateWatchAction, activityObject)
50 }
51
52 if (activityType === 'CacheFile') {
53 return retryTransactionWrapper(processCreateCacheFile, activity, activityObject, byActor)
54 }
55
56 if (activityType === 'Playlist') {
57 return retryTransactionWrapper(processCreatePlaylist, activity, activityObject, byActor)
58 }
59
60 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
61 return Promise.resolve(undefined)
62}
63
64// ---------------------------------------------------------------------------
65
66export {
67 processCreateActivity
68}
69
70// ---------------------------------------------------------------------------
71
72async function processCreateVideo (videoToCreateData: VideoObject, notify: boolean) {
73 const syncParam = { rates: false, shares: false, comments: false, refreshVideo: false }
74 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
75
76 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
77
78 return video
79}
80
81async function processCreateCacheFile (
82 activity: ActivityCreate<CacheFileObject | string>,
83 cacheFile: CacheFileObject,
84 byActor: MActorSignature
85) {
86 if (await isRedundancyAccepted(activity, byActor) !== true) return
87
88 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
89
90 await sequelizeTypescript.transaction(async t => {
91 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
92 })
93
94 if (video.isOwned()) {
95 // Don't resend the activity to the sender
96 const exceptions = [ byActor ]
97 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
98 }
99}
100
101async function processCreateWatchAction (watchAction: WatchActionObject) {
102 if (watchAction.actionStatus !== 'CompletedActionStatus') return
103
104 const video = await VideoModel.loadByUrl(watchAction.object)
105 if (video.remote) return
106
107 await sequelizeTypescript.transaction(async t => {
108 return createOrUpdateLocalVideoViewer(watchAction, video, t)
109 })
110}
111
112async function processCreateVideoComment (
113 activity: ActivityCreate<VideoCommentObject | string>,
114 commentObject: VideoCommentObject,
115 byActor: MActorSignature,
116 notify: boolean
117) {
118 const byAccount = byActor.Account
119
120 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
121
122 let video: MVideoAccountLightBlacklistAllFiles
123 let created: boolean
124 let comment: MCommentOwnerVideo
125
126 try {
127 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
128 if (!resolveThreadResult) return // Comment not accepted
129
130 video = resolveThreadResult.video
131 created = resolveThreadResult.commentCreated
132 comment = resolveThreadResult.comment
133 } catch (err) {
134 logger.debug(
135 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
136 commentObject.inReplyTo,
137 { err }
138 )
139 return
140 }
141
142 // Try to not forward unwanted comments on our videos
143 if (video.isOwned()) {
144 if (await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) {
145 logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url)
146 return
147 }
148
149 if (created === true) {
150 // Don't resend the activity to the sender
151 const exceptions = [ byActor ]
152
153 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
154 }
155 }
156
157 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
158}
159
160async function processCreatePlaylist (
161 activity: ActivityCreate<PlaylistObject | string>,
162 playlistObject: PlaylistObject,
163 byActor: MActorSignature
164) {
165 const byAccount = byActor.Account
166
167 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
168
169 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
170}