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/scripts/update-host.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/scripts/update-host.ts')
-rwxr-xr-x | server/scripts/update-host.ts | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/server/scripts/update-host.ts b/server/scripts/update-host.ts new file mode 100755 index 000000000..52ac4947a --- /dev/null +++ b/server/scripts/update-host.ts | |||
@@ -0,0 +1,140 @@ | |||
1 | import { updateTorrentMetadata } from '@server/helpers/webtorrent.js' | ||
2 | import { getServerActor } from '@server/models/application/application.js' | ||
3 | import { WEBSERVER } from '@server/initializers/constants.js' | ||
4 | import { initDatabaseModels } from '@server/initializers/database.js' | ||
5 | import { | ||
6 | getLocalAccountActivityPubUrl, | ||
7 | getLocalVideoActivityPubUrl, | ||
8 | getLocalVideoAnnounceActivityPubUrl, | ||
9 | getLocalVideoChannelActivityPubUrl, | ||
10 | getLocalVideoCommentActivityPubUrl | ||
11 | } from '@server/lib/activitypub/url.js' | ||
12 | import { AccountModel } from '@server/models/account/account.js' | ||
13 | import { ActorFollowModel } from '@server/models/actor/actor-follow.js' | ||
14 | import { ActorModel } from '@server/models/actor/actor.js' | ||
15 | import { VideoChannelModel } from '@server/models/video/video-channel.js' | ||
16 | import { VideoCommentModel } from '@server/models/video/video-comment.js' | ||
17 | import { VideoShareModel } from '@server/models/video/video-share.js' | ||
18 | import { VideoModel } from '@server/models/video/video.js' | ||
19 | |||
20 | run() | ||
21 | .then(() => process.exit(0)) | ||
22 | .catch(err => { | ||
23 | console.error(err) | ||
24 | process.exit(-1) | ||
25 | }) | ||
26 | |||
27 | async function run () { | ||
28 | await initDatabaseModels(true) | ||
29 | |||
30 | const serverAccount = await getServerActor() | ||
31 | |||
32 | { | ||
33 | const res = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined) | ||
34 | const hasFollowing = res.total > 0 | ||
35 | |||
36 | if (hasFollowing === true) { | ||
37 | throw new Error('Cannot update host because you follow other servers!') | ||
38 | } | ||
39 | } | ||
40 | |||
41 | console.log('Updating actors.') | ||
42 | |||
43 | const actors: ActorModel[] = await ActorModel.unscoped().findAll({ | ||
44 | include: [ | ||
45 | { | ||
46 | model: VideoChannelModel.unscoped(), | ||
47 | required: false | ||
48 | }, | ||
49 | { | ||
50 | model: AccountModel.unscoped(), | ||
51 | required: false | ||
52 | } | ||
53 | ] | ||
54 | }) | ||
55 | for (const actor of actors) { | ||
56 | if (actor.isOwned() === false) continue | ||
57 | |||
58 | console.log('Updating actor ' + actor.url) | ||
59 | |||
60 | const newUrl = actor.Account | ||
61 | ? getLocalAccountActivityPubUrl(actor.preferredUsername) | ||
62 | : getLocalVideoChannelActivityPubUrl(actor.preferredUsername) | ||
63 | |||
64 | actor.url = newUrl | ||
65 | actor.inboxUrl = newUrl + '/inbox' | ||
66 | actor.outboxUrl = newUrl + '/outbox' | ||
67 | actor.sharedInboxUrl = WEBSERVER.URL + '/inbox' | ||
68 | actor.followersUrl = newUrl + '/followers' | ||
69 | actor.followingUrl = newUrl + '/following' | ||
70 | |||
71 | await actor.save() | ||
72 | } | ||
73 | |||
74 | console.log('Updating video shares.') | ||
75 | |||
76 | const videoShares: VideoShareModel[] = await VideoShareModel.findAll({ | ||
77 | include: [ VideoModel.unscoped(), ActorModel.unscoped() ] | ||
78 | }) | ||
79 | for (const videoShare of videoShares) { | ||
80 | if (videoShare.Video.isOwned() === false) continue | ||
81 | |||
82 | console.log('Updating video share ' + videoShare.url) | ||
83 | |||
84 | videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video) | ||
85 | await videoShare.save() | ||
86 | } | ||
87 | |||
88 | console.log('Updating video comments.') | ||
89 | const videoComments: VideoCommentModel[] = await VideoCommentModel.findAll({ | ||
90 | include: [ | ||
91 | { | ||
92 | model: VideoModel.unscoped() | ||
93 | }, | ||
94 | { | ||
95 | model: AccountModel.unscoped(), | ||
96 | include: [ | ||
97 | { | ||
98 | model: ActorModel.unscoped() | ||
99 | } | ||
100 | ] | ||
101 | } | ||
102 | ] | ||
103 | }) | ||
104 | for (const comment of videoComments) { | ||
105 | if (comment.isOwned() === false) continue | ||
106 | |||
107 | console.log('Updating comment ' + comment.url) | ||
108 | |||
109 | comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment) | ||
110 | await comment.save() | ||
111 | } | ||
112 | |||
113 | console.log('Updating video and torrent files.') | ||
114 | |||
115 | const ids = await VideoModel.listLocalIds() | ||
116 | for (const id of ids) { | ||
117 | const video = await VideoModel.loadFull(id) | ||
118 | |||
119 | console.log('Updating video ' + video.uuid) | ||
120 | |||
121 | video.url = getLocalVideoActivityPubUrl(video) | ||
122 | await video.save() | ||
123 | |||
124 | for (const file of video.VideoFiles) { | ||
125 | console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid) | ||
126 | await updateTorrentMetadata(video, file) | ||
127 | |||
128 | await file.save() | ||
129 | } | ||
130 | |||
131 | const playlist = video.getHLSPlaylist() | ||
132 | for (const file of (playlist?.VideoFiles || [])) { | ||
133 | console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid) | ||
134 | |||
135 | await updateTorrentMetadata(playlist, file) | ||
136 | |||
137 | await file.save() | ||
138 | } | ||
139 | } | ||
140 | } | ||