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/share.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/share.ts')
-rw-r--r-- | server/lib/activitypub/share.ts | 120 |
1 files changed, 0 insertions, 120 deletions
diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts deleted file mode 100644 index 792a73f2a..000000000 --- a/server/lib/activitypub/share.ts +++ /dev/null | |||
@@ -1,120 +0,0 @@ | |||
1 | import { map } from 'bluebird' | ||
2 | import { Transaction } from 'sequelize' | ||
3 | import { getServerActor } from '@server/models/application/application' | ||
4 | import { logger, loggerTagsFactory } from '../../helpers/logger' | ||
5 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | ||
6 | import { VideoShareModel } from '../../models/video/video-share' | ||
7 | import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video' | ||
8 | import { fetchAP, getAPId } from './activity' | ||
9 | import { getOrCreateAPActor } from './actors' | ||
10 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' | ||
11 | import { checkUrlsSameHost, getLocalVideoAnnounceActivityPubUrl } from './url' | ||
12 | |||
13 | const lTags = loggerTagsFactory('share') | ||
14 | |||
15 | async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) { | ||
16 | if (!video.hasPrivacyForFederation()) return undefined | ||
17 | |||
18 | return Promise.all([ | ||
19 | shareByServer(video, t), | ||
20 | shareByVideoChannel(video, t) | ||
21 | ]) | ||
22 | } | ||
23 | |||
24 | async function changeVideoChannelShare ( | ||
25 | video: MVideoAccountLight, | ||
26 | oldVideoChannel: MChannelActorLight, | ||
27 | t: Transaction | ||
28 | ) { | ||
29 | logger.info( | ||
30 | 'Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name, | ||
31 | lTags(video.uuid) | ||
32 | ) | ||
33 | |||
34 | await undoShareByVideoChannel(video, oldVideoChannel, t) | ||
35 | |||
36 | await shareByVideoChannel(video, t) | ||
37 | } | ||
38 | |||
39 | async function addVideoShares (shareUrls: string[], video: MVideoId) { | ||
40 | await map(shareUrls, async shareUrl => { | ||
41 | try { | ||
42 | await addVideoShare(shareUrl, video) | ||
43 | } catch (err) { | ||
44 | logger.warn('Cannot add share %s.', shareUrl, { err }) | ||
45 | } | ||
46 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | ||
47 | } | ||
48 | |||
49 | export { | ||
50 | changeVideoChannelShare, | ||
51 | addVideoShares, | ||
52 | shareVideoByServerAndChannel | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | async function addVideoShare (shareUrl: string, video: MVideoId) { | ||
58 | const { body } = await fetchAP<any>(shareUrl) | ||
59 | if (!body?.actor) throw new Error('Body or body actor is invalid') | ||
60 | |||
61 | const actorUrl = getAPId(body.actor) | ||
62 | if (checkUrlsSameHost(shareUrl, actorUrl) !== true) { | ||
63 | throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`) | ||
64 | } | ||
65 | |||
66 | const actor = await getOrCreateAPActor(actorUrl) | ||
67 | |||
68 | const entry = { | ||
69 | actorId: actor.id, | ||
70 | videoId: video.id, | ||
71 | url: shareUrl | ||
72 | } | ||
73 | |||
74 | await VideoShareModel.upsert(entry) | ||
75 | } | ||
76 | |||
77 | async function shareByServer (video: MVideo, t: Transaction) { | ||
78 | const serverActor = await getServerActor() | ||
79 | |||
80 | const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(serverActor, video) | ||
81 | const [ serverShare ] = await VideoShareModel.findOrCreate({ | ||
82 | defaults: { | ||
83 | actorId: serverActor.id, | ||
84 | videoId: video.id, | ||
85 | url: serverShareUrl | ||
86 | }, | ||
87 | where: { | ||
88 | url: serverShareUrl | ||
89 | }, | ||
90 | transaction: t | ||
91 | }) | ||
92 | |||
93 | return sendVideoAnnounce(serverActor, serverShare, video, t) | ||
94 | } | ||
95 | |||
96 | async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) { | ||
97 | const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video) | ||
98 | const [ videoChannelShare ] = await VideoShareModel.findOrCreate({ | ||
99 | defaults: { | ||
100 | actorId: video.VideoChannel.actorId, | ||
101 | videoId: video.id, | ||
102 | url: videoChannelShareUrl | ||
103 | }, | ||
104 | where: { | ||
105 | url: videoChannelShareUrl | ||
106 | }, | ||
107 | transaction: t | ||
108 | }) | ||
109 | |||
110 | return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t) | ||
111 | } | ||
112 | |||
113 | async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) { | ||
114 | // Load old share | ||
115 | const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t) | ||
116 | if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id) | ||
117 | |||
118 | await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t) | ||
119 | await oldShare.destroy({ transaction: t }) | ||
120 | } | ||