2021-04-12 23:29:02 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const { promisify } = require('util');
|
|
|
|
const createKeccakHash = require('keccak');
|
|
|
|
const ColorEncoder = require('./ColorEncoder');
|
2021-05-09 10:52:41 +02:00
|
|
|
const UserModel = require('./UserModel');
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const level = require('level');
|
|
|
|
const { db } = require('./UserModel');
|
2021-05-09 22:34:22 +02:00
|
|
|
const Logger = require('./Logger');
|
|
|
|
|
|
|
|
var logger = new Logger("Database");
|
2021-05-09 10:52:41 +02:00
|
|
|
|
|
|
|
mongoose.connect(process.env.MONGO_URL, {
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true
|
|
|
|
}, err => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2021-05-09 22:34:22 +02:00
|
|
|
logger.log("Connected");
|
2021-05-09 10:52:41 +02:00
|
|
|
});
|
2021-04-12 23:29:02 +02:00
|
|
|
|
|
|
|
class Database {
|
|
|
|
static userdb;
|
2021-05-09 10:52:41 +02:00
|
|
|
static roomdb;
|
2021-04-12 23:29:02 +02:00
|
|
|
|
|
|
|
static async load() {
|
2021-05-09 10:52:41 +02:00
|
|
|
this.userdb = mongoose.connection;
|
|
|
|
this.roomdb = level('src/db/rooms.db');
|
|
|
|
// const writeFile = promisify(fs.writeFile);
|
|
|
|
// const readdir = promisify(fs.readdir);
|
|
|
|
|
|
|
|
// let files = await readdir("src/db/");
|
|
|
|
// if (!files.includes("users.json")) {
|
|
|
|
// await writeFile('src/db/users.json', JSON.stringify(this.default_db, null, 2))
|
|
|
|
// this.userdb = new Map(Object.entries(require("./db/users.json")));
|
|
|
|
// } else {
|
|
|
|
// this.userdb = new Map(Object.entries(require("./db/users.json")));
|
|
|
|
// }
|
2021-04-12 23:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static async getUserData(cl, server) {
|
2021-05-09 10:52:41 +02:00
|
|
|
if (!this.userdb) {
|
2021-04-12 23:29:02 +02:00
|
|
|
await this.load();
|
|
|
|
}
|
2021-05-09 10:52:41 +02:00
|
|
|
|
2021-04-12 23:29:02 +02:00
|
|
|
let _id = createKeccakHash('keccak256').update((cl.server._id_Private_Key + cl.ip)).digest('hex').substr(0, 24);
|
2021-05-09 10:52:41 +02:00
|
|
|
let user = await UserModel.findOne({ _id: _id }).exec();
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
user = this.createUser(_id);
|
|
|
|
}
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2021-05-09 10:52:41 +02:00
|
|
|
return user;
|
|
|
|
}
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2021-05-09 10:52:41 +02:00
|
|
|
static async createUser(_id) {
|
|
|
|
let user = new UserModel({
|
|
|
|
name: "Anonymous",
|
|
|
|
_id: _id,
|
|
|
|
color: "#" + ColorEncoder.intToRGB(ColorEncoder.hashCode(_id)),
|
|
|
|
flags: {
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2021-05-09 10:52:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
user.save();
|
2021-04-12 23:29:02 +02:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2021-05-09 10:52:41 +02:00
|
|
|
static async updateUser(_id, data) {
|
2021-05-10 03:13:15 +02:00
|
|
|
let user = await UserModel.findOne({_id: _id}).exec();
|
2021-05-10 04:02:04 +02:00
|
|
|
|
|
|
|
user.name = data.name;
|
|
|
|
user._id = data._id;
|
|
|
|
user.flags = data.flags;
|
|
|
|
user.color = data.color;
|
2021-05-10 03:13:15 +02:00
|
|
|
|
|
|
|
await user.save();
|
2021-05-09 10:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static async wipe() {
|
|
|
|
await UserModel.find({}, (err, docs) => {
|
|
|
|
docs.forEach(doc => {
|
|
|
|
doc.remove();
|
|
|
|
});
|
|
|
|
}).exec();
|
2021-04-12 23:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static strMapToObj(strMap) {
|
|
|
|
return [...strMap.entries()].reduce((obj, [key, value]) => (obj[key] = value, obj), {});
|
|
|
|
}
|
2021-05-09 10:52:41 +02:00
|
|
|
|
|
|
|
static getRoomSettings(_id, cb) {
|
|
|
|
let key = "room~"+_id;
|
|
|
|
|
|
|
|
roomSettings
|
|
|
|
|
|
|
|
this.roomdb.get(key, (err, value) => {
|
|
|
|
if (err || !value || value == "") {
|
|
|
|
cb(err, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cb(undefined, value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static setRoomSettings(_id, roomSettings, chat) {
|
|
|
|
let roomData = new RoomDataModel(roomSettings, chat);
|
|
|
|
let key = "room~"+_id;
|
|
|
|
this.roomdb.put(key, JSON.stringify(roomData));
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRoomSettings(_id, cb) {
|
|
|
|
let key = "room~"+_id;
|
|
|
|
this.roomdb.get(key, (err, value) => {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let settings = JSON.parse(value);
|
|
|
|
cb(err, settings);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static deleteRoomSettings(_id) {
|
|
|
|
this.roomdb.del("room~"+_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RoomDataModel {
|
|
|
|
constructor (settings, chat) {
|
|
|
|
this.settings = settings;
|
|
|
|
this.chat = chat;
|
|
|
|
}
|
2021-04-12 23:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Database;
|