]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/server/follows-command.ts
dce674ac5cf7284134084c10f53c88f5d39501df
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / follows-command.ts
1 import { pick } from 'lodash'
2 import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList } from '@shared/models'
3 import { AbstractCommand, OverrideCommandOptions } from '../shared'
4 import { PeerTubeServer } from './server'
5
6 export 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
24 path,
25 query,
26 implicitToken: false,
27 defaultExpectedStatus: HttpStatusCode.OK_200
28 })
29 }
30
31 getFollowings (options: OverrideCommandOptions & {
32 start: number
33 count: number
34 sort: string
35 search?: string
36 actorType?: ActivityPubActorType
37 state?: FollowState
38 }) {
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
47 path,
48 query,
49 implicitToken: false,
50 defaultExpectedStatus: HttpStatusCode.OK_200
51 })
52 }
53
54 follow (options: OverrideCommandOptions & {
55 targets: string[]
56 }) {
57 const path = '/api/v1/server/following'
58
59 const hosts = options.targets.map(f => f.replace(/^http:\/\//, ''))
60
61 return this.postBodyRequest({
62 ...options,
63
64 path,
65 fields: { hosts },
66 implicitToken: true,
67 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
68 })
69 }
70
71 async unfollow (options: OverrideCommandOptions & {
72 target: PeerTubeServer
73 }) {
74 const path = '/api/v1/server/following/' + options.target.host
75
76 return this.deleteRequest({
77 ...options,
78
79 path,
80 implicitToken: true,
81 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
82 })
83 }
84
85 acceptFollower (options: OverrideCommandOptions & {
86 follower: string
87 }) {
88 const path = '/api/v1/server/followers/' + options.follower + '/accept'
89
90 return this.postBodyRequest({
91 ...options,
92
93 path,
94 implicitToken: true,
95 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
96 })
97 }
98
99 rejectFollower (options: OverrideCommandOptions & {
100 follower: string
101 }) {
102 const path = '/api/v1/server/followers/' + options.follower + '/reject'
103
104 return this.postBodyRequest({
105 ...options,
106
107 path,
108 implicitToken: true,
109 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
110 })
111 }
112
113 removeFollower (options: OverrideCommandOptions & {
114 follower: PeerTubeServer
115 }) {
116 const path = '/api/v1/server/followers/peertube@' + options.follower.host
117
118 return this.deleteRequest({
119 ...options,
120
121 path,
122 implicitToken: true,
123 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
124 })
125 }
126 }