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 /packages/server-commands/src/users | |
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 'packages/server-commands/src/users')
-rw-r--r-- | packages/server-commands/src/users/accounts-command.ts | 76 | ||||
-rw-r--r-- | packages/server-commands/src/users/accounts.ts | 15 | ||||
-rw-r--r-- | packages/server-commands/src/users/blocklist-command.ts | 165 | ||||
-rw-r--r-- | packages/server-commands/src/users/index.ts | 10 | ||||
-rw-r--r-- | packages/server-commands/src/users/login-command.ts | 159 | ||||
-rw-r--r-- | packages/server-commands/src/users/login.ts | 19 | ||||
-rw-r--r-- | packages/server-commands/src/users/notifications-command.ts | 85 | ||||
-rw-r--r-- | packages/server-commands/src/users/registrations-command.ts | 157 | ||||
-rw-r--r-- | packages/server-commands/src/users/subscriptions-command.ts | 83 | ||||
-rw-r--r-- | packages/server-commands/src/users/two-factor-command.ts | 92 | ||||
-rw-r--r-- | packages/server-commands/src/users/users-command.ts | 389 |
11 files changed, 1250 insertions, 0 deletions
diff --git a/packages/server-commands/src/users/accounts-command.ts b/packages/server-commands/src/users/accounts-command.ts new file mode 100644 index 000000000..fd98b7eea --- /dev/null +++ b/packages/server-commands/src/users/accounts-command.ts | |||
@@ -0,0 +1,76 @@ | |||
1 | import { Account, AccountVideoRate, ActorFollow, HttpStatusCode, ResultList, VideoRateType } from '@peertube/peertube-models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
3 | |||
4 | export class AccountsCommand extends AbstractCommand { | ||
5 | |||
6 | list (options: OverrideCommandOptions & { | ||
7 | sort?: string // default -createdAt | ||
8 | } = {}) { | ||
9 | const { sort = '-createdAt' } = options | ||
10 | const path = '/api/v1/accounts' | ||
11 | |||
12 | return this.getRequestBody<ResultList<Account>>({ | ||
13 | ...options, | ||
14 | |||
15 | path, | ||
16 | query: { sort }, | ||
17 | implicitToken: false, | ||
18 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
19 | }) | ||
20 | } | ||
21 | |||
22 | get (options: OverrideCommandOptions & { | ||
23 | accountName: string | ||
24 | }) { | ||
25 | const path = '/api/v1/accounts/' + options.accountName | ||
26 | |||
27 | return this.getRequestBody<Account>({ | ||
28 | ...options, | ||
29 | |||
30 | path, | ||
31 | implicitToken: false, | ||
32 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | listRatings (options: OverrideCommandOptions & { | ||
37 | accountName: string | ||
38 | rating?: VideoRateType | ||
39 | }) { | ||
40 | const { rating, accountName } = options | ||
41 | const path = '/api/v1/accounts/' + accountName + '/ratings' | ||
42 | |||
43 | const query = { rating } | ||
44 | |||
45 | return this.getRequestBody<ResultList<AccountVideoRate>>({ | ||
46 | ...options, | ||
47 | |||
48 | path, | ||
49 | query, | ||
50 | implicitToken: true, | ||
51 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | listFollowers (options: OverrideCommandOptions & { | ||
56 | accountName: string | ||
57 | start?: number | ||
58 | count?: number | ||
59 | sort?: string | ||
60 | search?: string | ||
61 | }) { | ||
62 | const { accountName, start, count, sort, search } = options | ||
63 | const path = '/api/v1/accounts/' + accountName + '/followers' | ||
64 | |||
65 | const query = { start, count, sort, search } | ||
66 | |||
67 | return this.getRequestBody<ResultList<ActorFollow>>({ | ||
68 | ...options, | ||
69 | |||
70 | path, | ||
71 | query, | ||
72 | implicitToken: true, | ||
73 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
74 | }) | ||
75 | } | ||
76 | } | ||
diff --git a/packages/server-commands/src/users/accounts.ts b/packages/server-commands/src/users/accounts.ts new file mode 100644 index 000000000..3b8b9d36a --- /dev/null +++ b/packages/server-commands/src/users/accounts.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | import { PeerTubeServer } from '../server/server.js' | ||
2 | |||
3 | async function setDefaultAccountAvatar (serversArg: PeerTubeServer | PeerTubeServer[], token?: string) { | ||
4 | const servers = Array.isArray(serversArg) | ||
5 | ? serversArg | ||
6 | : [ serversArg ] | ||
7 | |||
8 | for (const server of servers) { | ||
9 | await server.users.updateMyAvatar({ fixture: 'avatar.png', token }) | ||
10 | } | ||
11 | } | ||
12 | |||
13 | export { | ||
14 | setDefaultAccountAvatar | ||
15 | } | ||
diff --git a/packages/server-commands/src/users/blocklist-command.ts b/packages/server-commands/src/users/blocklist-command.ts new file mode 100644 index 000000000..c77c56131 --- /dev/null +++ b/packages/server-commands/src/users/blocklist-command.ts | |||
@@ -0,0 +1,165 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@peertube/peertube-models' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
5 | |||
6 | type ListBlocklistOptions = OverrideCommandOptions & { | ||
7 | start: number | ||
8 | count: number | ||
9 | |||
10 | sort?: string // default -createdAt | ||
11 | |||
12 | search?: string | ||
13 | } | ||
14 | |||
15 | export class BlocklistCommand extends AbstractCommand { | ||
16 | |||
17 | listMyAccountBlocklist (options: ListBlocklistOptions) { | ||
18 | const path = '/api/v1/users/me/blocklist/accounts' | ||
19 | |||
20 | return this.listBlocklist<AccountBlock>(options, path) | ||
21 | } | ||
22 | |||
23 | listMyServerBlocklist (options: ListBlocklistOptions) { | ||
24 | const path = '/api/v1/users/me/blocklist/servers' | ||
25 | |||
26 | return this.listBlocklist<ServerBlock>(options, path) | ||
27 | } | ||
28 | |||
29 | listServerAccountBlocklist (options: ListBlocklistOptions) { | ||
30 | const path = '/api/v1/server/blocklist/accounts' | ||
31 | |||
32 | return this.listBlocklist<AccountBlock>(options, path) | ||
33 | } | ||
34 | |||
35 | listServerServerBlocklist (options: ListBlocklistOptions) { | ||
36 | const path = '/api/v1/server/blocklist/servers' | ||
37 | |||
38 | return this.listBlocklist<ServerBlock>(options, path) | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | getStatus (options: OverrideCommandOptions & { | ||
44 | accounts?: string[] | ||
45 | hosts?: string[] | ||
46 | }) { | ||
47 | const { accounts, hosts } = options | ||
48 | |||
49 | const path = '/api/v1/blocklist/status' | ||
50 | |||
51 | return this.getRequestBody<BlockStatus>({ | ||
52 | ...options, | ||
53 | |||
54 | path, | ||
55 | query: { | ||
56 | accounts, | ||
57 | hosts | ||
58 | }, | ||
59 | implicitToken: false, | ||
60 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | addToMyBlocklist (options: OverrideCommandOptions & { | ||
67 | account?: string | ||
68 | server?: string | ||
69 | }) { | ||
70 | const { account, server } = options | ||
71 | |||
72 | const path = account | ||
73 | ? '/api/v1/users/me/blocklist/accounts' | ||
74 | : '/api/v1/users/me/blocklist/servers' | ||
75 | |||
76 | return this.postBodyRequest({ | ||
77 | ...options, | ||
78 | |||
79 | path, | ||
80 | fields: { | ||
81 | accountName: account, | ||
82 | host: server | ||
83 | }, | ||
84 | implicitToken: true, | ||
85 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | addToServerBlocklist (options: OverrideCommandOptions & { | ||
90 | account?: string | ||
91 | server?: string | ||
92 | }) { | ||
93 | const { account, server } = options | ||
94 | |||
95 | const path = account | ||
96 | ? '/api/v1/server/blocklist/accounts' | ||
97 | : '/api/v1/server/blocklist/servers' | ||
98 | |||
99 | return this.postBodyRequest({ | ||
100 | ...options, | ||
101 | |||
102 | path, | ||
103 | fields: { | ||
104 | accountName: account, | ||
105 | host: server | ||
106 | }, | ||
107 | implicitToken: true, | ||
108 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | // --------------------------------------------------------------------------- | ||
113 | |||
114 | removeFromMyBlocklist (options: OverrideCommandOptions & { | ||
115 | account?: string | ||
116 | server?: string | ||
117 | }) { | ||
118 | const { account, server } = options | ||
119 | |||
120 | const path = account | ||
121 | ? '/api/v1/users/me/blocklist/accounts/' + account | ||
122 | : '/api/v1/users/me/blocklist/servers/' + server | ||
123 | |||
124 | return this.deleteRequest({ | ||
125 | ...options, | ||
126 | |||
127 | path, | ||
128 | implicitToken: true, | ||
129 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | removeFromServerBlocklist (options: OverrideCommandOptions & { | ||
134 | account?: string | ||
135 | server?: string | ||
136 | }) { | ||
137 | const { account, server } = options | ||
138 | |||
139 | const path = account | ||
140 | ? '/api/v1/server/blocklist/accounts/' + account | ||
141 | : '/api/v1/server/blocklist/servers/' + server | ||
142 | |||
143 | return this.deleteRequest({ | ||
144 | ...options, | ||
145 | |||
146 | path, | ||
147 | implicitToken: true, | ||
148 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
149 | }) | ||
150 | } | ||
151 | |||
152 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | ||
153 | const { start, count, search, sort = '-createdAt' } = options | ||
154 | |||
155 | return this.getRequestBody<ResultList<T>>({ | ||
156 | ...options, | ||
157 | |||
158 | path, | ||
159 | query: { start, count, sort, search }, | ||
160 | implicitToken: true, | ||
161 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
162 | }) | ||
163 | } | ||
164 | |||
165 | } | ||
diff --git a/packages/server-commands/src/users/index.ts b/packages/server-commands/src/users/index.ts new file mode 100644 index 000000000..baa048a43 --- /dev/null +++ b/packages/server-commands/src/users/index.ts | |||
@@ -0,0 +1,10 @@ | |||
1 | export * from './accounts-command.js' | ||
2 | export * from './accounts.js' | ||
3 | export * from './blocklist-command.js' | ||
4 | export * from './login.js' | ||
5 | export * from './login-command.js' | ||
6 | export * from './notifications-command.js' | ||
7 | export * from './registrations-command.js' | ||
8 | export * from './subscriptions-command.js' | ||
9 | export * from './two-factor-command.js' | ||
10 | export * from './users-command.js' | ||
diff --git a/packages/server-commands/src/users/login-command.ts b/packages/server-commands/src/users/login-command.ts new file mode 100644 index 000000000..92d123dfc --- /dev/null +++ b/packages/server-commands/src/users/login-command.ts | |||
@@ -0,0 +1,159 @@ | |||
1 | import { HttpStatusCode, PeerTubeProblemDocument } from '@peertube/peertube-models' | ||
2 | import { unwrapBody } from '../requests/index.js' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
4 | |||
5 | type LoginOptions = OverrideCommandOptions & { | ||
6 | client?: { id?: string, secret?: string } | ||
7 | user?: { username: string, password?: string } | ||
8 | otpToken?: string | ||
9 | } | ||
10 | |||
11 | export class LoginCommand extends AbstractCommand { | ||
12 | |||
13 | async login (options: LoginOptions = {}) { | ||
14 | const res = await this._login(options) | ||
15 | |||
16 | return this.unwrapLoginBody(res.body) | ||
17 | } | ||
18 | |||
19 | async loginAndGetResponse (options: LoginOptions = {}) { | ||
20 | const res = await this._login(options) | ||
21 | |||
22 | return { | ||
23 | res, | ||
24 | body: this.unwrapLoginBody(res.body) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | getAccessToken (arg1?: { username: string, password?: string }): Promise<string> | ||
29 | getAccessToken (arg1: string, password?: string): Promise<string> | ||
30 | async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) { | ||
31 | let user: { username: string, password?: string } | ||
32 | |||
33 | if (!arg1) user = this.server.store.user | ||
34 | else if (typeof arg1 === 'object') user = arg1 | ||
35 | else user = { username: arg1, password } | ||
36 | |||
37 | try { | ||
38 | const body = await this.login({ user }) | ||
39 | |||
40 | return body.access_token | ||
41 | } catch (err) { | ||
42 | throw new Error(`Cannot authenticate. Please check your username/password. (${err})`) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | loginUsingExternalToken (options: OverrideCommandOptions & { | ||
47 | username: string | ||
48 | externalAuthToken: string | ||
49 | }) { | ||
50 | const { username, externalAuthToken } = options | ||
51 | const path = '/api/v1/users/token' | ||
52 | |||
53 | const body = { | ||
54 | client_id: this.server.store.client.id, | ||
55 | client_secret: this.server.store.client.secret, | ||
56 | username, | ||
57 | response_type: 'code', | ||
58 | grant_type: 'password', | ||
59 | scope: 'upload', | ||
60 | externalAuthToken | ||
61 | } | ||
62 | |||
63 | return this.postBodyRequest({ | ||
64 | ...options, | ||
65 | |||
66 | path, | ||
67 | requestType: 'form', | ||
68 | fields: body, | ||
69 | implicitToken: false, | ||
70 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | logout (options: OverrideCommandOptions & { | ||
75 | token: string | ||
76 | }) { | ||
77 | const path = '/api/v1/users/revoke-token' | ||
78 | |||
79 | return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({ | ||
80 | ...options, | ||
81 | |||
82 | path, | ||
83 | requestType: 'form', | ||
84 | implicitToken: false, | ||
85 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
86 | })) | ||
87 | } | ||
88 | |||
89 | refreshToken (options: OverrideCommandOptions & { | ||
90 | refreshToken: string | ||
91 | }) { | ||
92 | const path = '/api/v1/users/token' | ||
93 | |||
94 | const body = { | ||
95 | client_id: this.server.store.client.id, | ||
96 | client_secret: this.server.store.client.secret, | ||
97 | refresh_token: options.refreshToken, | ||
98 | response_type: 'code', | ||
99 | grant_type: 'refresh_token' | ||
100 | } | ||
101 | |||
102 | return this.postBodyRequest({ | ||
103 | ...options, | ||
104 | |||
105 | path, | ||
106 | requestType: 'form', | ||
107 | fields: body, | ||
108 | implicitToken: false, | ||
109 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
110 | }) | ||
111 | } | ||
112 | |||
113 | getClient (options: OverrideCommandOptions = {}) { | ||
114 | const path = '/api/v1/oauth-clients/local' | ||
115 | |||
116 | return this.getRequestBody<{ client_id: string, client_secret: string }>({ | ||
117 | ...options, | ||
118 | |||
119 | path, | ||
120 | host: this.server.host, | ||
121 | implicitToken: false, | ||
122 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
123 | }) | ||
124 | } | ||
125 | |||
126 | private _login (options: LoginOptions) { | ||
127 | const { client = this.server.store.client, user = this.server.store.user, otpToken } = options | ||
128 | const path = '/api/v1/users/token' | ||
129 | |||
130 | const body = { | ||
131 | client_id: client.id, | ||
132 | client_secret: client.secret, | ||
133 | username: user.username, | ||
134 | password: user.password ?? 'password', | ||
135 | response_type: 'code', | ||
136 | grant_type: 'password', | ||
137 | scope: 'upload' | ||
138 | } | ||
139 | |||
140 | const headers = otpToken | ||
141 | ? { 'x-peertube-otp': otpToken } | ||
142 | : {} | ||
143 | |||
144 | return this.postBodyRequest({ | ||
145 | ...options, | ||
146 | |||
147 | path, | ||
148 | headers, | ||
149 | requestType: 'form', | ||
150 | fields: body, | ||
151 | implicitToken: false, | ||
152 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
153 | }) | ||
154 | } | ||
155 | |||
156 | private unwrapLoginBody (body: any) { | ||
157 | return body as { access_token: string, refresh_token: string } & PeerTubeProblemDocument | ||
158 | } | ||
159 | } | ||
diff --git a/packages/server-commands/src/users/login.ts b/packages/server-commands/src/users/login.ts new file mode 100644 index 000000000..c48c42c72 --- /dev/null +++ b/packages/server-commands/src/users/login.ts | |||
@@ -0,0 +1,19 @@ | |||
1 | import { PeerTubeServer } from '../server/server.js' | ||
2 | |||
3 | function setAccessTokensToServers (servers: PeerTubeServer[]) { | ||
4 | const tasks: Promise<any>[] = [] | ||
5 | |||
6 | for (const server of servers) { | ||
7 | const p = server.login.getAccessToken() | ||
8 | .then(t => { server.accessToken = t }) | ||
9 | tasks.push(p) | ||
10 | } | ||
11 | |||
12 | return Promise.all(tasks) | ||
13 | } | ||
14 | |||
15 | // --------------------------------------------------------------------------- | ||
16 | |||
17 | export { | ||
18 | setAccessTokensToServers | ||
19 | } | ||
diff --git a/packages/server-commands/src/users/notifications-command.ts b/packages/server-commands/src/users/notifications-command.ts new file mode 100644 index 000000000..d90d56900 --- /dev/null +++ b/packages/server-commands/src/users/notifications-command.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import { HttpStatusCode, ResultList, UserNotification, UserNotificationSetting } from '@peertube/peertube-models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
3 | |||
4 | export class NotificationsCommand extends AbstractCommand { | ||
5 | |||
6 | updateMySettings (options: OverrideCommandOptions & { | ||
7 | settings: UserNotificationSetting | ||
8 | }) { | ||
9 | const path = '/api/v1/users/me/notification-settings' | ||
10 | |||
11 | return this.putBodyRequest({ | ||
12 | ...options, | ||
13 | |||
14 | path, | ||
15 | fields: options.settings, | ||
16 | implicitToken: true, | ||
17 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
18 | }) | ||
19 | } | ||
20 | |||
21 | list (options: OverrideCommandOptions & { | ||
22 | start?: number | ||
23 | count?: number | ||
24 | unread?: boolean | ||
25 | sort?: string | ||
26 | }) { | ||
27 | const { start, count, unread, sort = '-createdAt' } = options | ||
28 | const path = '/api/v1/users/me/notifications' | ||
29 | |||
30 | return this.getRequestBody<ResultList<UserNotification>>({ | ||
31 | ...options, | ||
32 | |||
33 | path, | ||
34 | query: { | ||
35 | start, | ||
36 | count, | ||
37 | sort, | ||
38 | unread | ||
39 | }, | ||
40 | implicitToken: true, | ||
41 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
42 | }) | ||
43 | } | ||
44 | |||
45 | markAsRead (options: OverrideCommandOptions & { | ||
46 | ids: number[] | ||
47 | }) { | ||
48 | const { ids } = options | ||
49 | const path = '/api/v1/users/me/notifications/read' | ||
50 | |||
51 | return this.postBodyRequest({ | ||
52 | ...options, | ||
53 | |||
54 | path, | ||
55 | fields: { ids }, | ||
56 | implicitToken: true, | ||
57 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | markAsReadAll (options: OverrideCommandOptions) { | ||
62 | const path = '/api/v1/users/me/notifications/read-all' | ||
63 | |||
64 | return this.postBodyRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | implicitToken: true, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | async getLatest (options: OverrideCommandOptions = {}) { | ||
74 | const { total, data } = await this.list({ | ||
75 | ...options, | ||
76 | start: 0, | ||
77 | count: 1, | ||
78 | sort: '-createdAt' | ||
79 | }) | ||
80 | |||
81 | if (total === 0) return undefined | ||
82 | |||
83 | return data[0] | ||
84 | } | ||
85 | } | ||
diff --git a/packages/server-commands/src/users/registrations-command.ts b/packages/server-commands/src/users/registrations-command.ts new file mode 100644 index 000000000..2111fbd39 --- /dev/null +++ b/packages/server-commands/src/users/registrations-command.ts | |||
@@ -0,0 +1,157 @@ | |||
1 | import { pick } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | HttpStatusCode, | ||
4 | ResultList, | ||
5 | UserRegistration, | ||
6 | UserRegistrationRequest, | ||
7 | UserRegistrationUpdateState | ||
8 | } from '@peertube/peertube-models' | ||
9 | import { unwrapBody } from '../requests/index.js' | ||
10 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
11 | |||
12 | export class RegistrationsCommand extends AbstractCommand { | ||
13 | |||
14 | register (options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username'>) { | ||
15 | const { password = 'password', email = options.username + '@example.com' } = options | ||
16 | const path = '/api/v1/users/register' | ||
17 | |||
18 | return this.postBodyRequest({ | ||
19 | ...options, | ||
20 | |||
21 | path, | ||
22 | fields: { | ||
23 | ...pick(options, [ 'username', 'displayName', 'channel' ]), | ||
24 | |||
25 | password, | ||
26 | |||
27 | }, | ||
28 | implicitToken: false, | ||
29 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | requestRegistration ( | ||
34 | options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username' | 'registrationReason'> | ||
35 | ) { | ||
36 | const { password = 'password', email = options.username + '@example.com' } = options | ||
37 | const path = '/api/v1/users/registrations/request' | ||
38 | |||
39 | return unwrapBody<UserRegistration>(this.postBodyRequest({ | ||
40 | ...options, | ||
41 | |||
42 | path, | ||
43 | fields: { | ||
44 | ...pick(options, [ 'username', 'displayName', 'channel', 'registrationReason' ]), | ||
45 | |||
46 | password, | ||
47 | |||
48 | }, | ||
49 | implicitToken: false, | ||
50 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
51 | })) | ||
52 | } | ||
53 | |||
54 | // --------------------------------------------------------------------------- | ||
55 | |||
56 | accept (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) { | ||
57 | const { id } = options | ||
58 | const path = '/api/v1/users/registrations/' + id + '/accept' | ||
59 | |||
60 | return this.postBodyRequest({ | ||
61 | ...options, | ||
62 | |||
63 | path, | ||
64 | fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]), | ||
65 | implicitToken: true, | ||
66 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | reject (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) { | ||
71 | const { id } = options | ||
72 | const path = '/api/v1/users/registrations/' + id + '/reject' | ||
73 | |||
74 | return this.postBodyRequest({ | ||
75 | ...options, | ||
76 | |||
77 | path, | ||
78 | fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]), | ||
79 | implicitToken: true, | ||
80 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | // --------------------------------------------------------------------------- | ||
85 | |||
86 | delete (options: OverrideCommandOptions & { | ||
87 | id: number | ||
88 | }) { | ||
89 | const { id } = options | ||
90 | const path = '/api/v1/users/registrations/' + id | ||
91 | |||
92 | return this.deleteRequest({ | ||
93 | ...options, | ||
94 | |||
95 | path, | ||
96 | implicitToken: true, | ||
97 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
98 | }) | ||
99 | } | ||
100 | |||
101 | // --------------------------------------------------------------------------- | ||
102 | |||
103 | list (options: OverrideCommandOptions & { | ||
104 | start?: number | ||
105 | count?: number | ||
106 | sort?: string | ||
107 | search?: string | ||
108 | } = {}) { | ||
109 | const path = '/api/v1/users/registrations' | ||
110 | |||
111 | return this.getRequestBody<ResultList<UserRegistration>>({ | ||
112 | ...options, | ||
113 | |||
114 | path, | ||
115 | query: pick(options, [ 'start', 'count', 'sort', 'search' ]), | ||
116 | implicitToken: true, | ||
117 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
118 | }) | ||
119 | } | ||
120 | |||
121 | // --------------------------------------------------------------------------- | ||
122 | |||
123 | askSendVerifyEmail (options: OverrideCommandOptions & { | ||
124 | email: string | ||
125 | }) { | ||
126 | const { email } = options | ||
127 | const path = '/api/v1/users/registrations/ask-send-verify-email' | ||
128 | |||
129 | return this.postBodyRequest({ | ||
130 | ...options, | ||
131 | |||
132 | path, | ||
133 | fields: { email }, | ||
134 | implicitToken: false, | ||
135 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
136 | }) | ||
137 | } | ||
138 | |||
139 | verifyEmail (options: OverrideCommandOptions & { | ||
140 | registrationId: number | ||
141 | verificationString: string | ||
142 | }) { | ||
143 | const { registrationId, verificationString } = options | ||
144 | const path = '/api/v1/users/registrations/' + registrationId + '/verify-email' | ||
145 | |||
146 | return this.postBodyRequest({ | ||
147 | ...options, | ||
148 | |||
149 | path, | ||
150 | fields: { | ||
151 | verificationString | ||
152 | }, | ||
153 | implicitToken: false, | ||
154 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
155 | }) | ||
156 | } | ||
157 | } | ||
diff --git a/packages/server-commands/src/users/subscriptions-command.ts b/packages/server-commands/src/users/subscriptions-command.ts new file mode 100644 index 000000000..52a1f0e51 --- /dev/null +++ b/packages/server-commands/src/users/subscriptions-command.ts | |||
@@ -0,0 +1,83 @@ | |||
1 | import { HttpStatusCode, ResultList, VideoChannel } from '@peertube/peertube-models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
3 | |||
4 | export class SubscriptionsCommand extends AbstractCommand { | ||
5 | |||
6 | add (options: OverrideCommandOptions & { | ||
7 | targetUri: string | ||
8 | }) { | ||
9 | const path = '/api/v1/users/me/subscriptions' | ||
10 | |||
11 | return this.postBodyRequest({ | ||
12 | ...options, | ||
13 | |||
14 | path, | ||
15 | fields: { uri: options.targetUri }, | ||
16 | implicitToken: true, | ||
17 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
18 | }) | ||
19 | } | ||
20 | |||
21 | list (options: OverrideCommandOptions & { | ||
22 | sort?: string // default -createdAt | ||
23 | search?: string | ||
24 | } = {}) { | ||
25 | const { sort = '-createdAt', search } = options | ||
26 | const path = '/api/v1/users/me/subscriptions' | ||
27 | |||
28 | return this.getRequestBody<ResultList<VideoChannel>>({ | ||
29 | ...options, | ||
30 | |||
31 | path, | ||
32 | query: { | ||
33 | sort, | ||
34 | search | ||
35 | }, | ||
36 | implicitToken: true, | ||
37 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
38 | }) | ||
39 | } | ||
40 | |||
41 | get (options: OverrideCommandOptions & { | ||
42 | uri: string | ||
43 | }) { | ||
44 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
45 | |||
46 | return this.getRequestBody<VideoChannel>({ | ||
47 | ...options, | ||
48 | |||
49 | path, | ||
50 | implicitToken: true, | ||
51 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | remove (options: OverrideCommandOptions & { | ||
56 | uri: string | ||
57 | }) { | ||
58 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
59 | |||
60 | return this.deleteRequest({ | ||
61 | ...options, | ||
62 | |||
63 | path, | ||
64 | implicitToken: true, | ||
65 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
66 | }) | ||
67 | } | ||
68 | |||
69 | exist (options: OverrideCommandOptions & { | ||
70 | uris: string[] | ||
71 | }) { | ||
72 | const path = '/api/v1/users/me/subscriptions/exist' | ||
73 | |||
74 | return this.getRequestBody<{ [id: string ]: boolean }>({ | ||
75 | ...options, | ||
76 | |||
77 | path, | ||
78 | query: { 'uris[]': options.uris }, | ||
79 | implicitToken: true, | ||
80 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
81 | }) | ||
82 | } | ||
83 | } | ||
diff --git a/packages/server-commands/src/users/two-factor-command.ts b/packages/server-commands/src/users/two-factor-command.ts new file mode 100644 index 000000000..cf3d6cb68 --- /dev/null +++ b/packages/server-commands/src/users/two-factor-command.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import { TOTP } from 'otpauth' | ||
2 | import { HttpStatusCode, TwoFactorEnableResult } from '@peertube/peertube-models' | ||
3 | import { unwrapBody } from '../requests/index.js' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
5 | |||
6 | export class TwoFactorCommand extends AbstractCommand { | ||
7 | |||
8 | static buildOTP (options: { | ||
9 | secret: string | ||
10 | }) { | ||
11 | const { secret } = options | ||
12 | |||
13 | return new TOTP({ | ||
14 | issuer: 'PeerTube', | ||
15 | algorithm: 'SHA1', | ||
16 | digits: 6, | ||
17 | period: 30, | ||
18 | secret | ||
19 | }) | ||
20 | } | ||
21 | |||
22 | request (options: OverrideCommandOptions & { | ||
23 | userId: number | ||
24 | currentPassword?: string | ||
25 | }) { | ||
26 | const { currentPassword, userId } = options | ||
27 | |||
28 | const path = '/api/v1/users/' + userId + '/two-factor/request' | ||
29 | |||
30 | return unwrapBody<TwoFactorEnableResult>(this.postBodyRequest({ | ||
31 | ...options, | ||
32 | |||
33 | path, | ||
34 | fields: { currentPassword }, | ||
35 | implicitToken: true, | ||
36 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
37 | })) | ||
38 | } | ||
39 | |||
40 | confirmRequest (options: OverrideCommandOptions & { | ||
41 | userId: number | ||
42 | requestToken: string | ||
43 | otpToken: string | ||
44 | }) { | ||
45 | const { userId, requestToken, otpToken } = options | ||
46 | |||
47 | const path = '/api/v1/users/' + userId + '/two-factor/confirm-request' | ||
48 | |||
49 | return this.postBodyRequest({ | ||
50 | ...options, | ||
51 | |||
52 | path, | ||
53 | fields: { requestToken, otpToken }, | ||
54 | implicitToken: true, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | disable (options: OverrideCommandOptions & { | ||
60 | userId: number | ||
61 | currentPassword?: string | ||
62 | }) { | ||
63 | const { userId, currentPassword } = options | ||
64 | const path = '/api/v1/users/' + userId + '/two-factor/disable' | ||
65 | |||
66 | return this.postBodyRequest({ | ||
67 | ...options, | ||
68 | |||
69 | path, | ||
70 | fields: { currentPassword }, | ||
71 | implicitToken: true, | ||
72 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
73 | }) | ||
74 | } | ||
75 | |||
76 | async requestAndConfirm (options: OverrideCommandOptions & { | ||
77 | userId: number | ||
78 | currentPassword?: string | ||
79 | }) { | ||
80 | const { userId, currentPassword } = options | ||
81 | |||
82 | const { otpRequest } = await this.request({ userId, currentPassword }) | ||
83 | |||
84 | await this.confirmRequest({ | ||
85 | userId, | ||
86 | requestToken: otpRequest.requestToken, | ||
87 | otpToken: TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate() | ||
88 | }) | ||
89 | |||
90 | return otpRequest | ||
91 | } | ||
92 | } | ||
diff --git a/packages/server-commands/src/users/users-command.ts b/packages/server-commands/src/users/users-command.ts new file mode 100644 index 000000000..d3b11939e --- /dev/null +++ b/packages/server-commands/src/users/users-command.ts | |||
@@ -0,0 +1,389 @@ | |||
1 | import { omit, pick } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | HttpStatusCode, | ||
4 | MyUser, | ||
5 | ResultList, | ||
6 | ScopedToken, | ||
7 | User, | ||
8 | UserAdminFlagType, | ||
9 | UserCreateResult, | ||
10 | UserRole, | ||
11 | UserRoleType, | ||
12 | UserUpdate, | ||
13 | UserUpdateMe, | ||
14 | UserVideoQuota, | ||
15 | UserVideoRate | ||
16 | } from '@peertube/peertube-models' | ||
17 | import { unwrapBody } from '../requests/index.js' | ||
18 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
19 | |||
20 | export class UsersCommand extends AbstractCommand { | ||
21 | |||
22 | askResetPassword (options: OverrideCommandOptions & { | ||
23 | email: string | ||
24 | }) { | ||
25 | const { email } = options | ||
26 | const path = '/api/v1/users/ask-reset-password' | ||
27 | |||
28 | return this.postBodyRequest({ | ||
29 | ...options, | ||
30 | |||
31 | path, | ||
32 | fields: { email }, | ||
33 | implicitToken: false, | ||
34 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
35 | }) | ||
36 | } | ||
37 | |||
38 | resetPassword (options: OverrideCommandOptions & { | ||
39 | userId: number | ||
40 | verificationString: string | ||
41 | password: string | ||
42 | }) { | ||
43 | const { userId, verificationString, password } = options | ||
44 | const path = '/api/v1/users/' + userId + '/reset-password' | ||
45 | |||
46 | return this.postBodyRequest({ | ||
47 | ...options, | ||
48 | |||
49 | path, | ||
50 | fields: { password, verificationString }, | ||
51 | implicitToken: false, | ||
52 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
53 | }) | ||
54 | } | ||
55 | |||
56 | // --------------------------------------------------------------------------- | ||
57 | |||
58 | askSendVerifyEmail (options: OverrideCommandOptions & { | ||
59 | email: string | ||
60 | }) { | ||
61 | const { email } = options | ||
62 | const path = '/api/v1/users/ask-send-verify-email' | ||
63 | |||
64 | return this.postBodyRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | fields: { email }, | ||
69 | implicitToken: false, | ||
70 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | verifyEmail (options: OverrideCommandOptions & { | ||
75 | userId: number | ||
76 | verificationString: string | ||
77 | isPendingEmail?: boolean // default false | ||
78 | }) { | ||
79 | const { userId, verificationString, isPendingEmail = false } = options | ||
80 | const path = '/api/v1/users/' + userId + '/verify-email' | ||
81 | |||
82 | return this.postBodyRequest({ | ||
83 | ...options, | ||
84 | |||
85 | path, | ||
86 | fields: { | ||
87 | verificationString, | ||
88 | isPendingEmail | ||
89 | }, | ||
90 | implicitToken: false, | ||
91 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
92 | }) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | banUser (options: OverrideCommandOptions & { | ||
98 | userId: number | ||
99 | reason?: string | ||
100 | }) { | ||
101 | const { userId, reason } = options | ||
102 | const path = '/api/v1/users' + '/' + userId + '/block' | ||
103 | |||
104 | return this.postBodyRequest({ | ||
105 | ...options, | ||
106 | |||
107 | path, | ||
108 | fields: { reason }, | ||
109 | implicitToken: true, | ||
110 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
111 | }) | ||
112 | } | ||
113 | |||
114 | unbanUser (options: OverrideCommandOptions & { | ||
115 | userId: number | ||
116 | }) { | ||
117 | const { userId } = options | ||
118 | const path = '/api/v1/users' + '/' + userId + '/unblock' | ||
119 | |||
120 | return this.postBodyRequest({ | ||
121 | ...options, | ||
122 | |||
123 | path, | ||
124 | implicitToken: true, | ||
125 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
126 | }) | ||
127 | } | ||
128 | |||
129 | // --------------------------------------------------------------------------- | ||
130 | |||
131 | getMyScopedTokens (options: OverrideCommandOptions = {}) { | ||
132 | const path = '/api/v1/users/scoped-tokens' | ||
133 | |||
134 | return this.getRequestBody<ScopedToken>({ | ||
135 | ...options, | ||
136 | |||
137 | path, | ||
138 | implicitToken: true, | ||
139 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
140 | }) | ||
141 | } | ||
142 | |||
143 | renewMyScopedTokens (options: OverrideCommandOptions = {}) { | ||
144 | const path = '/api/v1/users/scoped-tokens' | ||
145 | |||
146 | return this.postBodyRequest({ | ||
147 | ...options, | ||
148 | |||
149 | path, | ||
150 | implicitToken: true, | ||
151 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
152 | }) | ||
153 | } | ||
154 | |||
155 | // --------------------------------------------------------------------------- | ||
156 | |||
157 | create (options: OverrideCommandOptions & { | ||
158 | username: string | ||
159 | password?: string | ||
160 | videoQuota?: number | ||
161 | videoQuotaDaily?: number | ||
162 | role?: UserRoleType | ||
163 | adminFlags?: UserAdminFlagType | ||
164 | }) { | ||
165 | const { | ||
166 | username, | ||
167 | adminFlags, | ||
168 | password = 'password', | ||
169 | videoQuota, | ||
170 | videoQuotaDaily, | ||
171 | role = UserRole.USER | ||
172 | } = options | ||
173 | |||
174 | const path = '/api/v1/users' | ||
175 | |||
176 | return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({ | ||
177 | ...options, | ||
178 | |||
179 | path, | ||
180 | fields: { | ||
181 | username, | ||
182 | password, | ||
183 | role, | ||
184 | adminFlags, | ||
185 | email: username + '@example.com', | ||
186 | videoQuota, | ||
187 | videoQuotaDaily | ||
188 | }, | ||
189 | implicitToken: true, | ||
190 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
191 | })).then(res => res.user) | ||
192 | } | ||
193 | |||
194 | async generate (username: string, role?: UserRoleType) { | ||
195 | const password = 'password' | ||
196 | const user = await this.create({ username, password, role }) | ||
197 | |||
198 | const token = await this.server.login.getAccessToken({ username, password }) | ||
199 | |||
200 | const me = await this.getMyInfo({ token }) | ||
201 | |||
202 | return { | ||
203 | token, | ||
204 | userId: user.id, | ||
205 | userChannelId: me.videoChannels[0].id, | ||
206 | userChannelName: me.videoChannels[0].name, | ||
207 | password | ||
208 | } | ||
209 | } | ||
210 | |||
211 | async generateUserAndToken (username: string, role?: UserRoleType) { | ||
212 | const password = 'password' | ||
213 | await this.create({ username, password, role }) | ||
214 | |||
215 | return this.server.login.getAccessToken({ username, password }) | ||
216 | } | ||
217 | |||
218 | // --------------------------------------------------------------------------- | ||
219 | |||
220 | getMyInfo (options: OverrideCommandOptions = {}) { | ||
221 | const path = '/api/v1/users/me' | ||
222 | |||
223 | return this.getRequestBody<MyUser>({ | ||
224 | ...options, | ||
225 | |||
226 | path, | ||
227 | implicitToken: true, | ||
228 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
229 | }) | ||
230 | } | ||
231 | |||
232 | getMyQuotaUsed (options: OverrideCommandOptions = {}) { | ||
233 | const path = '/api/v1/users/me/video-quota-used' | ||
234 | |||
235 | return this.getRequestBody<UserVideoQuota>({ | ||
236 | ...options, | ||
237 | |||
238 | path, | ||
239 | implicitToken: true, | ||
240 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
241 | }) | ||
242 | } | ||
243 | |||
244 | getMyRating (options: OverrideCommandOptions & { | ||
245 | videoId: number | string | ||
246 | }) { | ||
247 | const { videoId } = options | ||
248 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | ||
249 | |||
250 | return this.getRequestBody<UserVideoRate>({ | ||
251 | ...options, | ||
252 | |||
253 | path, | ||
254 | implicitToken: true, | ||
255 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
256 | }) | ||
257 | } | ||
258 | |||
259 | deleteMe (options: OverrideCommandOptions = {}) { | ||
260 | const path = '/api/v1/users/me' | ||
261 | |||
262 | return this.deleteRequest({ | ||
263 | ...options, | ||
264 | |||
265 | path, | ||
266 | implicitToken: true, | ||
267 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
268 | }) | ||
269 | } | ||
270 | |||
271 | updateMe (options: OverrideCommandOptions & UserUpdateMe) { | ||
272 | const path = '/api/v1/users/me' | ||
273 | |||
274 | const toSend: UserUpdateMe = omit(options, [ 'expectedStatus', 'token' ]) | ||
275 | |||
276 | return this.putBodyRequest({ | ||
277 | ...options, | ||
278 | |||
279 | path, | ||
280 | fields: toSend, | ||
281 | implicitToken: true, | ||
282 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
283 | }) | ||
284 | } | ||
285 | |||
286 | updateMyAvatar (options: OverrideCommandOptions & { | ||
287 | fixture: string | ||
288 | }) { | ||
289 | const { fixture } = options | ||
290 | const path = '/api/v1/users/me/avatar/pick' | ||
291 | |||
292 | return this.updateImageRequest({ | ||
293 | ...options, | ||
294 | |||
295 | path, | ||
296 | fixture, | ||
297 | fieldname: 'avatarfile', | ||
298 | |||
299 | implicitToken: true, | ||
300 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
301 | }) | ||
302 | } | ||
303 | |||
304 | // --------------------------------------------------------------------------- | ||
305 | |||
306 | get (options: OverrideCommandOptions & { | ||
307 | userId: number | ||
308 | withStats?: boolean // default false | ||
309 | }) { | ||
310 | const { userId, withStats } = options | ||
311 | const path = '/api/v1/users/' + userId | ||
312 | |||
313 | return this.getRequestBody<User>({ | ||
314 | ...options, | ||
315 | |||
316 | path, | ||
317 | query: { withStats }, | ||
318 | implicitToken: true, | ||
319 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
320 | }) | ||
321 | } | ||
322 | |||
323 | list (options: OverrideCommandOptions & { | ||
324 | start?: number | ||
325 | count?: number | ||
326 | sort?: string | ||
327 | search?: string | ||
328 | blocked?: boolean | ||
329 | } = {}) { | ||
330 | const path = '/api/v1/users' | ||
331 | |||
332 | return this.getRequestBody<ResultList<User>>({ | ||
333 | ...options, | ||
334 | |||
335 | path, | ||
336 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]), | ||
337 | implicitToken: true, | ||
338 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
339 | }) | ||
340 | } | ||
341 | |||
342 | remove (options: OverrideCommandOptions & { | ||
343 | userId: number | ||
344 | }) { | ||
345 | const { userId } = options | ||
346 | const path = '/api/v1/users/' + userId | ||
347 | |||
348 | return this.deleteRequest({ | ||
349 | ...options, | ||
350 | |||
351 | path, | ||
352 | implicitToken: true, | ||
353 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
354 | }) | ||
355 | } | ||
356 | |||
357 | update (options: OverrideCommandOptions & { | ||
358 | userId: number | ||
359 | email?: string | ||
360 | emailVerified?: boolean | ||
361 | videoQuota?: number | ||
362 | videoQuotaDaily?: number | ||
363 | password?: string | ||
364 | adminFlags?: UserAdminFlagType | ||
365 | pluginAuth?: string | ||
366 | role?: UserRoleType | ||
367 | }) { | ||
368 | const path = '/api/v1/users/' + options.userId | ||
369 | |||
370 | const toSend: UserUpdate = {} | ||
371 | if (options.password !== undefined && options.password !== null) toSend.password = options.password | ||
372 | if (options.email !== undefined && options.email !== null) toSend.email = options.email | ||
373 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified | ||
374 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota | ||
375 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily | ||
376 | if (options.role !== undefined && options.role !== null) toSend.role = options.role | ||
377 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags | ||
378 | if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth | ||
379 | |||
380 | return this.putBodyRequest({ | ||
381 | ...options, | ||
382 | |||
383 | path, | ||
384 | fields: toSend, | ||
385 | implicitToken: true, | ||
386 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
387 | }) | ||
388 | } | ||
389 | } | ||