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