]>
Commit | Line | Data |
---|---|---|
b033851f | 1 | import { pick } from '@shared/core-utils' |
4d029ef8 | 2 | import { ActivityPubActorType, ActorFollow, FollowState, HttpStatusCode, ResultList, ServerFollowCreate } from '@shared/models' |
c3d29f69 | 3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
254d3579 | 4 | import { PeerTubeServer } from './server' |
c3d29f69 C |
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 | ||
b033851f | 18 | const query = pick(options, [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ]) |
c3d29f69 C |
19 | |
20 | return this.getRequestBody<ResultList<ActorFollow>>({ | |
21 | ...options, | |
22 | ||
c3d29f69 C |
23 | path, |
24 | query, | |
a1637fa1 | 25 | implicitToken: false, |
c3d29f69 C |
26 | defaultExpectedStatus: HttpStatusCode.OK_200 |
27 | }) | |
28 | } | |
29 | ||
30 | getFollowings (options: OverrideCommandOptions & { | |
4d029ef8 C |
31 | start?: number |
32 | count?: number | |
33 | sort?: string | |
c3d29f69 C |
34 | search?: string |
35 | actorType?: ActivityPubActorType | |
36 | state?: FollowState | |
4d029ef8 | 37 | } = {}) { |
c3d29f69 C |
38 | const path = '/api/v1/server/following' |
39 | ||
b033851f | 40 | const query = pick(options, [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ]) |
c3d29f69 C |
41 | |
42 | return this.getRequestBody<ResultList<ActorFollow>>({ | |
43 | ...options, | |
44 | ||
c3d29f69 C |
45 | path, |
46 | query, | |
a1637fa1 | 47 | implicitToken: false, |
c3d29f69 C |
48 | defaultExpectedStatus: HttpStatusCode.OK_200 |
49 | }) | |
50 | } | |
51 | ||
52 | follow (options: OverrideCommandOptions & { | |
4d029ef8 C |
53 | hosts?: string[] |
54 | handles?: string[] | |
c3d29f69 C |
55 | }) { |
56 | const path = '/api/v1/server/following' | |
57 | ||
4d029ef8 C |
58 | const fields: ServerFollowCreate = {} |
59 | ||
60 | if (options.hosts) { | |
61 | fields.hosts = options.hosts.map(f => f.replace(/^http:\/\//, '')) | |
62 | } | |
63 | ||
64 | if (options.handles) { | |
65 | fields.handles = options.handles | |
66 | } | |
c3d29f69 C |
67 | |
68 | return this.postBodyRequest({ | |
69 | ...options, | |
70 | ||
71 | path, | |
4d029ef8 | 72 | fields, |
a1637fa1 | 73 | implicitToken: true, |
c3d29f69 C |
74 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
75 | }) | |
76 | } | |
77 | ||
78 | async unfollow (options: OverrideCommandOptions & { | |
4d029ef8 | 79 | target: PeerTubeServer | string |
c3d29f69 | 80 | }) { |
4d029ef8 C |
81 | const { target } = options |
82 | ||
83 | const handle = typeof target === 'string' | |
84 | ? target | |
85 | : target.host | |
86 | ||
87 | const path = '/api/v1/server/following/' + handle | |
c3d29f69 C |
88 | |
89 | return this.deleteRequest({ | |
90 | ...options, | |
91 | ||
92 | path, | |
a1637fa1 | 93 | implicitToken: true, |
c3d29f69 C |
94 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
95 | }) | |
96 | } | |
97 | ||
98 | acceptFollower (options: OverrideCommandOptions & { | |
99 | follower: string | |
100 | }) { | |
101 | const path = '/api/v1/server/followers/' + options.follower + '/accept' | |
102 | ||
103 | return this.postBodyRequest({ | |
104 | ...options, | |
105 | ||
106 | path, | |
a1637fa1 | 107 | implicitToken: true, |
c3d29f69 C |
108 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
109 | }) | |
110 | } | |
111 | ||
112 | rejectFollower (options: OverrideCommandOptions & { | |
113 | follower: string | |
114 | }) { | |
115 | const path = '/api/v1/server/followers/' + options.follower + '/reject' | |
116 | ||
117 | return this.postBodyRequest({ | |
118 | ...options, | |
119 | ||
120 | path, | |
a1637fa1 | 121 | implicitToken: true, |
c3d29f69 C |
122 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
123 | }) | |
124 | } | |
125 | ||
126 | removeFollower (options: OverrideCommandOptions & { | |
254d3579 | 127 | follower: PeerTubeServer |
c3d29f69 C |
128 | }) { |
129 | const path = '/api/v1/server/followers/peertube@' + options.follower.host | |
130 | ||
131 | return this.deleteRequest({ | |
132 | ...options, | |
133 | ||
134 | path, | |
a1637fa1 | 135 | implicitToken: true, |
c3d29f69 C |
136 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
137 | }) | |
138 | } | |
139 | } |