Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Blorum/blorum
Browse files Browse the repository at this point in the history
  • Loading branch information
SorenEricMent committed Oct 21, 2022
2 parents db0fc5e + 7ad1574 commit 1eec8bc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 100 deletions.
3 changes: 1 addition & 2 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ INSERT INTO `config`(`flag`, `value`) VALUES ("sendmail_config","{}");
INSERT INTO `config`(`flag`, `value`) VALUES ("smtp_config","{}");
INSERT INTO `config`(`flag`, `value`) VALUES ("ses_config","{}");

INSERT INTO `config`(`flag`, `value`) VALUES ("diff_timeout","0.5");
INSERT INTO `config`(`flag`, `value`) VALUES ("diff_editcost","12");
INSERT INTO `config`(`flag`, `value`) VALUES ("lazy_process","0");

INSERT INTO `config`(`flag`, `value`) VALUES ("removed_res_keep","true");
INSERT INTO `config`(`flag`, `value`) VALUES ("removed_res_keep_time","604800");
Expand Down
95 changes: 66 additions & 29 deletions modules/iapi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
mergeJSON, getFinalPermission, removeElementFromArray
} from "./utils.mjs";

import { default as DMP } from "diff-match-patch";

import { v4 as uuidv4 } from 'uuid';

import stringify from "quick-stable-stringify";
Expand All @@ -21,13 +19,34 @@ class IAPI {
this.log = log;
this.salt = salt;
this.rp = redisPrefix;
//For util functions in IAPI, redis prefix will not be automatically added.
//Redis prefix needed to be added manually in caller function.

this.log("log", "IAPI", "IAPI instance created.");
}
timestamp(){
return new Date().getTime();
}
changeSiteConfig(actions){
//Update IAPI object and database in the same time.
/* Actions = a list of {
"name": the keyname of the config,
"to": the desired value
} */
return new Promise((resolve, reject) => {
let arrLen = actions.length;
let mysqlWritePromisePool = [];
for(var i=0;i<arrLen;i++){
let actionName = actions[i].name;
let actionTo = actions[i].to;
siteConfig[actionName] = actionTo;
mysqlWritePromisePool.push(new Promise((resolve, reject) => {
//todo: write database
}));
}
mysqlWritePromisePool.allSettled((results) => {
//todo: check fail&success, return
})
});
}

