Skip to content

Commit 14da63d

Browse files
author
Ben
committed
implement some async await stuff, no error handling yet
1 parent 5cc71da commit 14da63d

File tree

1 file changed

+44
-51
lines changed

1 file changed

+44
-51
lines changed

src/bot.js

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,37 @@ class Bot extends EventEmitter {
2020
rooms: new Set()
2121
});
2222
}
23-
auth(email, password) {
24-
return request({
23+
async auth(email, password) {
24+
const body = await request({
2525
method: 'GET',
2626
uri: 'https://stackoverflow.com/users/login',
2727
jar: this.jar
28-
}).then(body => {
29-
const $ = cheerio.load(body);
30-
const fkey = $('input[name="fkey"]').val();
31-
return request({
32-
method: 'POST',
33-
uri: 'https://stackoverflow.com/users/login',
34-
jar: this.jar,
35-
followAllRedirects: true,
36-
form: {
37-
email, password, fkey
38-
}
39-
});
4028
});
41-
}
42-
connect() {
29+
const $ = cheerio.load(body);
30+
const fkey = $('input[name="fkey"]').val();
4331
return request({
32+
method: 'POST',
33+
uri: 'https://stackoverflow.com/users/login',
34+
jar: this.jar,
35+
followAllRedirects: true,
36+
form: {
37+
email, password, fkey
38+
}
39+
});
40+
}
41+
async connect() {
42+
const body = await request({
4443
method: 'GET',
4544
uri: BASE_URL,
4645
jar: this.jar
47-
}).then(body => {
48-
const $ = cheerio.load(body);
49-
this.fkey = $('input[name="fkey"]').val();
5046
});
47+
const $ = cheerio.load(body);
48+
this.fkey = $('input[name="fkey"]').val();
49+
return body;
5150
}
52-
createWsConnection(roomid, fkey) {
51+
async createWsConnection(roomid, fkey) {
5352
const form = stringify({roomid, fkey});
54-
return request({
53+
const body = await request({
5554
method: 'POST',
5655
uri: `${BASE_URL}/ws-auth`,
5756
jar: this.jar,
@@ -62,47 +61,42 @@ class Bot extends EventEmitter {
6261
'Content-Length': form.length,
6362
'Content-Type': 'application/x-www-form-urlencoded'
6463
}
65-
})
66-
.then(body => JSON.parse(body).url)
67-
.then(wsAddress => new WS(`${wsAddress}?l=99999999999`, {origin: BASE_URL}));
64+
});
65+
const wsAddress = JSON.parse(body).url;
66+
return new WS(`${wsAddress}?l=99999999999`, {origin: BASE_URL});
6867
}
69-
listen(roomid) {
68+
async listen(roomid) {
7069
if(!this.fkey) {
7170
throw new Error('Not connected');
7271
}
7372
if(!roomid) {
7473
roomid = this.mainRoom;
7574
}
7675
this.rooms.add(roomid);
77-
return this.createWsConnection(roomid, this.fkey).then(ws => {
78-
this.ws = ws;
79-
this.ws.on('error', error => this.emit('error', error));
80-
this.ws.on('message', (message, flags) => {
81-
const json = JSON.parse(message);
82-
for(let [room, data] of Object.entries(json)) {
83-
if(data.e && Array.isArray(data.e) && (data.t != data.d)) {
84-
data.e.forEach(event => {
85-
this.emit('event', {room, event})
86-
});
87-
}
76+
this.ws = await this.createWsConnection(roomid, this.fkey);
77+
this.ws.on('error', error => this.emit('error', error));
78+
this.ws.on('message', (message, flags) => {
79+
const json = JSON.parse(message);
80+
for(let [room, data] of Object.entries(json)) {
81+
if(data.e && Array.isArray(data.e) && (data.t != data.d)) {
82+
data.e.forEach(event => {
83+
this.emit('event', {room, event})
84+
});
8885
}
89-
});
90-
return new Promise(resolve => {
91-
this.ws.once('open', () => {
92-
this.emit('open');
93-
resolve();
94-
});
95-
});
86+
}
87+
});
88+
this.ws.once('open', () => {
89+
this.emit('open');
9690
});
9791
}
98-
join(roomid) {
92+
async join(roomid) {
9993
if(this.rooms.has(roomid)) {
10094
throw new Error(`Already joined room ${roomid}`);
10195
}
10296
this.rooms.add(roomid);
103-
return this.createWsConnection(roomid, this.fkey).then(ws => {
104-
ws.on('open', () => ws.close());
105-
});
97+
const ws = await this.createWsConnection(roomid, this.fkey);
98+
// TODO: implement rooms, for now just close the connection because it's not supported
99+
ws.on('open', () => ws.close());
106100
}
107101
leaveAll() {
108102
if(!this.fkey) {
@@ -131,14 +125,13 @@ class Bot extends EventEmitter {
131125
}
132126
}
133127
}
134-
apiRequest(path, form) {
135-
return request({
128+
async apiRequest(path, form) {
129+
const response = await request({
136130
method: 'POST',
137131
uri: `${BASE_URL}/${path}`,
138132
form
139-
}).then(response => {
140-
return (response && response.length) ? JSON.parse(response) : {};
141133
});
134+
return (response && response.length) ? JSON.parse(response) : {};
142135
}
143136
send(text, roomid) {
144137
if(!roomid) {

0 commit comments

Comments
 (0)