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/controllers/api/users/my-subscriptions.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/controllers/api/users/my-subscriptions.ts')
-rw-r--r-- | server/controllers/api/users/my-subscriptions.ts | 193 |
1 files changed, 0 insertions, 193 deletions
diff --git a/server/controllers/api/users/my-subscriptions.ts b/server/controllers/api/users/my-subscriptions.ts deleted file mode 100644 index c4360f59d..000000000 --- a/server/controllers/api/users/my-subscriptions.ts +++ /dev/null | |||
@@ -1,193 +0,0 @@ | |||
1 | import 'multer' | ||
2 | import express from 'express' | ||
3 | import { handlesToNameAndHost } from '@server/helpers/actors' | ||
4 | import { pickCommonVideoQuery } from '@server/helpers/query' | ||
5 | import { sendUndoFollow } from '@server/lib/activitypub/send' | ||
6 | import { Hooks } from '@server/lib/plugins/hooks' | ||
7 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
8 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | ||
9 | import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils' | ||
10 | import { getFormattedObjects } from '../../../helpers/utils' | ||
11 | import { sequelizeTypescript } from '../../../initializers/database' | ||
12 | import { JobQueue } from '../../../lib/job-queue' | ||
13 | import { | ||
14 | asyncMiddleware, | ||
15 | asyncRetryTransactionMiddleware, | ||
16 | authenticate, | ||
17 | commonVideosFiltersValidator, | ||
18 | paginationValidator, | ||
19 | setDefaultPagination, | ||
20 | setDefaultSort, | ||
21 | setDefaultVideosSort, | ||
22 | userSubscriptionAddValidator, | ||
23 | userSubscriptionGetValidator | ||
24 | } from '../../../middlewares' | ||
25 | import { | ||
26 | areSubscriptionsExistValidator, | ||
27 | userSubscriptionListValidator, | ||
28 | userSubscriptionsSortValidator, | ||
29 | videosSortValidator | ||
30 | } from '../../../middlewares/validators' | ||
31 | import { ActorFollowModel } from '../../../models/actor/actor-follow' | ||
32 | import { guessAdditionalAttributesFromQuery } from '../../../models/video/formatter' | ||
33 | import { VideoModel } from '../../../models/video/video' | ||
34 | |||
35 | const mySubscriptionsRouter = express.Router() | ||
36 | |||
37 | mySubscriptionsRouter.get('/me/subscriptions/videos', | ||
38 | authenticate, | ||
39 | paginationValidator, | ||
40 | videosSortValidator, | ||
41 | setDefaultVideosSort, | ||
42 | setDefaultPagination, | ||
43 | commonVideosFiltersValidator, | ||
44 | asyncMiddleware(getUserSubscriptionVideos) | ||
45 | ) | ||
46 | |||
47 | mySubscriptionsRouter.get('/me/subscriptions/exist', | ||
48 | authenticate, | ||
49 | areSubscriptionsExistValidator, | ||
50 | asyncMiddleware(areSubscriptionsExist) | ||
51 | ) | ||
52 | |||
53 | mySubscriptionsRouter.get('/me/subscriptions', | ||
54 | authenticate, | ||
55 | paginationValidator, | ||
56 | userSubscriptionsSortValidator, | ||
57 | setDefaultSort, | ||
58 | setDefaultPagination, | ||
59 | userSubscriptionListValidator, | ||
60 | asyncMiddleware(getUserSubscriptions) | ||
61 | ) | ||
62 | |||
63 | mySubscriptionsRouter.post('/me/subscriptions', | ||
64 | authenticate, | ||
65 | userSubscriptionAddValidator, | ||
66 | addUserSubscription | ||
67 | ) | ||
68 | |||
69 | mySubscriptionsRouter.get('/me/subscriptions/:uri', | ||
70 | authenticate, | ||
71 | userSubscriptionGetValidator, | ||
72 | asyncMiddleware(getUserSubscription) | ||
73 | ) | ||
74 | |||
75 | mySubscriptionsRouter.delete('/me/subscriptions/:uri', | ||
76 | authenticate, | ||
77 | userSubscriptionGetValidator, | ||
78 | asyncRetryTransactionMiddleware(deleteUserSubscription) | ||
79 | ) | ||
80 | |||
81 | // --------------------------------------------------------------------------- | ||
82 | |||
83 | export { | ||
84 | mySubscriptionsRouter | ||
85 | } | ||
86 | |||
87 | // --------------------------------------------------------------------------- | ||
88 | |||
89 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | ||
90 | const uris = req.query.uris as string[] | ||
91 | const user = res.locals.oauth.token.User | ||
92 | |||
93 | const sanitizedHandles = handlesToNameAndHost(uris) | ||
94 | |||
95 | const results = await ActorFollowModel.listSubscriptionsOf(user.Account.Actor.id, sanitizedHandles) | ||
96 | |||
97 | const existObject: { [id: string ]: boolean } = {} | ||
98 | for (const sanitizedHandle of sanitizedHandles) { | ||
99 | const obj = results.find(r => { | ||
100 | const server = r.ActorFollowing.Server | ||
101 | |||
102 | return r.ActorFollowing.preferredUsername.toLowerCase() === sanitizedHandle.name.toLowerCase() && | ||
103 | ( | ||
104 | (!server && !sanitizedHandle.host) || | ||
105 | (server.host === sanitizedHandle.host) | ||
106 | ) | ||
107 | }) | ||
108 | |||
109 | existObject[sanitizedHandle.handle] = obj !== undefined | ||
110 | } | ||
111 | |||
112 | return res.json(existObject) | ||
113 | } | ||
114 | |||
115 | function addUserSubscription (req: express.Request, res: express.Response) { | ||
116 | const user = res.locals.oauth.token.User | ||
117 | const [ name, host ] = req.body.uri.split('@') | ||
118 | |||
119 | const payload = { | ||
120 | name, | ||
121 | host, | ||
122 | assertIsChannel: true, | ||
123 | followerActorId: user.Account.Actor.id | ||
124 | } | ||
125 | |||
126 | JobQueue.Instance.createJobAsync({ type: 'activitypub-follow', payload }) | ||
127 | |||
128 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | ||
129 | } | ||
130 | |||
131 | async function getUserSubscription (req: express.Request, res: express.Response) { | ||
132 | const subscription = res.locals.subscription | ||
133 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(subscription.ActorFollowing.VideoChannel.id) | ||
134 | |||
135 | return res.json(videoChannel.toFormattedJSON()) | ||
136 | } | ||
137 | |||
138 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | ||
139 | const subscription = res.locals.subscription | ||
140 | |||
141 | await sequelizeTypescript.transaction(async t => { | ||
142 | if (subscription.state === 'accepted') { | ||
143 | sendUndoFollow(subscription, t) | ||
144 | } | ||
145 | |||
146 | return subscription.destroy({ transaction: t }) | ||
147 | }) | ||
148 | |||
149 | return res.type('json') | ||
150 | .status(HttpStatusCode.NO_CONTENT_204) | ||
151 | .end() | ||
152 | } | ||
153 | |||
154 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | ||
155 | const user = res.locals.oauth.token.User | ||
156 | const actorId = user.Account.Actor.id | ||
157 | |||
158 | const resultList = await ActorFollowModel.listSubscriptionsForApi({ | ||
159 | actorId, | ||
160 | start: req.query.start, | ||
161 | count: req.query.count, | ||
162 | sort: req.query.sort, | ||
163 | search: req.query.search | ||
164 | }) | ||
165 | |||
166 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
167 | } | ||
168 | |||
169 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response) { | ||
170 | const user = res.locals.oauth.token.User | ||
171 | const countVideos = getCountVideos(req) | ||
172 | const query = pickCommonVideoQuery(req.query) | ||
173 | |||
174 | const apiOptions = await Hooks.wrapObject({ | ||
175 | ...query, | ||
176 | |||
177 | displayOnlyForFollower: { | ||
178 | actorId: user.Account.Actor.id, | ||
179 | orLocalVideos: false | ||
180 | }, | ||
181 | nsfw: buildNSFWFilter(res, query.nsfw), | ||
182 | user, | ||
183 | countVideos | ||
184 | }, 'filter:api.user.me.subscription-videos.list.params') | ||
185 | |||
186 | const resultList = await Hooks.wrapPromiseFun( | ||
187 | VideoModel.listForApi, | ||
188 | apiOptions, | ||
189 | 'filter:api.user.me.subscription-videos.list.result' | ||
190 | ) | ||
191 | |||
192 | return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) | ||
193 | } | ||