Shuo/lib/models/chan.js

50 lines
943 B
JavaScript
Raw Normal View History

var _ = require("lodash");
2014-06-02 21:08:03 +00:00
var config = require("../../config") || {};
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-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) {
this.messages.push(msg);
};
Chan.prototype.addUser = function(user) {
this.users.push(user);
};
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() {
var clone = _.clone(this);
clone.count = clone.messages.length;
clone.messages = clone.messages.slice(-1 * (config.messages || 0));
return clone;
};