]>
Commit | Line | Data |
---|---|---|
1 | import { pick } from 'lodash' | |
2 | import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' | |
3 | import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' | |
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
5 | import { ServerInfo } from './servers' | |
6 | ||
7 | export 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 | ||
25 | path, | |
26 | query, | |
27 | implicitToken: false, | |
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 | ||
48 | path, | |
49 | query, | |
50 | implicitToken: false, | |
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 }, | |
67 | implicitToken: true, | |
68 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
69 | }) | |
70 | } | |
71 | ||
72 | async unfollow (options: OverrideCommandOptions & { | |
73 | target: ServerInfo | |
74 | }) { | |
75 | const path = '/api/v1/server/following/' + options.target.host | |
76 | ||
77 | return this.deleteRequest({ | |
78 | ...options, | |
79 | ||
80 | path, | |
81 | implicitToken: true, | |
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, | |
95 | implicitToken: true, | |
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, | |
109 | implicitToken: true, | |
110 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
111 | }) | |
112 | } | |
113 | ||
114 | removeFollower (options: OverrideCommandOptions & { | |
115 | follower: ServerInfo | |
116 | }) { | |
117 | const path = '/api/v1/server/followers/peertube@' + options.follower.host | |
118 | ||
119 | return this.deleteRequest({ | |
120 | ...options, | |
121 | ||
122 | path, | |
123 | implicitToken: true, | |
124 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
125 | }) | |
126 | } | |
127 | } |