channel.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var channel = function(){
  2. this.name = null;
  3. this.TS = null;
  4. this.topic = null;
  5. this.modes = {}; // indexed by mode name
  6. this.statusModes = {}; // indexed by UID
  7. this.listModes = {}; // indexed by mode name
  8. this.users = [];
  9. this.setTS = function(TS){
  10. this.TS = TS;
  11. };
  12. this.changeModes = function(modes){
  13. for(var mode in modes){
  14. if(mode == 'status'){
  15. for(var i=0; i<modes[mode].length; i++){
  16. var statusMode = modes[mode][i];
  17. this.statusModes[statusMode.user.uid][statusMode.name] = statusMode.status;
  18. console.log((statusMode.status?'Setting':'Unsetting')+' '+statusMode.name+' for '+statusMode.user.name+' on '+this.name);
  19. }
  20. } else if(mode == 'list'){
  21. for(var i=0; i<modes[mode].length; i++){
  22. var listMode = modes[mode][i];
  23. if(!(listMode.name in this.listModes)){
  24. this.listModes[listMode.name] = [];
  25. }
  26. if(listMode.status){ // add
  27. if(this.listModes[listMode.name].indexOf(listMode.value) < 0){
  28. this.listModes[listMode.name].push(listMode.value);
  29. }
  30. } else { // remove
  31. for(var j=this.listModes[listMode.name].length-1; j >= 0; j--){
  32. if(this.listModes[listMode.name][j] == listMode.value){
  33. this.listModes[listMode.name].splice(j, 1);
  34. }
  35. }
  36. }
  37. }
  38. } else {
  39. this.modes[mode] = modes[mode];
  40. }
  41. }
  42. };
  43. this.joinUser = function(user){
  44. if(this.users.indexOf(user) >= 0) return;
  45. this.users.push(user);
  46. this.statusModes[user.uid] = {};
  47. };
  48. this.removeUser = function(user){
  49. if(user.uid in this.statusModes){
  50. delete this.statusModes[user.uid]; // remove status modes for this user
  51. }
  52. for(var i=0; i<this.users.length; i++){
  53. if(this.users[i] == user){
  54. this.users.splice(i, 1);
  55. break;
  56. }
  57. }
  58. };
  59. }
  60. module.exports = channel;