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/server/follows-command.ts | 124 +++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 shared/extra-utils/server/follows-command.ts (limited to 'shared/extra-utils/server/follows-command.ts') 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 + }) + } +} -- cgit v1.2.3 From a1637fa1e25b60a88f7cfe50aac8953f50d55761 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 8 Jul 2021 10:55:16 +0200 Subject: Specify if we want to fallback to the server token --- shared/extra-utils/server/follows-command.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'shared/extra-utils/server/follows-command.ts') diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index aba7bce82..4e1e56d7a 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -22,10 +22,9 @@ export class FollowsCommand extends AbstractCommand { return this.getRequestBody>({ ...options, - token: null, - path, query, + implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 }) } @@ -46,10 +45,9 @@ export class FollowsCommand extends AbstractCommand { return this.getRequestBody>({ ...options, - token: null, - path, query, + implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 }) } @@ -66,6 +64,7 @@ export class FollowsCommand extends AbstractCommand { path, fields: { hosts }, + implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } @@ -79,6 +78,7 @@ export class FollowsCommand extends AbstractCommand { ...options, path, + implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } @@ -92,6 +92,7 @@ export class FollowsCommand extends AbstractCommand { ...options, path, + implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } @@ -105,6 +106,7 @@ export class FollowsCommand extends AbstractCommand { ...options, path, + implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } @@ -118,6 +120,7 @@ export class FollowsCommand extends AbstractCommand { ...options, path, + implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } -- cgit v1.2.3 From 254d3579f5338f5fd775c17d15cdfc37078bcfb4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 09:47:51 +0200 Subject: Use an object to represent a server --- shared/extra-utils/server/follows-command.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'shared/extra-utils/server/follows-command.ts') diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index 4e1e56d7a..4e9ed9494 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -2,7 +2,7 @@ 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' +import { PeerTubeServer } from './server' export class FollowsCommand extends AbstractCommand { @@ -70,7 +70,7 @@ export class FollowsCommand extends AbstractCommand { } async unfollow (options: OverrideCommandOptions & { - target: ServerInfo + target: PeerTubeServer }) { const path = '/api/v1/server/following/' + options.target.host @@ -112,7 +112,7 @@ export class FollowsCommand extends AbstractCommand { } removeFollower (options: OverrideCommandOptions & { - follower: ServerInfo + follower: PeerTubeServer }) { const path = '/api/v1/server/followers/peertube@' + options.follower.host -- cgit v1.2.3 From c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:42:24 +0200 Subject: Refactor requests --- shared/extra-utils/server/follows-command.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shared/extra-utils/server/follows-command.ts') diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index 4e9ed9494..694f5ea24 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -1,6 +1,6 @@ import { pick } from 'lodash' import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' import { PeerTubeServer } from './server' -- cgit v1.2.3 From 4c7e60bc17ee5830399bac4aa273356903421b4c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 14:27:30 +0200 Subject: Reorganize imports --- shared/extra-utils/server/follows-command.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'shared/extra-utils/server/follows-command.ts') diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index 694f5ea24..dce674ac5 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -1,6 +1,5 @@ import { pick } from 'lodash' -import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' -import { HttpStatusCode } from '@shared/models' +import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' import { PeerTubeServer } from './server' -- cgit v1.2.3 From 4d029ef8ec3d5274eeaa3ee6d808eb7035e7faef Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Jul 2021 14:15:15 +0200 Subject: Add ability for instances to follow any actor --- shared/extra-utils/server/follows-command.ts | 35 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'shared/extra-utils/server/follows-command.ts') diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index dce674ac5..2b889cf66 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -1,5 +1,5 @@ import { pick } from 'lodash' -import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList } from '@shared/models' +import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList, ServerFollowCreate } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' import { PeerTubeServer } from './server' @@ -29,13 +29,13 @@ export class FollowsCommand extends AbstractCommand { } getFollowings (options: OverrideCommandOptions & { - start: number - count: number - sort: string + 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' ] @@ -52,26 +52,41 @@ export class FollowsCommand extends AbstractCommand { } follow (options: OverrideCommandOptions & { - targets: string[] + hosts?: string[] + handles?: string[] }) { const path = '/api/v1/server/following' - const hosts = options.targets.map(f => f.replace(/^http:\/\//, '')) + const fields: ServerFollowCreate = {} + + if (options.hosts) { + fields.hosts = options.hosts.map(f => f.replace(/^http:\/\//, '')) + } + + if (options.handles) { + fields.handles = options.handles + } return this.postBodyRequest({ ...options, path, - fields: { hosts }, + fields, implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 }) } async unfollow (options: OverrideCommandOptions & { - target: PeerTubeServer + target: PeerTubeServer | string }) { - const path = '/api/v1/server/following/' + options.target.host + const { target } = options + + const handle = typeof target === 'string' + ? target + : target.host + + const path = '/api/v1/server/following/' + handle return this.deleteRequest({ ...options, -- cgit v1.2.3