diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-21 15:51:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-21 15:51:30 +0200 |
commit | a24bd1ed41b43790bab6ba789580bb4e85f07d85 (patch) | |
tree | a54b0f6c921ba83a6e909cd0ced325b2d4b8863c /shared/extra-utils/server/follows.ts | |
parent | 5f26f13b3c16ac5ae0a3b0a7142d84a9528cf565 (diff) | |
parent | c63830f15403ac4e750829f27d8bbbdc9a59282c (diff) | |
download | PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.gz PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.zst PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.zip |
Merge branch 'next' into develop
Diffstat (limited to 'shared/extra-utils/server/follows.ts')
-rw-r--r-- | shared/extra-utils/server/follows.ts | 133 |
1 files changed, 5 insertions, 128 deletions
diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts index 6aae4a31d..698238f29 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 { PeerTubeServer } from './server' |
5 | import { ActivityPubActorType, FollowState } from '@shared/models' | ||
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
7 | 3 | ||
8 | function getFollowersListPaginationAndSort (options: { | 4 | async function doubleFollow (server1: PeerTubeServer, server2: PeerTubeServer) { |
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 | |||
120 | 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.follows.follow({ hosts: [ server2.url ] }), |
123 | follow(server2.url, [ server1.url ], server2.accessToken) | 7 | server2.follows.follow({ hosts: [ 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 | } |