2017-11-16 11:16:42 -05:00
|
|
|
import * as request from 'supertest'
|
2017-11-17 05:35:10 -05:00
|
|
|
import { ServerInfo } from './servers'
|
2018-06-13 04:06:50 -04:00
|
|
|
import { waitJobs } from './jobs'
|
2019-06-06 08:45:57 -04:00
|
|
|
import { makePostBodyRequest } from '../requests/requests'
|
2019-11-29 04:55:17 -05:00
|
|
|
import { ActivityPubActorType, FollowState } from '@shared/models'
|
2020-12-07 08:32:36 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2017-11-16 11:16:42 -05:00
|
|
|
|
2019-11-29 04:55:17 -05:00
|
|
|
function getFollowersListPaginationAndSort (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
|
|
|
search?: string
|
|
|
|
actorType?: ActivityPubActorType
|
2019-11-29 04:55:17 -05:00
|
|
|
state?: FollowState
|
|
|
|
}) {
|
|
|
|
const { url, start, count, sort, search, state, actorType } = options
|
2017-11-17 05:35:10 -05:00
|
|
|
const path = '/api/v1/server/followers'
|
2017-11-16 11:16:42 -05:00
|
|
|
|
2019-06-06 09:39:11 -04:00
|
|
|
const query = {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
2019-11-28 05:37:32 -05:00
|
|
|
search,
|
2019-11-29 04:55:17 -05:00
|
|
|
state,
|
|
|
|
actorType
|
2019-06-06 09:39:11 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:16:42 -05:00
|
|
|
return request(url)
|
|
|
|
.get(path)
|
2019-06-06 09:39:11 -04:00
|
|
|
.query(query)
|
2017-11-16 11:16:42 -05:00
|
|
|
.set('Accept', 'application/json')
|
2020-12-07 08:32:36 -05:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2017-11-16 11:16:42 -05:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
|
2019-04-08 09:18:04 -04:00
|
|
|
const path = '/api/v1/server/followers/' + follower + '/accept'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
token,
|
|
|
|
path,
|
|
|
|
statusCodeExpected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
|
2019-04-08 09:18:04 -04:00
|
|
|
const path = '/api/v1/server/followers/' + follower + '/reject'
|
|
|
|
|
|
|
|
return makePostBodyRequest({
|
|
|
|
url,
|
|
|
|
token,
|
|
|
|
path,
|
|
|
|
statusCodeExpected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-29 04:55:17 -05:00
|
|
|
function getFollowingListPaginationAndSort (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
|
|
|
search?: string
|
|
|
|
actorType?: ActivityPubActorType
|
2019-11-29 04:55:17 -05:00
|
|
|
state?: FollowState
|
|
|
|
}) {
|
|
|
|
const { url, start, count, sort, search, state, actorType } = options
|
2017-11-17 05:35:10 -05:00
|
|
|
const path = '/api/v1/server/following'
|
2017-11-16 11:16:42 -05:00
|
|
|
|
2019-06-06 09:39:11 -04:00
|
|
|
const query = {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
2019-11-28 05:37:32 -05:00
|
|
|
search,
|
2019-11-29 04:55:17 -05:00
|
|
|
state,
|
|
|
|
actorType
|
2019-06-06 09:39:11 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:16:42 -05:00
|
|
|
return request(url)
|
|
|
|
.get(path)
|
2019-06-06 09:39:11 -04:00
|
|
|
.query(query)
|
2017-11-16 11:16:42 -05:00
|
|
|
.set('Accept', 'application/json')
|
2020-12-07 08:32:36 -05:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2017-11-16 11:16:42 -05:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
|
2017-11-17 09:20:42 -05:00
|
|
|
const path = '/api/v1/server/following'
|
2017-11-16 11:16:42 -05:00
|
|
|
|
2017-11-17 05:35:10 -05:00
|
|
|
const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
|
2019-04-08 09:18:04 -04:00
|
|
|
return request(follower)
|
2017-11-16 11:16:42 -05:00
|
|
|
.post(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2020-01-31 10:56:52 -05:00
|
|
|
.send({ hosts: followingHosts })
|
2017-11-16 11:16:42 -05:00
|
|
|
.expect(expectedStatus)
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
|
2017-12-14 11:38:41 -05:00
|
|
|
const path = '/api/v1/server/following/' + target.host
|
2017-11-21 07:43:29 -05:00
|
|
|
|
2019-04-08 05:52:29 -04:00
|
|
|
return request(url)
|
2017-11-21 07:43:29 -05:00
|
|
|
.delete(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.expect(expectedStatus)
|
2019-04-08 05:52:29 -04:00
|
|
|
}
|
2017-11-21 07:43:29 -05:00
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
|
2019-04-08 05:52:29 -04:00
|
|
|
const path = '/api/v1/server/followers/peertube@' + follower.host
|
|
|
|
|
|
|
|
return request(url)
|
|
|
|
.delete(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.expect(expectedStatus)
|
2017-11-21 07:43:29 -05:00
|
|
|
}
|
|
|
|
|
2017-11-17 05:35:10 -05:00
|
|
|
async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
|
|
|
|
await Promise.all([
|
|
|
|
follow(server1.url, [ server2.url ], server1.accessToken),
|
|
|
|
follow(server2.url, [ server1.url ], server2.accessToken)
|
|
|
|
])
|
|
|
|
|
2017-11-17 09:42:12 -05:00
|
|
|
// Wait request propagation
|
2018-06-13 04:06:50 -04:00
|
|
|
await waitJobs([ server1, server2 ])
|
2017-11-17 09:42:12 -05:00
|
|
|
|
2017-11-17 05:35:10 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-16 11:16:42 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getFollowersListPaginationAndSort,
|
|
|
|
getFollowingListPaginationAndSort,
|
2017-11-21 07:43:29 -05:00
|
|
|
unfollow,
|
2019-04-08 05:52:29 -04:00
|
|
|
removeFollower,
|
2017-11-17 05:35:10 -05:00
|
|
|
follow,
|
2019-04-08 09:18:04 -04:00
|
|
|
doubleFollow,
|
|
|
|
acceptFollower,
|
|
|
|
rejectFollower
|
2017-11-16 11:16:42 -05:00
|
|
|
}
|