fipamo/brain/data/Settings.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

import * as DataEvent from '../../src/com/events/DataEvent';
import fs from 'fs-extra';
const settings = require('../../site/settings.json');
const folks = require('../../site/folks.json');
const config = require('../../site/tags.json');
const _ = require('lodash');
export default class Settings {
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// methods
//--------------------------
sync(req) {
return new Promise((resolve, reject) => {
let payload = req.body;
let user = req.session.user;
let found = _.find(folks, { id: user.id });
let needToUpdate = false;
let response = [];
if (found) {
let index = found.id - 1;
if (
folks[index].handle != payload.member.handle ||
folks[index].email != payload.member.email
) {
folks[index].handle = payload.member.handle;
folks[index].email = payload.member.email;
fs.writeJson('site/folks.json', folks);
} else {
//no need to save
}
} else {
let response = {
type: DataEvent.REQUEST_LAME,
message: "You're not logged in, champ"
};
reject(response);
}
if (!_.isEqual(settings.global, payload.global)) {
let bg = payload.global.background;
payload.global.background = bg.substr(21, bg.length);
settings.global = payload.global;
needToUpdate = true;
} else {
//no need to save
}
if (!_.isEqual(settings.email, payload.email)) {
settings.email = payload.email;
needToUpdate = true;
} else {
//no need to save
}
if (needToUpdate) {
fs.writeJson('site/settings.json', settings)
.then(() => {
response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(() => {
//console.error(err);
});
} else {
//no need to update
}
});
}
saveTags(tags) {
return new Promise((resolve, reject) => {
if (!_.isEqual(config.tags, tags)) {
config.tags = tags;
fs.writeJson('site/tags.json', config)
.then(() => {
let response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(err => {
reject(err);
});
} else {
let response = {
type: DataEvent.SETTINGS_NOT_UPDATED,
message: 'Settings Already Saved'
};
resolve(response);
}
});
}
updatePageIndex() {
fs.readJSON('site/settings.json').then(settings => {
settings.library_stats.current_index = ++settings.library_stats.current_index;
//settings.library_stats.total_pages = ++settings.library_stats.total_pages;
fs.writeJSON('site/settings.json', settings);
});
}
//--------------------------
// event handlers
//--------------------------
}