2020-05-19 22:51:04 +02:00
|
|
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
|
|
|
import fs from 'fs-extra';
|
2020-05-23 21:40:01 +02:00
|
|
|
const settings = require('../../site/settings.json');
|
|
|
|
const folks = require('../../site/folks.json');
|
2020-05-22 06:18:32 +02:00
|
|
|
const config = require('../../site/tags.json');
|
2020-05-19 22:51:04 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
export default class Settings {
|
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {}
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
//--------------------------
|
2020-05-23 21:40:01 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-19 22:51:04 +02:00
|
|
|
saveTags(tags) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-21 03:14:19 +02:00
|
|
|
if (!_.isEqual(config.tags, tags)) {
|
|
|
|
config.tags = tags;
|
2020-05-22 06:18:32 +02:00
|
|
|
fs.writeJson('site/tags.json', config)
|
2020-05-19 22:51:04 +02:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-21 23:32:19 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:51:04 +02:00
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
}
|