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