hardlounge/src/models/chan.js

59 lines
932 B
JavaScript
Raw Normal View History

2014-09-13 21:29:45 +00:00
var _ = require("lodash");
module.exports = Chan;
Chan.Type = {
CHANNEL: "channel",
LOBBY: "lobby",
QUERY: "query"
};
var id = 0;
function Chan(attr) {
_.merge(this, _.extend({
id: id++,
messages: [],
name: "",
2014-10-10 20:05:25 +00:00
topic: "",
2014-09-13 21:29:45 +00:00
type: Chan.Type.CHANNEL,
2014-09-21 16:48:01 +00:00
unread: 0,
2014-09-13 21:29:45 +00:00
users: []
}, attr));
}
Chan.prototype.sortUsers = function() {
this.users = _.sortBy(
this.users,
function(u) { return u.name.toLowerCase(); }
);
var modes = [
"~",
"&",
2014-09-13 21:29:45 +00:00
"@",
"%",
2014-09-13 21:29:45 +00:00
"+",
].reverse();
modes.forEach(function(mode) {
this.users = _.remove(
this.users,
2015-09-30 22:39:57 +00:00
function(u) { return u.mode === mode; }
2014-09-13 21:29:45 +00:00
).concat(this.users);
}, this);
};
2014-10-04 12:31:45 +00:00
Chan.prototype.getMode = function(name) {
var user = _.find(this.users, {name: name});
if (user) {
return user.mode;
} else {
return "";
}
};
2014-09-13 21:29:45 +00:00
Chan.prototype.toJSON = function() {
var clone = _.clone(this);
clone.messages = clone.messages.slice(-100);
return clone;
};