Add scr.savefile.js to renderer with getter methods

This commit is contained in:
chylex 2016-10-25 23:25:10 +02:00
parent 1bd72406b6
commit 88258d102a

View File

@ -0,0 +1,45 @@
var SAVEFILE = function(parsedObj){
this.meta = parsedObj.meta;
this.meta.users = this.meta.users || {};
this.meta.userindex = this.meta.userindex || [];
this.meta.servers = this.meta.servers || [];
this.meta.channels = this.meta.channels || {};
this.data = parsedObj.data;
};
SAVEFILE.isValid = function(parsedObj){
return parsedObj && typeof parsedObj.meta === "object" && typeof parsedObj.data === "object";
};
SAVEFILE.prototype.getServer = function(index){
return this.meta.servers[index] || {
name: "<unknown>",
type: "ERROR"
};
};
SAVEFILE.prototype.getChannelList = function(){
return Object.keys(this.meta.channels).map(key => ({
id: key,
name: this.meta.channels[key].name,
server: this.getServer(this.meta.channels[key].server),
msgcount: Object.keys(this.data[key]).length || 0
}));
};
SAVEFILE.prototype.getUser = function(index){
return this.meta.users[this.meta.userindex[index]] || { name: "<unknown>" };
};
SAVEFILE.prototype.getChannelMessageObject = function(channel){
return this.data[channel] || [];
};
SAVEFILE.prototype.getMessage = function(message){
return {
user: this.getUser(message.u),
timestamp: message.t,
contents: message.m
};
};