aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /shared/server-commands/users
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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 'shared/server-commands/users')
-rw-r--r--shared/server-commands/users/accounts-command.ts76
-rw-r--r--shared/server-commands/users/accounts.ts15
-rw-r--r--shared/server-commands/users/blocklist-command.ts165
-rw-r--r--shared/server-commands/users/index.ts10
-rw-r--r--shared/server-commands/users/login-command.ts159
-rw-r--r--shared/server-commands/users/login.ts19
-rw-r--r--shared/server-commands/users/notifications-command.ts85
-rw-r--r--shared/server-commands/users/registrations-command.ts151
-rw-r--r--shared/server-commands/users/subscriptions-command.ts83
-rw-r--r--shared/server-commands/users/two-factor-command.ts92
-rw-r--r--shared/server-commands/users/users-command.ts388
11 files changed, 0 insertions, 1243 deletions
diff --git a/shared/server-commands/users/accounts-command.ts b/shared/server-commands/users/accounts-command.ts
deleted file mode 100644
index 5844b330b..000000000
--- a/shared/server-commands/users/accounts-command.ts
+++ /dev/null
@@ -1,76 +0,0 @@
1import { Account, AccountVideoRate, ActorFollow, HttpStatusCode, ResultList, VideoRateType } from '@shared/models'
2import { AbstractCommand, OverrideCommandOptions } from '../shared'
3
4export 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/shared/server-commands/users/accounts.ts b/shared/server-commands/users/accounts.ts
deleted file mode 100644
index 6387891f4..000000000
--- a/shared/server-commands/users/accounts.ts
+++ /dev/null
@@ -1,15 +0,0 @@
1import { PeerTubeServer } from '../server/server'
2
3async 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
13export {
14 setDefaultAccountAvatar
15}
diff --git a/shared/server-commands/users/blocklist-command.ts b/shared/server-commands/users/blocklist-command.ts
deleted file mode 100644
index 862d8945e..000000000
--- a/shared/server-commands/users/blocklist-command.ts
+++ /dev/null
@@ -1,165 +0,0 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@shared/models'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6type ListBlocklistOptions = OverrideCommandOptions & {
7 start: number
8 count: number
9
10 sort?: string // default -createdAt
11
12 search?: string
13}
14
15export 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/shared/server-commands/users/index.ts b/shared/server-commands/users/index.ts
deleted file mode 100644
index 404756539..000000000
--- a/shared/server-commands/users/index.ts
+++ /dev/null
@@ -1,10 +0,0 @@
1export * from './accounts-command'
2export * from './accounts'
3export * from './blocklist-command'
4export * from './login'
5export * from './login-command'
6export * from './notifications-command'
7export * from './registrations-command'
8export * from './subscriptions-command'
9export * from './two-factor-command'
10export * from './users-command'
diff --git a/shared/server-commands/users/login-command.ts b/shared/server-commands/users/login-command.ts
deleted file mode 100644
index f2fc6d1c5..000000000
--- a/shared/server-commands/users/login-command.ts
+++ /dev/null
@@ -1,159 +0,0 @@
1import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models'
2import { unwrapBody } from '../requests'
3import { AbstractCommand, OverrideCommandOptions } from '../shared'
4
5type LoginOptions = OverrideCommandOptions & {
6 client?: { id?: string, secret?: string }
7 user?: { username: string, password?: string }
8 otpToken?: string
9}
10
11export 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/shared/server-commands/users/login.ts b/shared/server-commands/users/login.ts
deleted file mode 100644
index f1df027d3..000000000
--- a/shared/server-commands/users/login.ts
+++ /dev/null
@@ -1,19 +0,0 @@
1import { PeerTubeServer } from '../server/server'
2
3function 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
17export {
18 setAccessTokensToServers
19}
diff --git a/shared/server-commands/users/notifications-command.ts b/shared/server-commands/users/notifications-command.ts
deleted file mode 100644
index 6bd815daa..000000000
--- a/shared/server-commands/users/notifications-command.ts
+++ /dev/null
@@ -1,85 +0,0 @@
1import { HttpStatusCode, ResultList, UserNotification, UserNotificationSetting } from '@shared/models'
2import { AbstractCommand, OverrideCommandOptions } from '../shared'
3
4export 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/shared/server-commands/users/registrations-command.ts b/shared/server-commands/users/registrations-command.ts
deleted file mode 100644
index f57f54b34..000000000
--- a/shared/server-commands/users/registrations-command.ts
+++ /dev/null
@@ -1,151 +0,0 @@
1import { pick } from '@shared/core-utils'
2import { HttpStatusCode, ResultList, UserRegistration, UserRegistrationRequest, UserRegistrationUpdateState } from '@shared/models'
3import { unwrapBody } from '../requests'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class RegistrationsCommand extends AbstractCommand {
7
8 register (options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username'>) {
9 const { password = 'password', email = options.username + '@example.com' } = options
10 const path = '/api/v1/users/register'
11
12 return this.postBodyRequest({
13 ...options,
14
15 path,
16 fields: {
17 ...pick(options, [ 'username', 'displayName', 'channel' ]),
18
19 password,
20 email
21 },
22 implicitToken: false,
23 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
24 })
25 }
26
27 requestRegistration (
28 options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username' | 'registrationReason'>
29 ) {
30 const { password = 'password', email = options.username + '@example.com' } = options
31 const path = '/api/v1/users/registrations/request'
32
33 return unwrapBody<UserRegistration>(this.postBodyRequest({
34 ...options,
35
36 path,
37 fields: {
38 ...pick(options, [ 'username', 'displayName', 'channel', 'registrationReason' ]),
39
40 password,
41 email
42 },
43 implicitToken: false,
44 defaultExpectedStatus: HttpStatusCode.OK_200
45 }))
46 }
47
48 // ---------------------------------------------------------------------------
49
50 accept (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) {
51 const { id } = options
52 const path = '/api/v1/users/registrations/' + id + '/accept'
53
54 return this.postBodyRequest({
55 ...options,
56
57 path,
58 fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]),
59 implicitToken: true,
60 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
61 })
62 }
63
64 reject (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) {
65 const { id } = options
66 const path = '/api/v1/users/registrations/' + id + '/reject'
67
68 return this.postBodyRequest({
69 ...options,
70
71 path,
72 fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]),
73 implicitToken: true,
74 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
75 })
76 }
77
78 // ---------------------------------------------------------------------------
79
80 delete (options: OverrideCommandOptions & {
81 id: number
82 }) {
83 const { id } = options
84 const path = '/api/v1/users/registrations/' + id
85
86 return this.deleteRequest({
87 ...options,
88
89 path,
90 implicitToken: true,
91 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
92 })
93 }
94
95 // ---------------------------------------------------------------------------
96
97 list (options: OverrideCommandOptions & {
98 start?: number
99 count?: number
100 sort?: string
101 search?: string
102 } = {}) {
103 const path = '/api/v1/users/registrations'
104
105 return this.getRequestBody<ResultList<UserRegistration>>({
106 ...options,
107
108 path,
109 query: pick(options, [ 'start', 'count', 'sort', 'search' ]),
110 implicitToken: true,
111 defaultExpectedStatus: HttpStatusCode.OK_200
112 })
113 }
114
115 // ---------------------------------------------------------------------------
116
117 askSendVerifyEmail (options: OverrideCommandOptions & {
118 email: string
119 }) {
120 const { email } = options
121 const path = '/api/v1/users/registrations/ask-send-verify-email'
122
123 return this.postBodyRequest({
124 ...options,
125
126 path,
127 fields: { email },
128 implicitToken: false,
129 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
130 })
131 }
132
133 verifyEmail (options: OverrideCommandOptions & {
134 registrationId: number
135 verificationString: string
136 }) {
137 const { registrationId, verificationString } = options
138 const path = '/api/v1/users/registrations/' + registrationId + '/verify-email'
139
140 return this.postBodyRequest({
141 ...options,
142
143 path,
144 fields: {
145 verificationString
146 },
147 implicitToken: false,
148 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
149 })
150 }
151}
diff --git a/shared/server-commands/users/subscriptions-command.ts b/shared/server-commands/users/subscriptions-command.ts
deleted file mode 100644
index b92f037f8..000000000
--- a/shared/server-commands/users/subscriptions-command.ts
+++ /dev/null
@@ -1,83 +0,0 @@
1import { HttpStatusCode, ResultList, VideoChannel } from '@shared/models'
2import { AbstractCommand, OverrideCommandOptions } from '../shared'
3
4export 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/shared/server-commands/users/two-factor-command.ts b/shared/server-commands/users/two-factor-command.ts
deleted file mode 100644
index 5542acfda..000000000
--- a/shared/server-commands/users/two-factor-command.ts
+++ /dev/null
@@ -1,92 +0,0 @@
1import { TOTP } from 'otpauth'
2import { HttpStatusCode, TwoFactorEnableResult } from '@shared/models'
3import { unwrapBody } from '../requests'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export 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/shared/server-commands/users/users-command.ts b/shared/server-commands/users/users-command.ts
deleted file mode 100644
index 5b39d3488..000000000
--- a/shared/server-commands/users/users-command.ts
+++ /dev/null
@@ -1,388 +0,0 @@
1import { omit, pick } from '@shared/core-utils'
2import {
3 HttpStatusCode,
4 MyUser,
5 ResultList,
6 ScopedToken,
7 User,
8 UserAdminFlag,
9 UserCreateResult,
10 UserRole,
11 UserUpdate,
12 UserUpdateMe,
13 UserVideoQuota,
14 UserVideoRate
15} from '@shared/models'
16import { unwrapBody } from '../requests'
17import { AbstractCommand, OverrideCommandOptions } from '../shared'
18
19export class UsersCommand extends AbstractCommand {
20
21 askResetPassword (options: OverrideCommandOptions & {
22 email: string
23 }) {
24 const { email } = options
25 const path = '/api/v1/users/ask-reset-password'
26
27 return this.postBodyRequest({
28 ...options,
29
30 path,
31 fields: { email },
32 implicitToken: false,
33 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
34 })
35 }
36
37 resetPassword (options: OverrideCommandOptions & {
38 userId: number
39 verificationString: string
40 password: string
41 }) {
42 const { userId, verificationString, password } = options
43 const path = '/api/v1/users/' + userId + '/reset-password'
44
45 return this.postBodyRequest({
46 ...options,
47
48 path,
49 fields: { password, verificationString },
50 implicitToken: false,
51 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
52 })
53 }
54
55 // ---------------------------------------------------------------------------
56
57 askSendVerifyEmail (options: OverrideCommandOptions & {
58 email: string
59 }) {
60 const { email } = options
61 const path = '/api/v1/users/ask-send-verify-email'
62
63 return this.postBodyRequest({
64 ...options,
65
66 path,
67 fields: { email },
68 implicitToken: false,
69 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
70 })
71 }
72
73 verifyEmail (options: OverrideCommandOptions & {
74 userId: number
75 verificationString: string
76 isPendingEmail?: boolean // default false
77 }) {
78 const { userId, verificationString, isPendingEmail = false } = options
79 const path = '/api/v1/users/' + userId + '/verify-email'
80
81 return this.postBodyRequest({
82 ...options,
83
84 path,
85 fields: {
86 verificationString,
87 isPendingEmail
88 },
89 implicitToken: false,
90 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
91 })
92 }
93
94 // ---------------------------------------------------------------------------
95
96 banUser (options: OverrideCommandOptions & {
97 userId: number
98 reason?: string
99 }) {
100 const { userId, reason } = options
101 const path = '/api/v1/users' + '/' + userId + '/block'
102
103 return this.postBodyRequest({
104 ...options,
105
106 path,
107 fields: { reason },
108 implicitToken: true,
109 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
110 })
111 }
112
113 unbanUser (options: OverrideCommandOptions & {
114 userId: number
115 }) {
116 const { userId } = options
117 const path = '/api/v1/users' + '/' + userId + '/unblock'
118
119 return this.postBodyRequest({
120 ...options,
121
122 path,
123 implicitToken: true,
124 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
125 })
126 }
127
128 // ---------------------------------------------------------------------------
129
130 getMyScopedTokens (options: OverrideCommandOptions = {}) {
131 const path = '/api/v1/users/scoped-tokens'
132
133 return this.getRequestBody<ScopedToken>({
134 ...options,
135
136 path,
137 implicitToken: true,
138 defaultExpectedStatus: HttpStatusCode.OK_200
139 })
140 }
141
142 renewMyScopedTokens (options: OverrideCommandOptions = {}) {
143 const path = '/api/v1/users/scoped-tokens'
144
145 return this.postBodyRequest({
146 ...options,
147
148 path,
149 implicitToken: true,
150 defaultExpectedStatus: HttpStatusCode.OK_200
151 })
152 }
153
154 // ---------------------------------------------------------------------------
155
156 create (options: OverrideCommandOptions & {
157 username: string
158 password?: string
159 videoQuota?: number
160 videoQuotaDaily?: number
161 role?: UserRole
162 adminFlags?: UserAdminFlag
163 }) {
164 const {
165 username,
166 adminFlags,
167 password = 'password',
168 videoQuota,
169 videoQuotaDaily,
170 role = UserRole.USER
171 } = options
172
173 const path = '/api/v1/users'
174
175 return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({
176 ...options,
177
178 path,
179 fields: {
180 username,
181 password,
182 role,
183 adminFlags,
184 email: username + '@example.com',
185 videoQuota,
186 videoQuotaDaily
187 },
188 implicitToken: true,
189 defaultExpectedStatus: HttpStatusCode.OK_200
190 })).then(res => res.user)
191 }
192
193 async generate (username: string, role?: UserRole) {
194 const password = 'password'
195 const user = await this.create({ username, password, role })
196
197 const token = await this.server.login.getAccessToken({ username, password })
198
199 const me = await this.getMyInfo({ token })
200
201 return {
202 token,
203 userId: user.id,
204 userChannelId: me.videoChannels[0].id,
205 userChannelName: me.videoChannels[0].name,
206 password
207 }
208 }
209
210 async generateUserAndToken (username: string, role?: UserRole) {
211 const password = 'password'
212 await this.create({ username, password, role })
213
214 return this.server.login.getAccessToken({ username, password })
215 }
216
217 // ---------------------------------------------------------------------------
218
219 getMyInfo (options: OverrideCommandOptions = {}) {
220 const path = '/api/v1/users/me'
221
222 return this.getRequestBody<MyUser>({
223 ...options,
224
225 path,
226 implicitToken: true,
227 defaultExpectedStatus: HttpStatusCode.OK_200
228 })
229 }
230
231 getMyQuotaUsed (options: OverrideCommandOptions = {}) {
232 const path = '/api/v1/users/me/video-quota-used'
233
234 return this.getRequestBody<UserVideoQuota>({
235 ...options,
236
237 path,
238 implicitToken: true,
239 defaultExpectedStatus: HttpStatusCode.OK_200
240 })
241 }
242
243 getMyRating (options: OverrideCommandOptions & {
244 videoId: number | string
245 }) {
246 const { videoId } = options
247 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
248
249 return this.getRequestBody<UserVideoRate>({
250 ...options,
251
252 path,
253 implicitToken: true,
254 defaultExpectedStatus: HttpStatusCode.OK_200
255 })
256 }
257
258 deleteMe (options: OverrideCommandOptions = {}) {
259 const path = '/api/v1/users/me'
260
261 return this.deleteRequest({
262 ...options,
263
264 path,
265 implicitToken: true,
266 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
267 })
268 }
269
270 updateMe (options: OverrideCommandOptions & UserUpdateMe) {
271 const path = '/api/v1/users/me'
272
273 const toSend: UserUpdateMe = omit(options, [ 'expectedStatus', 'token' ])
274
275 return this.putBodyRequest({
276 ...options,
277
278 path,
279 fields: toSend,
280 implicitToken: true,
281 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
282 })
283 }
284
285 updateMyAvatar (options: OverrideCommandOptions & {
286 fixture: string
287 }) {
288 const { fixture } = options
289 const path = '/api/v1/users/me/avatar/pick'
290
291 return this.updateImageRequest({
292 ...options,
293
294 path,
295 fixture,
296 fieldname: 'avatarfile',
297
298 implicitToken: true,
299 defaultExpectedStatus: HttpStatusCode.OK_200
300 })
301 }
302
303 // ---------------------------------------------------------------------------
304
305 get (options: OverrideCommandOptions & {
306 userId: number
307 withStats?: boolean // default false
308 }) {
309 const { userId, withStats } = options
310 const path = '/api/v1/users/' + userId
311
312 return this.getRequestBody<User>({
313 ...options,
314
315 path,
316 query: { withStats },
317 implicitToken: true,
318 defaultExpectedStatus: HttpStatusCode.OK_200
319 })
320 }
321
322 list (options: OverrideCommandOptions & {
323 start?: number
324 count?: number
325 sort?: string
326 search?: string
327 blocked?: boolean
328 } = {}) {
329 const path = '/api/v1/users'
330
331 return this.getRequestBody<ResultList<User>>({
332 ...options,
333
334 path,
335 query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]),
336 implicitToken: true,
337 defaultExpectedStatus: HttpStatusCode.OK_200
338 })
339 }
340
341 remove (options: OverrideCommandOptions & {
342 userId: number
343 }) {
344 const { userId } = options
345 const path = '/api/v1/users/' + userId
346
347 return this.deleteRequest({
348 ...options,
349
350 path,
351 implicitToken: true,
352 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
353 })
354 }
355
356 update (options: OverrideCommandOptions & {
357 userId: number
358 email?: string
359 emailVerified?: boolean
360 videoQuota?: number
361 videoQuotaDaily?: number
362 password?: string
363 adminFlags?: UserAdminFlag
364 pluginAuth?: string
365 role?: UserRole
366 }) {
367 const path = '/api/v1/users/' + options.userId
368
369 const toSend: UserUpdate = {}
370 if (options.password !== undefined && options.password !== null) toSend.password = options.password
371 if (options.email !== undefined && options.email !== null) toSend.email = options.email
372 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified
373 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota
374 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily
375 if (options.role !== undefined && options.role !== null) toSend.role = options.role
376 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags
377 if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth
378
379 return this.putBodyRequest({
380 ...options,
381
382 path,
383 fields: toSend,
384 implicitToken: true,
385 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
386 })
387 }
388}