From 3f9fee332464c58225c8164f4ba3ef6be62d20a2 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Wed, 4 Oct 2023 12:52:35 +0300 Subject: [PATCH 01/10] added-extra-detail-to-hook --- public/src/client/topic/postTools.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index cf410832..d7ed987b 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -1,5 +1,7 @@ 'use strict'; +const db = require('../../../../src/database'); + define('forum/topic/postTools', [ 'share', From da7c86d1ac5e22719f50b7173b9ea14ce3874c98 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Wed, 4 Oct 2023 13:01:20 +0300 Subject: [PATCH 02/10] added-try-catch-block-for-db-editing --- public/src/client/topic/postTools.js | 32 +++++++++------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index d7ed987b..f58a083c 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -41,14 +41,7 @@ define('forum/topic/postTools', [ PostTools.updatePostCount(ajaxify.data.postcount); }; - // Function to set the endorse messageText in local storage - function setTextInLocalStorage(key, text) { - sessionStorage.setItem(key, text); - } - // Getter function to fetch the endorse messageText from local storage - function getTextFromLocalStorage(key) { - return sessionStorage.getItem(key); - } + function renderMenu() { $('[component="topic"]').on('show.bs.dropdown', '.moderator-tools', function () { const $this = $(this); @@ -57,13 +50,6 @@ define('forum/topic/postTools', [ return; } - // Get endorsement message from session storage - const topicEndorseText = getTextFromLocalStorage('topicEndorseText'); - // If message is not empty - if (topicEndorseText) { - // Append it to the viewport object - $('[component="topic/endorse"]').text(topicEndorseText); - } const postEl = $this.parents('[data-pid]'); const pid = postEl.attr('data-pid'); const index = parseInt(postEl.attr('data-index'), 10); @@ -121,10 +107,6 @@ define('forum/topic/postTools', [ // Catch the actual click using the defined function postContainer.on('click', '[component ="post/endorse"]', function () { onEndorseClicked($(this), tid); - var message = 'Someone thinks this is a good response(0)'; - $(this).text(message); - const storageKey = 'postEndorseText'; - setTextInLocalStorage(storageKey, message); }); postContainer.on('click', '[component="post/quote"]', function () { @@ -143,9 +125,6 @@ define('forum/topic/postTools', [ $('.topic').on('click', '[component="topic/endorse"]', function (e) { e.preventDefault(); onEndorseClicked($(this), tid); - var message = 'Someone thinks this is a good response(0)'; - const storageKey = 'topicEndorseText'; - setTextInLocalStorage(storageKey, message); }); $('.topic').on('click', '[component="topic/reply-as-topic"]', function () { @@ -159,6 +138,15 @@ define('forum/topic/postTools', [ // Ansync Function to help us with the Click of Endorsed async function onEndorseClicked(button, tid) { + //try-catch block to adhere to existing asynchronous models + const topic_id = tid; + try{ + await db.setObjectField(`topic:${topic_id}`, 'endorsed_by_Instructor', true); + //Testing purposes only + console.log("Success"); + } catch (err){ + console.log(err); + } const selectedNode = await getSelectedNode(); showStaleWarning(async function () { let username = await getUserSlug(button); From a537538b11ae5986e9979fae51b12c5a5cbae8e1 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Wed, 4 Oct 2023 13:02:42 +0300 Subject: [PATCH 03/10] removed-session-storage-manipulation --- public/src/client/topic/postTools.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index f58a083c..0546dfca 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -21,16 +21,6 @@ define('forum/topic/postTools', [ PostTools.init = function (tid) { staleReplyAnyway = false; - // Load Session Storage when the page loads - const postEndorseText = getTextFromLocalStorage('postEndorseText'); - if (postEndorseText) { - $('[component="post/endorse"]').text(postEndorseText); - } - const topicEndorseText = getTextFromLocalStorage('topicEndorseText'); - if (topicEndorseText) { - $('[component="topic/endorse"]').text(topicEndorseText); - } - renderMenu(); addPostHandlers(tid); From 7f1ec24f9fdcd08799ff52ea90b0135cb1c7a691 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 05:37:02 +0300 Subject: [PATCH 04/10] setup-route-to-handle-endorse-get-requests --- public/src/client/topic/postTools.js | 18 ++++++++++++------ src/controllers/topics.js | 22 ++++++++++++++++++++++ src/routes/index.js | 2 ++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 0546dfca..e3a131df 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -96,6 +96,8 @@ define('forum/topic/postTools', [ // Catch the actual click using the defined function postContainer.on('click', '[component ="post/endorse"]', function () { + // REMOVE + console.log("Clicked....."); onEndorseClicked($(this), tid); }); @@ -128,13 +130,17 @@ define('forum/topic/postTools', [ // Ansync Function to help us with the Click of Endorsed async function onEndorseClicked(button, tid) { - //try-catch block to adhere to existing asynchronous models + console.log("Button CLicked...."); + // try-catch block to adhere to existing asynchronous models const topic_id = tid; - try{ - await db.setObjectField(`topic:${topic_id}`, 'endorsed_by_Instructor', true); - //Testing purposes only - console.log("Success"); - } catch (err){ + console.log(topic_tid); + try { + // Update the DB field endorsed of the this post + await db.setObjectField(`topic:${topic_id}`, 'endorsed', true); + // Testing purposes only + console.log('Success'); + } catch (err) { + // Or log to the console any errors encountered otherwise console.log(err); } const selectedNode = await getSelectedNode(); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index eefc2681..570f90ac 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -3,6 +3,8 @@ const nconf = require('nconf'); const qs = require('querystring'); +// Import the DB object to allow us to use it +const db = require('../database'); const user = require('../user'); const meta = require('../meta'); const topics = require('../topics'); @@ -123,6 +125,26 @@ topicsController.get = async function getTopic(req, res, next) { res.render('topic', topicData); }; +// Set up a whole new controller to update the DB when called upon +topicsController.topicIsEndorsed = async function (req, res){ + // Use a try-catch block to adhere to the existing async models + try { + // Get the topic ID + const topic_id = req.params.topic_id; + // Asynchronously try to update the field in the DB + await db.setObjectField(`topic: ${topic_id}`, 'endorsed_by_Instrucor', true); + // Use the get route to receive data related to post + const ret = await topicsController.get(req, req.params); + // Dispatch the appropriate API Response + helpers.formatApiResponse(200, res, ret); + } catch (err) { + // Log to the console any errors we get and dispatch the + // relevant API response + helpers.formatApiResponse(500, res, {err: 'Unable to update DB field'}); + } + +} + function generateQueryString(query) { const qString = qs.stringify(query); return qString.length ? `?${qString}` : ''; diff --git a/src/routes/index.js b/src/routes/index.js index 91e9f001..37daa8f6 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -58,6 +58,8 @@ _mounts.globalMod = (app, middleware, controllers) => { _mounts.topic = (app, name, middleware, controllers) => { setupPageRoute(app, `/${name}/:topic_id/:slug/:post_index?`, [], controllers.topics.get); setupPageRoute(app, `/${name}/:topic_id/:slug?`, [], controllers.topics.get); + // Set up a route to handle endorse requests + setupPageRoute(app, `/${name}/:topic_id/:slug/isEndorsed`, [], controllers.topics.topicIsEndorsed); }; _mounts.post = (app, name, middleware, controllers) => { From 51ab967702c7557b1d6d12f7ebf67f3000be956d Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 06:36:22 +0300 Subject: [PATCH 05/10] added-front-end-logic-for-get-request-to-API --- public/src/client/topic/postTools.js | 30 ++++++++++++++-------------- src/controllers/topics.js | 14 ++++++------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index e3a131df..814b57fe 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -1,6 +1,5 @@ 'use strict'; -const db = require('../../../../src/database'); define('forum/topic/postTools', [ @@ -97,7 +96,7 @@ define('forum/topic/postTools', [ // Catch the actual click using the defined function postContainer.on('click', '[component ="post/endorse"]', function () { // REMOVE - console.log("Clicked....."); + console.log('Clicked.....'); onEndorseClicked($(this), tid); }); @@ -130,19 +129,20 @@ define('forum/topic/postTools', [ // Ansync Function to help us with the Click of Endorsed async function onEndorseClicked(button, tid) { - console.log("Button CLicked...."); - // try-catch block to adhere to existing asynchronous models - const topic_id = tid; - console.log(topic_tid); - try { - // Update the DB field endorsed of the this post - await db.setObjectField(`topic:${topic_id}`, 'endorsed', true); - // Testing purposes only - console.log('Success'); - } catch (err) { - // Or log to the console any errors encountered otherwise - console.log(err); - } + console.log('Button CLicked....'); + // Fetch request to the designated route in the backend + fetch(window.location.href + "/isEndorsed", { + mathod: 'GET', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + } + }).then( res => res.json()) + .then( data => console.log(data)) + .catch( err => console.log(err)); + // Testing purposes only + console.log('Finished Fetching'); + const selectedNode = await getSelectedNode(); showStaleWarning(async function () { let username = await getUserSlug(button); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 570f90ac..26dc4d25 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -126,25 +126,23 @@ topicsController.get = async function getTopic(req, res, next) { }; // Set up a whole new controller to update the DB when called upon -topicsController.topicIsEndorsed = async function (req, res){ +topicsController.topicIsEndorsed = async function (req, res) { // Use a try-catch block to adhere to the existing async models try { // Get the topic ID - const topic_id = req.params.topic_id; + const topic_id = Number(req.params.topic_id); // Asynchronously try to update the field in the DB - await db.setObjectField(`topic: ${topic_id}`, 'endorsed_by_Instrucor', true); - // Use the get route to receive data related to post + await db.setObjectField(`topic: ${topic_id}`, 'endorsed', true); + // Use the get route to receive data related to post const ret = await topicsController.get(req, req.params); // Dispatch the appropriate API Response helpers.formatApiResponse(200, res, ret); } catch (err) { // Log to the console any errors we get and dispatch the // relevant API response - helpers.formatApiResponse(500, res, {err: 'Unable to update DB field'}); + helpers.formatApiResponse(500, res, { err: 'Unable to update DB field' }); } - -} - +}; function generateQueryString(query) { const qString = qs.stringify(query); return qString.length ? `?${qString}` : ''; From ba8a8e76483b5ff22d81c8839c52a0e9891c8827 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 13:06:15 +0300 Subject: [PATCH 06/10] added-fetch-logic-to-post-tpl --- public/src/client/topic/postTools.js | 1 + .../templates/partials/topic/post.tpl | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 814b57fe..c2f847ec 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -143,6 +143,7 @@ define('forum/topic/postTools', [ // Testing purposes only console.log('Finished Fetching'); + ///////////////////////////////////////////////////// const selectedNode = await getSelectedNode(); showStaleWarning(async function () { let username = await getUserSlug(button); diff --git a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl index c2b3d208..0a4ee3ef 100644 --- a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl +++ b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl @@ -51,8 +51,10 @@
{posts.content} +
{{{ if posts.endorsed_by_Instructor }}}

This response has been Endorsed !

+ {{{ end }}}
@@ -107,4 +109,24 @@
+
+ +
\ No newline at end of file From 909085f422373e6113069703d34cd664fe981abe Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 19:29:53 +0300 Subject: [PATCH 07/10] added-back-session-storage-to-mitigate-endorsement-dysfunction --- public/src/client/topic/postTools.js | 34 +++- .../templates/partials/topic/post.tpl | 163 ++++++++++++++++-- 2 files changed, 173 insertions(+), 24 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index c2f847ec..7ac8cd8d 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -20,6 +20,11 @@ define('forum/topic/postTools', [ PostTools.init = function (tid) { staleReplyAnyway = false; + // Load Session Storage when the page loads + const postEndorseText = getTextFromLocalStorage('postEndorseText'); + if (postEndorseText) { + $('[component="post/endorse"]').text(postEndorseText); + renderMenu(); addPostHandlers(tid); @@ -31,6 +36,15 @@ define('forum/topic/postTools', [ PostTools.updatePostCount(ajaxify.data.postcount); }; + // Function to set the endorse messageText in local storage + function setTextInLocalStorage(key, text) { + sessionStorage.setItem(key, text); + } + // Getter function to fetch the endorse messageText from local storage + function getTextFromLocalStorage(key) { + return sessionStorage.getItem(key); + } + function renderMenu() { $('[component="topic"]').on('show.bs.dropdown', '.moderator-tools', function () { const $this = $(this); @@ -38,6 +52,11 @@ define('forum/topic/postTools', [ if (dropdownMenu.html()) { return; } + // Load Endorsement text as soon as menu loads + const topicEndorseText = getTextFromLocalStorage('topicEndorseText'); + if (topicEndorseText) { + $('[component="topic/endorse"]').text(topicEndorseText); + } const postEl = $this.parents('[data-pid]'); const pid = postEl.attr('data-pid'); @@ -95,9 +114,12 @@ define('forum/topic/postTools', [ // Catch the actual click using the defined function postContainer.on('click', '[component ="post/endorse"]', function () { - // REMOVE - console.log('Clicked.....'); onEndorseClicked($(this), tid); + var message = "Someone thinks this is a good response(0)" + $(this).text(message); + const storageKey = 'postEndorseText'; + // localStorage.setItem(storageKey, text); + setTextInLocalStorage(storageKey, message); }); postContainer.on('click', '[component="post/quote"]', function () { @@ -116,6 +138,8 @@ define('forum/topic/postTools', [ $('.topic').on('click', '[component="topic/endorse"]', function (e) { e.preventDefault(); onEndorseClicked($(this), tid); + // Set the new button Message + var message = "Someone thinks this is a good response(0)" }); $('.topic').on('click', '[component="topic/reply-as-topic"]', function () { @@ -129,7 +153,6 @@ define('forum/topic/postTools', [ // Ansync Function to help us with the Click of Endorsed async function onEndorseClicked(button, tid) { - console.log('Button CLicked....'); // Fetch request to the designated route in the backend fetch(window.location.href + "/isEndorsed", { mathod: 'GET', @@ -140,9 +163,8 @@ define('forum/topic/postTools', [ }).then( res => res.json()) .then( data => console.log(data)) .catch( err => console.log(err)); - // Testing purposes only - console.log('Finished Fetching'); - + //Throws an error if there is any + // Second part of the action of dispatching the associated hook ///////////////////////////////////////////////////// const selectedNode = await getSelectedNode(); showStaleWarning(async function () { diff --git a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl index 0a4ee3ef..4f2fc0d6 100644 --- a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl +++ b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl @@ -51,11 +51,6 @@
{posts.content} -
- {{{ if posts.endorsed_by_Instructor }}} -

This response has been Endorsed !

- - {{{ end }}}
+
\ No newline at end of file From b01c1a16c58199561a3ac58112e0f5d7fb9adc80 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 19:36:28 +0300 Subject: [PATCH 08/10] removed-unnecessary-and-buggy-code-from-posts-tpl-file --- .../templates/partials/topic/post.tpl | 154 +----------------- 1 file changed, 1 insertion(+), 153 deletions(-) diff --git a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl index 4f2fc0d6..3feeb84c 100644 --- a/themes/nodebb-theme-persona/templates/partials/topic/post.tpl +++ b/themes/nodebb-theme-persona/templates/partials/topic/post.tpl @@ -75,27 +75,7 @@ {{{ end }}} - -
- - - + @@ -124,136 +104,4 @@
-
- - -
\ No newline at end of file From 3edb3e31d2da0ca5845704a10f5c2a6332f9047d Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 19:49:24 +0300 Subject: [PATCH 09/10] modified-and-enhanced-the-topics-endorsement-controller-in-src/controllers/topics-and-reviewed-get-routes --- public/src/client/topic/postTools.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 7ac8cd8d..d5fc7075 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -20,10 +20,6 @@ define('forum/topic/postTools', [ PostTools.init = function (tid) { staleReplyAnyway = false; - // Load Session Storage when the page loads - const postEndorseText = getTextFromLocalStorage('postEndorseText'); - if (postEndorseText) { - $('[component="post/endorse"]').text(postEndorseText); renderMenu(); @@ -34,6 +30,17 @@ define('forum/topic/postTools', [ votes.addVoteHandler(); PostTools.updatePostCount(ajaxify.data.postcount); + // Load Session Storage when the page loads + const postEndorseText = getTextFromLocalStorage('postEndorseText'); + if (postEndorseText) { + $('[component="post/endorse"]').text(postEndorseText); + } + // Load Endorsement text as soon as menu loads + const topicEndorseText = getTextFromLocalStorage('topicEndorseText'); + if (topicEndorseText) { + $('[component="topic/endorse"]').text(topicEndorseText); + } + }; // Function to set the endorse messageText in local storage @@ -52,6 +59,10 @@ define('forum/topic/postTools', [ if (dropdownMenu.html()) { return; } + const postEndorseText = getTextFromLocalStorage('postEndorseText'); + if (postEndorseText) { + $('[component="post/endorse"]').text(postEndorseText); + } // Load Endorsement text as soon as menu loads const topicEndorseText = getTextFromLocalStorage('topicEndorseText'); if (topicEndorseText) { From 7dd1e5eb3afc925d27d10e61fb67d634c4e41897 Mon Sep 17 00:00:00 2001 From: Steve0012345 Date: Thu, 5 Oct 2023 19:58:56 +0300 Subject: [PATCH 10/10] fixed-prevalent-linter-errors-in-code --- public/src/client/topic/postTools.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index d5fc7075..9d63bec6 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -40,9 +40,7 @@ define('forum/topic/postTools', [ if (topicEndorseText) { $('[component="topic/endorse"]').text(topicEndorseText); } - }; - // Function to set the endorse messageText in local storage function setTextInLocalStorage(key, text) { sessionStorage.setItem(key, text); @@ -126,7 +124,7 @@ define('forum/topic/postTools', [ // Catch the actual click using the defined function postContainer.on('click', '[component ="post/endorse"]', function () { onEndorseClicked($(this), tid); - var message = "Someone thinks this is a good response(0)" + var message = 'Someone thinks this is a good response(0)'; $(this).text(message); const storageKey = 'postEndorseText'; // localStorage.setItem(storageKey, text); @@ -149,8 +147,6 @@ define('forum/topic/postTools', [ $('.topic').on('click', '[component="topic/endorse"]', function (e) { e.preventDefault(); onEndorseClicked($(this), tid); - // Set the new button Message - var message = "Someone thinks this is a good response(0)" }); $('.topic').on('click', '[component="topic/reply-as-topic"]', function () { @@ -165,18 +161,17 @@ define('forum/topic/postTools', [ // Ansync Function to help us with the Click of Endorsed async function onEndorseClicked(button, tid) { // Fetch request to the designated route in the backend - fetch(window.location.href + "/isEndorsed", { + fetch(window.location.href + '/isEndorsed', { mathod: 'GET', headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - } - }).then( res => res.json()) - .then( data => console.log(data)) - .catch( err => console.log(err)); - //Throws an error if there is any + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }).then(res => res.json()) + .then(data => console.log(data)) + .catch(err => console.log(err)); + // Throws an error if there is any // Second part of the action of dispatching the associated hook - ///////////////////////////////////////////////////// const selectedNode = await getSelectedNode(); showStaleWarning(async function () { let username = await getUserSlug(button);