Merge pull request #59 from notsapinho/main
MongooseCache resubscription
This commit is contained in:
commit
19b1ca48ca
@ -20,6 +20,7 @@
|
|||||||
"lambert-db": "^1.1.8",
|
"lambert-db": "^1.1.8",
|
||||||
"lambert-server": "^1.1.7",
|
"lambert-server": "^1.1.7",
|
||||||
"missing-native-js-functions": "^1.2.3",
|
"missing-native-js-functions": "^1.2.3",
|
||||||
|
"mongoose-autopopulate": "^0.12.3",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"typescript": "^4.2.3",
|
"typescript": "^4.2.3",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2",
|
||||||
@ -27,6 +28,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
|
"@types/mongoose-autopopulate": "^0.10.1",
|
||||||
"@types/uuid": "^8.3.0",
|
"@types/uuid": "^8.3.0",
|
||||||
"@types/ws": "^7.4.0"
|
"@types/ws": "^7.4.0"
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import { db, Event, MongooseCache, UserModel, getPermission, Permissions } from
|
|||||||
import { OPCODES } from "../util/Constants";
|
import { OPCODES } from "../util/Constants";
|
||||||
import { Send } from "../util/Send";
|
import { Send } from "../util/Send";
|
||||||
import WebSocket from "../util/WebSocket";
|
import WebSocket from "../util/WebSocket";
|
||||||
|
import "missing-native-js-functions";
|
||||||
|
|
||||||
// TODO: close connection on Invalidated Token
|
// TODO: close connection on Invalidated Token
|
||||||
// TODO: check intent
|
// TODO: check intent
|
||||||
@ -11,34 +12,40 @@ import WebSocket from "../util/WebSocket";
|
|||||||
// Sharding: calculate if the current shard id matches the formula: shard_id = (guild_id >> 22) % num_shards
|
// Sharding: calculate if the current shard id matches the formula: shard_id = (guild_id >> 22) % num_shards
|
||||||
// https://discord.com/developers/docs/topics/gateway#sharding
|
// https://discord.com/developers/docs/topics/gateway#sharding
|
||||||
|
|
||||||
export async function setupListener(this: WebSocket) {
|
export interface DispatchOpts {
|
||||||
const user = await UserModel.findOne({ id: this.user_id }).lean().exec();
|
eventStream: MongooseCache;
|
||||||
var guilds = user.guilds;
|
guilds: Array<string>;
|
||||||
const shard_count = 10n;
|
}
|
||||||
const shard_id = 0n;
|
|
||||||
|
|
||||||
if (shard_count) {
|
function getPipeline(this: WebSocket, guilds: string[]) {
|
||||||
guilds = user.guilds.filter((x) => (BigInt(x) >> 22n) % shard_count === shard_id);
|
if (this.shard_count) {
|
||||||
|
guilds = guilds.filter((x) => (BigInt(x) >> 22n) % this.shard_count === this.shard_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const eventStream = new MongooseCache(
|
return [
|
||||||
db.collection("events"),
|
|
||||||
[
|
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$or: [{ "fullDocument.guild_id": { $in: guilds } }, { "fullDocument.user_id": this.user_id }],
|
$or: [{ "fullDocument.guild_id": { $in: guilds } }, { "fullDocument.user_id": this.user_id }],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
];
|
||||||
{ onlyEvents: true }
|
}
|
||||||
);
|
|
||||||
|
export async function setupListener(this: WebSocket) {
|
||||||
|
const user = await UserModel.findOne({ id: this.user_id }).lean().exec();
|
||||||
|
var guilds = user.guilds;
|
||||||
|
|
||||||
|
const eventStream = new MongooseCache(db.collection("events"), getPipeline.call(this, guilds), {
|
||||||
|
onlyEvents: true,
|
||||||
|
});
|
||||||
|
|
||||||
await eventStream.init();
|
await eventStream.init();
|
||||||
eventStream.on("insert", dispatch.bind(this));
|
eventStream.on("insert", (document: Event) => dispatch.call(this, document, { eventStream, guilds }));
|
||||||
|
|
||||||
this.once("close", () => eventStream.destroy());
|
this.once("close", () => eventStream.destroy());
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function dispatch(this: WebSocket, document: Event) {
|
export async function dispatch(this: WebSocket, document: Event, { eventStream, guilds }: DispatchOpts) {
|
||||||
var permission = new Permissions("ADMINISTRATOR"); // default permission for dms
|
var permission = new Permissions("ADMINISTRATOR"); // default permission for dms
|
||||||
console.log("event", document);
|
console.log("event", document);
|
||||||
|
|
||||||
@ -48,6 +55,14 @@ export async function dispatch(this: WebSocket, document: Event) {
|
|||||||
permission = await getPermission(this.user_id, document.guild_id, channel_id);
|
permission = await getPermission(this.user_id, document.guild_id, channel_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (document.event === "GUILD_CREATE") {
|
||||||
|
guilds.push(document.guild_id);
|
||||||
|
eventStream.changeStream(getPipeline.call(this, guilds));
|
||||||
|
} else if (document.event === "GUILD_DELETE") {
|
||||||
|
guilds.remove(document.guild);
|
||||||
|
eventStream.changeStream(getPipeline.call(this, guilds));
|
||||||
|
}
|
||||||
|
|
||||||
// check intents: https://discord.com/developers/docs/topics/gateway#gateway-intents
|
// check intents: https://discord.com/developers/docs/topics/gateway#gateway-intents
|
||||||
switch (document.event) {
|
switch (document.event) {
|
||||||
case "GUILD_CREATE":
|
case "GUILD_CREATE":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user