Registry & userdb
This commit is contained in:
parent
b0828f049b
commit
cdd07ee328
61
src/server/components/Registry.js
Normal file
61
src/server/components/Registry.js
Normal file
@ -0,0 +1,61 @@
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const { Util } = require('../../util');
|
||||
|
||||
class Registry {
|
||||
|
||||
constructor (server, options) {
|
||||
if (!options?.path) Util.fatal(new Error(`Missing path to endpoints directory`));
|
||||
this.endpoints = new Map();
|
||||
this.path = options.path;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
get print () {
|
||||
const out = [];
|
||||
this.endpoints.forEach(ep => {
|
||||
const spacer = ' '.repeat(7);
|
||||
let str = `${spacer}[${ep.methods.map(([ method ]) => method.toUpperCase()).join(', ') || 'NONE'}] ${ep.path}`;
|
||||
|
||||
for (const [ sub, method ] of ep.subpaths) str += `\n${spacer.repeat(2)} - ${method.toUpperCase()}: ${sub}`;
|
||||
out.push(str);
|
||||
});
|
||||
|
||||
if (!out.length) return `Registry empty`;
|
||||
return `Registry print:\n${out.join('\n')}`;
|
||||
}
|
||||
|
||||
async loadEndpoints () {
|
||||
|
||||
const { server, endpoints } = this;
|
||||
|
||||
return (async function read (pth) {
|
||||
|
||||
const dir = fs.readdirSync(pth, { withFileTypes: true });
|
||||
|
||||
for (let file of dir) {
|
||||
|
||||
if (file.isDirectory()) await read(path.join(pth, file.name));
|
||||
else {
|
||||
|
||||
file = path.join(pth, file.name);
|
||||
const Endpoint = require(file);
|
||||
if (!Endpoint) continue;
|
||||
|
||||
const endpoint = new Endpoint(server);
|
||||
endpoint.init();
|
||||
|
||||
endpoints.set(endpoint.name, endpoint);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}(this.path));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Registry;
|
83
src/server/components/UserDatabase.js
Normal file
83
src/server/components/UserDatabase.js
Normal file
@ -0,0 +1,83 @@
|
||||
const { ObjectId } = require("mongodb");
|
||||
const { AbstractUserDatabase } = require("../interfaces/");
|
||||
const { User } = require("../structures");
|
||||
|
||||
// MongoDB based user db
|
||||
class UserDatabase extends AbstractUserDatabase {
|
||||
|
||||
constructor (db, userColl = 'users') {
|
||||
super();
|
||||
this.db = db;
|
||||
this.collectionName = userColl;
|
||||
this.cache = new Map();
|
||||
|
||||
this.collection = null;
|
||||
|
||||
}
|
||||
|
||||
init () {
|
||||
this.collection = this.db.collection(this.collectionName);
|
||||
}
|
||||
|
||||
async fetchUser (id, force = false) {
|
||||
|
||||
if (!force && this.cache.has(id)) return this.cache.get(id);
|
||||
|
||||
const data = this.collection.findOne({ _id: ObjectId(id) });
|
||||
if (!data) return null;
|
||||
|
||||
const user = this._createUser(data);
|
||||
this.cache.set(id, user);
|
||||
|
||||
return user;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the user to whom the token belongs
|
||||
*
|
||||
* @param {string} token
|
||||
* @return {User}
|
||||
* @memberof UserDatabase
|
||||
*/
|
||||
async matchToken (token) {
|
||||
if (!token) throw new Error('Missing token');
|
||||
const data = await this.collection.findOne({ apiKey: token });
|
||||
if (!data) return null;
|
||||
|
||||
let user = this.cache.get(data._id);
|
||||
if (!user) {
|
||||
user = this._createUser(data);
|
||||
this.cache.set(user.id, user);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves or creates a user based on a discord profile
|
||||
*
|
||||
* @param {*} profile
|
||||
* @return {*}
|
||||
* @memberof UserDatabase
|
||||
*/
|
||||
async userFromDiscord (profile) {
|
||||
const data = await this.collection.findOne({ 'discord.id': profile.id });
|
||||
let user = this.cache.get(data._id);
|
||||
|
||||
if (!data) {
|
||||
user = this._createUser({ discord: profile });
|
||||
await user.save();
|
||||
this.cache.set(user.id, user);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
_createUser (data) {
|
||||
return new User(this, data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = UserDatabase;
|
4
src/server/components/index.js
Normal file
4
src/server/components/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
Registry: require('./Registry'),
|
||||
UserDatabase: require('./UserDatabase')
|
||||
};
|
Loading…
Reference in New Issue
Block a user