aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/follows.ts
blob: 0379bb1098de61a9534541e4cbe19059d9648141 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as request from 'supertest'
import { ServerInfo } from './servers'
import { waitJobs } from './jobs'
import { makePostBodyRequest } from '../requests/requests'

function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
  const path = '/api/v1/server/followers'

  const query = {
    start,
    count,
    sort,
    search
  }

  return request(url)
    .get(path)
    .query(query)
    .set('Accept', 'application/json')
    .expect(200)
    .expect('Content-Type', /json/)
}

function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
  const path = '/api/v1/server/followers/' + follower + '/accept'

  return makePostBodyRequest({
    url,
    token,
    path,
    statusCodeExpected
  })
}

function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
  const path = '/api/v1/server/followers/' + follower + '/reject'

  return makePostBodyRequest({
    url,
    token,
    path,
    statusCodeExpected
  })
}

function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
  const path = '/api/v1/server/following'

  const query = {
    start,
    count,
    sort,
    search
  }

  return request(url)
    .get(path)
    .query(query)
    .set('Accept', 'application/json')
    .expect(200)
    .expect('Content-Type', /json/)
}

function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
  const path = '/api/v1/server/following'

  const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
  return request(follower)
    .post(path)
    .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + accessToken)
    .send({ 'hosts': followingHosts })
    .expect(expectedStatus)
}

async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
  const path = '/api/v1/server/following/' + target.host

  return request(url)
    .delete(path)
    .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + accessToken)
    .expect(expectedStatus)
}

function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
  const path = '/api/v1/server/followers/peertube@' + follower.host

  return request(url)
    .delete(path)
    .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + accessToken)
    .expect(expectedStatus)
}

async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
  await Promise.all([
    follow(server1.url, [ server2.url ], server1.accessToken),
    follow(server2.url, [ server1.url ], server2.accessToken)
  ])

  // Wait request propagation
  await waitJobs([ server1, server2 ])

  return true
}

// ---------------------------------------------------------------------------

export {
  getFollowersListPaginationAndSort,
  getFollowingListPaginationAndSort,
  unfollow,
  removeFollower,
  follow,
  doubleFollow,
  acceptFollower,
  rejectFollower
}