From c3d29f694bf8c910f917be655626d0f80871124f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 7 Jul 2021 09:16:40 +0200 Subject: Introduce follows command --- shared/extra-utils/index.ts | 5 +- shared/extra-utils/server/follows-command.ts | 124 +++++++++++++++++++++++++ shared/extra-utils/server/follows.ts | 131 +-------------------------- shared/extra-utils/server/index.ts | 2 + shared/extra-utils/server/servers.ts | 3 + 5 files changed, 135 insertions(+), 130 deletions(-) create mode 100644 shared/extra-utils/server/follows-command.ts (limited to 'shared') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 7b5835e47..48431ed83 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -7,15 +7,14 @@ export * from './miscs' export * from './mock-servers' export * from './moderation' export * from './overviews' +export * from './search' +export * from './server' export * from './requests/check-api-params' export * from './requests/requests' -export * from './search' - export * from './server/clients' export * from './server/config' -export * from './server/follows' export * from './server/jobs' export * from './server/plugins' export * from './server/servers' diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts new file mode 100644 index 000000000..aba7bce82 --- /dev/null +++ b/shared/extra-utils/server/follows-command.ts @@ -0,0 +1,124 @@ +import { pick } from 'lodash' +import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { AbstractCommand, OverrideCommandOptions } from '../shared' +import { ServerInfo } from './servers' + +export class FollowsCommand extends AbstractCommand { + + getFollowers (options: OverrideCommandOptions & { + start: number + count: number + sort: string + search?: string + actorType?: ActivityPubActorType + state?: FollowState + }) { + const path = '/api/v1/server/followers' + + const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ] + const query = pick(options, toPick) + + return this.getRequestBody>({ + ...options, + + token: null, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getFollowings (options: OverrideCommandOptions & { + start: number + count: number + sort: string + search?: string + actorType?: ActivityPubActorType + state?: FollowState + }) { + const path = '/api/v1/server/following' + + const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ] + const query = pick(options, toPick) + + return this.getRequestBody>({ + ...options, + + token: null, + + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + follow (options: OverrideCommandOptions & { + targets: string[] + }) { + const path = '/api/v1/server/following' + + const hosts = options.targets.map(f => f.replace(/^http:\/\//, '')) + + return this.postBodyRequest({ + ...options, + + path, + fields: { hosts }, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + async unfollow (options: OverrideCommandOptions & { + target: ServerInfo + }) { + const path = '/api/v1/server/following/' + options.target.host + + return this.deleteRequest({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + acceptFollower (options: OverrideCommandOptions & { + follower: string + }) { + const path = '/api/v1/server/followers/' + options.follower + '/accept' + + return this.postBodyRequest({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + rejectFollower (options: OverrideCommandOptions & { + follower: string + }) { + const path = '/api/v1/server/followers/' + options.follower + '/reject' + + return this.postBodyRequest({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + removeFollower (options: OverrideCommandOptions & { + follower: ServerInfo + }) { + const path = '/api/v1/server/followers/peertube@' + options.follower.host + + return this.deleteRequest({ + ...options, + + path, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } +} diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts index 6aae4a31d..c23cebd81 100644 --- a/shared/extra-utils/server/follows.ts +++ b/shared/extra-utils/server/follows.ts @@ -1,126 +1,10 @@ -import * as request from 'supertest' -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' - -function getFollowersListPaginationAndSort (options: { - url: string - start: number - count: number - sort: string - search?: string - actorType?: ActivityPubActorType - state?: FollowState -}) { - const { url, start, count, sort, search, state, actorType } = options - const path = '/api/v1/server/followers' - - const query = { - start, - count, - sort, - search, - state, - actorType - } - - return request(url) - .get(path) - .query(query) - .set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .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: { - url: string - start: number - count: number - sort: string - search?: string - actorType?: ActivityPubActorType - state?: FollowState -}) { - const { url, start, count, sort, search, state, actorType } = options - const path = '/api/v1/server/following' - - const query = { - start, - count, - sort, - search, - state, - actorType - } - - return request(url) - .get(path) - .query(query) - .set('Accept', 'application/json') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /json/) -} - -function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_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 = HttpStatusCode.NO_CONTENT_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 = 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) -} +import { ServerInfo } from './servers' async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { await Promise.all([ - follow(server1.url, [ server2.url ], server1.accessToken), - follow(server2.url, [ server1.url ], server2.accessToken) + server1.followsCommand.follow({ targets: [ server2.url ] }), + server2.followsCommand.follow({ targets: [ server1.url ] }) ]) // Wait request propagation @@ -132,12 +16,5 @@ async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { // --------------------------------------------------------------------------- export { - getFollowersListPaginationAndSort, - getFollowingListPaginationAndSort, - unfollow, - removeFollower, - follow, - doubleFollow, - acceptFollower, - rejectFollower + doubleFollow } diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts index f0071ba72..258084623 100644 --- a/shared/extra-utils/server/index.ts +++ b/shared/extra-utils/server/index.ts @@ -1,2 +1,4 @@ export * from './contact-form-command' export * from './debug-command' +export * from './follows-command' +export * from './follows' diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 30e712ab8..7ac80cea0 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -18,6 +18,7 @@ import { makeGetRequest } from '../requests/requests' import { SearchCommand } from '../search' import { ContactFormCommand } from './contact-form-command' import { DebugCommand } from './debug-command' +import { FollowsCommand } from './follows-command' interface ServerInfo { app: ChildProcess @@ -81,6 +82,7 @@ interface ServerInfo { searchCommand?: SearchCommand contactFormCommand?: ContactFormCommand debugCommand?: DebugCommand + followsCommand?: FollowsCommand } function parallelTests () { @@ -296,6 +298,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.searchCommand = new SearchCommand(server) server.contactFormCommand = new ContactFormCommand(server) server.debugCommand = new DebugCommand(server) + server.followsCommand = new FollowsCommand(server) res(server) }) -- cgit v1.2.3