From f7a08b3d0c83da4dc31113dd419a8d68105123a6 Mon Sep 17 00:00:00 2001 From: Phillip Araujo Date: Thu, 14 Nov 2024 12:15:56 -0500 Subject: [PATCH 1/5] Fixed lint errors, changed testing scope of two linters --- .github/workflows/standardjs.yml | 2 +- public/src/client/topic.js | 24 ++++++++++++------------ src/posts/create.js | 2 +- src/posts/data.js | 2 +- src/translate/index.js | 12 ++++++------ 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/standardjs.yml b/.github/workflows/standardjs.yml index 9a871e93ea..705cb3459d 100644 --- a/.github/workflows/standardjs.yml +++ b/.github/workflows/standardjs.yml @@ -28,4 +28,4 @@ jobs: working-directory: ./install - name: Run StandardJS - run: npx standard + run: npx standard public/src/modules/chat.js diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 5b61bb19f8..8667b1d361 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -79,18 +79,18 @@ define('forum/topic', [ }; function configurePostToggle() { - $(".topic").on("click", ".view-translated-btn", function () { - // Toggle the visibility of the next .translated-content div - $(this).closest('.sensitive-content-message').next('.translated-content').toggle(); - // Optionally, change the button text based on visibility - var isVisible = $(this).closest('.sensitive-content-message').next('.translated-content').is(':visible'); - if (isVisible) { - $(this).text('Hide the translated message.'); - } else { - $(this).text('Click here to view the translated message.'); - } - }); - } + $('.topic').on('click', '.view-translated-btn', function () { + // Toggle the visibility of the next .translated-content div + $(this).closest('.sensitive-content-message').next('.translated-content').toggle(); + // Optionally, change the button text based on visibility + var isVisible = $(this).closest('.sensitive-content-message').next('.translated-content').is(':visible'); + if (isVisible) { + $(this).text('Hide the translated message.'); + } else { + $(this).text('Click here to view the translated message.'); + } + }); + } function handleTopicSearch() { require(['mousetrap'], (mousetrap) => { diff --git a/src/posts/create.js b/src/posts/create.js index 171ac414b5..7dddfe7ede 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -20,7 +20,7 @@ module.exports = function (Posts) { const content = data.content.toString(); const timestamp = data.timestamp || Date.now(); const isMain = data.isMain || false; - const [isEnglish, translatedContent] = await translate.translate(data) + const [isEnglish, translatedContent] = await translate.translate(data); if (!uid && parseInt(uid, 10) !== 0) { throw new Error('[[error:invalid-uid]]'); diff --git a/src/posts/data.js b/src/posts/data.js index 577b94319c..0691f7184d 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -69,6 +69,6 @@ function modifyPost(post, fields) { post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : ''; } // Mark post as "English" if decided by translator service or if it has no info - post.isEnglish = post.isEnglish == "true" || post.isEnglish === undefined; + post.isEnglish = post.isEnglish === 'true' || post.isEnglish === undefined; } } diff --git a/src/translate/index.js b/src/translate/index.js index 5f7392d6b7..337a3538d0 100644 --- a/src/translate/index.js +++ b/src/translate/index.js @@ -1,10 +1,10 @@ -var request = require('request'); +'use strict'; const translatorApi = module.exports; translatorApi.translate = async function (postData) { - const TRANSLATOR_API = "https://slackers-translator-d7bcacgqd5a2gsap.canadacentral-01.azurewebsites.net/"; - const response = await fetch(TRANSLATOR_API+'/?content='+postData.content); - const data = await response.json(); - return [data["is_english"], data["translated_content"]]; -} \ No newline at end of file + const TRANSLATOR_API = 'https://slackers-translator-d7bcacgqd5a2gsap.canadacentral-01.azurewebsites.net/'; + const response = await fetch(`${TRANSLATOR_API}/?content=${postData.content}`); + const data = await response.json(); + return [data.is_english, data.translated_content]; +}; From fd76ce1a7bb1b361efc4aa3cce34c3f6261b3dbb Mon Sep 17 00:00:00 2001 From: Phillip Araujo Date: Thu, 14 Nov 2024 12:19:32 -0500 Subject: [PATCH 2/5] Changed lint actions to run on every branch, not just f24 --- .github/workflows/standardjs.yml | 6 ++---- .github/workflows/test.yaml | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/standardjs.yml b/.github/workflows/standardjs.yml index 705cb3459d..6f06a609eb 100644 --- a/.github/workflows/standardjs.yml +++ b/.github/workflows/standardjs.yml @@ -3,12 +3,10 @@ name: StandardJS Lint on: pull_request: branches: - - f24 - - standard-js-tool + - '*' push: branches: - - f24 - - standard-js-tool + - '*' jobs: lint: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c4e8f090bd..de86123d47 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -3,7 +3,7 @@ name: Lint and test on: pull_request: branches: - - f24 + - '*' workflow_call: # Usually called from deploy defaults: From 5805aca9b5753921e896c38adc355afbb52866e1 Mon Sep 17 00:00:00 2001 From: Phillip Araujo Date: Thu, 14 Nov 2024 15:59:38 -0500 Subject: [PATCH 3/5] Trying to fix translate API --- src/translate/index.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/translate/index.js b/src/translate/index.js index 337a3538d0..95d799fda2 100644 --- a/src/translate/index.js +++ b/src/translate/index.js @@ -4,7 +4,29 @@ const translatorApi = module.exports; translatorApi.translate = async function (postData) { const TRANSLATOR_API = 'https://slackers-translator-d7bcacgqd5a2gsap.canadacentral-01.azurewebsites.net/'; - const response = await fetch(`${TRANSLATOR_API}/?content=${postData.content}`); - const data = await response.json(); - return [data.is_english, data.translated_content]; + + try { + const response = await fetch(`${TRANSLATOR_API}/?content=${encodeURIComponent(postData.content)}`); + if (!response.ok) { + throw new Error(`Failed to fetch translation: ${response.status} ${response.statusText}`); + } + + // Attempt to parse response as JSON + let data; + try { + data = await response.json(); + } catch (err) { + throw new Error('Unexpected response format: Expected JSON but received non-JSON data'); + } + + // Check if the necessary fields are present in the data + if (!data || typeof data.is_english !== 'boolean' || typeof data.translated_content !== 'string') { + throw new Error('Incomplete data: Missing expected fields in response'); + } + + return [data.is_english, data.translated_content]; + } catch (error) { + console.error('Translation API error:', error.message); + return [null, 'Error in translation service']; + } }; From 98a67f13413c20fb454f8fc57aadc7d86caf75cf Mon Sep 17 00:00:00 2001 From: Phillip Araujo Date: Thu, 14 Nov 2024 16:07:33 -0500 Subject: [PATCH 4/5] We can't modify package.json, so I have to modify the yaml lint command --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index de86123d47..489b0c4c39 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -75,7 +75,7 @@ jobs: node app --setup="${SETUP}" --ci="${CI}" - name: Run ESLint - run: npm run lint + run: eslint --cache --ignore-pattern 'public/src/modules/chat.js' ./nodebb . - name: Node tests run: npm test From 3cbda92c15af9405c5c24b5dc5a52bd096ffb2c7 Mon Sep 17 00:00:00 2001 From: Phillip Araujo Date: Thu, 14 Nov 2024 16:13:13 -0500 Subject: [PATCH 5/5] Modifying test yml again --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 489b0c4c39..7ad8a0716c 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -75,7 +75,7 @@ jobs: node app --setup="${SETUP}" --ci="${CI}" - name: Run ESLint - run: eslint --cache --ignore-pattern 'public/src/modules/chat.js' ./nodebb . + run: npx eslint --cache --ignore-pattern 'public/src/modules/chat.js' ./nodebb . - name: Node tests run: npm test