Shuo/lib/models/chan.js

55 lines
1008 B
JavaScript
Raw Normal View History

var _ = require("lodash");
2014-06-20 13:57:11 +00:00
var log = require("../log");
2014-06-18 22:16:44 +00:00
var moment = require("moment");
module.exports = Chan;
function Chan(attr) {
_.merge(this, _.extend({
id: global.id = ++global.id || 1,
name: "",
2014-05-16 13:12:57 +00:00
type: "channel",
2014-06-15 20:36:55 +00:00
network: "",
2014-06-02 21:08:03 +00:00
count: 0,
messages: [],
users: [],
}, attr));
};
2014-04-29 13:01:30 +00:00
2014-06-15 15:01:38 +00:00
Chan.prototype.addMsg = function(msg) {
2014-06-18 22:16:44 +00:00
this.messages.push(msg);
2014-06-20 13:57:11 +00:00
if (this.type != "lobby") {
log(this, msg);
2014-06-18 22:16:44 +00:00
}
2014-06-15 15:01:38 +00:00
};
Chan.prototype.addUser = function(user) {
2014-06-18 22:16:44 +00:00
this.users.push(user);
2014-06-15 15:01:38 +00:00
};
2014-04-30 15:14:22 +00:00
Chan.prototype.sortUsers = function() {
this.users = _.sortBy(
this.users,
function(u) { return u.name.toLowerCase(); }
);
var modes = [
"~",
"%",
"@",
"+",
].reverse();
2014-04-30 15:14:22 +00:00
modes.forEach(function(mode) {
this.users = _.remove(
2014-04-29 13:01:30 +00:00
this.users,
2014-04-30 15:14:22 +00:00
function(u) { return u.mode == mode; }
).concat(this.users);
}, this);
2014-04-29 13:01:30 +00:00
};
2014-06-02 21:08:03 +00:00
Chan.prototype.toJSON = function() {
2014-06-20 13:57:11 +00:00
var clone = _.omit(this, ["network"]);
2014-06-02 21:08:03 +00:00
clone.count = clone.messages.length;
2014-06-16 13:13:43 +00:00
clone.messages = clone.messages.slice(-100);
2014-06-02 21:08:03 +00:00
return clone;
};