12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- var channel = function(){
- this.name = null;
- this.TS = null;
- this.topic = { 'setter': null, 'TS': null, 'text': null };
- this.modes = {}; // indexed by mode name
- this.statusModes = {}; // indexed by UID
- this.listModes = {}; // indexed by mode name
- this.users = [];
- this.metadata = {};
-
- this.events = {};
-
- this.setEvents = function(events){
- this.events = events;
- };
- this.setTS = function(TS){
- this.TS = TS;
- };
-
- this.changeModes = function(modes){
- for(var mode in modes){
- if(mode == 'status'){
- for(var i=0; i<modes[mode].length; i++){
- var statusMode = modes[mode][i];
- this.statusModes[statusMode.user.uid][statusMode.name] = statusMode.status;
- console.log((statusMode.status?'Setting':'Unsetting')+' '+statusMode.name+' for '+statusMode.user.name+' on '+this.name);
- this.events.doEvent('channelStatusMode', this, statusMode);
- }
- } else if(mode == 'list'){
- for(var i=0; i<modes[mode].length; i++){
- var listMode = modes[mode][i];
- if(!(listMode.name in this.listModes)){
- this.listModes[listMode.name] = [];
- }
-
- if(listMode.status){ // add
- if(this.listModes[listMode.name].indexOf(listMode.value) < 0){
- this.listModes[listMode.name].push(listMode.value);
- }
- } else { // remove
- for(var j=this.listModes[listMode.name].length-1; j >= 0; j--){
- if(this.listModes[listMode.name][j] == listMode.value){
- this.listModes[listMode.name].splice(j, 1);
- }
- }
- }
- this.events.doEvent('channelListMode', this, listMode);
- }
- } else {
- this.modes[mode] = modes[mode];
- this.events.doEvent('channelModes', this);
- }
- }
- };
-
- this.joinUser = function(user){
- if(this.users.indexOf(user) >= 0) return;
- this.users.push(user);
- this.statusModes[user.uid] = {};
- user.joinChannel(this);
- this.events.doEvent('channelJoin', this, user);
- };
-
- this.removeUser = function(user){
- if(user.uid in this.statusModes){
- delete this.statusModes[user.uid]; // remove status modes for this user
- }
- for(var i=0; i<this.users.length; i++){
- if(this.users[i] == user){
- user.leaveChannel(this);
- this.users.splice(i, 1);
- this.events.doEvent('channelLeave', this, user);
- break;
- }
- }
- };
-
- this.setMetadata = function(name, visibility, value){
- if(value){
- this.metadata[name] = { 'visibility': visibility, 'value': value };
- } else {
- if(name in this.metadata){
- delete this.metadata[name];
- }
- }
- this.events.doEvent('channelMetadata', this, name);
- };
-
- this.setTopic = function(topic){
- this.topic = topic;
- this.events.doEvent('channelTopic', this);
- };
- }
- module.exports = channel;
|