summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-13 13:00:31 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-13 13:00:31 +0200
commitad6e082db2f830d4567c262c3cc12ce7df10d32a (patch)
tree348f0b902d1813c4a31a3e9b0475d52c0c7936f0 /util
parentcleanup rtc (diff)
downloadserver-ad6e082db2f830d4567c262c3cc12ce7df10d32a.tar.xz
:sparkles: event emit
Diffstat (limited to 'util')
-rw-r--r--util/package-lock.json121
-rw-r--r--util/package.json7
-rw-r--r--util/src/models/index.ts3
-rw-r--r--util/src/util/Config.ts3
-rw-r--r--util/src/util/Database.ts3
-rw-r--r--util/src/util/Event.ts94
-rw-r--r--util/src/util/index.ts1
7 files changed, 139 insertions, 93 deletions
diff --git a/util/package-lock.json b/util/package-lock.json
index 4977468d..4412d90e 100644
--- a/util/package-lock.json
+++ b/util/package-lock.json
@@ -1,12 +1,13 @@
 {
-	"name": "@fosscord/server-util",
+	"name": "@fosscord/util",
 	"version": "1.3.52",
 	"lockfileVersion": 2,
 	"requires": true,
 	"packages": {
 		"": {
-			"name": "@fosscord/server-util",
+			"name": "@fosscord/util",
 			"version": "1.3.52",
+			"hasInstallScript": true,
 			"license": "GPLV3",
 			"dependencies": {
 				"@types/jsonwebtoken": "^8.5.0",
@@ -20,7 +21,7 @@
 				"jsonwebtoken": "^8.5.1",
 				"missing-native-js-functions": "^1.2.2",
 				"mongodb": "^3.6.9",
-				"mongoose": "^5.12.3",
+				"mongoose": "^5.13.7",
 				"mongoose-autopopulate": "^0.12.3",
 				"typescript": "^4.1.3"
 			},
@@ -382,9 +383,9 @@
 			"integrity": "sha512-kNdwKWXh1hM8RdNqW2BIHsqD6fYN9RV27M+0uQF1pGF1yLKVc+xIv1VB8WEN1HxQ22N8Rj9sdEezOX2yBpsMZA=="
 		},
 		"node_modules/mongodb": {
-			"version": "3.6.9",
-			"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.9.tgz",
-			"integrity": "sha512-1nSCKgSunzn/CXwgOWgbPHUWOO5OfERcuOWISmqd610jn0s8BU9K4879iJVabqgpPPbA6hO7rG48eq+fGED3Mg==",
+			"version": "3.6.11",
+			"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.11.tgz",
+			"integrity": "sha512-4Y4lTFHDHZZdgMaHmojtNAlqkvddX2QQBEN0K//GzxhGwlI9tZ9R0vhbjr1Decw+TF7qK0ZLjQT292XgHRRQgw==",
 			"dependencies": {
 				"bl": "^2.2.1",
 				"bson": "^1.1.4",
@@ -420,25 +421,30 @@
 			}
 		},
 		"node_modules/mongoose": {
-			"version": "5.12.3",
-			"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.12.3.tgz",
-			"integrity": "sha512-frsSR9yeldaRpSUeTegXCSB0Tu5UGq8sHuHBuEV31Jk3COyxlKFQPL7UsdMhxPUCmk74FpOYSmNwxhWBEqgzQg==",
+			"version": "5.13.7",
+			"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.13.7.tgz",
+			"integrity": "sha512-ADIvftZ+KfoTALMZ0n8HvBlezFhcUd73hQaHQDwQ+3X+JZlqE47fUy9yhFZ2SjT+qzmuaCcIXCfhewIc38t2fQ==",
 			"dependencies": {
 				"@types/mongodb": "^3.5.27",
 				"bson": "^1.1.4",
 				"kareem": "2.3.2",
-				"mongodb": "3.6.5",
+				"mongodb": "3.6.11",
 				"mongoose-legacy-pluralize": "1.0.2",
 				"mpath": "0.8.3",
 				"mquery": "3.2.5",
 				"ms": "2.1.2",
+				"optional-require": "1.0.x",
 				"regexp-clone": "1.0.0",
 				"safe-buffer": "5.2.1",
-				"sift": "7.0.1",
+				"sift": "13.5.2",
 				"sliced": "1.0.1"
 			},
 			"engines": {
 				"node": ">=4.0.0"
+			},
+			"funding": {
+				"type": "opencollective",
+				"url": "https://opencollective.com/mongoose"
 			}
 		},
 		"node_modules/mongoose-autopopulate": {
@@ -451,24 +457,6 @@
 			"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
 			"integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
 		},
-		"node_modules/mongoose/node_modules/mongodb": {
-			"version": "3.6.5",
-			"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.5.tgz",
-			"integrity": "sha512-mQlYKw1iGbvJJejcPuyTaytq0xxlYbIoVDm2FODR+OHxyEiMR021vc32bTvamgBjCswsD54XIRwhg3yBaWqJjg==",
-			"dependencies": {
-				"bl": "^2.2.1",
-				"bson": "^1.1.4",
-				"denque": "^1.4.1",
-				"require_optional": "^1.0.1",
-				"safe-buffer": "^5.1.2"
-			},
-			"engines": {
-				"node": ">=4"
-			},
-			"optionalDependencies": {
-				"saslprep": "^1.0.0"
-			}
-		},
 		"node_modules/mpath": {
 			"version": "0.8.3",
 			"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.3.tgz",
@@ -565,15 +553,6 @@
 			"resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz",
 			"integrity": "sha512-TuAasHQNamyyJ2hb97IuBEif4qBHGjPHBS64sZwytpLEqtBQ1gPJTnOaQ6qmpET16cK14kkjbazl6+p0RRv0yw=="
 		},
-		"node_modules/require_optional": {
-			"version": "1.0.1",
-			"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
-			"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
-			"dependencies": {
-				"resolve-from": "^2.0.0",
-				"semver": "^5.1.0"
-			}
-		},
 		"node_modules/require-from-string": {
 			"version": "2.0.2",
 			"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@@ -587,14 +566,6 @@
 			"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
 			"integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
 		},
-		"node_modules/resolve-from": {
-			"version": "2.0.0",
-			"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
-			"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=",
-			"engines": {
-				"node": ">=0.10.0"
-			}
-		},
 		"node_modules/safe-buffer": {
 			"version": "5.2.1",
 			"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
@@ -621,9 +592,9 @@
 			}
 		},
 		"node_modules/sift": {
-			"version": "7.0.1",
-			"resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz",
-			"integrity": "sha512-oqD7PMJ+uO6jV9EQCl0LrRw1OwsiPsiFQR5AR30heR+4Dl7jBBbDLnNvWiak20tzZlSE1H7RB30SX/1j/YYT7g=="
+			"version": "13.5.2",
+			"resolved": "https://registry.npmjs.org/sift/-/sift-13.5.2.tgz",
+			"integrity": "sha512-+gxdEOMA2J+AI+fVsCqeNn7Tgx3M9ZN9jdi95939l1IJ8cZsqS8sqpJyOkic2SJk+1+98Uwryt/gL6XDaV+UZA=="
 		},
 		"node_modules/sliced": {
 			"version": "1.0.1",
@@ -1016,9 +987,9 @@
 			"integrity": "sha512-kNdwKWXh1hM8RdNqW2BIHsqD6fYN9RV27M+0uQF1pGF1yLKVc+xIv1VB8WEN1HxQ22N8Rj9sdEezOX2yBpsMZA=="
 		},
 		"mongodb": {
-			"version": "3.6.9",
-			"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.9.tgz",
-			"integrity": "sha512-1nSCKgSunzn/CXwgOWgbPHUWOO5OfERcuOWISmqd610jn0s8BU9K4879iJVabqgpPPbA6hO7rG48eq+fGED3Mg==",
+			"version": "3.6.11",
+			"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.11.tgz",
+			"integrity": "sha512-4Y4lTFHDHZZdgMaHmojtNAlqkvddX2QQBEN0K//GzxhGwlI9tZ9R0vhbjr1Decw+TF7qK0ZLjQT292XgHRRQgw==",
 			"requires": {
 				"bl": "^2.2.1",
 				"bson": "^1.1.4",
@@ -1029,37 +1000,23 @@
 			}
 		},
 		"mongoose": {
-			"version": "5.12.3",
-			"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.12.3.tgz",
-			"integrity": "sha512-frsSR9yeldaRpSUeTegXCSB0Tu5UGq8sHuHBuEV31Jk3COyxlKFQPL7UsdMhxPUCmk74FpOYSmNwxhWBEqgzQg==",
+			"version": "5.13.7",
+			"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.13.7.tgz",
+			"integrity": "sha512-ADIvftZ+KfoTALMZ0n8HvBlezFhcUd73hQaHQDwQ+3X+JZlqE47fUy9yhFZ2SjT+qzmuaCcIXCfhewIc38t2fQ==",
 			"requires": {
 				"@types/mongodb": "^3.5.27",
 				"bson": "^1.1.4",
 				"kareem": "2.3.2",
-				"mongodb": "3.6.5",
+				"mongodb": "3.6.11",
 				"mongoose-legacy-pluralize": "1.0.2",
 				"mpath": "0.8.3",
 				"mquery": "3.2.5",
 				"ms": "2.1.2",
+				"optional-require": "1.0.x",
 				"regexp-clone": "1.0.0",
 				"safe-buffer": "5.2.1",
-				"sift": "7.0.1",
+				"sift": "13.5.2",
 				"sliced": "1.0.1"
-			},
-			"dependencies": {
-				"mongodb": {
-					"version": "3.6.5",
-					"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.5.tgz",
-					"integrity": "sha512-mQlYKw1iGbvJJejcPuyTaytq0xxlYbIoVDm2FODR+OHxyEiMR021vc32bTvamgBjCswsD54XIRwhg3yBaWqJjg==",
-					"requires": {
-						"bl": "^2.2.1",
-						"bson": "^1.1.4",
-						"denque": "^1.4.1",
-						"require_optional": "^1.0.1",
-						"safe-buffer": "^5.1.2",
-						"saslprep": "^1.0.0"
-					}
-				}
 			}
 		},
 		"mongoose-autopopulate": {
@@ -1160,15 +1117,6 @@
 			"resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz",
 			"integrity": "sha512-TuAasHQNamyyJ2hb97IuBEif4qBHGjPHBS64sZwytpLEqtBQ1gPJTnOaQ6qmpET16cK14kkjbazl6+p0RRv0yw=="
 		},
-		"require_optional": {
-			"version": "1.0.1",
-			"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
-			"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
-			"requires": {
-				"resolve-from": "^2.0.0",
-				"semver": "^5.1.0"
-			}
-		},
 		"require-from-string": {
 			"version": "2.0.2",
 			"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@@ -1179,11 +1127,6 @@
 			"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
 			"integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
 		},
-		"resolve-from": {
-			"version": "2.0.0",
-			"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
-			"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
-		},
 		"safe-buffer": {
 			"version": "5.2.1",
 			"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
@@ -1204,9 +1147,9 @@
 			"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
 		},
 		"sift": {
-			"version": "7.0.1",
-			"resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz",
-			"integrity": "sha512-oqD7PMJ+uO6jV9EQCl0LrRw1OwsiPsiFQR5AR30heR+4Dl7jBBbDLnNvWiak20tzZlSE1H7RB30SX/1j/YYT7g=="
+			"version": "13.5.2",
+			"resolved": "https://registry.npmjs.org/sift/-/sift-13.5.2.tgz",
+			"integrity": "sha512-+gxdEOMA2J+AI+fVsCqeNn7Tgx3M9ZN9jdi95939l1IJ8cZsqS8sqpJyOkic2SJk+1+98Uwryt/gL6XDaV+UZA=="
 		},
 		"sliced": {
 			"version": "1.0.1",
diff --git a/util/package.json b/util/package.json
index 69af0f26..c2b08a3d 100644
--- a/util/package.json
+++ b/util/package.json
@@ -1,10 +1,11 @@
 {
-	"name": "@fosscord/server-util",
+	"name": "@fosscord/util",
 	"version": "1.3.52",
-	"description": "Utility functions for the all server repositories",
+	"description": "Utility functions and database models for fosscord",
 	"main": "dist/index.js",
 	"types": "dist/index.d.ts",
 	"scripts": {
+		"link": "npm run build && npm link",
 		"build": "tsc -b ."
 	},
 	"repository": {
@@ -36,7 +37,7 @@
 		"jsonwebtoken": "^8.5.1",
 		"missing-native-js-functions": "^1.2.2",
 		"mongodb": "^3.6.9",
-		"mongoose": "^5.12.3",
+		"mongoose": "^5.13.7",
 		"mongoose-autopopulate": "^0.12.3",
 		"typescript": "^4.1.3"
 	},
diff --git a/util/src/models/index.ts b/util/src/models/index.ts
index d0a46bf9..db74ef40 100644
--- a/util/src/models/index.ts
+++ b/util/src/models/index.ts
@@ -12,6 +12,9 @@ type UpdateAggregationStage =
 type EnforceDocument<T, TMethods> = T extends Document ? T : T & Document & TMethods;
 
 declare module "mongoose" {
+	interface SchemaOptions {
+		removeResponse?: string[];
+	}
 	interface Model<T, TQueryHelpers = {}, TMethods = {}> {
 		// removed null -> always return document -> throw error if it doesn't exist
 		findOne(
diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts
index 78b44315..b4648668 100644
--- a/util/src/util/Config.ts
+++ b/util/src/util/Config.ts
@@ -1,6 +1,6 @@
 import { Schema, model, Types, Document } from "mongoose";
 import "missing-native-js-functions";
-import db, { MongooseCache } from "./Database";
+import db from "./Database";
 import { Snowflake } from "./Snowflake";
 import crypto from "crypto";
 
@@ -15,6 +15,7 @@ export default {
 		return config as DefaultOptions;
 	},
 	set: function set(val: any) {
+		config = val.merge(config);
 		return db.collection("config").updateOne({}, { $set: val }, { upsert: true });
 	},
 };
diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts
index 8c6847a8..233152f1 100644
--- a/util/src/util/Database.ts
+++ b/util/src/util/Database.ts
@@ -14,6 +14,9 @@ const connection = mongoose.createConnection(uri, {
 	useFindAndModify: false,
 });
 console.log(`[Database] connect: mongodb://${url.username}@${url.host}${url.pathname}${url.search}`);
+connection.once("open", () => {
+	console.log("[Database] connected");
+});
 
 export default <Connection>connection;
 
diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts
new file mode 100644
index 00000000..13dd797a
--- /dev/null
+++ b/util/src/util/Event.ts
@@ -0,0 +1,94 @@
+import { Channel, ConsumeMessage } from "amqplib";
+import { EVENT, Event, EventModel } from "../models";
+import { RabbitMQ } from "./RabbitMQ";
+import EventEmitter from "events";
+const events = new EventEmitter();
+
+export async function emitEvent(payload: Omit<Event, "created_at">) {
+	const id = (payload.channel_id || payload.user_id || payload.guild_id) as string;
+	if (!id) console.error("event doesn't contain any id", payload);
+
+	if (RabbitMQ.connection) {
+		const data = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.data; // use rabbitmq for event transmission
+		await RabbitMQ.channel?.assertExchange(id, "fanout", { durable: false });
+
+		// assertQueue isn't needed, because a queue will automatically created if it doesn't exist
+		const successful = RabbitMQ.channel?.publish(id, "", Buffer.from(`${data}`), { type: payload.event });
+		if (!successful) throw new Error("failed to send event");
+	} else {
+		events.emit(id, payload);
+	}
+}
+
+export async function initEvent() {
+	await RabbitMQ.init(); // does nothing if rabbitmq is not setup
+	if (RabbitMQ.connection) {
+	} else {
+		// use event emitter
+	}
+}
+
+export interface EventOpts extends Event {
+	acknowledge?: Function;
+	channel?: Channel;
+	cancel: Function;
+}
+
+export interface ListenEventOpts {
+	channel?: Channel;
+	acknowledge?: boolean;
+}
+
+export async function listenEvent(event: string, callback: (event: EventOpts) => any, opts?: ListenEventOpts) {
+	if (RabbitMQ.connection) {
+		// @ts-ignore
+		return rabbitListen(opts?.channel || RabbitMQ.channel, event, callback, { acknowledge: opts?.acknowledge });
+	} else {
+		const cancel = () => events.removeListener(event, callback);
+		events.addListener(event, (opts) => callback({ ...opts, cancel }));
+
+		return cancel;
+	}
+}
+
+async function rabbitListen(
+	channel: Channel,
+	id: string,
+	callback: (event: EventOpts) => any,
+	opts?: { acknowledge?: boolean }
+) {
+	await channel.assertExchange(id, "fanout", { durable: false });
+	const q = await channel.assertQueue("", { exclusive: true, autoDelete: true });
+
+	const cancel = () => {
+		channel.cancel(q.queue);
+		channel.unbindQueue(q.queue, id, "");
+	};
+
+	channel.bindQueue(q.queue, id, "");
+	channel.consume(
+		q.queue,
+		(opts) => {
+			if (!opts) return;
+
+			const data = JSON.parse(opts.content.toString());
+			const event = opts.properties.type as EVENT;
+
+			callback({
+				event,
+				data,
+				acknowledge() {
+					channel.ack(opts);
+				},
+				channel,
+				cancel,
+			});
+			// rabbitCh.ack(opts);
+		},
+		{
+			noAck: !opts?.acknowledge,
+		}
+	);
+
+	return cancel;
+}
diff --git a/util/src/util/index.ts b/util/src/util/index.ts
index 7523a6ad..0aad15f2 100644
--- a/util/src/util/index.ts
+++ b/util/src/util/index.ts
@@ -7,3 +7,4 @@ export * from "./Snowflake";
 export * from "./UserFlags";
 export * from "./toBigInt";
 export * from "./RabbitMQ";
+export * from "./Event";