Updated the models

This commit is contained in:
Mattias Erming 2014-06-15 17:01:38 +02:00
parent 23b4bc74b9
commit 317ac63a6c
3 changed files with 48 additions and 36 deletions

View File

@ -14,6 +14,14 @@ function Chan(attr) {
}, attr));
};
Chan.prototype.addMsg = function(msg) {
this.messages.push(msg);
};
Chan.prototype.addUser = function(user) {
this.users.push(user);
};
Chan.prototype.sortUsers = function() {
this.users = _.sortBy(
this.users,

View File

@ -19,6 +19,10 @@ function Network(attr) {
);
};
Network.prototype.addChan = function(chan) {
this.channels.push(chan);
};
Network.prototype.toJSON = function() {
var clone = _.omit(this, [
"client",

View File

@ -158,7 +158,7 @@ function input(data) {
from: user,
text: text,
});
chan.messages.push(msg)
chan.addMsg(msg)
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -189,7 +189,7 @@ function input(data) {
from: user,
text: text,
});
chan.messages.push(msg)
chan.addMsg(msg)
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -210,7 +210,7 @@ function input(data) {
from: user,
text: text,
});
chan.messages.push(msg)
chan.addMsg(msg)
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -376,7 +376,7 @@ function fetch(socket, data) {
function event(e, data) {
var data = _.last(data);
var channels = this.channels;
var network = this;
switch (e) {
case "errors":
@ -396,12 +396,12 @@ function event(e, data) {
break;
case "join":
var chan = _.findWhere(channels, {name: data.channel});
var chan = _.findWhere(this.channels, {name: data.channel});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.channel,
});
channels.push(chan);
this.addChan(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
@ -418,7 +418,7 @@ function event(e, data) {
from: data.nick,
type: "join",
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -426,7 +426,7 @@ function event(e, data) {
break;
case "kick":
var chan = _.findWhere(channels, {name: data.channel});
var chan = _.findWhere(this.channels, {name: data.channel});
if (typeof chan === "undefined") {
break;
}
@ -444,7 +444,7 @@ function event(e, data) {
from: data.nick,
text: data.client,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -452,7 +452,7 @@ function event(e, data) {
break;
case "mode":
var chan = _.findWhere(channels, {name: data.target});
var chan = _.findWhere(this.channels, {name: data.target});
if (typeof chan !== "undefined") {
clearTimeout(this.timer);
this.timer = setTimeout((function() {
@ -467,7 +467,7 @@ function event(e, data) {
from: nick,
text: data.mode + " " + data.client,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -476,14 +476,14 @@ function event(e, data) {
break;
case "motd":
var chan = channels[0];
var chan = this.channels[0];
data.motd.forEach(function(m) {
var msg = new Msg({
type: "motd",
from: "-!-",
text: m,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -493,13 +493,13 @@ function event(e, data) {
case "message":
var target = data.to;
var chan = _.findWhere(channels, {name: target.charAt(0) == "#" ? target : data.from});
var chan = _.findWhere(this.channels, {name: target.charAt(0) == "#" ? target : data.from});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.from,
type: "query",
});
channels.push(chan);
this.addChan(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
@ -520,7 +520,7 @@ function event(e, data) {
from: data.from,
text: text,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -528,13 +528,13 @@ function event(e, data) {
break;
case "names":
var chan = _.findWhere(channels, {name: data.channel});
var chan = _.findWhere(this.channels, {name: data.channel});
if (typeof chan === "undefined") {
break;
}
chan.users = [];
_.each(data.names, function(n) {
chan.users.push(new User(n));
chan.addUser(new User(n));
});
chan.sortUsers();
sockets.emit("users", {
@ -545,18 +545,18 @@ function event(e, data) {
case "nick":
if (data["new"] == this.client.me) {
var chan = channels[0];
var chan = this.channels[0];
var msg = new Msg({
from: "-!-",
text: "You're now known as " + data["new"],
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
}
channels.forEach(function(chan) {
this.channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (!user) {
return;
@ -572,7 +572,7 @@ function event(e, data) {
from: data.nick,
text: data["new"],
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -581,7 +581,7 @@ function event(e, data) {
break;
case "notice":
var chan = channels[0];
var chan = this.channels[0];
var from = data.from || "-!-";
if (data.to == "*" || data.from.indexOf(".") !== -1) {
from = "-!-";
@ -591,7 +591,7 @@ function event(e, data) {
from: from,
text: data.message,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -599,7 +599,7 @@ function event(e, data) {
break;
case "part":
var chan = _.findWhere(channels, {name: data.channels[0]});
var chan = _.findWhere(this.channels, {name: data.channels[0]});
if (typeof chan === "undefined") {
break;
}
@ -618,7 +618,7 @@ function event(e, data) {
type: "part",
from: data.nick,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -627,7 +627,7 @@ function event(e, data) {
break;
case "quit":
channels.forEach(function(chan) {
this.channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (!user) {
return;
@ -641,7 +641,7 @@ function event(e, data) {
type: "quit",
from: data.nick,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -650,7 +650,7 @@ function event(e, data) {
break;
case "topic":
var chan = _.findWhere(channels, {name: data.channel});
var chan = _.findWhere(this.channels, {name: data.channel});
if (typeof chan === "undefined") {
break;
}
@ -660,7 +660,7 @@ function event(e, data) {
from: from,
text: data.topic,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -669,12 +669,12 @@ function event(e, data) {
case "welcome":
this.connected = true;
var chan = channels[0];
var chan = this.channels[0];
var msg = new Msg({
from: "-!-",
text: "You're now known as " + data,
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -685,13 +685,13 @@ function event(e, data) {
if (!data) {
break;
}
var chan = _.findWhere(channels, {name: data.nickname});
var chan = _.findWhere(this.channels, {name: data.nickname});
if (typeof chan === "undefined") {
chan = new Chan({
type: "query",
name: data.nickname,
});
channels.push(chan);
this.addChan(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
@ -714,7 +714,7 @@ function event(e, data) {
from: data.nickname,
text: key + " " + data[k],
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
@ -725,7 +725,7 @@ function event(e, data) {
from: data.nickname,
text: "End of /WHOIS list.",
});
chan.messages.push(msg);
chan.addMsg(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,