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:03:18 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-13 13:03:18 +0200
commitebff2f42417e0e579d59d296f5150024f8c850ee (patch)
tree707b264d239859853411adb5142b64727debd51b /util
parentFix naming (diff)
parentabstract Event emission (diff)
downloadserver-ebff2f42417e0e579d59d296f5150024f8c850ee.tar.xz
Merge branch 'master' into pr/darkhpp/261-2
Diffstat (limited to 'util')
-rw-r--r--util/package.json11
-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
6 files changed, 108 insertions, 7 deletions
diff --git a/util/package.json b/util/package.json
index 0478fe69..c2b08a3d 100644
--- a/util/package.json
+++ b/util/package.json
@@ -1,13 +1,12 @@
 {
-	"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": {
-		"test": "echo \"Error: no test specified\" && exit 1",
-		"build": "tsc -b .",
-		"prepublish": "npm run build"
+		"link": "npm run build && npm link",
+		"build": "tsc -b ."
 	},
 	"repository": {
 		"type": "git",
@@ -38,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";