Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to generate Eco News from the Manage Eco News Page #8083

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/main/java/greencity/controller/AIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public ResponseEntity<String> forecast(@Parameter(hidden = true) @CurrentUser Us
public ResponseEntity<String> creatingEcoNews(@Parameter(hidden = true) Locale locale,
@RequestParam(required = false) String query) {
return ResponseEntity.status(HttpStatus.OK)
.body(aiService.getNews(locale.getDisplayLanguage(), query));
.body(
aiService.getNews(locale.toString().equals("ua") ? "українська" : locale.getDisplayLanguage(), query));
}
}
3 changes: 3 additions & 0 deletions core/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ greenCity.pages.popup.hidden.EcoNews=This Eco News is hidden and is not visible
greenCity.pages.popup.goback.=Go Back
greenCity.pages.popup.update.=Update
greenCity.pages.popup.preview.= Preview
greenCity.pages.popup.generate=Generate
/*paginationpages*/
greenCity.page.paging.show=Showing
greenCity.page.paging.out.of=out of
Expand Down Expand Up @@ -206,6 +207,8 @@ greenCity.econews.page.comment=comment
greenCity.econews.page.view.userLikes=Users who liked post
greenCity.econews.page.view.userDislikes=Users who disliked post
greenCity.econews.page.view=view
greenCity.econews.page.ai.h=Generate AI Eco News
greenCity.econews.page.ai.topic=Enter topic for Eco News generation:
/*events*/
greenCity.events.page.h=Manage Events
greenCity.events.page.add.event=Add Event
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/resources/messages_ua.properties
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ greenCity.pages.popup.types.=\u0412\u0441\u0456 \u0442\u0438\u043f\u0438
greenCity.pages.popup.goback.=\u041f\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u0438\u0441\u044c
greenCity.pages.popup.save.changes=\u0053\u0061\u0076\u0065 \u0063\u0068\u0061\u006e\u0067\u0065\u0073
greenCity.pages.popup.preview.= \u041f\u0440\u0435\u0432\u0027\u044e
greenCity.pages.popup.generate=\u0421\u0442\u0432\u043e\u0440\u0438\u0442\u0438

greenCity.pages.popup.hidden.EcoNews=\u0426\u044f\u0020\u0415\u043a\u043e\u0020\u041d\u043e\u0432\u0438\u043d\u0430\u0020\u043f\u0440\u0438\u0445\u043e\u0432\u0430\u043d\u0430\u0020\u0456\u0020\u043d\u0435\u0020\u0432\u0456\u0434\u043e\u0431\u0440\u0430\u0436\u0430\u0454\u0442\u044c\u0441\u044f\u0020\u0434\u043b\u044f\u0020\u043a\u043e\u0440\u0438\u0441\u0442\u0443\u0432\u0430\u0447\u0456\u0432\u002e

Expand Down Expand Up @@ -195,6 +196,8 @@ greenCity.econews.page.delete.eco.news=\u0412\u0438\u0434\u0430\u043B\u0438\u044
greenCity.econews.page.edit.eco.news=\u0420\u0435\u0434\u0430\u0433\u0443\u0432\u0430\u0442\u0438\u0020\u0415\u043A\u043E\u0020\u041D\u043E\u0432\u0438\u043D\u0438
greenCity.econews.page.comments=\u041A\u043E\u043C\u0435\u043D\u0442\u0430\u0440\u0456
greenCity.econews.page.comment=\u043A\u043E\u043C\u0435\u043D\u0442\u0456\u0432
greenCity.econews.page.ai.h=\u0421\u0442\u0432\u043e\u0440\u0438\u0442\u0438 AI \u0415\u043a\u043e \u041d\u043e\u0432\u0438\u043d\u0438
greenCity.econews.page.ai.topic=\u0412\u0432\u0435\u0434\u0456\u0442\u044c \u0442\u0435\u043c\u0443 \u0434\u043b\u044f \u0441\u0442\u0432\u043e\u0440\u0435\u043d\u043d\u044f \u0415\u043a\u043e \u041d\u043e\u0432\u0438\u043d:

