Solve nick collisions

This commit is contained in:
Mattias Erming 2014-03-14 03:00:01 +01:00
parent 0d880802a9
commit cc4b6cdfe5
2 changed files with 56 additions and 24 deletions

View file

@ -110,9 +110,28 @@ $(function() {
}); });
} }
var timer;
var resizing = false;
$(window).on("resize", function() {
// This will prevent the scroll event from triggering
// while resizing the window.
resizing = true;
clearTimeout(timer);
timer = setTimeout(function() {
resizing = false;
}, 100);
if (sticky) {
self.scrollToBottom();
}
});
var sticky = false; var sticky = false;
self.on("scroll", function() { self.on("scroll", function() {
sticky = self.isScrollAtBottom(); if (!resizing) {
sticky = self.isScrollAtBottom();
}
}); });
self.trigger("scroll"); self.trigger("scroll");
self.on("append", function() { self.on("append", function() {

View file

@ -8,7 +8,7 @@ var models = require("./models.js");
module.exports = Server; module.exports = Server;
function Server() { function Server() {
this.sockets = false; this.sockets = false;
this.networks = new models.NetworkCollection; this.networks = new models.NetworkCollection;
} }
@ -179,7 +179,7 @@ function handleInput(input) {
} }
function handleEvent(argv) { function handleEvent(argv) {
var network = this; var network = this;
var channels = network.get("channels"); var channels = network.get("channels");
var event = argv.commandType != "error" ? argv.command var event = argv.commandType != "error" ? argv.command
@ -188,9 +188,10 @@ function handleEvent(argv) {
switch (event) { switch (event) {
case "ERROR": case "ERROR":
var args = argv.args;
channels.first().get("messages").add( channels.first().get("messages").add(
new models.Message({ new models.Message({
text: argv.args.slice(2).join(" "), text: args[args.length - 1],
type: "error" type: "error"
}) })
); );
@ -234,29 +235,32 @@ function handleEvent(argv) {
break; break;
case "JOIN": case "JOIN":
if (argv.nick == network.get("nick")) { var channel = channels.findWhere({name: argv.args[0]});
channels.add( if (typeof channel === "undefined") {
channel = channels.add(
new models.Channel({ new models.Channel({
name: argv.args[0] name: argv.args[0]
}) })
); );
} else { }
var channel = channels.findWhere({name: argv.args[0]});
var users = channel.get("users"); var users = channel.get("users");
var messages = channel.get("messages");
if (argv.nick != network.get("nick")) {
users.add( users.add(
new models.User({ new models.User({
name: argv.nick name: argv.nick
}) })
); );
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "has joined the channel.",
type: "join"
})
);
} }
messages.add(
new models.Message({
user: argv.nick,
text: "has joined the channel.",
type: "join"
})
);
break; break;
case "PART": case "PART":
@ -286,9 +290,6 @@ function handleEvent(argv) {
user: argv.nick, user: argv.nick,
text: "changed name to " + argv.args[0] text: "changed name to " + argv.args[0]
}); });
if (network.get("nick") == argv.nick) {
network.set("nick", argv.nick);
}
channels.each(function(channel) { channels.each(function(channel) {
var user = channel.get("users").findWhere({name: argv.nick}); var user = channel.get("users").findWhere({name: argv.nick});
if (typeof user !== "undefined") { if (typeof user !== "undefined") {
@ -296,10 +297,13 @@ function handleEvent(argv) {
channel.get("messages").add(message); channel.get("messages").add(message);
} }
}); });
if (argv.nick == network.get("nick")) {
network.set("nick", argv.args[0]);
}
break; break;
case "TOPIC": case "TOPIC":
var channel = channels.findWhere({name: argv.args[0]}); var channel = channels.findWhere({name: argv.args[0]});
var messages = channel.get("messages"); var messages = channel.get("messages");
messages.add( messages.add(
new models.Message({ new models.Message({
@ -325,10 +329,17 @@ function handleEvent(argv) {
break; break;
case "rpl_namreply": case "rpl_namreply":
var names = argv.args[3].split(' '); var names = argv.args[3].split(' ');
var channel = network.get("channels").findWhere({name: argv.args[2]}); var channel = network.get("channels").findWhere({name: argv.args[2]});
var users = channel.get("users"); if (typeof channel === "undefined") {
channel = channels.add(
new models.Channel({
name: argv.args[2]
})
);
}
var users = channel.get("users");
if (names[0] == network.get("nick")) { if (names[0] == network.get("nick")) {
users.reset(); users.reset();
} }
@ -349,6 +360,8 @@ function handleEvent(argv) {
); );
break; break;
case "001": // `registered`
network.set("nick", argv.args[0]);
case "rpl_motdstart": case "rpl_motdstart":
case "rpl_endofmotd": case "rpl_endofmotd":
case "rpl_motd": case "rpl_motd":