diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-07 09:16:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:17 +0200 |
commit | c3d29f694bf8c910f917be655626d0f80871124f (patch) | |
tree | c90dfdc1245c8a9aca49e9ea9c71ed8e6b9dd35f /shared/extra-utils/server/follows-command.ts | |
parent | 883a9019085ff9013079d6b1539b86f2f519175a (diff) | |
download | PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.tar.gz PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.tar.zst PeerTube-c3d29f694bf8c910f917be655626d0f80871124f.zip |
Introduce follows command
Diffstat (limited to 'shared/extra-utils/server/follows-command.ts')
-rw-r--r-- | shared/extra-utils/server/follows-command.ts | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts new file mode 100644 index 000000000..aba7bce82 --- /dev/null +++ b/shared/extra-utils/server/follows-command.ts | |||
@@ -0,0 +1,124 @@ | |||
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 | token: null, | ||
26 | |||
27 | path, | ||
28 | query, | ||
29 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | getFollowings (options: OverrideCommandOptions & { | ||
34 | start: number | ||
35 | count: number | ||
36 | sort: string | ||
37 | search?: string | ||
38 | actorType?: ActivityPubActorType | ||
39 | state?: FollowState | ||
40 | }) { | ||
41 | const path = '/api/v1/server/following' | ||
42 | |||
43 | const toPick = [ 'start', 'count', 'sort', 'search', 'state', 'actorType' ] | ||
44 | const query = pick(options, toPick) | ||
45 | |||
46 | return this.getRequestBody<ResultList<ActorFollow>>({ | ||
47 | ...options, | ||
48 | |||
49 | token: null, | ||
50 | |||
51 | path, | ||
52 | query, | ||
53 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | follow (options: OverrideCommandOptions & { | ||
58 | targets: string[] | ||
59 | }) { | ||
60 | const path = '/api/v1/server/following' | ||
61 | |||
62 | const hosts = options.targets.map(f => f.replace(/^http:\/\//, '')) | ||
63 | |||
64 | return this.postBodyRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | fields: { hosts }, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | async unfollow (options: OverrideCommandOptions & { | ||
74 | target: ServerInfo | ||
75 | }) { | ||
76 | const path = '/api/v1/server/following/' + options.target.host | ||
77 | |||
78 | return this.deleteRequest({ | ||
79 | ...options, | ||
80 | |||
81 | path, | ||
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 | 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 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | removeFollower (options: OverrideCommandOptions & { | ||
113 | follower: ServerInfo | ||
114 | }) { | ||
115 | const path = '/api/v1/server/followers/peertube@' + options.follower.host | ||
116 | |||
117 | return this.deleteRequest({ | ||
118 | ...options, | ||
119 | |||
120 | path, | ||
121 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
122 | }) | ||
123 | } | ||
124 | } | ||