diff --git a/src/cli/manage.js b/src/cli/manage.js index 82472d115e..0d1c514ddd 100644 --- a/src/cli/manage.js +++ b/src/cli/manage.js @@ -206,6 +206,7 @@ async function maintenance(toggle) { async function buildWrapper(targets, options) { try { await build.build(targets, options); + process.exit(0); } catch (err) { winston.error(err.stack); @@ -213,6 +214,8 @@ async function buildWrapper(targets, options) { } } + + exports.build = buildWrapper; exports.install = install; exports.activate = activate; diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js index f6564b2da6..c81a559463 100644 --- a/src/controllers/write/posts.js +++ b/src/controllers/write/posts.js @@ -179,14 +179,44 @@ Posts.getReplies = async (req, res) => { helpers.formatApiResponse(200, res, { replies }); }; -// Add the approve function + + +async function markPostAsApproved(pid, status) { + try { + // 1. Fetch the post by pid + const post = await db.getObject(`post:${pid}`); + if (!post) { + throw new Error(`Post with ID ${pid} not found`); + } + + const newIsApproved = !(status === 'true' || status === true); + post.isApproved = newIsApproved; + + await db.setObject(`post:${pid}`, post); + return newIsApproved; + } catch (err) { + console.error(`Failed to approve post ${pid}:`, err); + throw err; + } +} + Posts.approve = async (req, res) => { + console.log("Trying to approve"); try { + // console.log("Approving post for req"); + // console.log(req); const { pid } = req.params; // Assuming you have a function to mark the post as approved - await markPostAsApproved(pid, req.user.id); - res.status(200).json({ message: 'Post approved successfully' }); + const isApproved = await markPostAsApproved(pid, req.body.isApproved); + if (isApproved){ + console.log("[SERVER] Approved post"); + res.status(200).json({ message: '[Server] Post approved successfully', isApproved: isApproved }); + } else { + console.log("[SERVER] Disapproved post"); + res.status(200).json({ message: '[Server] Post disapproved successfully', isApproved: isApproved }); + } } catch (error) { - res.status(500).json({ error: 'An error occurred while approving the post' }); + res.status(500).json({ error: '[Alert] An error occurred while approving the post' }); + } }; diff --git a/src/install.js b/src/install.js index 89b40d7b39..7df4c467e6 100644 --- a/src/install.js +++ b/src/install.js @@ -486,6 +486,8 @@ async function createWelcomePost() { cid: 2, title: 'Welcome to your NodeBB!', content: content, + annonymousType: "none", + isApproved: true, }); } } diff --git a/src/meta/build.js b/src/meta/build.js index d7b545dc80..9b838f930d 100644 --- a/src/meta/build.js +++ b/src/meta/build.js @@ -189,19 +189,154 @@ exports.build = async function (targets, options) { } else { winston.info('[build] Building in series mode'); } - + await editTemplateFiles(); + winston.info(`[build] Editing node_modules successful.`); + const startTime = Date.now(); await buildTargets(targets, !series, options); const totalTime = (Date.now() - startTime) / 1000; await cacheBuster.write(); winston.info(`[build] Asset compilation successful. Completed in ${totalTime}sec.`); + } catch (err) { winston.error(`[build] Encountered error during build step`); throw err; } + }; +async function editTemplateFiles() { + // Your code to modify files in node_modules/templates + + await editPostTPL(); + + console.log(chalk.bold(chalk.green("Editing node_modules/nodebb-theme-harmony/templates/partials/topic/post.tpl"))); +} + +const fs = require('fs').promises; + +async function editPostTPL() { + const templatePath = path.join('node_modules', 'nodebb-theme-harmony', 'templates', 'partials', 'topic', 'post.tpl'); + + try { + // Read the template file + let content = await fs.readFile(templatePath, 'utf8'); + + // Split the content into an array of lines + let lines = content.split('\n'); + + // Define the string to add at line 59 + const stringToAddAtLine59 = `{{{ if (./isApproved == "true") }}} + + + Instructor Approved + + {{{ else }}} + + + Instructor Unapproved + + {{{ end }}}`; + + // Define the string to add at line 117 + const stringToAddAtLine117 = ` + +`; + + // Check if the content already contains the string for line 59 + if (!content.includes(stringToAddAtLine59.trim())) { + // Insert the string at line 59 (keeping array zero-based, so line 58 in the array) + lines.splice(58, 0, stringToAddAtLine59); + } else { + console.log('String to add at line 59 already exists, skipping...'); + } + + // Check if the content already contains the string for line 117 + if (!content.includes(stringToAddAtLine117.trim())) { + // Insert the string at line 117 (keeping array zero-based, so line 116 in the array) + lines.splice(116, 0, stringToAddAtLine117); + } else { + console.log('String to add at line 117 already exists, skipping...'); + } + + // Join the array back into a single string + content = lines.join('\n'); + + // Write the modified content back to the file + await fs.writeFile(templatePath, content, 'utf8'); + console.log('Template file updated successfully!'); + } catch (error) { + winston.error(`Failed to edit template file: ${error.message}`); + } +} + + function getWebpackConfig() { return require(process.env.NODE_ENV !== 'development' ? '../../webpack.prod' : '../../webpack.dev'); } diff --git a/src/routes/write/posts.js b/src/routes/write/posts.js index 33fda85a2a..11d95e6575 100644 --- a/src/routes/write/posts.js +++ b/src/routes/write/posts.js @@ -43,6 +43,7 @@ module.exports = function () { // Shorthand route to access post routes by topic index router.all('/+byIndex/:index*?', [middleware.checkRequired.bind(null, ['tid'])], controllers.write.posts.redirectByIndex); + setupApiRoute(router, 'put', '/:pid/approve', [...middlewares], controllers.write.posts.approve); return router; };