/*events*/
greenCity.events.page.h=\u041C\u0435\u043D\u0435\u0440\u0436\u0435\u0440 \u043F\u043E\u0434\u0456\u0439
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/resources/static/management/econews/buttonsAJAX.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,46 @@ $(document).ready(function () {

});

//generate EcoNews content button in addEcoNewsModal
$('#toggleGenerateEcoNewsForm').on('click', function (event) {
const form = document.getElementById('generateEcoNewsForm');
form.style.display = form.style.display === 'none' ? 'block' : 'none';
});

$('#generateEcoNewsContent').on('click', function (event) {
const query = $('#generateQueryInput').val().trim();
const language = localStorage.getItem("language") || "en";
const locale = language === "ua" ? "uk-UA" : "en-US";

$.ajax({
url: '/ai/generate/eco-news',
type: 'GET',
data: { query: query },
headers: { 'Accept-Language': locale },
contentType: 'application/json',
success: function (response) {
console.log(response);
const titleMatch = response.match(/\*\*Title:\s*(.+?)\s*\*\*/) || response.match(/\*\*\s*(.+?)\s*\*\*/);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove console.log of potentially sensitive data.

The response from the AI service might contain sensitive information. Remove the console.log or replace it with appropriate logging.

-                console.log(response);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log(response);
const titleMatch = response.match(/\*\*Title:\s*(.+?)\s*\*\*/) || response.match(/\*\*\s*(.+?)\s*\*\*/);
const titleMatch = response.match(/\*\*Title:\s*(.+?)\s*\*\*/) || response.match(/\*\*\s*(.+?)\s*\*\*/);


if (titleMatch) {
const title = titleMatch[1];
$('#inputTitle').val(title);

const updatedContent = response.replace(titleMatch[0], '').trim();

tinymce.get('ecoNewsContent').setContent(updatedContent);
} else {
tinymce.get('ecoNewsContent').setContent(response);
}
},
error: function (xhr, status, error) {
console.error('Error generating Eco News content:', status, error);
document.getElementById("errorModalGenerateContent").innerText =
'Failed to generate Eco News content. Please try again.';
}
});
});

//view users who liked/disliked modal
$('.open-likesButton').on("click", function () {
let newsId = $(this).data('id');
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/resources/templates/core/management_eco_news.html
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,19 @@ <h4 class="modal-title">[[#{greenCity.econews.page.add.eco.news}]]</h4>
<textarea id="ecoNewsContent" type="text" class="form-control" name="text" required></textarea>
<span th:id="errorModalSavetext" id="errorModalSavetext" class="errorSpan"></span>
</div>

<button type="button" class="btn btn-default" id="toggleGenerateEcoNewsForm">[[#{greenCity.econews.page.ai.h}]]</button>

<div id="generateEcoNewsForm" style="display: none; margin: 15px 0;">
<div class="form-group">
<label for="generateQueryInput">[[#{greenCity.econews.page.ai.topic}]]</label>
<input type="text" id="generateQueryInput" class="form-control">
<span th:id="errorModalGenerateContent" id="errorModalGenerateContent" class="errorSpan"></span>
</div>
<button type="button" class="btn btn-secondary" id="generateEcoNewsContent">[[#{greenCity.pages.popup.generate}]]</button>
</div>


<div class="form-group">
<label>[[#{greenCity.pages.table.tags}]]</label>
<div class="form-check" th:each="allTags, iterStatus: ${ecoNewsTag}" name="tags">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest,
UserVO userVO = userService.findById(toSave.getAuthor().getId());
achievementCalculation
.calculateAchievement(userVO, AchievementCategoryType.CREATE_NEWS, AchievementAction.ASSIGN);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("CREATE_NEWS"),
modelMapper.map(toSave, UserVO.class));
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("CREATE_NEWS"), userVO);
userNotificationService.createNewNotification(userVO, NotificationType.ECONEWS_CREATED, toSave.getId(),
toSave.getTitle());
return modelMapper.map(toSave, AddEcoNewsDtoResponse.class);
Expand Down
Loading