]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/follows.ts
Upgrade server dep
[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'
ad3405d0 4import { makePostBodyRequest } from '../requests/requests'
97ecddae 5import { ActivityPubActorType, FollowState } from '@shared/models'
4610bc5b 6
97ecddae
C
7function 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
afffe988 17 const path = '/api/v1/server/followers'
4610bc5b 18
60919831
C
19 const query = {
20 start,
21 count,
22 sort,
b8f4167f 23 search,
97ecddae
C
24 state,
25 actorType
60919831
C
26 }
27
4610bc5b
C
28 return request(url)
29 .get(path)
60919831 30 .query(query)
4610bc5b
C
31 .set('Accept', 'application/json')
32 .expect(200)
33 .expect('Content-Type', /json/)
34}
35
14893eb7
C
36function 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
47function 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
97ecddae
C
58function 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
afffe988 68 const path = '/api/v1/server/following'
4610bc5b 69
60919831
C
70 const query = {
71 start,
72 count,
73 sort,
b8f4167f 74 search,
97ecddae
C
75 state,
76 actorType
60919831
C
77 }
78
4610bc5b
C
79 return request(url)
80 .get(path)
60919831 81 .query(query)
4610bc5b
C
82 .set('Accept', 'application/json')
83 .expect(200)
84 .expect('Content-Type', /json/)
85}
86
14893eb7 87function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
9a27cdc2 88 const path = '/api/v1/server/following'
4610bc5b 89
afffe988 90 const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
14893eb7 91 return request(follower)
4610bc5b
C
92 .post(path)
93 .set('Accept', 'application/json')
94 .set('Authorization', 'Bearer ' + accessToken)
afffe988 95 .send({ 'hosts': followingHosts })
4610bc5b 96 .expect(expectedStatus)
4610bc5b
C
97}
98
50d6de9c
C
99async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
100 const path = '/api/v1/server/following/' + target.host
0f91ae62 101
0e9c48c2 102 return request(url)
0f91ae62
C
103 .delete(path)
104 .set('Accept', 'application/json')
105 .set('Authorization', 'Bearer ' + accessToken)
106 .expect(expectedStatus)
0e9c48c2 107}
0f91ae62 108
0e9c48c2
C
109function 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)
0f91ae62
C
117}
118
afffe988
C
119async 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
572f8d3d 125 // Wait request propagation
3cd0734f 126 await waitJobs([ server1, server2 ])
572f8d3d 127
afffe988
C
128 return true
129}
130
4610bc5b
C
131// ---------------------------------------------------------------------------
132
133export {
134 getFollowersListPaginationAndSort,
135 getFollowingListPaginationAndSort,
0f91ae62 136 unfollow,
0e9c48c2 137 removeFollower,
afffe988 138 follow,
14893eb7
C
139 doubleFollow,
140 acceptFollower,
141 rejectFollower
4610bc5b 142}