channel.js 2.1 KB

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