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