]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/follows-command.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / follows-command.ts
CommitLineData
c3d29f69
C
1import { pick } from 'lodash'
2import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models'
3import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
254d3579 5import { PeerTubeServer } from './server'
c3d29f69
C
6
7export class FollowsCommand extends AbstractCommand {
8
9 getFollowers (options: OverrideCommandOptions & {
10 start: number
11 count: number
12 sort: string
13 search?: string
14 actorType?: ActivityPubActorType
15 state?: FollowState
16 }) {
17 const path = '/api/v1/server/followers'
18
19 const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ]
20 const query = pick(options, toPick)
21
22 return this.getRequestBody<ResultList<ActorFollow>>({
23 ...options,
24
c3d29f69
C
25 path,
26 query,
a1637fa1 27 implicitToken: false,
c3d29f69
C
28 defaultExpectedStatus: HttpStatusCode.OK_200
29 })
30 }
31
32 getFollowings (options: OverrideCommandOptions & {
33 start: number
34 count: number
35 sort: string
36 search?: string
37 actorType?: ActivityPubActorType
38 state?: FollowState
39 }) {
40 const path = '/api/v1/server/following'
41
42 const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ]
43 const query = pick(options, toPick)
44
45 return this.getRequestBody<ResultList<ActorFollow>>({
46 ...options,
47
c3d29f69
C
48 path,
49 query,
a1637fa1 50 implicitToken: false,
c3d29f69
C
51 defaultExpectedStatus: HttpStatusCode.OK_200
52 })
53 }
54
55 follow (options: OverrideCommandOptions & {
56 targets: string[]
57 }) {
58 const path = '/api/v1/server/following'
59
60 const hosts = options.targets.map(f => f.replace(/^http:\/\//, ''))
61
62 return this.postBodyRequest({
63 ...options,
64
65 path,
66 fields: { hosts },
a1637fa1 67 implicitToken: true,
c3d29f69
C
68 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
69 })
70 }
71
72 async unfollow (options: OverrideCommandOptions & {
254d3579 73 target: PeerTubeServer
c3d29f69
C
74 }) {
75 const path = '/api/v1/server/following/' + options.target.host
76
77 return this.deleteRequest({
78 ...options,
79
80 path,
a1637fa1 81 implicitToken: true,
c3d29f69
C
82 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
83 })
84 }
85
86 acceptFollower (options: OverrideCommandOptions & {
87 follower: string
88 }) {
89 const path = '/api/v1/server/followers/' + options.follower + '/accept'
90
91 return this.postBodyRequest({
92 ...options,
93
94 path,
a1637fa1 95 implicitToken: true,
c3d29f69
C
96 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
97 })
98 }
99
100 rejectFollower (options: OverrideCommandOptions & {
101 follower: string
102 }) {
103 const path = '/api/v1/server/followers/' + options.follower + '/reject'
104
105 return this.postBodyRequest({
106 ...options,
107
108 path,
a1637fa1 109 implicitToken: true,
c3d29f69
C
110 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
111 })
112 }
113
114 removeFollower (options: OverrideCommandOptions & {
254d3579 115 follower: PeerTubeServer
c3d29f69
C
116 }) {
117 const path = '/api/v1/server/followers/peertube@' + options.follower.host
118
119 return this.deleteRequest({
120 ...options,
121
122 path,
a1637fa1 123 implicitToken: true,
c3d29f69
C
124 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
125 })
126 }
127}