]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/server/follows.ts
Add ability to manually approves instance followers in REST API
[github/Chocobozzz/PeerTube.git] / shared / utils / server / follows.ts
1 import * as request from 'supertest'
2 import { ServerInfo } from './servers'
3 import { waitJobs } from './jobs'
4 import { makeGetRequest, makePostBodyRequest } from '..'
5
6 function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
7 const path = '/api/v1/server/followers'
8
9 return request(url)
10 .get(path)
11 .query({ start })
12 .query({ count })
13 .query({ sort })
14 .query({ search })
15 .set('Accept', 'application/json')
16 .expect(200)
17 .expect('Content-Type', /json/)
18 }
19
20 function 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
31 function 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
42 function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
43 const path = '/api/v1/server/following'
44
45 return request(url)
46 .get(path)
47 .query({ start })
48 .query({ count })
49 .query({ sort })
50 .query({ search })
51 .set('Accept', 'application/json')
52 .expect(200)
53 .expect('Content-Type', /json/)
54 }
55
56 function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
57 const path = '/api/v1/server/following'
58
59 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
60 return request(follower)
61 .post(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
64 .send({ 'hosts': followingHosts })
65 .expect(expectedStatus)
66 }
67
68 async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
69 const path = '/api/v1/server/following/' + target.host
70
71 return request(url)
72 .delete(path)
73 .set('Accept', 'application/json')
74 .set('Authorization', 'Bearer ' + accessToken)
75 .expect(expectedStatus)
76 }
77
78 function 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)
86 }
87
88 async 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
94 // Wait request propagation
95 await waitJobs([ server1, server2 ])
96
97 return true
98 }
99
100 // ---------------------------------------------------------------------------
101
102 export {
103 getFollowersListPaginationAndSort,
104 getFollowingListPaginationAndSort,
105 unfollow,
106 removeFollower,
107 follow,
108 doubleFollow,
109 acceptFollower,
110 rejectFollower
111 }