]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/follows.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / follows.ts
CommitLineData
4610bc5b 1import * as request from 'supertest'
afffe988 2import { ServerInfo } from './servers'
3cd0734f 3import { waitJobs } from './jobs'
14893eb7 4import { makeGetRequest, makePostBodyRequest } from '..'
4610bc5b 5
b014b6b9 6function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
afffe988 7 const path = '/api/v1/server/followers'
4610bc5b
C
8
9 return request(url)
10 .get(path)
11 .query({ start })
12 .query({ count })
13 .query({ sort })
b014b6b9 14 .query({ search })
4610bc5b
C
15 .set('Accept', 'application/json')
16 .expect(200)
17 .expect('Content-Type', /json/)
18}
19
14893eb7
C
20function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
21 const path = '/api/v1/server/followers/' + follower + '/accept'
22
23 return makePostBodyRequest({
24 url,
25 token,
26 path,
27 statusCodeExpected
28 })
29}
30
31function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
32 const path = '/api/v1/server/followers/' + follower + '/reject'
33
34 return makePostBodyRequest({
35 url,
36 token,
37 path,
38 statusCodeExpected
39 })
40}
41
b014b6b9 42function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
afffe988 43 const path = '/api/v1/server/following'
4610bc5b
C
44
45 return request(url)
46 .get(path)
47 .query({ start })
48 .query({ count })
49 .query({ sort })
b014b6b9 50 .query({ search })
4610bc5b
C
51 .set('Accept', 'application/json')
52 .expect(200)
53 .expect('Content-Type', /json/)
54}
55
14893eb7 56function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
9a27cdc2 57 const path = '/api/v1/server/following'
4610bc5b 58
afffe988 59 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
14893eb7 60 return request(follower)
4610bc5b
C
61 .post(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
afffe988 64 .send({ 'hosts': followingHosts })
4610bc5b 65 .expect(expectedStatus)
4610bc5b
C
66}
67
50d6de9c
C
68async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
69 const path = '/api/v1/server/following/' + target.host
0f91ae62 70
0e9c48c2 71 return request(url)
0f91ae62
C
72 .delete(path)
73 .set('Accept', 'application/json')
74 .set('Authorization', 'Bearer ' + accessToken)
75 .expect(expectedStatus)
0e9c48c2 76}
0f91ae62 77
0e9c48c2
C
78function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
79 const path = '/api/v1/server/followers/peertube@' + follower.host
80
81 return request(url)
82 .delete(path)
83 .set('Accept', 'application/json')
84 .set('Authorization', 'Bearer ' + accessToken)
85 .expect(expectedStatus)
0f91ae62
C
86}
87
afffe988
C
88async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
89 await Promise.all([
90 follow(server1.url, [ server2.url ], server1.accessToken),
91 follow(server2.url, [ server1.url ], server2.accessToken)
92 ])
93
572f8d3d 94 // Wait request propagation
3cd0734f 95 await waitJobs([ server1, server2 ])
572f8d3d 96
afffe988
C
97 return true
98}
99
4610bc5b
C
100// ---------------------------------------------------------------------------
101
102export {
103 getFollowersListPaginationAndSort,
104 getFollowingListPaginationAndSort,
0f91ae62 105 unfollow,
0e9c48c2 106 removeFollower,
afffe988 107 follow,
14893eb7
C
108 doubleFollow,
109 acceptFollower,
110 rejectFollower
4610bc5b 111}