channel.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var channel = function(){
  2. this.name = null;
  3. this.TS = null;
  4. this.topic = { 'setter': null, 'TS': null, 'text': 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.metadata = {};
  10. this.events = {};
  11. this.setEvents = function(events){
  12. this.events = events;
  13. };
  14. this.setTS = function(TS){
  15. this.TS = TS;
  16. };
  17. this.changeModes = function(modes){
  18. for(var mode in modes){
  19. if(mode == 'status'){
  20. for(var i=0; i<modes[mode].length; i++){
  21. var statusMode = modes[mode][i];
  22. this.statusModes[statusMode.user.uid][statusMode.name] = statusMode.status;
  23. console.log((statusMode.status?'Setting':'Unsetting')+' '+statusMode.name+' for '+statusMode.user.name+' on '+this.name);
  24. this.events.doEvent('channelStatusMode', this, statusMode);
  25. }
  26. } else if(mode == 'list'){
  27. for(var i=0; i<modes[mode].length; i++){
  28. var listMode = modes[mode][i];
  29. if(!(listMode.name in this.listModes)){
  30. this.listModes[listMode.name] = [];
  31. }
  32. if(listMode.status){ // add
  33. if(this.listModes[listMode.name].indexOf(listMode.value) < 0){
  34. this.listModes[listMode.name].push(listMode.value);
  35. }
  36. } else { // remove
  37. for(var j=this.listModes[listMode.name].length-1; j >= 0; j--){
  38. if(this.listModes[listMode.name][j] == listMode.value){
  39. this.listModes[listMode.name].splice(j, 1);
  40. }
  41. }
  42. }
  43. this.events.doEvent('channelListMode', this, listMode);
  44. }
  45. } else {
  46. this.modes[mode] = modes[mode];
  47. this.events.doEvent('channelModes', this);
  48. }
  49. }
  50. };
  51. this.joinUser = function(user){
  52. if(this.users.indexOf(user) >= 0) return;
  53. this.users.push(user);
  54. this.statusModes[user.uid] = {};
  55. this.events.doEvent('channelJoin', this, user);
  56. };
  57. this.removeUser = function(user){
  58. if(user.uid in this.statusModes){
  59. delete this.statusModes[user.uid]; // remove status modes for this user
  60. }
  61. for(var i=0; i<this.users.length; i++){
  62. if(this.users[i] == user){
  63. this.users.splice(i, 1);
  64. this.events.doEvent('channelLeave', this, user);
  65. break;
  66. }
  67. }
  68. };
  69. this.setMetadata = function(name, visibility, value){
  70. if(value){
  71. this.metadata[name] = { 'visibility': visibility, 'value': value };
  72. } else {
  73. if(name in this.metadata){
  74. delete this.metadata[name];
  75. }
  76. }
  77. this.events.doEvent('channelMetadata', this, name);
  78. };
  79. this.setTopic = function(topic){
  80. this.topic = topic;
  81. this.events.doEvent('channelTopic', this);
  82. };
  83. }
  84. module.exports = channel;