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/local-actor.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/local-actor.ts')
-rw-r--r-- | server/lib/local-actor.ts | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts deleted file mode 100644 index 611e6d0af..000000000 --- a/server/lib/local-actor.ts +++ /dev/null | |||
@@ -1,102 +0,0 @@ | |||
1 | import { remove } from 'fs-extra' | ||
2 | import { join } from 'path' | ||
3 | import { Transaction } from 'sequelize/types' | ||
4 | import { ActorModel } from '@server/models/actor/actor' | ||
5 | import { getLowercaseExtension } from '@shared/core-utils' | ||
6 | import { buildUUID } from '@shared/extra-utils' | ||
7 | import { ActivityPubActorType, ActorImageType } from '@shared/models' | ||
8 | import { retryTransactionWrapper } from '../helpers/database-utils' | ||
9 | import { CONFIG } from '../initializers/config' | ||
10 | import { ACTOR_IMAGES_SIZE, WEBSERVER } from '../initializers/constants' | ||
11 | import { sequelizeTypescript } from '../initializers/database' | ||
12 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' | ||
13 | import { deleteActorImages, updateActorImages } from './activitypub/actors' | ||
14 | import { sendUpdateActor } from './activitypub/send' | ||
15 | import { processImageFromWorker } from './worker/parent-process' | ||
16 | |||
17 | export function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | ||
18 | return new ActorModel({ | ||
19 | type, | ||
20 | url, | ||
21 | preferredUsername, | ||
22 | publicKey: null, | ||
23 | privateKey: null, | ||
24 | followersCount: 0, | ||
25 | followingCount: 0, | ||
26 | inboxUrl: url + '/inbox', | ||
27 | outboxUrl: url + '/outbox', | ||
28 | sharedInboxUrl: WEBSERVER.URL + '/inbox', | ||
29 | followersUrl: url + '/followers', | ||
30 | followingUrl: url + '/following' | ||
31 | }) as MActor | ||
32 | } | ||
33 | |||
34 | export async function updateLocalActorImageFiles ( | ||
35 | accountOrChannel: MAccountDefault | MChannelDefault, | ||
36 | imagePhysicalFile: Express.Multer.File, | ||
37 | type: ActorImageType | ||
38 | ) { | ||
39 | const processImageSize = async (imageSize: { width: number, height: number }) => { | ||
40 | const extension = getLowercaseExtension(imagePhysicalFile.filename) | ||
41 | |||
42 | const imageName = buildUUID() + extension | ||
43 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, imageName) | ||
44 | await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true }) | ||
45 | |||
46 | return { | ||
47 | imageName, | ||
48 | imageSize | ||
49 | } | ||
50 | } | ||
51 | |||
52 | const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize)) | ||
53 | await remove(imagePhysicalFile.path) | ||
54 | |||
55 | return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => { | ||
56 | const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({ | ||
57 | name: imageName, | ||
58 | fileUrl: null, | ||
59 | height: imageSize.height, | ||
60 | width: imageSize.width, | ||
61 | onDisk: true | ||
62 | })) | ||
63 | |||
64 | const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t) | ||
65 | await updatedActor.save({ transaction: t }) | ||
66 | |||
67 | await sendUpdateActor(accountOrChannel, t) | ||
68 | |||
69 | return type === ActorImageType.AVATAR | ||
70 | ? updatedActor.Avatars | ||
71 | : updatedActor.Banners | ||
72 | })) | ||
73 | } | ||
74 | |||
75 | export async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) { | ||
76 | return retryTransactionWrapper(() => { | ||
77 | return sequelizeTypescript.transaction(async t => { | ||
78 | const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t) | ||
79 | await updatedActor.save({ transaction: t }) | ||
80 | |||
81 | await sendUpdateActor(accountOrChannel, t) | ||
82 | |||
83 | return updatedActor.Avatars | ||
84 | }) | ||
85 | }) | ||
86 | } | ||
87 | |||
88 | // --------------------------------------------------------------------------- | ||
89 | |||
90 | export async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) { | ||
91 | let actor = await ActorModel.loadLocalByName(baseActorName, transaction) | ||
92 | if (!actor) return baseActorName | ||
93 | |||
94 | for (let i = 1; i < 30; i++) { | ||
95 | const name = `${baseActorName}-${i}` | ||
96 | |||
97 | actor = await ActorModel.loadLocalByName(name, transaction) | ||
98 | if (!actor) return name | ||
99 | } | ||
100 | |||
101 | throw new Error('Cannot find available actor local name (too much iterations).') | ||
102 | } | ||