thelounge/src/models/chan.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

"use strict";
2014-09-13 23:29:45 +02:00
var _ = require("lodash");
var Helper = require("../helper");
const storage = require("../plugins/storage");
2014-09-13 23:29:45 +02:00
module.exports = Chan;
Chan.Type = {
CHANNEL: "channel",
LOBBY: "lobby",
QUERY: "query",
SPECIAL: "special",
2014-09-13 23:29:45 +02:00
};
var id = 0;
function Chan(attr) {
_.defaults(this, attr, {
2014-09-13 23:29:45 +02:00
id: id++,
messages: [],
name: "",
2017-04-01 10:33:17 +02:00
key: "",
2014-10-10 22:05:25 +02:00
topic: "",
2014-09-13 23:29:45 +02:00
type: Chan.Type.CHANNEL,
2016-05-13 12:23:05 +02:00
firstUnread: 0,
2014-09-21 18:48:01 +02:00
unread: 0,
highlight: false,
2014-09-13 23:29:45 +02:00
users: []
});
2014-09-13 23:29:45 +02:00
}
Chan.prototype.pushMessage = function(client, msg, increasesUnread) {
var obj = {
chan: this.id,
msg: msg
};
// If this channel is open in any of the clients, do not increase unread counter
var isOpen = _.includes(client.attachedClients, this.id);
if ((increasesUnread || msg.highlight) && !isOpen) {
obj.unread = ++this.unread;
}
client.emit("msg", obj);
// 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) {
const deleted = this.messages.splice(0, this.messages.length - Helper.config.maxHistory);
if (Helper.config.prefetch && Helper.config.prefetchStorage) {
deleted.forEach((deletedMessage) => {
if (deletedMessage.preview && deletedMessage.preview.thumb) {
storage.dereference(deletedMessage.preview.thumb);
}
});
}
}
2016-05-13 12:23:05 +02:00
if (!msg.self && !isOpen) {
2016-05-13 12:23:05 +02:00
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((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.nick.toLowerCase() < b.nick.toLowerCase() ? -1 : 1;
}
return userModeSortPriority[a.mode] - userModeSortPriority[b.mode];
});
2014-09-13 23:29:45 +02:00
};
Chan.prototype.findMessage = function(msgId) {
return this.messages.find((message) => message.id === msgId);
};
Chan.prototype.findUser = function(nick) {
return _.find(this.users, {nick: nick});
};
2014-10-04 14:31:45 +02:00
Chan.prototype.getMode = function(name) {
var user = this.findUser(name);
2014-10-04 14:31:45 +02:00
if (user) {
return user.mode;
}
2016-10-09 10:54:44 +02:00
return "";
2014-10-04 14:31:45 +02:00
};
2014-09-13 23:29:45 +02:00
Chan.prototype.toJSON = function() {
var clone = _.clone(this);
2017-07-10 12:56:58 +02:00
clone.users = []; // Do not send user list, the client will explicitly request it when needed
2014-09-13 23:29:45 +02:00
clone.messages = clone.messages.slice(-100);
return clone;
};