irgendwas mit user- und channelcaching

This commit is contained in:
Flummi 2019-11-19 01:49:17 +01:00
parent af80c04968
commit 6885508e01

View File

@ -14,8 +14,8 @@ export default class slack extends EventEmitter {
this.socket = null; this.socket = null;
this.server = { this.server = {
set: this.set, set: this.set,
channel: null, channel: new Map(),
user: null, user: new Map(),
wss: { wss: {
url: null, url: null,
socket: null socket: null
@ -29,7 +29,17 @@ export default class slack extends EventEmitter {
})(); })();
} }
async connect() { async connect() {
const res = await (await fetch(`${this.api}/rtm.connect?token=${this.token}`)).json(); const res = await (await fetch(`${this.api}/rtm.start?token=${this.token}`)).json();
res.channels.forEach(channel => {
this.server.channel.set(channel.id, channel.name);
});
res.users.forEach(user => {
this.server.user.set(user.id, {
account: user.name,
nickname: user.real_name
});
});
if (!res.ok) if (!res.ok)
throw this.emit("data", [ "error", res.description ]); // more infos throw this.emit("data", [ "error", res.description ]); // more infos
@ -51,7 +61,7 @@ export default class slack extends EventEmitter {
setInterval(async () => await this.ping(), 3e4); // 30 seconds lul setInterval(async () => await this.ping(), 3e4); // 30 seconds lul
this.server.wss.socket.on("data", data => { this.server.wss.socket.on("data", async data => {
data = data.toString("utf-8").replace(/\0/g, ""); data = data.toString("utf-8").replace(/\0/g, "");
data = JSON.parse(data.substr(data.indexOf("{"))); data = JSON.parse(data.substr(data.indexOf("{")));
@ -60,6 +70,9 @@ export default class slack extends EventEmitter {
if(data.type !== "message") if(data.type !== "message")
return false; return false;
await Promise.all([this.getChannel(data.channel), this.getUser(data.user)]).catch(err => this.emit("data", [ "error", err ]));
//await this.getUser(data.user).catch(err => this.emit("data", [ "error", err ]));
return this.emit("data", [ "message", this.reply(data) ]); return this.emit("data", [ "message", this.reply(data) ]);
}) })
.on("end", () => this.emit("data", [ "debug", "stream ended" ])) .on("end", () => this.emit("data", [ "debug", "stream ended" ]))
@ -71,6 +84,26 @@ export default class slack extends EventEmitter {
}); });
} }
async getChannel(channelId) {
if(this.server.channel.has(channelId))
return this.server.channel.get(channelId);
const res = await (await fetch(`${this.api}/conversations.info?channel=${channelId}&token=${this.token}`)).json();
this.server.channel.set(channelId, res.channel.name);
return res.channel.name;
}
async getUser(userId) {
if(this.server.user.has(userId))
return this.server.user.get(userId);
const res = await (await fetch(`${this.api}/users.info?user=${userId}&token=${this.token}`)).json();
this.server.user.set(userId, {
account: res.user.name,
nickname: res.user.real_name
});
}
async send(channel, text) { async send(channel, text) {
await this.write({ await this.write({
type: "message", type: "message",
@ -113,20 +146,19 @@ export default class slack extends EventEmitter {
this.server.wss.socket.write(frame); this.server.wss.socket.write(frame);
this.server.wss.socket.write(Buffer.from(msg)); this.server.wss.socket.write(Buffer.from(msg));
this.server.wss.socket.uncork(); this.server.wss.socket.uncork();
return true;
} }
reply(tmp) { reply(tmp) {
return { return {
type: "slack", type: "slack",
network: "Slack", network: "Slack",
channel: tmp.channel, // get channelname channel: this.server.channel.get(tmp.channel), // get channelname
channelid: tmp.channel, channelid: tmp.channel,
user: { user: {
prefix: `${tmp.user}!${tmp.user}`, // get username prefix: `${tmp.user}!${this.server.user.get(tmp.user).account}`, // get username
nick: tmp.user, // get username nick: this.server.user.get(tmp.user).nickname, // get username
username: tmp.user, // get username username: this.server.user.get(tmp.user).nickname, // get username
account: tmp.user account: this.server.user.get(tmp.user).account
}, },
self: this.server, self: this.server,
message: tmp.text, message: tmp.text,