diff --git a/lib/easyrtc_public_obj.js b/lib/easyrtc_public_obj.js index 4147d519..76a54b05 100644 --- a/lib/easyrtc_public_obj.js +++ b/lib/easyrtc_public_obj.js @@ -923,9 +923,9 @@ pub.util.isValidIncomingMessage = function(type, msg, appObj, callback) { } break; - case "stillAlive": - break; - + case "stillAlive": + break; + case "setPresence" : if (!_.isObject(msg.msgData)) { callback(null, false, "MSG_REJECT_BAD_DATA"); @@ -1258,6 +1258,47 @@ pub.app = function(appName, callback) { }; + /** + * Returns an array of all easyrtcids connected to the application associated with a given username + * + * @memberof pub.appObj + * @param {string} username Username to search for. + * @param {function(?Error, Array.)} callback Callback with error and array of easyrtcids. + */ + appObj.getConnectedEasyrtcidsWithUsername = function(username, callback) { + var easyrtcids = []; + + for (var currentEasyrtcid in e.app[appName].connection) { + if (!e.app[appName].connection.hasOwnProperty(currentEasyrtcid)) continue; + if (e.app[appName].connection[currentEasyrtcid].username == username){ + easyrtcids.push(currentEasyrtcid); + } + } + + callback(null, easyrtcids); + }; + + /** + * Returns an array of all easyrtcids connected to the application associated with a given username + * + * @memberof pub.appObj + * @param {string} username Username to search for. + * @returns {Array.} array of easyrtcids. + */ + appObj.getConnectedEasyrtcidsWithUsernameSync = function(username) { + var easyrtcids = []; + + for (var currentEasyrtcid in e.app[appName].connection) { + if (!e.app[appName].connection.hasOwnProperty(currentEasyrtcid)) continue; + if (e.app[appName].connection[currentEasyrtcid].username == username){ + easyrtcids.push(currentEasyrtcid); + } + } + + return easyrtcids; + }; + + /** * Returns application level field object for a given field name to a provided callback. * @@ -1624,7 +1665,7 @@ pub.app = function(appName, callback) { * @param {function(?Error, Object=)} callback Callback with error and field object (any type) */ connectionObj.getField = function(fieldName, callback) { - if (connectionObj.hasFieldValueSync(fieldName) && + if (connectionObj.hasFieldValueSync(fieldName) && e.app[appName].connection[easyrtcid] ) { callback(null, pub.util.deepCopy(e.app[appName].connection[easyrtcid].field[fieldName])); } else { @@ -1643,7 +1684,7 @@ pub.app = function(appName, callback) { * @returns {Object} Field object */ connectionObj.getFieldSync = function(fieldName) { - if (connectionObj.hasFieldValueSync(fieldName) && + if (connectionObj.hasFieldValueSync(fieldName) && e.app[appName].connection[easyrtcid]) { return pub.util.deepCopy(e.app[appName].connection[easyrtcid].field[fieldName]); } else { @@ -1656,15 +1697,16 @@ pub.app = function(appName, callback) { }; - /** * Returns connection level field value for a given field name. If the field is not set, it will return a null field value. This is a synchronous function, thus may not be available in custom cases where state is not kept in memory. + /** + * Returns connection level field value for a given field name. If the field is not set, it will return a null field value. This is a synchronous function, thus may not be available in custom cases where state is not kept in memory. * * @memberof pub.appObj.connectionObj * @param {string} fieldName Field name * @returns {?*} Field value */ connectionObj.getFieldValueSync = function(fieldName) { - if (connectionObj.hasFieldValueSync(fieldName) && - e.app[appName].connection[easyrtcid] ) { + if (connectionObj.hasFieldValueSync(fieldName) && + e.app[appName].connection[easyrtcid] ) { return pub.util.deepCopy(e.app[appName].connection[easyrtcid].field[fieldName].fieldValue); } else { return null; @@ -1681,11 +1723,11 @@ pub.app = function(appName, callback) { */ connectionObj.getFields = function(limitToIsShared, callback) { var fieldObj = {}; - if( e.app[appName].connection[easyrtcid] ) { + if( e.app[appName].connection[easyrtcid] ) { for (var fieldName in e.app[appName].connection[easyrtcid].field) { - if( !e.app[appName].connection[easyrtcid] ) { - // do nothing - } + if( !e.app[appName].connection[easyrtcid] ) { + // do nothing + } else if (!limitToIsShared || e.app[appName].connection[easyrtcid].field[fieldName].fieldOption.isShared) { fieldObj[fieldName] = { fieldName: fieldName, @@ -1693,7 +1735,7 @@ pub.app = function(appName, callback) { }; } } - } + } callback(null, fieldObj); }; @@ -1705,10 +1747,10 @@ pub.app = function(appName, callback) { * @param {function(?Error, Array.)} callback Callback with error and array of room names. */ connectionObj.getRoomNames = function(callback) { - var roomNames = []; - if( e.app[appName].connection[easyrtcid] ) { + var roomNames = []; + if( e.app[appName].connection[easyrtcid] ) { roomNames = Object.keys(e.app[appName].connection[easyrtcid].room); - } + } callback(null, roomNames); }; @@ -1754,12 +1796,12 @@ pub.app = function(appName, callback) { * @return {String} The username associated with the connection. */ connectionObj.getUsername = function() { - if( e.app[appName].connection[easyrtcid] ) { + if( e.app[appName].connection[easyrtcid] ) { return e.app[appName].connection[easyrtcid].username; - } - else { - return "ZOMBIE"; - } + } + else { + return "ZOMBIE"; + } }; @@ -1806,8 +1848,8 @@ pub.app = function(appName, callback) { * @param {nextCallback} next A success callback of form next(err). */ connectionObj.setAuthenticated = function(isAuthenticated, next) { - if( !e.app[appName].connection[easyrtcid]) { - } + if( !e.app[appName].connection[easyrtcid]) { + } else if (isAuthenticated) { e.app[appName].connection[easyrtcid].isAuthenticated = true; } else { @@ -1825,9 +1867,9 @@ pub.app = function(appName, callback) { * @param {nextCallback} next A success callback of form next(err). */ connectionObj.setCredential = function(credential, next) { - if( e.app[appName].connection[easyrtcid]) { + if( e.app[appName].connection[easyrtcid]) { e.app[appName].connection[easyrtcid].credential = credential; - } + } next(null); }; @@ -1853,14 +1895,14 @@ pub.app = function(appName, callback) { return; } - if( e.app[appName].connection[easyrtcid] ) { + if( e.app[appName].connection[easyrtcid] ) { e.app[appName].connection[easyrtcid].field[fieldName] = { fieldName: fieldName, fieldValue: fieldValue, fieldOption: {isShared: ((_.isObject(fieldOption) && fieldOption.isShared) ? true : false)} }; - } + } next(null); }; @@ -1873,7 +1915,7 @@ pub.app = function(appName, callback) { * @param {nextCallback} next A success callback of form next(err). */ connectionObj.setPresence = function(presenceObj, next) { - if( e.app[appName].connection[easyrtcid]) { + if( e.app[appName].connection[easyrtcid]) { if (presenceObj.show !== undefined) { e.app[appName].connection[easyrtcid].presence.show = presenceObj.show; } @@ -1883,7 +1925,7 @@ pub.app = function(appName, callback) { if (presenceObj.type !== undefined) { e.app[appName].connection[easyrtcid].presence.type = presenceObj.type; } - } + } next(null); }; @@ -1896,9 +1938,9 @@ pub.app = function(appName, callback) { * @param {nextCallback} next A success callback of form next(err). */ connectionObj.setUsername = function(username, next) { - if( e.app[appName].connection[easyrtcid]) { + if( e.app[appName].connection[easyrtcid]) { e.app[appName].connection[easyrtcid].username = username; - } + } next(null); }; @@ -1992,10 +2034,10 @@ pub.app = function(appName, callback) { } var roomData = {}; - if( !e.app[appName].connection[easyrtcid] ) { - callback(null, roomData); - return; - } + if( !e.app[appName].connection[easyrtcid] ) { + callback(null, roomData); + return; + } for (var currentRoomName in e.app[appName].connection[easyrtcid].room) { if (e.app[appName].connection[easyrtcid].room.hasOwnProperty(currentRoomName)) { @@ -2081,10 +2123,10 @@ pub.app = function(appName, callback) { var roomDataDelta = {}; - if( !e.app[appName].connection[easyrtcid] ) { + if( !e.app[appName].connection[easyrtcid] ) { callback(null, roomDataDelta); - return; - } + return; + } // set the roomData's clientListDelta for each room the client is in for (var currentRoomName in e.app[appName].connection[easyrtcid].room) { if (e.app[appName].connection[easyrtcid].room.hasOwnProperty(currentRoomName)) { @@ -2225,12 +2267,12 @@ pub.app = function(appName, callback) { * @param {function(?Error, Object=)} callback Callback with error and object containing EasyRTC connection room object (same as calling room(roomName)) */ connectionObj.joinRoom = function(roomName, callback) { - if( !e.app[appName].connection[easyrtcid]) { - pub.util.logWarning("[" + appName + "][zombie=" + easyrtcid + + if( !e.app[appName].connection[easyrtcid]) { + pub.util.logWarning("[" + appName + "][zombie=" + easyrtcid + "] Can not enter room: '" + roomName + "'"); callback(new pub.util.ConnectionWarning("Can not enter room as zombie: '" + roomName + "'")); return; - } + } if (!roomName || !appObj.getOption("roomNameRegExp").test(roomName)) { pub.util.logWarning("[" + appName + "][" + easyrtcid + "] Can not enter room with improper name: '" + roomName + "'"); callback(new pub.util.ConnectionWarning("Can not enter room with improper name: '" + roomName + "'")); @@ -2296,12 +2338,12 @@ pub.app = function(appName, callback) { * @param {function(?Error, Object=)} callback Callback with error and object containing EasyRTC connection room object. */ connectionObj.room = function(roomName, callback) { - if( !e.app[appName].connection[easyrtcid] ) { + if( !e.app[appName].connection[easyrtcid] ) { pub.util.logWarning("Zombie attempt to request room name: '" + roomName + "'"); callback(new pub.util.ConnectionWarning("Attempt to request non-existent room name: '" + roomName + "'")); return; } - + if (_.isUndefined(e.app[appName].connection[easyrtcid].room[roomName])) { pub.util.logWarning("Attempt to request non-existent room name: '" + roomName + "'"); callback(new pub.util.ConnectionWarning("Attempt to request non-existent room name: '" + roomName + "'")); @@ -2478,11 +2520,11 @@ pub.app = function(appName, callback) { if (!_.isFunction(callback)) { callback = pub.util.nextToNowhere; } - if( !e.app[appName].connection[easyrtcid]) { + if( !e.app[appName].connection[easyrtcid]) { pub.util.logWarning("Zombie attempt to request room name: '" + roomName + "'"); callback(new pub.util.ApplicationWarning("Zombie attempt to request room name: '" + roomName + "'")); return; - } + } if (!appObj.isRoomSync(roomName)) { pub.util.logWarning("Attempt to request non-existent room name: '" + roomName + "'"); callback(new pub.util.ApplicationWarning("Attempt to request non-existent room name: '" + roomName + "'")); @@ -2524,10 +2566,10 @@ pub.app = function(appName, callback) { if (!_.isFunction(next)) { next = pub.util.nextToNowhere; } - if( e.app[appName].connection[easyrtcid]) { + if( e.app[appName].connection[easyrtcid]) { e.app[appName].connection[easyrtcid].room[roomName].apiField = pub.util.deepCopy(apiFieldObj); - } + } next(null); };