Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Fix: getHistory, errors, playlist, command args
Browse files Browse the repository at this point in the history
  • Loading branch information
thedark1337 committed Feb 6, 2017
1 parent d2b0900 commit f1d8167
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
85 changes: 45 additions & 40 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 0 additions & 4 deletions lib/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
};
Expand Down

0 comments on commit f1d8167

Please sign in to comment.