Skip to content

Commit

Permalink
#302 Added methods to obtain connected easyrtcids with a given username
Browse files Browse the repository at this point in the history
  • Loading branch information
dryswabbie committed Mar 5, 2017
1 parent 400fcd2 commit 0ed579f
Showing 1 changed file with 89 additions and 47 deletions.
136 changes: 89 additions & 47 deletions lib/easyrtc_public_obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.<string>)} 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.<string>} 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.
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -1681,19 +1723,19 @@ 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,
fieldValue: pub.util.deepCopy(e.app[appName].connection[easyrtcid].field[fieldName].fieldValue)
};
}
}
}
}
callback(null, fieldObj);
};

Expand All @@ -1705,10 +1747,10 @@ pub.app = function(appName, callback) {
* @param {function(?Error, Array.<string>)} 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);
};

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


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

Expand All @@ -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);
};

Expand All @@ -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;
}
Expand All @@ -1883,7 +1925,7 @@ pub.app = function(appName, callback) {
if (presenceObj.type !== undefined) {
e.app[appName].connection[easyrtcid].presence.type = presenceObj.type;
}
}
}
next(null);
};

Expand All @@ -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);
};

Expand Down Expand Up @@ -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)) {

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 + "'"));
Expand Down Expand Up @@ -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 + "'"));
Expand Down Expand Up @@ -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 + "'"));
Expand Down Expand Up @@ -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);
};

Expand Down

0 comments on commit 0ed579f

Please sign in to comment.