added / fixed some types

This commit is contained in:
mrfry
2022-03-14 19:35:42 +01:00
parent 5f12284bb8
commit bc5c293539
41 changed files with 4378 additions and 8304 deletions
+301 -206
View File
@@ -4,10 +4,40 @@ import logger from '../../../utils/logger'
import utils from '../../../utils/utils'
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
interface Comment {
date: string
user: number
content: string
admin: boolean
subComments?: Comment[]
reacts?: {
[key: string]: number[]
}
}
interface ForumEntry {
date: string
user: number
title: string
content: string
admin: boolean
comments: Comment[]
reacts: {
[key: string]: number[]
}
}
interface ForumContents {
user: number
title: string
admin: boolean
commentCount: number
}
const adminUsersFile = 'data/admins.json'
const forumContentsFileName = '.contents.json'
function countComments(comments) {
function countComments(comments: Comment[]) {
return comments.reduce((acc, comment) => {
if (comment.subComments) {
acc += countComments(comment.subComments) + 1
@@ -18,7 +48,7 @@ function countComments(comments) {
}, 0)
}
function addComment(obj, path, comment) {
function addComment(obj: Comment[], path: number[], comment: Comment) {
if (path.length === 0) {
obj.push(comment)
} else {
@@ -30,7 +60,11 @@ function addComment(obj, path, comment) {
}
}
function deleteComment(obj: any, path: Array<number>, userid: number): boolean {
function deleteComment(
obj: Comment[],
path: number[],
userid: number
): boolean {
if (path.length === 1) {
if (obj[path[0]].user === userid) {
obj.splice(path[0], 1)
@@ -45,7 +79,15 @@ function deleteComment(obj: any, path: Array<number>, userid: number): boolean {
}
}
function addReaction(obj, path, { reaction, isDelete, uid }) {
function addReaction(
obj: Comment[],
path: number[],
{
reaction,
isDelete,
uid,
}: { reaction: string; isDelete: boolean; uid: number }
) {
if (path.length === 1) {
const index = path[0]
if (!obj[index].reacts) {
@@ -54,8 +96,8 @@ function addReaction(obj, path, { reaction, isDelete, uid }) {
if (isDelete) {
if (obj[index].reacts[reaction]) {
obj[index].reacts[reaction] = obj[index].reacts[reaction].filter(
(uid) => {
return uid !== uid
(currUid: number) => {
return uid !== currUid
}
)
if (obj[index].reacts[reaction].length === 0) {
@@ -84,7 +126,7 @@ function addReaction(obj, path, { reaction, isDelete, uid }) {
function getForumData(
forumName: string,
forumDir: string
): { forumPath: string; contentFilePath: string; contents: any } {
): { forumPath: string; contentFilePath: string; contents: ForumContents[] } {
const safeForumName = forumName.replace(/\./g, '').replace(/\/+/g, '')
const forumPath = forumDir + '/' + safeForumName
const contentFilePath = forumPath + '/' + forumContentsFileName
@@ -96,7 +138,7 @@ function getForumData(
if (!utils.FileExists(contentFilePath)) {
utils.WriteFile('{}', contentFilePath)
}
const contents = utils.ReadJSON(contentFilePath)
const contents: ForumContents[] = utils.ReadJSON(contentFilePath)
return {
forumPath: forumPath,
contentFilePath: contentFilePath,
@@ -104,11 +146,18 @@ function getForumData(
}
}
function getPostData(postKey, contents, forumPath) {
function getPostData(
postKey: string,
contents: ForumContents[],
forumPath: string
): {
postData: ForumEntry
postPath: string
} {
const safePostKey = postKey.replace(/\./g, '').replace(/\/+/g, '')
const postPath = forumPath + '/' + safePostKey
if (!contents[postKey] || !utils.FileExists(postPath)) {
return
return null
}
return { postData: utils.ReadJSON(postPath), postPath: postPath }
}
@@ -167,6 +216,7 @@ function setup(data: SubmoduleData): void {
if (passed) {
i++
}
return false
})
res.json({
@@ -175,218 +225,263 @@ function setup(data: SubmoduleData): void {
})
})
app.post('/addPost', (req: Request, res) => {
logger.LogReq(req)
app.post(
'/addPost',
(
req: Request<{
forumName: string
content: string
title: string
}>,
res
) => {
logger.LogReq(req)
const { title, content } = req.body
const forumName: any = req.body.forumName
if (!forumName) {
res.json({
success: false,
msg: `Forum name is not specified!`,
})
return
}
const { forumPath, contents, contentFilePath } = getForumData(
forumName,
forumDir
)
const user: User = req.session.user
const admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { title, content } = req.body
const forumName = req.body.forumName
if (!forumName) {
res.json({
success: false,
msg: `Forum name is not specified!`,
})
return
}
const { forumPath, contents, contentFilePath } = 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,
}
contents[newPostKey] = postData
utils.WriteFile(JSON.stringify(contents, null, 2), contentFilePath)
utils.WriteFile(
JSON.stringify({ ...postData, content: content }),
forumPath + '/' + newPostKey
)
res.json({
success: true,
newPostKey: newPostKey,
newEntry: { ...postData, content: content },
})
})
app.post('/rmPost', (req: Request, res) => {
logger.LogReq(req)
const { postKey } = req.body
const forumName: any = req.body.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)
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) => {
logger.LogReq(req)
const forumName: any = req.body.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 admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { type, path, postKey } = req.body
if (!postKey || !type || !path) {
res.json({ success: false, msg: 'type or path or postKey is undefined' })
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') {
const { content } = req.body
const comment = {
const newPostKey = uuidv4()
const postData = {
date: utils.GetDateString(),
user: user.id,
content: content,
title: title,
admin: admins.includes(user.id),
commentCount: 0,
}
if (!postData.comments) {
postData.comments = []
contents[newPostKey] = postData
utils.WriteFile(JSON.stringify(contents, null, 2), contentFilePath)
utils.WriteFile(
JSON.stringify({ ...postData, content: content }),
forumPath + '/' + newPostKey
)
res.json({
success: true,
newPostKey: newPostKey,
newEntry: { ...postData, content: content },
})
}
)
app.post(
'/rmPost',
(
req: Request<{
postKey: string
forumName: string
}>,
res
) => {
logger.LogReq(req)
const { postKey } = req.body
const forumName: any = req.body.forumName
if (!forumName) {
res.json({
success: false,
msg: `Forum name is not specified!`,
})
return
}
addComment(postData.comments, path, comment)
} else if (type === 'delete') {
if (postData.comments) {
const success = deleteComment(postData.comments, path, user.id)
if (!success) {
res.json({
success: false,
msg: 'you cant delete other users comments',
postData: postData,
})
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)
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<{
type: string
path: number[]
postKey: string
forumName: string
content: string
}>,
res
) => {
logger.LogReq(req)
const forumName: any = req.body.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 admins: any = utils.FileExists(adminUsersFile)
? utils.ReadJSON(adminUsersFile)
: []
const { type, path, postKey } = req.body
if (!postKey || !type || !path) {
res.json({
success: false,
msg: 'type or path or postKey is undefined',
})
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') {
const { content } = req.body
const comment = {
date: utils.GetDateString(),
user: user.id,
content: content,
admin: admins.includes(user.id),
}
}
} else {
res.json({ success: false, msg: 'no such type' })
return
}
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, postKey: postKey, postData: postData })
})
app.post('/react', (req: Request, res) => {
logger.LogReq(req)
const forumName: any = req.body.forumName
if (!forumName) {
res.json({
success: false,
msg: `Forum name is not specified!`,
})
return
}
const { forumPath, contents } = getForumData(forumName, forumDir)
const user: User = req.session.user
const { postKey, path, reaction, isDelete } = req.body
if (!postKey || !reaction) {
res.json({ success: false, msg: 'no postkey or reaction' })
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 (!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]
}
if (!postData.comments) {
postData.comments = []
}
addComment(postData.comments, path, comment)
} else if (type === 'delete') {
if (postData.comments) {
const success = deleteComment(postData.comments, path, user.id)
if (!success) {
res.json({
success: false,
msg: 'you cant delete other users comments',
postData: postData,
})
return
}
} else {
if (!postData.reacts) {
postData.reacts = { [reaction]: [user.id] }
}
} else {
res.json({ success: false, msg: 'no such type' })
return
}
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, postKey: postKey, postData: postData })
}
)
app.post(
'/react',
(
req: Request<{
forumName: string
postKey: string
path: number[]
reaction: string
isDelete: boolean
}>,
res
) => {
logger.LogReq(req)
const forumName: any = req.body.forumName
if (!forumName) {
res.json({
success: false,
msg: `Forum name is not specified!`,
})
return
}
const { forumPath, contents } = getForumData(forumName, forumDir)
const user: User = req.session.user
const { postKey, path, reaction, isDelete } = req.body
if (!postKey || !reaction) {
res.json({ success: false, msg: 'no postkey or reaction' })
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 (!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 (Array.isArray(postData.reacts[reaction])) {
if (!postData.reacts[reaction].includes(user.id)) {
postData.reacts[reaction].push(user.id)
}
if (!postData.reacts) {
postData.reacts = { [reaction]: [user.id] }
} else {
postData.reacts[reaction] = [user.id]
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,
})
}
} 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 })
})
utils.WriteFile(JSON.stringify(postData, null, 2), postPath)
res.json({ success: true, postData: postData })
}
)
}
export default {