]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/server/follows.ts
Add ability to remove an instance follower in 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
5 function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
6 const path = '/api/v1/server/followers'
7
8 return request(url)
9 .get(path)
10 .query({ start })
11 .query({ count })
12 .query({ sort })
13 .query({ search })
14 .set('Accept', 'application/json')
15 .expect(200)
16 .expect('Content-Type', /json/)
17 }
18
19 function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
20 const path = '/api/v1/server/following'
21
22 return request(url)
23 .get(path)
24 .query({ start })
25 .query({ count })
26 .query({ sort })
27 .query({ search })
28 .set('Accept', 'application/json')
29 .expect(200)
30 .expect('Content-Type', /json/)
31 }
32
33 async function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
34 const path = '/api/v1/server/following'
35
36 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
37 const res = await request(follower)
38 .post(path)
39 .set('Accept', 'application/json')
40 .set('Authorization', 'Bearer ' + accessToken)
41 .send({ 'hosts': followingHosts })
42 .expect(expectedStatus)
43
44 return res
45 }
46
47 async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
48 const path = '/api/v1/server/following/' + target.host
49
50 return request(url)
51 .delete(path)
52 .set('Accept', 'application/json')
53 .set('Authorization', 'Bearer ' + accessToken)
54 .expect(expectedStatus)
55 }
56
57 function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
58 const path = '/api/v1/server/followers/peertube@' + follower.host
59
60 return request(url)
61 .delete(path)
62 .set('Accept', 'application/json')
63 .set('Authorization', 'Bearer ' + accessToken)
64 .expect(expectedStatus)
65 }
66
67 async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
68 await Promise.all([
69 follow(server1.url, [ server2.url ], server1.accessToken),
70 follow(server2.url, [ server1.url ], server2.accessToken)
71 ])
72
73 // Wait request propagation
74 await waitJobs([ server1, server2 ])
75
76 return true
77 }
78
79 // ---------------------------------------------------------------------------
80
81 export {
82 getFollowersListPaginationAndSort,
83 getFollowingListPaginationAndSort,
84 unfollow,
85 removeFollower,
86 follow,
87 doubleFollow
88 }