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)); }, attr));
}; };
Chan.prototype.addMsg = function(msg) {
this.messages.push(msg);
};
Chan.prototype.addUser = function(user) {
this.users.push(user);
};
Chan.prototype.sortUsers = function() { Chan.prototype.sortUsers = function() {
this.users = _.sortBy( this.users = _.sortBy(
this.users, this.users,

View file

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

View file

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