Shuo/lib/plugins/join.js

38 lines
805 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
2014-06-23 17:28:36 +00:00
module.exports = function(slate, network) {
var client = this;
slate.on("join", function(data) {
var chan = _.find(network.channels, {name: data.channel});
if (!chan) {
2014-06-19 15:28:53 +00:00
chan = new Chan({
2014-06-23 17:28:36 +00:00
name: data.channel
2014-06-19 15:28:53 +00:00
});
network.addChan(chan);
2014-06-23 17:28:36 +00:00
client.emit("join", {
2014-06-19 15:28:53 +00:00
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();
2014-06-23 17:28:36 +00:00
client.emit("users", {
2014-06-21 12:17:59 +00:00
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);
2014-06-23 17:28:36 +00:00
client.emit("msg", {
2014-06-19 15:28:53 +00:00
id: chan.id,
msg: msg,
});
});
};