diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/activitypub/process/process-create.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 170 |
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 @@ | |||
1 | import { isBlockedByServerOrAccount } from '@server/lib/blocklist' | ||
2 | import { isRedundancyAccepted } from '@server/lib/redundancy' | ||
3 | import { VideoModel } from '@server/models/video/video' | ||
4 | import { | ||
5 | AbuseObject, | ||
6 | ActivityCreate, | ||
7 | ActivityCreateObject, | ||
8 | ActivityObject, | ||
9 | CacheFileObject, | ||
10 | PlaylistObject, | ||
11 | VideoCommentObject, | ||
12 | VideoObject, | ||
13 | WatchActionObject | ||
14 | } from '@shared/models' | ||
15 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
16 | import { logger } from '../../../helpers/logger' | ||
17 | import { sequelizeTypescript } from '../../../initializers/database' | ||
18 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | ||
19 | import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' | ||
20 | import { Notifier } from '../../notifier' | ||
21 | import { fetchAPObjectIfNeeded } from '../activity' | ||
22 | import { createOrUpdateCacheFile } from '../cache-file' | ||
23 | import { createOrUpdateLocalVideoViewer } from '../local-video-viewer' | ||
24 | import { createOrUpdateVideoPlaylist } from '../playlists' | ||
25 | import { forwardVideoRelatedActivity } from '../send/shared/send-utils' | ||
26 | import { resolveThread } from '../video-comments' | ||
27 | import { getOrCreateAPVideo } from '../videos' | ||
28 | |||
29 | async 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 | |||
66 | export { | ||
67 | processCreateActivity | ||
68 | } | ||
69 | |||
70 | // --------------------------------------------------------------------------- | ||
71 | |||
72 | async 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 | |||
81 | async 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 | |||
101 | async 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 | |||
112 | async 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 | |||
160 | async 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 | } | ||