Seperated api module to submodules

This commit is contained in:
mrfry 2021-03-30 16:59:37 +02:00
parent 2a0926aa7e
commit c64c189ce3
11 changed files with 2072 additions and 1895 deletions
src/modules/api/submodules

View file

@ -0,0 +1,220 @@
import { v4 as uuidv4 } from 'uuid'
import logger from '../../../utils/logger'
import utils from '../../../utils/utils'
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
const adminUsersFile = 'data/admins.json'
function addComment(obj, path, comment) {
if (path.length === 0) {
obj.push(comment)
} else {
const i = path.pop()
if (!obj[i].subComments) {
obj[i].subComments = []
}
addComment(obj[i].subComments, path, comment)
}
}
function deleteComment(obj: any, path: Array<number>, userid: number): boolean {
if (path.length === 1) {
if (obj[path[0]].user === userid) {
obj.splice(path[0], 1)
return true
} else {
return false
}
} else {
const i = path.pop()
deleteComment(obj[i].subComments, path, userid)
return true
}
}
function addReaction(obj, path, { reaction, isDelete, uid }) {
if (path.length === 1) {
const index = path[0]
if (!obj[index].reacts) {
obj[index].reacts = {}
}
if (isDelete) {
if (obj[index].reacts[reaction]) {
obj[index].reacts[reaction] = obj[index].reacts[reaction].filter(
(uid) => {
return uid !== uid
}
)
if (obj[index].reacts[reaction].length === 0) {
delete obj[index].reacts[reaction]
}
}
} else {
if (!obj[index].reacts[reaction]) {
obj[index].reacts[reaction] = [uid]
} else {
if (!obj[index].reacts[reaction].includes(uid)) {
obj[index].reacts[reaction].push(uid)
}
}
}
} else {
const i = path.pop()
addReaction(obj[i].subComments, path, {
reaction: reaction,
isDelete: isDelete,
uid: uid,
})
}
}
function setup(data: SubmoduleData): void {
const { app, /* userDB, url, */ publicdirs /*, moduleSpecificData */ } = data
const publicDir = publicdirs[0]
const newsFile = publicDir + 'news.json'
app.post('/react', (req: Request, res) => {
logger.LogReq(req)
const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile)
const { newsKey, path, reaction, isDelete } = req.body
if (!newsKey || !reaction) {
res.json({ status: 'fail', msg: 'no newskey or reaction' })
return
}
if (!path || path.length === 0) {
if (news[newsKey]) {
if (isDelete) {
if (news[newsKey].reacts) {
news[newsKey].reacts[reaction] = news[newsKey].reacts[
reaction
].filter((uid) => {
return uid !== user.id
})
if (news[newsKey].reacts[reaction].length === 0) {
delete news[newsKey].reacts[reaction]
}
}
} else {
if (!news[newsKey].reacts) {
news[newsKey].reacts = { [reaction]: [user.id] }
} else {
if (Array.isArray(news[newsKey].reacts[reaction])) {
if (!news[newsKey].reacts[reaction].includes(user.id)) {
news[newsKey].reacts[reaction].push(user.id)
}
} else {
news[newsKey].reacts[reaction] = [user.id]
}
}
}
}
} else {
addReaction(news[newsKey].comments, path, {
reaction: reaction,
isDelete: isDelete,
uid: user.id,
})
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
app.post('/comment', (req: Request, res) => {
logger.LogReq(req)
const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile)
const admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { type, path, newsKey } = req.body
if (!type || !path || !newsKey) {
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
return
}
if (type === 'add') {
const { content } = req.body
const comment = {
date: utils.GetDateString(),
user: user.id,
content: content,
admin: admins.includes(user.id),
}
if (!news[newsKey].comments) {
news[newsKey].comments = []
}
addComment(news[newsKey].comments, path, comment)
} else if (type === 'delete') {
if (news[newsKey].comments) {
const success = deleteComment(news[newsKey].comments, path, user.id)
if (!success) {
res.json({
status: 'fail',
msg: 'you cant delete other users comments',
news: news,
})
return
}
}
} else {
res.json({ status: 'fail', msg: 'no such type', news: news })
return
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
app.post('/rmPost', (req: Request, res) => {
logger.LogReq(req)
const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile)
const { newsKey } = req.body
if (news[newsKey].user === user.id) {
delete news[newsKey]
} else {
res.json({
status: 'fail',
msg: 'u cant delete other users posts!',
news: news,
})
return
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
app.post('/addPost', (req: Request, res) => {
logger.LogReq(req)
const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile)
const admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { title, content } = req.body
news[uuidv4()] = {
date: utils.GetDateString(),
user: user.id,
title: title,
content: content,
admin: admins.includes(user.id),
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
}
export default {
setup: setup,
}