diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-07 09:16:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:17 +0200 |
commit | c3d29f694bf8c910f917be655626d0f80871124f (patch) | |
tree | c90dfdc1245c8a9aca49e9ea9c71ed8e6b9dd35f /shared/extra-utils/server | |
parent | 883a9019085ff9013079d6b1539b86f2f519175a (diff) | |
download | PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.tar.gz PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.tar.zst PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.zip |
Introduce follows command
Diffstat (limited to 'shared/extra-utils/server')
-rw-r--r-- | shared/extra-utils/server/follows-command.ts | 124 | ||||
-rw-r--r-- | shared/extra-utils/server/follows.ts | 131 | ||||
-rw-r--r-- | shared/extra-utils/server/index.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 3 |
4 files changed, 133 insertions, 127 deletions
diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts new file mode 100644 index 000000000..aba7bce82 --- /dev/null +++ b/shared/extra-utils/server/follows-command.ts | |||
@@ -0,0 +1,124 @@ | |||
1 | import { pick } from 'lodash' | ||
2 | import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' | ||
3 | import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | import { ServerInfo } from './servers' | ||
6 | |||
7 | export class FollowsCommand extends AbstractCommand { | ||
8 | |||
9 | getFollowers (options: OverrideCommandOptions & { | ||
10 | start: number | ||
11 | count: number | ||
12 | sort: string | ||
13 | search?: string | ||
14 | actorType?: ActivityPubActorType | ||
15 | state?: FollowState | ||
16 | }) { | ||
17 | const path = '/api/v1/server/followers' | ||
18 | |||
19 | const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ] | ||
20 | const query = pick(options, toPick) | ||
21 | |||
22 | return this.getRequestBody<ResultList<ActorFollow>>({ | ||
23 | ...options, | ||
24 | |||
25 | token: null, | ||
26 | |||
27 | path, | ||
28 | query, | ||
29 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | getFollowings (options: OverrideCommandOptions & { | ||
34 | start: number | ||
35 | count: number | ||
36 | sort: string | ||
37 | search?: string | ||
38 | actorType?: ActivityPubActorType | ||
39 | state?: FollowState | ||
40 | }) { | ||
41 | const path = '/api/v1/server/following' | ||
42 | |||
43 | const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ] | ||
44 | const query = pick(options, toPick) | ||
45 | |||
46 | return this.getRequestBody<ResultList<ActorFollow>>({ | ||
47 | ...options, | ||
48 | |||
49 | token: null, | ||
50 | |||
51 | path, | ||
52 | query, | ||
53 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | follow (options: OverrideCommandOptions & { | ||
58 | targets: string[] | ||
59 | }) { | ||
60 | const path = '/api/v1/server/following' | ||
61 | |||
62 | const hosts = options.targets.map(f => f.replace(/^http:\/\//, '')) | ||
63 | |||
64 | return this.postBodyRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | fields: { hosts }, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | async unfollow (options: OverrideCommandOptions & { | ||
74 | target: ServerInfo | ||
75 | }) { | ||
76 | const path = '/api/v1/server/following/' + options.target.host | ||
77 | |||
78 | return this.deleteRequest({ | ||
79 | ...options, | ||
80 | |||
81 | path, | ||
82 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | acceptFollower (options: OverrideCommandOptions & { | ||
87 | follower: string | ||
88 | }) { | ||
89 | const path = '/api/v1/server/followers/' + options.follower + '/accept' | ||
90 | |||
91 | return this.postBodyRequest({ | ||
92 | ...options, | ||
93 | |||
94 | path, | ||
95 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
96 | }) | ||
97 | } | ||
98 | |||
99 | rejectFollower (options: OverrideCommandOptions & { | ||
100 | follower: string | ||
101 | }) { | ||
102 | const path = '/api/v1/server/followers/' + options.follower + '/reject' | ||
103 | |||
104 | return this.postBodyRequest({ | ||
105 | ...options, | ||
106 | |||
107 | path, | ||
108 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | removeFollower (options: OverrideCommandOptions & { | ||
113 | follower: ServerInfo | ||
114 | }) { | ||
115 | const path = '/api/v1/server/followers/peertube@' + options.follower.host | ||
116 | |||
117 | return this.deleteRequest({ | ||
118 | ...options, | ||
119 | |||
120 | path, | ||
121 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
122 | }) | ||
123 | } | ||
124 | } | ||
diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts index 6aae4a31d..c23cebd81 100644 --- a/shared/extra-utils/server/follows.ts +++ b/shared/extra-utils/server/follows.ts | |||
@@ -1,126 +1,10 @@ | |||
1 | import * as request from 'supertest' | ||
2 | import { ServerInfo } from './servers' | ||
3 | import { waitJobs } from './jobs' | 1 | import { waitJobs } from './jobs' |
4 | import { makePostBodyRequest } from '../requests/requests' | 2 | import { ServerInfo } from './servers' |
5 | import { ActivityPubActorType, FollowState } from '@shared/models' | ||
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
7 | |||
8 | function getFollowersListPaginationAndSort (options: { | ||
9 | url: string | ||
10 | start: number | ||
11 | count: number | ||
12 | sort: string | ||
13 | search?: string | ||
14 | actorType?: ActivityPubActorType | ||
15 | state?: FollowState | ||
16 | }) { | ||
17 | const { url, start, count, sort, search, state, actorType } = options | ||
18 | const path = '/api/v1/server/followers' | ||
19 | |||
20 | const query = { | ||
21 | start, | ||
22 | count, | ||
23 | sort, | ||
24 | search, | ||
25 | state, | ||
26 | actorType | ||
27 | } | ||
28 | |||
29 | return request(url) | ||
30 | .get(path) | ||
31 | .query(query) | ||
32 | .set('Accept', 'application/json') | ||
33 | .expect(HttpStatusCode.OK_200) | ||
34 | .expect('Content-Type', /json/) | ||
35 | } | ||
36 | |||
37 | function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { | ||
38 | const path = '/api/v1/server/followers/' + follower + '/accept' | ||
39 | |||
40 | return makePostBodyRequest({ | ||
41 | url, | ||
42 | token, | ||
43 | path, | ||
44 | statusCodeExpected | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { | ||
49 | const path = '/api/v1/server/followers/' + follower + '/reject' | ||
50 | |||
51 | return makePostBodyRequest({ | ||
52 | url, | ||
53 | token, | ||
54 | path, | ||
55 | statusCodeExpected | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | function getFollowingListPaginationAndSort (options: { | ||
60 | url: string | ||
61 | start: number | ||
62 | count: number | ||
63 | sort: string | ||
64 | search?: string | ||
65 | actorType?: ActivityPubActorType | ||
66 | state?: FollowState | ||
67 | }) { | ||
68 | const { url, start, count, sort, search, state, actorType } = options | ||
69 | const path = '/api/v1/server/following' | ||
70 | |||
71 | const query = { | ||
72 | start, | ||
73 | count, | ||
74 | sort, | ||
75 | search, | ||
76 | state, | ||
77 | actorType | ||
78 | } | ||
79 | |||
80 | return request(url) | ||
81 | .get(path) | ||
82 | .query(query) | ||
83 | .set('Accept', 'application/json') | ||
84 | .expect(HttpStatusCode.OK_200) | ||
85 | .expect('Content-Type', /json/) | ||
86 | } | ||
87 | |||
88 | function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { | ||
89 | const path = '/api/v1/server/following' | ||
90 | |||
91 | const followingHosts = following.map(f => f.replace(/^http:\/\//, '')) | ||
92 | return request(follower) | ||
93 | .post(path) | ||
94 | .set('Accept', 'application/json') | ||
95 | .set('Authorization', 'Bearer ' + accessToken) | ||
96 | .send({ hosts: followingHosts }) | ||
97 | .expect(expectedStatus) | ||
98 | } | ||
99 | |||
100 | async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) { | ||
101 | const path = '/api/v1/server/following/' + target.host | ||
102 | |||
103 | return request(url) | ||
104 | .delete(path) | ||
105 | .set('Accept', 'application/json') | ||
106 | .set('Authorization', 'Bearer ' + accessToken) | ||
107 | .expect(expectedStatus) | ||
108 | } | ||
109 | |||
110 | function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) { | ||
111 | const path = '/api/v1/server/followers/peertube@' + follower.host | ||
112 | |||
113 | return request(url) | ||
114 | .delete(path) | ||
115 | .set('Accept', 'application/json') | ||
116 | .set('Authorization', 'Bearer ' + accessToken) | ||
117 | .expect(expectedStatus) | ||
118 | } | ||
119 | 3 | ||
120 | async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { | 4 | async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { |
121 | await Promise.all([ | 5 | await Promise.all([ |
122 | follow(server1.url, [ server2.url ], server1.accessToken), | 6 | server1.followsCommand.follow({ targets: [ server2.url ] }), |
123 | follow(server2.url, [ server1.url ], server2.accessToken) | 7 | server2.followsCommand.follow({ targets: [ server1.url ] }) |
124 | ]) | 8 | ]) |
125 | 9 | ||
126 | // Wait request propagation | 10 | // Wait request propagation |
@@ -132,12 +16,5 @@ async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { | |||
132 | // --------------------------------------------------------------------------- | 16 | // --------------------------------------------------------------------------- |
133 | 17 | ||
134 | export { | 18 | export { |
135 | getFollowersListPaginationAndSort, | 19 | doubleFollow |
136 | getFollowingListPaginationAndSort, | ||
137 | unfollow, | ||
138 | removeFollower, | ||
139 | follow, | ||
140 | doubleFollow, | ||
141 | acceptFollower, | ||
142 | rejectFollower | ||
143 | } | 20 | } |
diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts index f0071ba72..258084623 100644 --- a/shared/extra-utils/server/index.ts +++ b/shared/extra-utils/server/index.ts | |||
@@ -1,2 +1,4 @@ | |||
1 | export * from './contact-form-command' | 1 | export * from './contact-form-command' |
2 | export * from './debug-command' | 2 | export * from './debug-command' |
3 | export * from './follows-command' | ||
4 | export * from './follows' | ||
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 30e712ab8..7ac80cea0 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -18,6 +18,7 @@ import { makeGetRequest } from '../requests/requests' | |||
18 | import { SearchCommand } from '../search' | 18 | import { SearchCommand } from '../search' |
19 | import { ContactFormCommand } from './contact-form-command' | 19 | import { ContactFormCommand } from './contact-form-command' |
20 | import { DebugCommand } from './debug-command' | 20 | import { DebugCommand } from './debug-command' |
21 | import { FollowsCommand } from './follows-command' | ||
21 | 22 | ||
22 | interface ServerInfo { | 23 | interface ServerInfo { |
23 | app: ChildProcess | 24 | app: ChildProcess |
@@ -81,6 +82,7 @@ interface ServerInfo { | |||
81 | searchCommand?: SearchCommand | 82 | searchCommand?: SearchCommand |
82 | contactFormCommand?: ContactFormCommand | 83 | contactFormCommand?: ContactFormCommand |
83 | debugCommand?: DebugCommand | 84 | debugCommand?: DebugCommand |
85 | followsCommand?: FollowsCommand | ||
84 | } | 86 | } |
85 | 87 | ||
86 | function parallelTests () { | 88 | function parallelTests () { |
@@ -296,6 +298,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
296 | server.searchCommand = new SearchCommand(server) | 298 | server.searchCommand = new SearchCommand(server) |
297 | server.contactFormCommand = new ContactFormCommand(server) | 299 | server.contactFormCommand = new ContactFormCommand(server) |
298 | server.debugCommand = new DebugCommand(server) | 300 | server.debugCommand = new DebugCommand(server) |
301 | server.followsCommand = new FollowsCommand(server) | ||
299 | 302 | ||
300 | res(server) | 303 | res(server) |
301 | }) | 304 | }) |