From e822fea91a8e1bc835012dd22ccba884958eb4af Mon Sep 17 00:00:00 2001 From: SorenEricMent Date: Fri, 21 Oct 2022 15:14:58 +0800 Subject: [PATCH 1/2] Scaffolded IAPI,remove uncessary perm bundle & DMP, fix dep import --- default_perm.sql | 140 +++++++++++++++++++++++++++++------------ init.sql | 3 +- modules/iapi.mjs | 95 +++++++++++++++++++--------- modules/permission.mjs | 7 --- modules/router.mjs | 27 +++++++- modules/utils.mjs | 10 ++- package-lock.json | 11 ---- package.json | 1 - 8 files changed, 198 insertions(+), 96 deletions(-) delete mode 100644 modules/permission.mjs diff --git a/default_perm.sql b/default_perm.sql index 6b3297f1..5c0ea155 100644 --- a/default_perm.sql +++ b/default_perm.sql @@ -8,26 +8,55 @@ INSERT INTO `roles` (`name`, `type`, `with_rate_limit`, `permissions`, `rate_lim "max_session":10, "cookie_expire_after":13150000000 }' -,'{ - "login":-1, - "edit":{ - "post":-1, - "article":-1, - "comment":-1 - }, - "create":{ - "post":-1, - "react":-1, - "article":-1, - "comment":-1 - }, - "remove":{ - "post":-1, - "react":-1, - "article":-1, - "comment":-1 - } -}'), +,' "rate_limits": { + "login": -1, + "invite": -1, + "report": -1, + "edit": { + "post": { + "self": -1, + "tag": -1, + "category": -1, + "forum": -1 + }, + "article": { + "self": -1, + "tag": -1, + "category": -1 + }, + "comment": -1, + "note": -1, + "user": -1, + "category": -1, + "forum": -1, + }, + "create": { + "category": -1, + "post": -1, + "react": -1, + "article": -1, + "comment": -1, + "note": -1, + "forum": -1, + "report": -1, + "user": -1 + }, + "remove": { + "tag": -1, + "category": -1, + "post": -1, + "react": -1, + "article": -1, + "comment": -1, + "note": -1, + "forum": -1, + "report": -1, + "user": -1 + }, + "site": { + "change_config": -1 + } + }'), ('auditor', 1,1, ' { @@ -38,27 +67,56 @@ INSERT INTO `roles` (`name`, `type`, `with_rate_limit`, `permissions`, `rate_lim "cookie_expire_after":13150000000 }' , ' -{ - "edit":{ - "post":15, - "react":240, - "article":1, - "comment":360 - }, - "login":20, - "create":{ - "post":10, - "react":60, - "article":0, - "comment":60 - }, - "remove":{ - "post":15, - "react":240, - "article":1, - "comment":360 - } -}'), +"rate_limits": { + "login": 20, + "invite": 8, + "report": 512, + "edit": { + "post": { + "self": 60, + "tag": 60, + "category": 60, + "forum": 30 + }, + "article": { + "self": 0, + "tag": 0, + "category": 0 + }, + "comment": 20, + "note": 20, + "user": 0, + "category": 0, + "forum": 0, + }, + "create": { + "category": 10, + "post": 20, + "react": 60, + "article": -1, + "comment": -1, + "note": -1, + "forum": -1, + "report": -1, + "user": -1 + }, + "remove": { + "tag": -1, + "category": -1, + "post": -1, + "react": -1, + "article": -1, + "comment": -1, + "note": -1, + "forum": -1, + "report": -1, + "user": -1 + }, + "site": { + "change_config": -1 + } + } +'), ('forum_admin', 1,1, ' { diff --git a/init.sql b/init.sql index c222e6ea..ba0556eb 100644 --- a/init.sql +++ b/init.sql @@ -58,7 +58,6 @@ 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"); COMMIT; \ No newline at end of file diff --git a/modules/iapi.mjs b/modules/iapi.mjs index a130fb53..e1dec145 100644 --- a/modules/iapi.mjs +++ b/modules/iapi.mjs @@ -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"; @@ -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 { + //todo: write database + })); + } + mysqlWritePromisePool.allSettled((results) => { + //todo: check fail&success, return + }) + }); + } getRedisKeyIfExists(redisKey){ return new Promise((resolve, reject) => { @@ -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, ""); } @@ -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]); } } @@ -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 @@ -675,6 +705,13 @@ var dmp = new diff_match_patch(); ) }); } + depriveTag(name){ + + } + + createReact(targetType, targetID, react){ + + } } export { IAPI }; \ No newline at end of file diff --git a/modules/permission.mjs b/modules/permission.mjs deleted file mode 100644 index 5fc1be1f..00000000 --- a/modules/permission.mjs +++ /dev/null @@ -1,7 +0,0 @@ -//Builtin helper for permission assigning. - -const bundles = { - -} - -export {bundles} \ No newline at end of file diff --git a/modules/router.mjs b/modules/router.mjs index 51e12d19..01ab0b0c 100644 --- a/modules/router.mjs +++ b/modules/router.mjs @@ -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){ @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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){ diff --git a/modules/utils.mjs b/modules/utils.mjs index f9354858..9f90dc15 100644 --- a/modules/utils.mjs +++ b/modules/utils.mjs @@ -314,6 +314,7 @@ function getPermissionSum(arr) { "post": { "self": 0, "tag": 0, + "category": 0, "forum": 0 }, "article": { @@ -328,7 +329,6 @@ function getPermissionSum(arr) { "forum": 0, }, "create": { - "tag": 0, "category": 0, "post": 0, "react": 0, @@ -350,6 +350,9 @@ function getPermissionSum(arr) { "forum": 0, "report": 0, "user": 0 + }, + "site": { + "change_config": 0 } } }; @@ -406,10 +409,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 }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 72b06537..d7d8c5b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,6 @@ "blake3": "^2.1.7", "body-parser": "^1.20.0", "commonjs": "^0.0.1", - "diff-match-patch": "^1.0.5", "express": "^4.18.1", "express-fileupload": "^1.4.0", "inquirer": "^9.0.0", @@ -656,11 +655,6 @@ "npm": "1.2.8000 || >= 1.4.16" } }, - "node_modules/diff-match-patch": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.5.tgz", - "integrity": "sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==" - }, "node_modules/dir-glob": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", @@ -3708,11 +3702,6 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" }, - "diff-match-patch": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.5.tgz", - "integrity": "sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==" - }, "dir-glob": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", diff --git a/package.json b/package.json index f13c9074..ff42065e 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "blake3": "^2.1.7", "body-parser": "^1.20.0", "commonjs": "^0.0.1", - "diff-match-patch": "^1.0.5", "express": "^4.18.1", "express-fileupload": "^1.4.0", "inquirer": "^9.0.0", From 7ad15743233553b917b8115f2b6b247ba9747a64 Mon Sep 17 00:00:00 2001 From: SorenEricMent Date: Fri, 21 Oct 2022 15:31:01 +0800 Subject: [PATCH 2/2] remove mysqlIntChk, starting StatMiddleware --- modules/init.mjs | 79 +++++++++++++++++++------------------------ modules/statistic.mjs | 7 +++- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/modules/init.mjs b/modules/init.mjs index 5648bde2..9849744c 100644 --- a/modules/init.mjs +++ b/modules/init.mjs @@ -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'); @@ -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); diff --git a/modules/statistic.mjs b/modules/statistic.mjs index 36d4e8ff..1f79b91d 100644 --- a/modules/statistic.mjs +++ b/modules/statistic.mjs @@ -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(); }; }