Browse Source

Fixed some backend bugs with cache and stations

Kristian Vos 4 years ago
parent
commit
e304e81dd0
3 changed files with 22 additions and 18 deletions
  1. 1 3
      backend/index.js
  2. 12 2
      backend/logic/cache/index.js
  3. 9 13
      backend/logic/stations.js

+ 1 - 3
backend/index.js

@@ -13,9 +13,7 @@ process.on("uncaughtException", err => {
 	console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
 });
 
-const blacklistedConsoleLogs = [
-	
-];
+const blacklistedConsoleLogs = [];
 
 const oldConsole = {};
 oldConsole.log = console.log;

+ 12 - 2
backend/logic/cache/index.js

@@ -171,6 +171,9 @@ class _CacheModule extends CoreClass {
 
 			let { key } = payload;
 
+			if (!payload.table) return reject(new Error("Invalid table!"));
+			if (!key) return reject(new Error("Invalid key!"));
+
 			if (mongoose.Types.ObjectId.isValid(key)) key = key.toString();
 
 			CacheModule.client.hdel(payload.table, key, err => {
@@ -191,6 +194,8 @@ class _CacheModule extends CoreClass {
 	HGETALL(payload) {
 		// table, cb, parseJson = true
 		return new Promise((resolve, reject) => {
+			if (!payload.table) return reject(new Error("Invalid table!"));
+
 			CacheModule.client.hgetall(payload.table, (err, obj) => {
 				if (err) return reject(new Error(err));
 				if (obj)
@@ -221,12 +226,15 @@ class _CacheModule extends CoreClass {
             pubs[channel].on('error', (err) => console.error);
             } */
 
-			let { value } = payload;
+			let { channel, value } = payload;
+
+			if (!channel) return reject(new Error("Invalid channel!"));
+			if (!value) return reject(new Error("Invalid value!"));
 
 			if (["object", "array"].includes(typeof value)) value = JSON.stringify(value);
 
 			// pubs[channel].publish(channel, value);
-			CacheModule.client.publish(payload.channel, value, err => {
+			CacheModule.client.publish(channel, value, err => {
 				if (err) reject(err);
 				else resolve();
 			});
@@ -244,6 +252,8 @@ class _CacheModule extends CoreClass {
 	SUB(payload) {
 		// channel, cb, parseJson = true
 		return new Promise(resolve => {
+			if (!payload.channel) return reject(new Error("Invalid channel!"));
+
 			if (subs[payload.channel] === undefined) {
 				subs[payload.channel] = {
 					client: redis.createClient({

+ 9 - 13
backend/logic/stations.js

@@ -233,19 +233,15 @@ class _StationsModule extends CoreClass {
 							.then()
 							.catch()
 							.finally(() => {
-								NotificationsModule.runJob(
-									"SUBSCRIBE",
-									{
-										name: `stations.nextSong?id=${station._id}`,
-										cb: () =>
-											StationsModule.runJob("SKIP_STATION", {
-												stationId: station._id
-											}),
-										unique: true,
-										station
-									},
-									this
-								)
+								NotificationsModule.runJob("SUBSCRIBE", {
+									name: `stations.nextSong?id=${station._id}`,
+									cb: () =>
+										StationsModule.runJob("SKIP_STATION", {
+											stationId: station._id
+										}),
+									unique: true,
+									station
+								})
 									.then()
 									.catch();