Shuo/lib/plugins/join.js

38 lines
872 B
JavaScript
Raw Normal View History

2014-06-19 15:28:53 +00:00
var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
2014-06-21 12:17:59 +00:00
var User = require("../models/user");
2014-06-19 15:28:53 +00:00
module.exports = function(client, sockets) {
var network = this;
client.on("join", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.channel,
});
network.addChan(chan);
sockets.in("chat").emit("join", {
id: network.id,
chan: chan,
});
}
2014-06-21 12:17:59 +00:00
var users = chan.users;
users.push(new User({name: data.nick}));
chan.sortUsers();
sockets.in("chat").emit("users", {
id: chan.id,
users: users,
});
2014-06-19 15:28:53 +00:00
var msg = new Msg({
from: data.nick,
type: "join",
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
id: chan.id,
msg: msg,
});
});
};