From f1d8167771628b9e6e0dfcf0d28f98043b29e55b Mon Sep 17 00:00:00 2001 From: Thedark1337 Date: Sun, 5 Feb 2017 21:25:11 -0600 Subject: [PATCH] Fix: getHistory, errors, playlist, command args --- lib/client.js | 85 +++++++++++++++++++++++++++------------------------ lib/room.js | 4 --- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/client.js b/lib/client.js index 4a6c120..5ed7563 100644 --- a/lib/client.js +++ b/lib/client.js @@ -178,6 +178,22 @@ var playlists = new BufferObject(null, function(callback) { /* eslint-enable valid-jsdoc */ +/** +* Reconnect +* @type {Function} +* @param {*} roomSlug The room's slug +* @private +*/ +function reconnect(roomSlug) { + var slug = room.getRoomMeta().slug ? room.getRoomMeta().slug : roomSlug; + + that.close(); + logger.info('[Socket Server] Reconnecting'); + setImmediate(function() { + that.connect(slug); + }); +} + /** * Authentication information (e-mail and password) * THIS MUST NEVER BE ACCESSIBLE NOR PRINTING OUT TO CONSOLE @@ -204,7 +220,7 @@ function __bind(fn, me) { var timeout = nextMessage.timeout; sendEvent('chat', msg); - if (timeout !== undefined && isFinite(timeout) && ~~timeout > 0) { + if (timeout !== undefined && isFinite(~~timeout) && ~~timeout > 0) { var specificChatDeleter = function(data) { if (data.raw.uid === room.getSelf().id && data.message.trim() === msg.trim()) { setTimeout(function() { @@ -422,9 +438,9 @@ function queueREST(method, endpoint, data, callback, skipQueue) { */ function sendREST(opts, callback) { request(opts, function(err, res, body) { - if (err || res && res.statusCode !== 200) { - logger.error('[REST Error]', (err && err || ''), (res && res.statusCode && 'HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || '')); - return callback((err && err || '') + ' ' + (res && res.statusCode && 'HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || ''), null); + if (err || (res && res.statusCode !== 200) || !res) { + logger.error('[REST Error]', (err ? err : ''), (res && res.statusCode ? 'HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] : '')); + return callback((err ? err : ' ') + ' ' + (res && res.statusCode ? 'HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] : ''), null); } try { body = JSON.parse(body); @@ -437,7 +453,7 @@ function sendREST(opts, callback) { return callback(null, body.data); } - return callback(body.status && body.status || new Error("Can't connect to plug.dj"), null); + return callback(body && body.status ? body.status : new Error("Can't connect to plug.dj"), null); }); } @@ -648,14 +664,14 @@ function receivedChatMessage(messageData) { } // Arguments - messageData.args = messageData.args.split(' '); if (messageData.args === '') { messageData.args = []; } else { - for (i in messageData.args) { - if (!messageData.args.hasOwnProperty(i)) continue; - if (isFinite(messageData.args[i])) - messageData.args[i] = ~~messageData.args[i]; + messageData.args = messageData.args.split(' '); + + for (i = 0; i < messageData.args.length; i++) { + if (isFinite(Number(messageData.args[i]))) + messageData.args[i] = Number(messageData.args[i]); } } @@ -776,7 +792,7 @@ function getAuthCode(roomSlug, callback) { DateUtilities.setServerTime(_st); return callback(); } else { - logger.error(res && 'Error getting auth code: HTTP ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || 'Unknown Error'); + logger.error(res ? 'Error getting auth code: HTTP ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] : 'Unknown Error'); var slug = room.getRoomMeta().slug; that.close(); @@ -830,26 +846,13 @@ function connectSocket(roomSlug) { ws.on('error', function(a) { logger.error('[Socket Server] Error:', a); process.nextTick(function() { - var slug = room.getRoomMeta().slug ? room.getRoomMeta().slug : roomSlug; - - that.close(); - logger.info('[Socket Server] Reconnecting'); - setImmediate(function() { - that.connect(slug); - }); - + reconnect(roomSlug); }); }); ws.on('close', function(a) { logger.warn('[Socket Server] Closed with code', a); process.nextTick(function() { - var slug = room.getRoomMeta().slug ? room.getRoomMeta().slug : roomSlug; - - that.close(); - logger.info('[Socket Server] Reconnecting'); - setImmediate(function() { - that.connect(slug); - }); + reconnect(roomSlug); }); }); } @@ -963,6 +966,7 @@ function messageHandler(msg) { }; room.advance(data); + queueREST('GET', endpoints.HISTORY, undefined, __bind(room.setHistory, room)); // Override parts of the event data with actual User objects data.currentDJ = room.getDJ(); @@ -1046,7 +1050,7 @@ function messageHandler(msg) { logger.warning('Flood protection: Slowing down the sending of chat messages temporary'); break; case PlugAPI.events.MAINT_MODE_ALERT: - logger.warning('Plug is going into maintenance in ' + data.p + ' minutes.'); + logger.warning('Plug is going into maintenance in ' + data + ' minutes.'); break; case PlugAPI.events.MODERATE_STAFF: @@ -1154,8 +1158,8 @@ function performLoginCredentials(callback) { }, function(err, res, body) { var csrfToken; - if (res && res.statusCode !== 200 || err) { - handleErrors.error('Login Error: ' + (res && res.statusCode !== 200 && ' HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || '') + ' ' + (err && err || ''), callback); + if ((res && res.statusCode !== 200) || err) { + handleErrors.error('Login Error: ' + (res && res.statusCode !== 200 ? ' HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] : '') + ' ' + (err ? err : ''), callback); } try { _cookies.fromHeaders(res.headers); @@ -1177,8 +1181,8 @@ function performLoginCredentials(callback) { } }, function(error, response, data) { if (!error) { - if (data && data.status !== 'ok' || response && response.statusCode !== 200 || error) { - handleErrors.error('Login Error: ' + (data && data.status !== 'ok' && 'API Status: ' + data.status || '') + ' ' + (response && response.statusCode !== 200 && 'HTTP Status: ' + response.statusCode + ' - ' + http.STATUS_CODES[response.statusCode] || '') + ' ' + (error && error || ''), callback); + if ((data && data.status !== 'ok') || (response && response.statusCode !== 200) || error) { + handleErrors.error('Login Error: ' + (data && data.status !== 'ok' ? 'API Status: ' + data.status : '') + ' ' + (response && response.statusCode !== 200 ? 'HTTP Status: ' + response.statusCode + ' - ' + http.STATUS_CODES[response.statusCode] : '') + ' ' + (error ? error : ''), callback); } else { _cookies.fromHeaders(response.headers); loggingIn = false; @@ -1370,20 +1374,21 @@ PlugAPI.prototype.getChatHistory = function() { * @param {Function} callback Callback to get the history. History will be sent as argument. */ PlugAPI.prototype.getHistory = function(callback) { - if (typeof callback !== 'function') { - logger.error('You must specify callback to get history!'); - } if (initialized) { var history = room.getHistory(); if (history.length > 1) { - callback(history); - return; + if (typeof callback === 'function') { + return callback(history); + } else { + return history; + } } + } else { + setImmediate(function() { + that.getHistory(callback); + }); } - setImmediate(function() { - that.getHistory(callback); - }); }; PlugAPI.prototype.getBoothMeta = function() { @@ -1927,7 +1932,7 @@ PlugAPI.prototype.removeSongFromPlaylist = function(pid, mid, callback) { var callbackWrapper = function(err, data) { if (!err) - playlist.count++; + playlist.count--; if (typeof callback === 'function') { return callback(err, data); } diff --git a/lib/room.js b/lib/room.js index 20391f2..319e7c9 100644 --- a/lib/room.js +++ b/lib/room.js @@ -535,10 +535,6 @@ Room.prototype.advance = function(data) { } }; - if (songHistory.unshift(historyObj) > 50) { - songHistory.splice(50, songHistory.length - 50); - } - // Clear cache of users cacheUsers = {}; };