2014-09-13 21:29:45 +00:00
|
|
|
var _ = require("lodash");
|
2016-04-19 10:20:18 +00:00
|
|
|
var Helper = require("../helper");
|
2014-09-13 21:29:45 +00:00
|
|
|
|
|
|
|
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,
|
2016-05-13 10:23:05 +00:00
|
|
|
firstUnread: 0,
|
2014-09-21 16:48:01 +00:00
|
|
|
unread: 0,
|
2016-03-13 16:05:05 +00:00
|
|
|
highlight: false,
|
2014-09-13 21:29:45 +00:00
|
|
|
users: []
|
|
|
|
}, attr));
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:20:18 +00:00
|
|
|
Chan.prototype.pushMessage = function(client, msg) {
|
2016-04-19 10:28:07 +00:00
|
|
|
client.emit("msg", {
|
|
|
|
chan: this.id,
|
|
|
|
msg: msg
|
|
|
|
});
|
|
|
|
|
|
|
|
// Never store messages in public mode as the session
|
|
|
|
// is completely destroyed when the page gets closed
|
2016-06-08 09:26:24 +00:00
|
|
|
if (Helper.config.public) {
|
2016-04-19 10:28:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:20:18 +00:00
|
|
|
this.messages.push(msg);
|
|
|
|
|
2016-06-08 09:26:24 +00:00
|
|
|
if (Helper.config.maxHistory >= 0 && this.messages.length > Helper.config.maxHistory) {
|
|
|
|
this.messages.splice(0, this.messages.length - Helper.config.maxHistory);
|
2016-04-19 10:20:18 +00:00
|
|
|
}
|
2016-05-13 10:23:05 +00:00
|
|
|
|
|
|
|
if (!msg.self && this.id !== client.activeChannel) {
|
|
|
|
if (!this.firstUnread) {
|
|
|
|
this.firstUnread = msg.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.highlight) {
|
|
|
|
this.highlight = true;
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 10:20:18 +00:00
|
|
|
};
|
|
|
|
|
2016-03-14 11:44:06 +00:00
|
|
|
Chan.prototype.sortUsers = function(irc) {
|
|
|
|
var userModeSortPriority = {};
|
|
|
|
irc.network.options.PREFIX.forEach(function(prefix, index) {
|
|
|
|
userModeSortPriority[prefix.symbol] = index;
|
|
|
|
});
|
|
|
|
|
|
|
|
userModeSortPriority[""] = 99; // No mode is lowest
|
|
|
|
|
|
|
|
this.users = this.users.sort(function(a, b) {
|
|
|
|
if (a.mode === b.mode) {
|
|
|
|
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return userModeSortPriority[a.mode] - userModeSortPriority[b.mode];
|
|
|
|
});
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|