hardlounge/src/models/chan.js

91 lines
1.8 KiB
JavaScript
Raw Normal View History

"use strict";
2014-09-13 21:29:45 +00:00
var _ = require("lodash");
var Helper = require("../helper");
2014-09-13 21:29:45 +00:00
module.exports = Chan;
Chan.Type = {
CHANNEL: "channel",
LOBBY: "lobby",
QUERY: "query",
SPECIAL: "special",
2014-09-13 21:29:45 +00:00
};
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,
highlight: false,
2014-09-13 21:29:45 +00:00
users: []
}, attr));
}
Chan.prototype.pushMessage = function(client, msg) {
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
if (Helper.config.public) {
return;
}
this.messages.push(msg);
if (Helper.config.maxHistory >= 0 && this.messages.length > Helper.config.maxHistory) {
this.messages.splice(0, this.messages.length - Helper.config.maxHistory);
}
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;
}
}
};
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;
};