-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
232 lines (203 loc) · 6.38 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const axios = require('axios');
const { Ban, Token } = require('./types');
const {
UnauthorizedError,
ForbiddenError,
TooManyRequestsError,
SpamWatchError,
} = require('./errors');
class Client {
/**
* Client to interface with the SpamWatch API.
* @param {String} token The Authorization Token
* @param {String} [host='https://api.spamwat.ch'] The API host. Defaults to the official API.
*/
constructor(token, host = 'https://api.spamwat.ch') {
this._host = host;
this._instance = axios.create({
validateStatus(status) {
return status < 500;
},
});
this._instance.defaults.headers.common.Authorization = `Bearer ${token}`;
this._token = token;
}
/**
* Make a request and handle errors
* @param {String} path Path on the API without a leading slash
* @param {String} [method='get'] The request method. Defaults to GET
* @param {Object} [kwargs] Keyword arguments passed to the request method.
* @returns {Promise<axios.AxiosResponse>} The json response and the request object
* @throws {UnauthorizedError} Make sure your token is correct
* @throws {ForbiddenError}
*/
async _makeRequest(path, method = 'get', kwargs) {
const response = await this._instance.request({
method: method,
url: `${this._host}/${path}`,
...kwargs,
});
switch (response.status) {
default:
return response;
case 400:
throw new SpamWatchError(response, response.data.reason);
case 401:
throw new UnauthorizedError(response, 'Make sure your token is correct');
case 403:
throw new ForbiddenError(response, this._token);
case 429:
throw new TooManyRequestsError(response);
}
}
/**
* Get the API version
* @returns {Object}
*/
async version() {
const { data } = await this._makeRequest('version');
return data;
}
/**
* Get all tokens
* Requires Root permission
* @returns {Token[]}
*/
async getTokens() {
const { data } = await this._makeRequest('tokens');
return data.map(token => new Token(token.id, token.permission, token.token, token.userid, token.retired));
}
/**
* Creates a token with the given parameters
* Requires Root permission
* @param {Number} userid The Telegram User ID of the token owner
* @param {'Root' | 'Admin' | 'User'} permission The permission level the Token should have
* @returns {Token|null} The created tokern
*/
async createToken(userid, permission) {
let data;
try {
({ data } = await this._makeRequest('tokens', 'post', {
data: {
id: userid,
permission,
},
}));
} catch (error) {
if (error instanceof SpamWatchError && error.status === 400) {
return null;
}
throw error;
}
return new Token(data.id, data.permission, data.token, data.userid, data.retired);
}
/**
* Gets the Token that the request was made with.
* @returns {Token}
*/
async getSelf() {
const { data } = await this._makeRequest('tokens/self');
return new Token(data.id, data.permission, data.token, data.userid, data.retired);
}
/**
* Get a token using its ID
* Requires Root permission
* @param {Number} tokenid The token ID
* @returns {Token} The token
*/
async getToken(tokenid) {
const { data } = await this._makeRequest(`tokens/${tokenid}`);
return new Token(data.id, data.permission, data.token, data.userid, data.retired);
}
/**
* Get a token using UserID
* Requires Root permission
* @param {Number} userid The user ID
* @returns {Array} The token
*/
async getTokenUser(userid) {
const { data } = await this._makeRequest(`tokens/userid/${userid}`);
return data.map(token => new Token(token.id, token.permission, token.token, token.userid, token.retired));
}
/**
* Delete the token using its ID
* @param tokenid The ID of the token
*/
async deleteToken(tokenid) {
await this._makeRequest(`tokens/${tokenid}`, 'delete');
}
/**
* Get a ban
* @param {Number} userid ID of the user
* @returns {Ban|Boolean} Ban object or null
*/
async getBan(userid) {
const { status, data } = await this._makeRequest(`banlist/${userid}`);
if (status === 404) {
return false;
}
return new Ban(data.id, data.reason, data.admin, data.date, data.message);
}
/**
* Get a list of all bans
* Requires Admin Permission
* @returns {Ban[]} A list of bans
*/
async getBans() {
const { data } = await this._makeRequest('banlist');
return data.map(ban => new Ban(ban.id, ban.reason, ban.admin, ban.date, ban.message));
}
/**
* Remove a ban
*/
async deleteBan(userid) {
await this._makeRequest(`banlist/${userid}`, 'delete');
}
/**
* @returns {Number[]}
*/
async getBansMin() {
const { data } = await this._makeRequest('banlist/all');
if (!data) {
return [];
} else if (typeof data === 'number') {
return [data];
}
return data.split('\n').map(uid => Number(uid));
}
/**
* Adds a ban
* @param {Number} userid ID of the banned user
* @param {String} reason Reason why the user was banned
*/
async addBan(userid, reason, message) {
await this._makeRequest('banlist', 'post', {
data: [
{
id: userid,
reason,
message,
},
],
});
}
/**
* Add a list of Bans
* @param {Ban[]} data List of Ban objects
*/
async addBans(data) {
await this._makeRequest('banlist', 'post', {
data: data.map(d => ({
id: d.id,
reason: d.reason,
})),
});
}
async stats() {
const { data } = await this._makeRequest('stats');
return data;
}
}
module.exports = {
Client,
};