mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Forum updates to allow multiple forums, and storing entries in seperate files
This commit is contained in:
parent
406caabefd
commit
7010d5e8ba
1 changed files with 221 additions and 86 deletions
|
@ -5,6 +5,18 @@ import utils from '../../../utils/utils'
|
||||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||||
|
|
||||||
const adminUsersFile = 'data/admins.json'
|
const adminUsersFile = 'data/admins.json'
|
||||||
|
const forumContentsFileName = '.contents.json'
|
||||||
|
|
||||||
|
function countComments(comments) {
|
||||||
|
return comments.reduce((acc, comment) => {
|
||||||
|
if (comment.subComments) {
|
||||||
|
acc += countComments(comment.subComments) + 1
|
||||||
|
} else {
|
||||||
|
acc += 1
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
function addComment(obj, path, comment) {
|
function addComment(obj, path, comment) {
|
||||||
if (path.length === 0) {
|
if (path.length === 0) {
|
||||||
|
@ -69,77 +81,171 @@ function addReaction(obj, path, { reaction, isDelete, uid }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getForumData(
|
||||||
|
forumName: string,
|
||||||
|
forumDir: string
|
||||||
|
): { forumPath: string; contentFilePath: string; content: any } {
|
||||||
|
const safeForumName = forumName.replace(/\./g, '').replace(/\/+/g, '')
|
||||||
|
const forumPath = forumDir + '/' + safeForumName
|
||||||
|
const contentFilePath = forumPath + '/' + forumContentsFileName
|
||||||
|
|
||||||
|
if (!utils.FileExists(forumPath)) {
|
||||||
|
utils.CreatePath(forumPath, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!utils.FileExists(contentFilePath)) {
|
||||||
|
utils.WriteFile('[]', contentFilePath)
|
||||||
|
}
|
||||||
|
const contents = utils.ReadJSON(contentFilePath)
|
||||||
|
return {
|
||||||
|
forumPath: forumPath,
|
||||||
|
contentFilePath: contentFilePath,
|
||||||
|
contents: contents,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPostData(postKey, contents, forumPath) {
|
||||||
|
const safePostKey = postKey.replace(/\./g, '').replace(/\/+/g, '')
|
||||||
|
const postPath = forumPath + '/' + safePostKey
|
||||||
|
if (!contents[postKey] || !utils.FileExists(postPath)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return { postData: utils.ReadJSON(postPath), postPath: postPath }
|
||||||
|
}
|
||||||
|
|
||||||
function setup(data: SubmoduleData): void {
|
function setup(data: SubmoduleData): void {
|
||||||
const { app, /* userDB, url, */ publicdirs /*, moduleSpecificData */ } = data
|
const { app, /* userDB, url, */ publicdirs /*, moduleSpecificData */ } = data
|
||||||
|
|
||||||
const publicDir = publicdirs[0]
|
const publicDir = publicdirs[0]
|
||||||
|
|
||||||
const newsFile = publicDir + 'news.json'
|
const forumDir = publicDir + 'forum'
|
||||||
|
|
||||||
app.post('/react', (req: Request, res) => {
|
if (!utils.FileExists(forumDir)) {
|
||||||
|
utils.CreatePath(forumDir, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get('/forumEntries', (req: Request, res) => {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
|
|
||||||
const user: User = req.session.user
|
const forumName: any = req.query.forumName
|
||||||
const news: any = utils.ReadJSON(newsFile)
|
if (!forumName) {
|
||||||
|
res.json({
|
||||||
const { newsKey, path, reaction, isDelete } = req.body
|
success: false,
|
||||||
if (!newsKey || !reaction) {
|
msg: `Forum name is not specified!`,
|
||||||
res.json({ status: 'fail', msg: 'no newskey or reaction' })
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!path || path.length === 0) {
|
|
||||||
if (news[newsKey]) {
|
const { forumPath, contents } = getForumData(forumName, forumDir)
|
||||||
if (isDelete) {
|
|
||||||
if (news[newsKey].reacts) {
|
res.json({
|
||||||
news[newsKey].reacts[reaction] = news[newsKey].reacts[
|
entriesPath: forumPath.replace(publicDir, ''),
|
||||||
reaction
|
contents: contents,
|
||||||
].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,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.post('/addPost', (req: Request, res) => {
|
||||||
|
logger.LogReq(req)
|
||||||
|
|
||||||
|
const { title, content } = req.body
|
||||||
|
const forumName: any = req.query.forumName
|
||||||
|
if (!forumName) {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg: `Forum name is not specified!`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const { contents } = getForumData(forumName, forumDir)
|
||||||
|
const user: User = req.session.user
|
||||||
|
const admins: any = utils.FileExists(adminUsersFile)
|
||||||
|
? utils.ReadJSON(adminUsersFile)
|
||||||
|
: []
|
||||||
|
|
||||||
|
const newPostKey = uuidv4()
|
||||||
|
const postData = {
|
||||||
|
date: utils.GetDateString(),
|
||||||
|
user: user.id,
|
||||||
|
title: title,
|
||||||
|
admin: admins.includes(user.id),
|
||||||
|
commentCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
contents[newPostKey] = postData
|
||||||
res.json({ status: 'ok', news: news })
|
utils.WriteFile(
|
||||||
|
JSON.stringify({ ...postData, content: content }),
|
||||||
|
newPostKey + '.json'
|
||||||
|
)
|
||||||
|
|
||||||
|
res.json({ success: true, newEntry: { ...postData, content: content } })
|
||||||
|
})
|
||||||
|
|
||||||
|
app.post('/rmPost', (req: Request, res) => {
|
||||||
|
logger.LogReq(req)
|
||||||
|
const { postKey } = req.body
|
||||||
|
const forumName: any = req.query.forumName
|
||||||
|
if (!forumName) {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg: `Forum name is not specified!`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const { forumPath, contentFilePath, contents } = getForumData(
|
||||||
|
forumName,
|
||||||
|
forumDir
|
||||||
|
)
|
||||||
|
const user: User = req.session.user
|
||||||
|
|
||||||
|
if (contents[postKey] && contents[postKey].user === user.id) {
|
||||||
|
utils.deleteFile(forumPath + '/' + postKey + '.json')
|
||||||
|
delete contents[postKey]
|
||||||
|
utils.WriteFile(JSON.stringify(contents), contentFilePath)
|
||||||
|
|
||||||
|
res.json({ success: true, deletedId: postKey })
|
||||||
|
} else {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg:
|
||||||
|
'Entry does not exists, or you are not authorized to delete this post',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/comment', (req: Request, res) => {
|
app.post('/comment', (req: Request, res) => {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
|
|
||||||
|
const forumName: any = req.query.forumName
|
||||||
|
if (!forumName) {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg: `Forum name is not specified!`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const { forumPath, contentFilePath, contents } = getForumData(
|
||||||
|
forumName,
|
||||||
|
forumDir
|
||||||
|
)
|
||||||
const user: User = req.session.user
|
const user: User = req.session.user
|
||||||
const news: any = utils.ReadJSON(newsFile)
|
|
||||||
const admins: any = utils.FileExists(adminUsersFile)
|
const admins: any = utils.FileExists(adminUsersFile)
|
||||||
? utils.ReadJSON(adminUsersFile)
|
? utils.ReadJSON(adminUsersFile)
|
||||||
: []
|
: []
|
||||||
const { type, path, newsKey } = req.body
|
const { type, path, postKey } = req.body
|
||||||
if (!type || !path || !newsKey) {
|
if (!postKey || !type || !path) {
|
||||||
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
|
res.json({ success: false, msg: 'type or path or postKey is undefined' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { postPath, postData } = getPostData(postKey, contents, forumPath)
|
||||||
|
if (!postData) {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg: `Post entry '${postKey}' from forum '${forumName}' does not exits!`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (type === 'add') {
|
if (type === 'add') {
|
||||||
const { content } = req.body
|
const { content } = req.body
|
||||||
const comment = {
|
const comment = {
|
||||||
|
@ -148,70 +254,99 @@ function setup(data: SubmoduleData): void {
|
||||||
content: content,
|
content: content,
|
||||||
admin: admins.includes(user.id),
|
admin: admins.includes(user.id),
|
||||||
}
|
}
|
||||||
if (!news[newsKey].comments) {
|
if (!postData.comments) {
|
||||||
news[newsKey].comments = []
|
postData.comments = []
|
||||||
}
|
}
|
||||||
addComment(news[newsKey].comments, path, comment)
|
addComment(postData.comments, path, comment)
|
||||||
} else if (type === 'delete') {
|
} else if (type === 'delete') {
|
||||||
if (news[newsKey].comments) {
|
if (postData.comments) {
|
||||||
const success = deleteComment(news[newsKey].comments, path, user.id)
|
const success = deleteComment(postData.comments, path, user.id)
|
||||||
if (!success) {
|
if (!success) {
|
||||||
res.json({
|
res.json({
|
||||||
status: 'fail',
|
success: false,
|
||||||
msg: 'you cant delete other users comments',
|
msg: 'you cant delete other users comments',
|
||||||
news: news,
|
postData: postData,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.json({ status: 'fail', msg: 'no such type', news: news })
|
res.json({ success: false, msg: 'no such type' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
|
||||||
res.json({ status: 'ok', news: news })
|
contents[postKey].commentCount = countComments(postData.comments)
|
||||||
|
|
||||||
|
utils.WriteFile(JSON.stringify(postData, null, 2), postPath)
|
||||||
|
utils.WriteFile(JSON.stringify(contents, null, 2), contentFilePath)
|
||||||
|
res.json({ success: true, postData: postData })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/rmPost', (req: Request, res) => {
|
app.post('/react', (req: Request, res) => {
|
||||||
logger.LogReq(req)
|
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) {
|
const forumName: any = req.query.forumName
|
||||||
delete news[newsKey]
|
if (!forumName) {
|
||||||
} else {
|
|
||||||
res.json({
|
res.json({
|
||||||
status: 'fail',
|
success: false,
|
||||||
msg: 'u cant delete other users posts!',
|
msg: `Forum name is not specified!`,
|
||||||
news: news,
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const { forumPath, contents } = getForumData(forumName, forumDir)
|
||||||
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 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()] = {
|
const { postKey, path, reaction, isDelete } = req.body
|
||||||
date: utils.GetDateString(),
|
if (!postKey || !reaction) {
|
||||||
user: user.id,
|
res.json({ success: false, msg: 'no postkey or reaction' })
|
||||||
title: title,
|
return
|
||||||
content: content,
|
|
||||||
admin: admins.includes(user.id),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
const { postPath, postData } = getPostData(postKey, contents, forumPath)
|
||||||
res.json({ status: 'ok', news: news })
|
if (!postData) {
|
||||||
|
res.json({
|
||||||
|
success: false,
|
||||||
|
msg: `Post entry '${postKey}' from forum '${forumName}' does not exits!`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!path || path.length === 0) {
|
||||||
|
if (postData) {
|
||||||
|
if (isDelete) {
|
||||||
|
if (postData.reacts) {
|
||||||
|
postData.reacts[reaction] = postData.reacts[reaction].filter(
|
||||||
|
(uid) => {
|
||||||
|
return uid !== user.id
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if (postData.reacts[reaction].length === 0) {
|
||||||
|
delete postData.reacts[reaction]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!postData.reacts) {
|
||||||
|
postData.reacts = { [reaction]: [user.id] }
|
||||||
|
} else {
|
||||||
|
if (Array.isArray(postData.reacts[reaction])) {
|
||||||
|
if (!postData.reacts[reaction].includes(user.id)) {
|
||||||
|
postData.reacts[reaction].push(user.id)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
postData.reacts[reaction] = [user.id]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
addReaction(postData.comments, path, {
|
||||||
|
reaction: reaction,
|
||||||
|
isDelete: isDelete,
|
||||||
|
uid: user.id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.WriteFile(JSON.stringify(postData, null, 2), postPath)
|
||||||
|
res.json({ success: true, postData: postData })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue