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