diff options
Diffstat (limited to 'shared/server-commands/users')
-rw-r--r-- | shared/server-commands/users/accounts-command.ts | 76 | ||||
-rw-r--r-- | shared/server-commands/users/blocklist-command.ts | 162 | ||||
-rw-r--r-- | shared/server-commands/users/index.ts | 7 | ||||
-rw-r--r-- | shared/server-commands/users/login-command.ts | 132 | ||||
-rw-r--r-- | shared/server-commands/users/login.ts | 19 | ||||
-rw-r--r-- | shared/server-commands/users/notifications-command.ts | 85 | ||||
-rw-r--r-- | shared/server-commands/users/subscriptions-command.ts | 99 | ||||
-rw-r--r-- | shared/server-commands/users/users-command.ts | 416 |
8 files changed, 996 insertions, 0 deletions
diff --git a/shared/server-commands/users/accounts-command.ts b/shared/server-commands/users/accounts-command.ts new file mode 100644 index 000000000..5844b330b --- /dev/null +++ b/shared/server-commands/users/accounts-command.ts | |||
@@ -0,0 +1,76 @@ | |||
1 | import { Account, AccountVideoRate, ActorFollow, HttpStatusCode, ResultList, VideoRateType } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
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/shared/server-commands/users/blocklist-command.ts b/shared/server-commands/users/blocklist-command.ts new file mode 100644 index 000000000..2e7ed074d --- /dev/null +++ b/shared/server-commands/users/blocklist-command.ts | |||
@@ -0,0 +1,162 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@shared/models' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | type ListBlocklistOptions = OverrideCommandOptions & { | ||
7 | start: number | ||
8 | count: number | ||
9 | sort: string // default -createdAt | ||
10 | } | ||
11 | |||
12 | export class BlocklistCommand extends AbstractCommand { | ||
13 | |||
14 | listMyAccountBlocklist (options: ListBlocklistOptions) { | ||
15 | const path = '/api/v1/users/me/blocklist/accounts' | ||
16 | |||
17 | return this.listBlocklist<AccountBlock>(options, path) | ||
18 | } | ||
19 | |||
20 | listMyServerBlocklist (options: ListBlocklistOptions) { | ||
21 | const path = '/api/v1/users/me/blocklist/servers' | ||
22 | |||
23 | return this.listBlocklist<ServerBlock>(options, path) | ||
24 | } | ||
25 | |||
26 | listServerAccountBlocklist (options: ListBlocklistOptions) { | ||
27 | const path = '/api/v1/server/blocklist/accounts' | ||
28 | |||
29 | return this.listBlocklist<AccountBlock>(options, path) | ||
30 | } | ||
31 | |||
32 | listServerServerBlocklist (options: ListBlocklistOptions) { | ||
33 | const path = '/api/v1/server/blocklist/servers' | ||
34 | |||
35 | return this.listBlocklist<ServerBlock>(options, path) | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | getStatus (options: OverrideCommandOptions & { | ||
41 | accounts?: string[] | ||
42 | hosts?: string[] | ||
43 | }) { | ||
44 | const { accounts, hosts } = options | ||
45 | |||
46 | const path = '/api/v1/blocklist/status' | ||
47 | |||
48 | return this.getRequestBody<BlockStatus>({ | ||
49 | ...options, | ||
50 | |||
51 | path, | ||
52 | query: { | ||
53 | accounts, | ||
54 | hosts | ||
55 | }, | ||
56 | implicitToken: false, | ||
57 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | // --------------------------------------------------------------------------- | ||
62 | |||
63 | addToMyBlocklist (options: OverrideCommandOptions & { | ||
64 | account?: string | ||
65 | server?: string | ||
66 | }) { | ||
67 | const { account, server } = options | ||
68 | |||
69 | const path = account | ||
70 | ? '/api/v1/users/me/blocklist/accounts' | ||
71 | : '/api/v1/users/me/blocklist/servers' | ||
72 | |||
73 | return this.postBodyRequest({ | ||
74 | ...options, | ||
75 | |||
76 | path, | ||
77 | fields: { | ||
78 | accountName: account, | ||
79 | host: server | ||
80 | }, | ||
81 | implicitToken: true, | ||
82 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | addToServerBlocklist (options: OverrideCommandOptions & { | ||
87 | account?: string | ||
88 | server?: string | ||
89 | }) { | ||
90 | const { account, server } = options | ||
91 | |||
92 | const path = account | ||
93 | ? '/api/v1/server/blocklist/accounts' | ||
94 | : '/api/v1/server/blocklist/servers' | ||
95 | |||
96 | return this.postBodyRequest({ | ||
97 | ...options, | ||
98 | |||
99 | path, | ||
100 | fields: { | ||
101 | accountName: account, | ||
102 | host: server | ||
103 | }, | ||
104 | implicitToken: true, | ||
105 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
106 | }) | ||
107 | } | ||
108 | |||
109 | // --------------------------------------------------------------------------- | ||
110 | |||
111 | removeFromMyBlocklist (options: OverrideCommandOptions & { | ||
112 | account?: string | ||
113 | server?: string | ||
114 | }) { | ||
115 | const { account, server } = options | ||
116 | |||
117 | const path = account | ||
118 | ? '/api/v1/users/me/blocklist/accounts/' + account | ||
119 | : '/api/v1/users/me/blocklist/servers/' + server | ||
120 | |||
121 | return this.deleteRequest({ | ||
122 | ...options, | ||
123 | |||
124 | path, | ||
125 | implicitToken: true, | ||
126 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
127 | }) | ||
128 | } | ||
129 | |||
130 | removeFromServerBlocklist (options: OverrideCommandOptions & { | ||
131 | account?: string | ||
132 | server?: string | ||
133 | }) { | ||
134 | const { account, server } = options | ||
135 | |||
136 | const path = account | ||
137 | ? '/api/v1/server/blocklist/accounts/' + account | ||
138 | : '/api/v1/server/blocklist/servers/' + server | ||
139 | |||
140 | return this.deleteRequest({ | ||
141 | ...options, | ||
142 | |||
143 | path, | ||
144 | implicitToken: true, | ||
145 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
146 | }) | ||
147 | } | ||
148 | |||
149 | private listBlocklist <T> (options: ListBlocklistOptions, path: string) { | ||
150 | const { start, count, sort = '-createdAt' } = options | ||
151 | |||
152 | return this.getRequestBody<ResultList<T>>({ | ||
153 | ...options, | ||
154 | |||
155 | path, | ||
156 | query: { start, count, sort }, | ||
157 | implicitToken: true, | ||
158 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
159 | }) | ||
160 | } | ||
161 | |||
162 | } | ||
diff --git a/shared/server-commands/users/index.ts b/shared/server-commands/users/index.ts new file mode 100644 index 000000000..c2bc5c44f --- /dev/null +++ b/shared/server-commands/users/index.ts | |||
@@ -0,0 +1,7 @@ | |||
1 | export * from './accounts-command' | ||
2 | export * from './blocklist-command' | ||
3 | export * from './login' | ||
4 | export * from './login-command' | ||
5 | export * from './notifications-command' | ||
6 | export * from './subscriptions-command' | ||
7 | export * from './users-command' | ||
diff --git a/shared/server-commands/users/login-command.ts b/shared/server-commands/users/login-command.ts new file mode 100644 index 000000000..143f72a59 --- /dev/null +++ b/shared/server-commands/users/login-command.ts | |||
@@ -0,0 +1,132 @@ | |||
1 | import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models' | ||
2 | import { unwrapBody } from '../requests' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class LoginCommand extends AbstractCommand { | ||
6 | |||
7 | login (options: OverrideCommandOptions & { | ||
8 | client?: { id?: string, secret?: string } | ||
9 | user?: { username: string, password?: string } | ||
10 | } = {}) { | ||
11 | const { client = this.server.store.client, user = this.server.store.user } = options | ||
12 | const path = '/api/v1/users/token' | ||
13 | |||
14 | const body = { | ||
15 | client_id: client.id, | ||
16 | client_secret: client.secret, | ||
17 | username: user.username, | ||
18 | password: user.password ?? 'password', | ||
19 | response_type: 'code', | ||
20 | grant_type: 'password', | ||
21 | scope: 'upload' | ||
22 | } | ||
23 | |||
24 | return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({ | ||
25 | ...options, | ||
26 | |||
27 | path, | ||
28 | requestType: 'form', | ||
29 | fields: body, | ||
30 | implicitToken: false, | ||
31 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
32 | })) | ||
33 | } | ||
34 | |||
35 | getAccessToken (arg1?: { username: string, password?: string }): Promise<string> | ||
36 | getAccessToken (arg1: string, password?: string): Promise<string> | ||
37 | async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) { | ||
38 | let user: { username: string, password?: string } | ||
39 | |||
40 | if (!arg1) user = this.server.store.user | ||
41 | else if (typeof arg1 === 'object') user = arg1 | ||
42 | else user = { username: arg1, password } | ||
43 | |||
44 | try { | ||
45 | const body = await this.login({ user }) | ||
46 | |||
47 | return body.access_token | ||
48 | } catch (err) { | ||
49 | throw new Error(`Cannot authenticate. Please check your username/password. (${err})`) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | loginUsingExternalToken (options: OverrideCommandOptions & { | ||
54 | username: string | ||
55 | externalAuthToken: string | ||
56 | }) { | ||
57 | const { username, externalAuthToken } = options | ||
58 | const path = '/api/v1/users/token' | ||
59 | |||
60 | const body = { | ||
61 | client_id: this.server.store.client.id, | ||
62 | client_secret: this.server.store.client.secret, | ||
63 | username: username, | ||
64 | response_type: 'code', | ||
65 | grant_type: 'password', | ||
66 | scope: 'upload', | ||
67 | externalAuthToken | ||
68 | } | ||
69 | |||
70 | return this.postBodyRequest({ | ||
71 | ...options, | ||
72 | |||
73 | path, | ||
74 | requestType: 'form', | ||
75 | fields: body, | ||
76 | implicitToken: false, | ||
77 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | logout (options: OverrideCommandOptions & { | ||
82 | token: string | ||
83 | }) { | ||
84 | const path = '/api/v1/users/revoke-token' | ||
85 | |||
86 | return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({ | ||
87 | ...options, | ||
88 | |||
89 | path, | ||
90 | requestType: 'form', | ||
91 | implicitToken: false, | ||
92 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
93 | })) | ||
94 | } | ||
95 | |||
96 | refreshToken (options: OverrideCommandOptions & { | ||
97 | refreshToken: string | ||
98 | }) { | ||
99 | const path = '/api/v1/users/token' | ||
100 | |||
101 | const body = { | ||
102 | client_id: this.server.store.client.id, | ||
103 | client_secret: this.server.store.client.secret, | ||
104 | refresh_token: options.refreshToken, | ||
105 | response_type: 'code', | ||
106 | grant_type: 'refresh_token' | ||
107 | } | ||
108 | |||
109 | return this.postBodyRequest({ | ||
110 | ...options, | ||
111 | |||
112 | path, | ||
113 | requestType: 'form', | ||
114 | fields: body, | ||
115 | implicitToken: false, | ||
116 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
117 | }) | ||
118 | } | ||
119 | |||
120 | getClient (options: OverrideCommandOptions = {}) { | ||
121 | const path = '/api/v1/oauth-clients/local' | ||
122 | |||
123 | return this.getRequestBody<{ client_id: string, client_secret: string }>({ | ||
124 | ...options, | ||
125 | |||
126 | path, | ||
127 | host: this.server.host, | ||
128 | implicitToken: false, | ||
129 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
130 | }) | ||
131 | } | ||
132 | } | ||
diff --git a/shared/server-commands/users/login.ts b/shared/server-commands/users/login.ts new file mode 100644 index 000000000..f1df027d3 --- /dev/null +++ b/shared/server-commands/users/login.ts | |||
@@ -0,0 +1,19 @@ | |||
1 | import { PeerTubeServer } from '../server/server' | ||
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/shared/server-commands/users/notifications-command.ts b/shared/server-commands/users/notifications-command.ts new file mode 100644 index 000000000..6bd815daa --- /dev/null +++ b/shared/server-commands/users/notifications-command.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import { HttpStatusCode, ResultList, UserNotification, UserNotificationSetting } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
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/shared/server-commands/users/subscriptions-command.ts b/shared/server-commands/users/subscriptions-command.ts new file mode 100644 index 000000000..edc60e612 --- /dev/null +++ b/shared/server-commands/users/subscriptions-command.ts | |||
@@ -0,0 +1,99 @@ | |||
1 | import { HttpStatusCode, ResultList, Video, VideoChannel } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
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 | listVideos (options: OverrideCommandOptions & { | ||
42 | sort?: string // default -createdAt | ||
43 | } = {}) { | ||
44 | const { sort = '-createdAt' } = options | ||
45 | const path = '/api/v1/users/me/subscriptions/videos' | ||
46 | |||
47 | return this.getRequestBody<ResultList<Video>>({ | ||
48 | ...options, | ||
49 | |||
50 | path, | ||
51 | query: { sort }, | ||
52 | implicitToken: true, | ||
53 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | get (options: OverrideCommandOptions & { | ||
58 | uri: string | ||
59 | }) { | ||
60 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
61 | |||
62 | return this.getRequestBody<VideoChannel>({ | ||
63 | ...options, | ||
64 | |||
65 | path, | ||
66 | implicitToken: true, | ||
67 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | remove (options: OverrideCommandOptions & { | ||
72 | uri: string | ||
73 | }) { | ||
74 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
75 | |||
76 | return this.deleteRequest({ | ||
77 | ...options, | ||
78 | |||
79 | path, | ||
80 | implicitToken: true, | ||
81 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
82 | }) | ||
83 | } | ||
84 | |||
85 | exist (options: OverrideCommandOptions & { | ||
86 | uris: string[] | ||
87 | }) { | ||
88 | const path = '/api/v1/users/me/subscriptions/exist' | ||
89 | |||
90 | return this.getRequestBody<{ [id: string ]: boolean }>({ | ||
91 | ...options, | ||
92 | |||
93 | path, | ||
94 | query: { 'uris[]': options.uris }, | ||
95 | implicitToken: true, | ||
96 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
97 | }) | ||
98 | } | ||
99 | } | ||
diff --git a/shared/server-commands/users/users-command.ts b/shared/server-commands/users/users-command.ts new file mode 100644 index 000000000..b5ae9008e --- /dev/null +++ b/shared/server-commands/users/users-command.ts | |||
@@ -0,0 +1,416 @@ | |||
1 | import { omit } from 'lodash' | ||
2 | import { pick } from '@shared/core-utils' | ||
3 | import { | ||
4 | HttpStatusCode, | ||
5 | MyUser, | ||
6 | ResultList, | ||
7 | ScopedToken, | ||
8 | User, | ||
9 | UserAdminFlag, | ||
10 | UserCreateResult, | ||
11 | UserRole, | ||
12 | UserUpdate, | ||
13 | UserUpdateMe, | ||
14 | UserVideoQuota, | ||
15 | UserVideoRate | ||
16 | } from '@shared/models' | ||
17 | import { unwrapBody } from '../requests' | ||
18 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
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?: UserRole | ||
163 | adminFlags?: UserAdminFlag | ||
164 | }) { | ||
165 | const { | ||
166 | username, | ||
167 | adminFlags, | ||
168 | password = 'password', | ||
169 | videoQuota = 42000000, | ||
170 | videoQuotaDaily = -1, | ||
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?: UserRole) { | ||
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 | } | ||
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 | register (options: OverrideCommandOptions & { | ||
218 | username: string | ||
219 | password?: string | ||
220 | displayName?: string | ||
221 | channel?: { | ||
222 | name: string | ||
223 | displayName: string | ||
224 | } | ||
225 | }) { | ||
226 | const { username, password = 'password', displayName, channel } = options | ||
227 | const path = '/api/v1/users/register' | ||
228 | |||
229 | return this.postBodyRequest({ | ||
230 | ...options, | ||
231 | |||
232 | path, | ||
233 | fields: { | ||
234 | username, | ||
235 | password, | ||
236 | email: username + '@example.com', | ||
237 | displayName, | ||
238 | channel | ||
239 | }, | ||
240 | implicitToken: false, | ||
241 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
242 | }) | ||
243 | } | ||
244 | |||
245 | // --------------------------------------------------------------------------- | ||
246 | |||
247 | getMyInfo (options: OverrideCommandOptions = {}) { | ||
248 | const path = '/api/v1/users/me' | ||
249 | |||
250 | return this.getRequestBody<MyUser>({ | ||
251 | ...options, | ||
252 | |||
253 | path, | ||
254 | implicitToken: true, | ||
255 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
256 | }) | ||
257 | } | ||
258 | |||
259 | getMyQuotaUsed (options: OverrideCommandOptions = {}) { | ||
260 | const path = '/api/v1/users/me/video-quota-used' | ||
261 | |||
262 | return this.getRequestBody<UserVideoQuota>({ | ||
263 | ...options, | ||
264 | |||
265 | path, | ||
266 | implicitToken: true, | ||
267 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
268 | }) | ||
269 | } | ||
270 | |||
271 | getMyRating (options: OverrideCommandOptions & { | ||
272 | videoId: number | string | ||
273 | }) { | ||
274 | const { videoId } = options | ||
275 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | ||
276 | |||
277 | return this.getRequestBody<UserVideoRate>({ | ||
278 | ...options, | ||
279 | |||
280 | path, | ||
281 | implicitToken: true, | ||
282 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
283 | }) | ||
284 | } | ||
285 | |||
286 | deleteMe (options: OverrideCommandOptions = {}) { | ||
287 | const path = '/api/v1/users/me' | ||
288 | |||
289 | return this.deleteRequest({ | ||
290 | ...options, | ||
291 | |||
292 | path, | ||
293 | implicitToken: true, | ||
294 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
295 | }) | ||
296 | } | ||
297 | |||
298 | updateMe (options: OverrideCommandOptions & UserUpdateMe) { | ||
299 | const path = '/api/v1/users/me' | ||
300 | |||
301 | const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') | ||
302 | |||
303 | return this.putBodyRequest({ | ||
304 | ...options, | ||
305 | |||
306 | path, | ||
307 | fields: toSend, | ||
308 | implicitToken: true, | ||
309 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
310 | }) | ||
311 | } | ||
312 | |||
313 | updateMyAvatar (options: OverrideCommandOptions & { | ||
314 | fixture: string | ||
315 | }) { | ||
316 | const { fixture } = options | ||
317 | const path = '/api/v1/users/me/avatar/pick' | ||
318 | |||
319 | return this.updateImageRequest({ | ||
320 | ...options, | ||
321 | |||
322 | path, | ||
323 | fixture, | ||
324 | fieldname: 'avatarfile', | ||
325 | |||
326 | implicitToken: true, | ||
327 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
328 | }) | ||
329 | } | ||
330 | |||
331 | // --------------------------------------------------------------------------- | ||
332 | |||
333 | get (options: OverrideCommandOptions & { | ||
334 | userId: number | ||
335 | withStats?: boolean // default false | ||
336 | }) { | ||
337 | const { userId, withStats } = options | ||
338 | const path = '/api/v1/users/' + userId | ||
339 | |||
340 | return this.getRequestBody<User>({ | ||
341 | ...options, | ||
342 | |||
343 | path, | ||
344 | query: { withStats }, | ||
345 | implicitToken: true, | ||
346 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
347 | }) | ||
348 | } | ||
349 | |||
350 | list (options: OverrideCommandOptions & { | ||
351 | start?: number | ||
352 | count?: number | ||
353 | sort?: string | ||
354 | search?: string | ||
355 | blocked?: boolean | ||
356 | } = {}) { | ||
357 | const path = '/api/v1/users' | ||
358 | |||
359 | return this.getRequestBody<ResultList<User>>({ | ||
360 | ...options, | ||
361 | |||
362 | path, | ||
363 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]), | ||
364 | implicitToken: true, | ||
365 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
366 | }) | ||
367 | } | ||
368 | |||
369 | remove (options: OverrideCommandOptions & { | ||
370 | userId: number | ||
371 | }) { | ||
372 | const { userId } = options | ||
373 | const path = '/api/v1/users/' + userId | ||
374 | |||
375 | return this.deleteRequest({ | ||
376 | ...options, | ||
377 | |||
378 | path, | ||
379 | implicitToken: true, | ||
380 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
381 | }) | ||
382 | } | ||
383 | |||
384 | update (options: OverrideCommandOptions & { | ||
385 | userId: number | ||
386 | email?: string | ||
387 | emailVerified?: boolean | ||
388 | videoQuota?: number | ||
389 | videoQuotaDaily?: number | ||
390 | password?: string | ||
391 | adminFlags?: UserAdminFlag | ||
392 | pluginAuth?: string | ||
393 | role?: UserRole | ||
394 | }) { | ||
395 | const path = '/api/v1/users/' + options.userId | ||
396 | |||
397 | const toSend: UserUpdate = {} | ||
398 | if (options.password !== undefined && options.password !== null) toSend.password = options.password | ||
399 | if (options.email !== undefined && options.email !== null) toSend.email = options.email | ||
400 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified | ||
401 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota | ||
402 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily | ||
403 | if (options.role !== undefined && options.role !== null) toSend.role = options.role | ||
404 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags | ||
405 | if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth | ||
406 | |||
407 | return this.putBodyRequest({ | ||
408 | ...options, | ||
409 | |||
410 | path, | ||
411 | fields: toSend, | ||
412 | implicitToken: true, | ||
413 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
414 | }) | ||
415 | } | ||
416 | } | ||