getRedisKeyIfExists(redisKey){
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -175,7 +194,8 @@ class IAPI {
resolve(null);
}else{
let roles = results[0].roles.split(",");
for(let i = 0; i < roles.length; i++){
let arrLen = roles.length;
for(let i = 0; i < arrLen; i++){
roles[i] = roles[i].replace(/{{/g, "");
roles[i] = roles[i].replace(/}}/g, "");
}
Expand Down Expand Up @@ -531,12 +551,14 @@ class IAPI {
});
}else{
if(uid != null){
for(var i = 0; i < uid.length; i++){
let uidArrLen = uid.length;
for(var i = 0; i < uidArrLen; i++){
constraint.uid.add("uid = " + uid[i]);
}
}
if(level != null){
for(var i = 0; i < level.length; i++){
let levelArrLen = level.length;
for(var i = 0; i < levelArrLen; i++){
constraint.level.add("level = " + level[i]);
}
}
Expand Down Expand Up @@ -571,46 +593,54 @@ class IAPI {

}
alterArticle(){
//diff-match-patch
/*
var dmp = new diff_match_patch();

var text1
var text2
dmp.Diff_Timeout
dmp.Diff_EditCost
}
deleteArticle(id){

}

createPost(uid, title, content, forum, attach_to, category){

}

editPost(postID, actions){
//Action types: title, user, content, forum, attach, category
}

dmp.diff_cleanupSemantic(d);
deletePost(postID){

dmp.diff_cleanupEfficiency(d);
*/
}
deleteArticle(){

createComment(targetType, targetID, content, uid, reply_to){

}

createPost(){
editComment(targetType, targetID, actions){

}

editPost(){}
deleteComment(targetType, targetID){

deletePost(){}
}

createComment(){}
createCategory(name, description){

editComment(){}
}

deleteComment(){}
editCategory(name, actions){
//Action types: description, name
return new Promise((resolve, reject) => {
//todo. 1st: find category ID with name.
});
}

createTag(){}
deleteCategory(name){

deleteTag(){}
}

createCategory(){}
depriveCategory(name){

deleteCategory(){}
}

createRole(roleType, name, permission){
//todo
Expand Down Expand Up @@ -675,6 +705,13 @@ var dmp = new diff_match_patch();
)
});
}
depriveTag(name){

}

createReact(targetType, targetID, react){

}
}

export { IAPI };
79 changes: 34 additions & 45 deletions modules/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import { promisifiedMysqlConnect, promisifiedRedisConnect } from "./utils.mjs";

import stringify from "quick-stable-stringify";

function MysqlIntegrityCheck(mysqlConnection) {
return new Promise((resolve, reject) => {
resolve(); //TODO
});
}

function initializeBlorumServer() {
const __dirname = fileURLToPath(import.meta.url);
let bootConfigPath = join(__dirname, '..', '..', 'config.json');
Expand Down Expand Up @@ -68,47 +62,42 @@ function initializeBlorumServer() {
Promise.all([redisPromise, mysqlPromise]).then((values) => {
let mysqlConn = values[1];
let redisConn = values[0];
MysqlIntegrityCheck(mysqlConn).then(() => {
mysqlConn.query("SELECT * FROM config;", (err,results) => {
if (err) {
log("error", "INIT/db/mysql", "Failed to query config table.");
reject(err);
} else {
log("log", "INIT/db/mysql", "Site config loaded.");
let siteConfig = {};
for(const element of results){
siteConfig[element.flag] = element.value;
}
mysqlConn.query("SELECT * FROM roles;", (err,results) => {
if (err) {
log("error", "INIT/db/mysql", "Failed to query roles table.");
reject(err);
}else{
let redisKey = bootConfig.database.redis.prefix + ":roles:";
for(let element of results){
try {
let keyName = redisKey + element.name;
delete element.name;
redisConn.set(keyName, stringify(element));
} catch (error) {
log("error", "INIT/db/redis", "Failed to set role in redis.");
reject(error);
}
resolve({
"log": log,
"mysql": mysqlConn,
"redis": redisConn,
"siteConfig": siteConfig,
"bootConfig": bootConfig
});
mysqlConn.query("SELECT * FROM config;", (err,results) => {
if (err) {
log("error", "INIT/db/mysql", "Failed to query config table.");
reject(err);
} else {
log("log", "INIT/db/mysql", "Site config loaded.");
let siteConfig = {};
for(const element of results){
siteConfig[element.flag] = element.value;
}
mysqlConn.query("SELECT * FROM roles;", (err,results) => {
if (err) {
log("error", "INIT/db/mysql", "Failed to query roles table.");
reject(err);
}else{
let redisKey = bootConfig.database.redis.prefix + ":roles:";
for(let element of results){
try {
let keyName = redisKey + element.name;
delete element.name;
redisConn.set(keyName, stringify(element));
} catch (error) {
log("error", "INIT/db/redis", "Failed to set role in redis.");
reject(error);
}
resolve({
"log": log,
"mysql": mysqlConn,
"redis": redisConn,
"siteConfig": siteConfig,
"bootConfig": bootConfig
});
}
});
}
});
}).catch(function (err) {
log("error", "INIT/db/mysql", "MySQL Database integrity check failed. ");
reject(err);
}
});
}
});
}).catch(function (err) {
reject(err);
Expand Down
7 changes: 0 additions & 7 deletions modules/permission.mjs

This file was deleted.

27 changes: 24 additions & 3 deletions modules/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { default as RCM } from "./rate_control.mjs";
import { default as SCM } from "./session_check.mjs";
import { default as STM } from "./statistic.mjs";
import { default as CSM } from "./cache.mjs";
import { stringify } from "querystring";
import stringify from "quick-stable-stringify";

function rejectForLoginStatusDecorator(func){
return function(req, res){
Expand Down Expand Up @@ -295,8 +295,13 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
}
});

blorumRouter.post('/user/permissions', function(req, res) {
blorumRouter.put('/user/permissions', function(req, res) {
//Todo: set user permission API
if(req.isUserSessionValid){
//add permission check here!
let b = req.body;
let actionList = b.actions;
}
});

blorumRouter.post('/user/logout', function (req, res) {
Expand Down Expand Up @@ -345,6 +350,14 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
});

blorumRouter.post('/user/invite', function (req, res) {
if(req.isUserSessionValid){
let b = req.body;
let inviteeEmail = b.email;
let msgLeft = b.msg;
//todo
}else{
res.sendStatus(401);
}
});

blorumRouter.post('/user/remove', function (req, res) {
Expand Down Expand Up @@ -395,6 +408,8 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
});

blorumRouter.put('/react', function (req, res) {
let b = req.body;
let targetType = b.type; // 0 = Article, 1 = Post, 2 = Comment, 3 = Note
});

blorumRouter.put('/forum', function (req, res) {
Expand All @@ -408,6 +423,8 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal


blorumRouter.delete('/article', function (req, res) {
let b = req.body;
let id = b.id;
});

blorumRouter.delete('/post', function (req, res) {
Expand All @@ -428,11 +445,15 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
blorumRouter.delete('/category', function (req, res) {
});

blorumRouter.delete('/tag', function (req, res) {
blorumRouter.put('/site_config', function (req,res){
if(isUserSessionValid){

}
});


blorumRouter.post('/heartbeat', function (req, res) {

});

blorumRouter.get('*', function(req, res){
Expand Down
7 changes: 6 additions & 1 deletion modules/statistic.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
function StatisticsMiddleware(log, redisConnection, mysqlConnection, siteConfig, iapi, getReqInfo){

this.log = log;
this.mysql = mysqlConnection;
//todo: scheduled connection!!!
this.middleware = (req, res, next) => {
switch(req.path){

}
next();
};
}
Expand Down
5 changes: 4 additions & 1 deletion modules/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,13 @@ function removeElementFromArray(arr, element){
}
}

function filterAction(obj){
//Remove all dumplicate actions in an actionList
}
export {
version, innerVersion, outputLogs, outputLogsColored, blake3Hash, generateNewToken,
isModuleAvailable, promisifiedMysqlConnect, promisifiedRedisConnect,
strASCIIOnly, strStrictLegal, basicPasswordRequirement, isValidEmail, isAllString,
objHasAllProperties, strNotOnlyNumber, mergeJSON, mergeArray, cookieParser, pureArray, filterSpace, getPermissionSum, getLPermissionSum, getFinalPermission,
removeElementFromArray, InfFixProxy
removeElementFromArray, InfFixProxy, filterAction
};
Loading

0 comments on commit 1eec8bc

Please sign in to comment.