diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-07 16:40:49 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:17 +0200 |
commit | 2c27e70471120c92e0bc8c8114141fbb31ff98ac (patch) | |
tree | f52d89adc0351168fd1d89cbc07652e1408caaf2 /shared | |
parent | 5f8bd4cbb178290da7d8f81e996f19f0eccc8e4c (diff) | |
download | PeerTube-2c27e70471120c92e0bc8c8114141fbb31ff98ac.tar.gz PeerTube-2c27e70471120c92e0bc8c8114141fbb31ff98ac.tar.zst PeerTube-2c27e70471120c92e0bc8c8114141fbb31ff98ac.zip |
Introduce subscriptions command
Diffstat (limited to 'shared')
-rw-r--r-- | shared/extra-utils/server/servers.ts | 4 | ||||
-rw-r--r-- | shared/extra-utils/users/index.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/users/subscriptions-command.ts | 94 | ||||
-rw-r--r-- | shared/extra-utils/users/user-subscriptions.ts | 93 |
4 files changed, 98 insertions, 95 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 3c709666d..57b37728a 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -17,7 +17,7 @@ import { OverviewsCommand } from '../overviews' | |||
17 | import { makeGetRequest } from '../requests/requests' | 17 | import { makeGetRequest } from '../requests/requests' |
18 | import { SearchCommand } from '../search' | 18 | import { SearchCommand } from '../search' |
19 | import { SocketIOCommand } from '../socket' | 19 | import { SocketIOCommand } from '../socket' |
20 | import { AccountsCommand, BlocklistCommand } from '../users' | 20 | import { AccountsCommand, BlocklistCommand, SubscriptionsCommand } from '../users' |
21 | import { ConfigCommand } from './config-command' | 21 | import { ConfigCommand } from './config-command' |
22 | import { ContactFormCommand } from './contact-form-command' | 22 | import { ContactFormCommand } from './contact-form-command' |
23 | import { DebugCommand } from './debug-command' | 23 | import { DebugCommand } from './debug-command' |
@@ -98,6 +98,7 @@ interface ServerInfo { | |||
98 | socketIOCommand?: SocketIOCommand | 98 | socketIOCommand?: SocketIOCommand |
99 | accountsCommand?: AccountsCommand | 99 | accountsCommand?: AccountsCommand |
100 | blocklistCommand?: BlocklistCommand | 100 | blocklistCommand?: BlocklistCommand |
101 | subscriptionsCommand?: SubscriptionsCommand | ||
101 | } | 102 | } |
102 | 103 | ||
103 | function parallelTests () { | 104 | function parallelTests () { |
@@ -322,6 +323,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
322 | server.socketIOCommand = new SocketIOCommand(server) | 323 | server.socketIOCommand = new SocketIOCommand(server) |
323 | server.accountsCommand = new AccountsCommand(server) | 324 | server.accountsCommand = new AccountsCommand(server) |
324 | server.blocklistCommand = new BlocklistCommand(server) | 325 | server.blocklistCommand = new BlocklistCommand(server) |
326 | server.subscriptionsCommand = new SubscriptionsCommand(server) | ||
325 | 327 | ||
326 | res(server) | 328 | res(server) |
327 | }) | 329 | }) |
diff --git a/shared/extra-utils/users/index.ts b/shared/extra-utils/users/index.ts index ea5dbbf14..94ad37242 100644 --- a/shared/extra-utils/users/index.ts +++ b/shared/extra-utils/users/index.ts | |||
@@ -4,5 +4,5 @@ export * from './blocklist-command' | |||
4 | 4 | ||
5 | export * from './login' | 5 | export * from './login' |
6 | export * from './user-notifications' | 6 | export * from './user-notifications' |
7 | export * from './user-subscriptions' | 7 | export * from './subscriptions-command' |
8 | export * from './users' | 8 | export * from './users' |
diff --git a/shared/extra-utils/users/subscriptions-command.ts b/shared/extra-utils/users/subscriptions-command.ts new file mode 100644 index 000000000..94d2af67a --- /dev/null +++ b/shared/extra-utils/users/subscriptions-command.ts | |||
@@ -0,0 +1,94 @@ | |||
1 | import { ResultList, Video, VideoChannel } from '@shared/models' | ||
2 | import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class SubscriptionsCommand extends AbstractCommand { | ||
6 | |||
7 | add (options: OverrideCommandOptions & { | ||
8 | targetUri: string | ||
9 | }) { | ||
10 | const path = '/api/v1/users/me/subscriptions' | ||
11 | |||
12 | return this.postBodyRequest({ | ||
13 | ...options, | ||
14 | |||
15 | path, | ||
16 | fields: { uri: options.targetUri }, | ||
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 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
37 | }) | ||
38 | } | ||
39 | |||
40 | listVideos (options: OverrideCommandOptions & { | ||
41 | sort?: string // default -createdAt | ||
42 | } = {}) { | ||
43 | const { sort = '-createdAt' } = options | ||
44 | const path = '/api/v1/users/me/subscriptions/videos' | ||
45 | |||
46 | return this.getRequestBody<ResultList<Video>>({ | ||
47 | ...options, | ||
48 | |||
49 | path, | ||
50 | query: { sort }, | ||
51 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | get (options: OverrideCommandOptions & { | ||
56 | uri: string | ||
57 | }) { | ||
58 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
59 | |||
60 | return this.getRequestBody<VideoChannel>({ | ||
61 | ...options, | ||
62 | |||
63 | path, | ||
64 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
65 | }) | ||
66 | } | ||
67 | |||
68 | remove (options: OverrideCommandOptions & { | ||
69 | uri: string | ||
70 | }) { | ||
71 | const path = '/api/v1/users/me/subscriptions/' + options.uri | ||
72 | |||
73 | return this.deleteRequest({ | ||
74 | ...options, | ||
75 | |||
76 | path, | ||
77 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | exist (options: OverrideCommandOptions & { | ||
82 | uris: string[] | ||
83 | }) { | ||
84 | const path = '/api/v1/users/me/subscriptions/exist' | ||
85 | |||
86 | return this.getRequestBody<{ [id: string ]: boolean }>({ | ||
87 | ...options, | ||
88 | |||
89 | path, | ||
90 | query: { 'uris[]': options.uris }, | ||
91 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
92 | }) | ||
93 | } | ||
94 | } | ||
diff --git a/shared/extra-utils/users/user-subscriptions.ts b/shared/extra-utils/users/user-subscriptions.ts deleted file mode 100644 index edc7a3562..000000000 --- a/shared/extra-utils/users/user-subscriptions.ts +++ /dev/null | |||
@@ -1,93 +0,0 @@ | |||
1 | import { makeDeleteRequest, makeGetRequest, makePostBodyRequest } from '../requests/requests' | ||
2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | |||
4 | function addUserSubscription (url: string, token: string, targetUri: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { | ||
5 | const path = '/api/v1/users/me/subscriptions' | ||
6 | |||
7 | return makePostBodyRequest({ | ||
8 | url, | ||
9 | path, | ||
10 | token, | ||
11 | statusCodeExpected, | ||
12 | fields: { uri: targetUri } | ||
13 | }) | ||
14 | } | ||
15 | |||
16 | function listUserSubscriptions (parameters: { | ||
17 | url: string | ||
18 | token: string | ||
19 | sort?: string | ||
20 | search?: string | ||
21 | statusCodeExpected?: number | ||
22 | }) { | ||
23 | const { url, token, sort = '-createdAt', search, statusCodeExpected = HttpStatusCode.OK_200 } = parameters | ||
24 | const path = '/api/v1/users/me/subscriptions' | ||
25 | |||
26 | return makeGetRequest({ | ||
27 | url, | ||
28 | path, | ||
29 | token, | ||
30 | statusCodeExpected, | ||
31 | query: { | ||
32 | sort, | ||
33 | search | ||
34 | } | ||
35 | }) | ||
36 | } | ||
37 | |||
38 | function listUserSubscriptionVideos (url: string, token: string, sort = '-createdAt', statusCodeExpected = HttpStatusCode.OK_200) { | ||
39 | const path = '/api/v1/users/me/subscriptions/videos' | ||
40 | |||
41 | return makeGetRequest({ | ||
42 | url, | ||
43 | path, | ||
44 | token, | ||
45 | statusCodeExpected, | ||
46 | query: { sort } | ||
47 | }) | ||
48 | } | ||
49 | |||
50 | function getUserSubscription (url: string, token: string, uri: string, statusCodeExpected = HttpStatusCode.OK_200) { | ||
51 | const path = '/api/v1/users/me/subscriptions/' + uri | ||
52 | |||
53 | return makeGetRequest({ | ||
54 | url, | ||
55 | path, | ||
56 | token, | ||
57 | statusCodeExpected | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | function removeUserSubscription (url: string, token: string, uri: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { | ||
62 | const path = '/api/v1/users/me/subscriptions/' + uri | ||
63 | |||
64 | return makeDeleteRequest({ | ||
65 | url, | ||
66 | path, | ||
67 | token, | ||
68 | statusCodeExpected | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | function areSubscriptionsExist (url: string, token: string, uris: string[], statusCodeExpected = HttpStatusCode.OK_200) { | ||
73 | const path = '/api/v1/users/me/subscriptions/exist' | ||
74 | |||
75 | return makeGetRequest({ | ||
76 | url, | ||
77 | path, | ||
78 | query: { 'uris[]': uris }, | ||
79 | token, | ||
80 | statusCodeExpected | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | // --------------------------------------------------------------------------- | ||
85 | |||
86 | export { | ||
87 | areSubscriptionsExist, | ||
88 | addUserSubscription, | ||
89 | listUserSubscriptions, | ||
90 | getUserSubscription, | ||
91 | listUserSubscriptionVideos, | ||
92 | removeUserSubscription | ||
93 | } | ||