1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 04:32:35 +01:00

🚧 db listener

This commit is contained in:
Flam3rboy 2021-02-06 22:33:25 +01:00
parent 4eba7f5068
commit d1e7f3a37e
4 changed files with 41 additions and 14 deletions

22
package-lock.json generated
View File

@ -12,7 +12,7 @@
"discord-server-util": "github:discord-open-source/discord-server-util",
"erlpack": "^0.1.3",
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.2",
"lambert-db": "^1.1.5",
"lambert-server": "^1.1.0",
"missing-native-js-functions": "^1.2.2",
"node-fetch": "^2.6.1",
@ -364,11 +364,11 @@
},
"node_modules/discord-server-util": {
"version": "1.0.0",
"resolved": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#84d714324ad65de94cfb3076daa8f6622a54e438",
"resolved": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#862200a80d7db52c04abb2300cbb480e2f835dfb",
"license": "ISC",
"dependencies": {
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.4",
"lambert-db": "^1.1.5",
"missing-native-js-functions": "^1.2.2"
}
},
@ -760,9 +760,9 @@
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
},
"node_modules/lambert-db": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.4.tgz",
"integrity": "sha512-rRAZuGYiDGZTq8mgx4kqpvE1ESH6WUZdp+lvQJhAs/sJ2hAPIZg4IbsJh2RR3RiUt6E950WOzTPBgN786D0tYA==",
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.5.tgz",
"integrity": "sha512-07n2YzY0D9PiJySUR9zpe9/MFIzdvJ/pRWUF4GyvgqoUw3Z0eIUYjQ+Yd4o5ZANaZAwgCiTAwGTBtGmGXycOsw==",
"dependencies": {
"missing-native-js-functions": "^1.1.8",
"mongodb": "^3.6.2",
@ -2050,11 +2050,11 @@
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"discord-server-util": {
"version": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#84d714324ad65de94cfb3076daa8f6622a54e438",
"version": "git+ssh://git@github.com/discord-open-source/discord-server-util.git#862200a80d7db52c04abb2300cbb480e2f835dfb",
"from": "discord-server-util@github:discord-open-source/discord-server-util",
"requires": {
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.4",
"lambert-db": "^1.1.5",
"missing-native-js-functions": "^1.2.2"
}
},
@ -2367,9 +2367,9 @@
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
},
"lambert-db": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.4.tgz",
"integrity": "sha512-rRAZuGYiDGZTq8mgx4kqpvE1ESH6WUZdp+lvQJhAs/sJ2hAPIZg4IbsJh2RR3RiUt6E950WOzTPBgN786D0tYA==",
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.5.tgz",
"integrity": "sha512-07n2YzY0D9PiJySUR9zpe9/MFIzdvJ/pRWUF4GyvgqoUw3Z0eIUYjQ+Yd4o5ZANaZAwgCiTAwGTBtGmGXycOsw==",
"requires": {
"missing-native-js-functions": "^1.1.8",
"mongodb": "^3.6.2",

View File

@ -15,7 +15,7 @@
"discord-server-util": "github:discord-open-source/discord-server-util",
"erlpack": "^0.1.3",
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.2",
"lambert-db": "^1.1.5",
"lambert-server": "^1.1.0",
"missing-native-js-functions": "^1.2.2",
"node-fetch": "^2.6.1",

View File

@ -5,11 +5,12 @@ import { Message } from "./Message";
import { setHeartbeat } from "../util/setHeartbeat";
import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants";
import { setupListener } from "../listener/listener";
// TODO: check rate limit
// TODO: specify rate limit in config
export function Connection(this: Server, socket: WebSocket, request: IncomingMessage) {
export async function Connection(this: Server, socket: WebSocket, request: IncomingMessage) {
try {
socket.on("close", Close);
socket.on("message", Message);
@ -29,13 +30,15 @@ export function Connection(this: Server, socket: WebSocket, request: IncomingMes
setHeartbeat(socket);
Send(socket, {
await Send(socket, {
op: OPCODES.Hello,
d: {
heartbeat_interval: 1000 * 30,
},
});
await setupListener.call(socket);
socket.readyTimeout = setTimeout(() => {
return socket.close(CLOSECODES.Session_timed_out);
}, 1000 * 30);

24
src/listener/listener.ts Normal file
View File

@ -0,0 +1,24 @@
import { db, User } from "discord-server-util";
import { ProviderCache } from "lambert-db";
import { MongodbProviderCache } from "lambert-db/dist/Mongodb";
import WebSocket from "../util/WebSocket";
export async function setupListener(this: WebSocket) {
// TODO: shard guilds (only for bots)
const user: User = await db.data.users({ id: this.userid }).get();
// * MongoDB specific $in query to get all guilds of the user
const guildCache: MongodbProviderCache = await db.data
.guilds({ id: { $id: user.guilds } })
.cache({ onlyEvents: true })
.init();
guildCache.on("change", (data) => {
console.log(data);
});
this.once("close", async () => {
await guildCache.destroy();
});
}