Shuo/lib/models/chan.js

84 lines
1.6 KiB
JavaScript
Raw Normal View History

var _ = require("lodash");
2014-06-02 21:08:03 +00:00
var config = require("../../config") || {};
2014-06-18 22:16:44 +00:00
var fs = require('fs');
var moment = require("moment");
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-15 20:36:55 +00:00
network: "",
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) {
2014-06-18 22:16:44 +00:00
this.messages.push(msg);
if (config.log != true || this.type == "lobby") {
return;
}
var dir = "logs/";
dir += this.network + "/";
if (!fs.existsSync(dir)) {
fs.mkdir(dir);
}
var date = moment().format("YYYY-MM-DD");
var file = dir + this.name + ".log";
var line = "[" + msg.time + "] ";
if (msg.type == "normal") {
// Format:
// [00:00] <Arnold> Put that cookie down.. Now!!
line += "<" + msg.from + "> " + msg.text;
} else {
// Format:
// [00:00] * Arnold quit
line += "* " + msg.from + " " + msg.type;
if (msg.text) {
line += " " + msg.text;
}
}
fs.appendFile(
file,
line + "\n"
);
2014-06-15 15:01:38 +00:00
};
Chan.prototype.addUser = function(user) {
2014-06-18 22:16:44 +00:00
this.users.push(user);
2014-06-15 15:01:38 +00:00
};
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;
2014-06-16 13:13:43 +00:00
clone.messages = clone.messages.slice(-100);
2014-06-02 21:08:03 +00:00
return clone;
};