Adding entries to forum

This commit is contained in:
mrfry 2021-03-05 19:40:22 +01:00
parent 65d4ef3223
commit 661d0dd3e4

View file

@ -73,6 +73,7 @@ const rootRedirectToFile = 'data/apiRootRedirectTo'
const recievedQuestionFile = 'stats/recievedQuestions' const recievedQuestionFile = 'stats/recievedQuestions'
const registeredScriptsFile = 'stats/registeredScripts.json' const registeredScriptsFile = 'stats/registeredScripts.json'
const savedQuestionsFileName = 'savedQuestions.json' const savedQuestionsFileName = 'savedQuestions.json'
const adminUsersFile = 'data/admins.json'
const oldMotdFile = 'publicDirs/qminingPublic/oldMotd' const oldMotdFile = 'publicDirs/qminingPublic/oldMotd'
// other constants // other constants
@ -702,6 +703,42 @@ function GetApp(): ModuleType {
logger.Log('New feedback file', logger.GetColor('bluebg')) logger.Log('New feedback file', logger.GetColor('bluebg'))
}) })
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!' })
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.ReadJSON(adminUsersFile)
const { title, content } = req.body
news[uuidv4()] = {
title: title,
content: content,
date: new Date().toLocaleString(),
user: user.id,
admin: admins.includes(user),
}
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', news: news })
})
app.post('/postfeedback', function(req: Request, res: any) { app.post('/postfeedback', function(req: Request, res: any) {
logger.LogReq(req) logger.LogReq(req)
if (req.body.fromLogin) { if (req.body.fromLogin) {
@ -1322,10 +1359,6 @@ function GetApp(): ModuleType {
if (news[newsKey].reacts[react].length === 0) { if (news[newsKey].reacts[react].length === 0) {
delete news[newsKey].reacts[react] delete news[newsKey].reacts[react]
} }
logger.Log(
`User #${user.id} removed reaction '${react}' from '${newsKey}' `,
logger.GetColor('blue')
)
} }
} else { } else {
if (!news[newsKey].reacts) { if (!news[newsKey].reacts) {
@ -1339,14 +1372,13 @@ function GetApp(): ModuleType {
news[newsKey].reacts[react] = [user.id] news[newsKey].reacts[react] = [user.id]
} }
} }
logger.Log(
`User #${user.id} reacted on '${newsKey}' with '${react}'`,
logger.GetColor('blue')
)
} }
} }
utils.WriteFile(JSON.stringify(news, null, 2), newsFile) utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ msg: 'done', news: news })
return
} }
res.json({ msg: 'done' }) res.json({ msg: 'done' })
@ -1414,18 +1446,20 @@ function GetApp(): ModuleType {
const user: User = req.session.user const user: User = req.session.user
const news: any = utils.ReadJSON(newsFile) const news: any = utils.ReadJSON(newsFile)
const admins: any = utils.ReadJSON(adminUsersFile)
const { type, path, newsKey } = req.body const { type, path, newsKey } = req.body
if (!type || !path || !newsKey) { if (!type || !path || !newsKey) {
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' }) res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
return
} }
if (type === 'add') { if (type === 'add') {
const { text } = req.body const { content } = req.body
const comment = { const comment = {
date: new Date().toLocaleString(), date: new Date().toLocaleString(),
user: user.id, user: user.id,
text: text, content: content,
admin: admins.includes(user),
} }
if (!news[newsKey].comments) { if (!news[newsKey].comments) {
news[newsKey].comments = [] news[newsKey].comments = []
@ -1446,7 +1480,7 @@ function GetApp(): ModuleType {
res.json({ status: 'fail', msg: 'no such type' }) res.json({ status: 'fail', msg: 'no such type' })
} }
utils.WriteFile(JSON.stringify(news, null, 2), newsFile) utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
res.json({ status: 'ok', newNews: news }) res.json({ status: 'ok', news: news })
}) })
app.post('/registerscript', function(req: Request, res) { app.post('/registerscript', function(req: Request, res) {