1
0
Fork 0
peertube/shared/extra-utils/server/follows.ts

144 lines
3.7 KiB
TypeScript
Raw Normal View History

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