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

Adding a question resolved feature to the current NodeBB frame. #32

Open
wants to merge 3 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file added dump.rdb
Binary file not shown.
36 changes: 36 additions & 0 deletions public/src/admin/appearance/skins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
method: 'get',
url: 'https://bootswatch.com/api/5.json',
}).done((bsData) => {
console.log("Hello, world!3");

Check failure on line 15 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
hooks.on('action:settings.sorted-list.loaded', (data) => {
if (data.hash === 'custom-skins') {
// slugify all custom-skin ids after load
Expand All @@ -22,10 +23,12 @@
}
});
settings.load('custom-skins', $('.custom-skin-settings'));
// setThemeBasedOnTime(bsData); // Call the theme selection logic
Skins.render(bsData);
});

$('#save-custom-skins').on('click', function () {
console.log("Hello, world!3");

Check failure on line 31 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
settings.save('custom-skins', $('.custom-skin-settings'), function () {
alerts.success('[[admin/appearance/skins:save-custom-skins-success]]');
});
Expand All @@ -34,6 +37,7 @@


$('#skins').on('click', function (e) {
console.log("Hello, world!3");

Check failure on line 40 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
let target = $(e.target);

if (!target.attr('data-action')) {
Expand Down Expand Up @@ -124,5 +128,37 @@
});
}

function setThemeBasedOnTime() {

Check failure on line 131 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

'setThemeBasedOnTime' is defined but never used
console.log("Hello, world!1"); // Logs a message

Check failure on line 132 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote

Check failure on line 132 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Multiple spaces found before '// Logs a mess...'

const hour = new Date().getHours();
const isNight = (hour >= 18 || hour < 6); // Night from 6 PM to 6 AM

Check failure on line 135 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Multiple spaces found before '// Night from ...'

Check failure on line 136 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
if (isNight) {
// Assuming `dark-theme` is the theme ID for dark mode
const darkTheme = bsData.themes.find(theme => theme.name.toLowerCase().includes('dark'));

Check failure on line 139 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

'bsData' is not defined
if (darkTheme) {
applyTheme(darkTheme);
}
} else {
// Assuming `light-theme` is the theme ID for light mode
const lightTheme = bsData.themes.find(theme => theme.name.toLowerCase().includes('light'));

Check failure on line 145 in public/src/admin/appearance/skins.js

View workflow job for this annotation

GitHub Actions / test

'bsData' is not defined
if (lightTheme) {
applyTheme(lightTheme);
}
}
}

function applyTheme(theme) {
console.log("Hello, world!2"); // Logs a message
const cssSrc = theme.css; // Assuming the theme object contains a 'css' property
const themeId = theme.name;

// Update the theme using similar logic as the 'click' event for user selection
$('link#theme').attr('href', cssSrc);
app.config.bootswatchSkin = themeId;
highlightSelectedTheme(themeId); // Reuse the existing function
}

return Skins;
});
22 changes: 22 additions & 0 deletions public/src/client/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ define('forum/topic', [
handleBookmark(tid);
handleThumbs();

$(document).on('click', '.mark-as-solved', function(e) {
e.preventDefault();
const postIndex = $(this).closest('[data-index]').attr('data-index');
const tid = ajaxify.data.tid;
const pid = $(this).attr('data-pid'); // Assuming the post has a data-pid attribute

markAsSolved(postIndex, tid, pid);
});


$(window).on('scroll', utils.debounce(updateTopicTitle, 250));

handleTopicSearch();
Expand Down Expand Up @@ -207,6 +217,18 @@ define('forum/topic', [
});
}

function markAsSolved(postIndex, tid, pid) {
socket.emit('plugins.markAsSolved', { tid: tid, pid: pid }, function(err, data) {
if (err) {
alerts.error(err.message);
} else {
alerts.success('Marked as Solved!');
// Optionally update the UI to reflect the solved status
$('[data-index="' + postIndex + '"]').addClass('solved-post');
}
});
}

function addBlockQuoteHandler() {
components.get('topic').on('click', 'blockquote .toggle', function () {
const blockQuote = $(this).parent('blockquote');
Expand Down
16 changes: 16 additions & 0 deletions src/controllers/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ topicsController.get = async function getTopic(req, res, next) {
user.auth.getFeedToken(req.uid),
]);

console.log(posts);

// const postsArray = await posts.getPostsByTid(tid, req.uid, req.query.page, req.query);

// // Iterate over the posts to set 'canMarkAsSolved' and 'solved'
// postsArray.forEach(async (post) => {
// const canMarkAsSolved = (req.uid === post.uid || await privileges.isAdminOrMod(req.uid));
// post.canMarkAsSolved = canMarkAsSolved;

// // Ensure 'solved' status is passed correctly
// post.solved = post.solved || false;
// });

// // Pass the updated posts and other necessary data to the template
// res.render('topic', { topicData, posts: postsArray, userPrivileges, settings });

let currentPage = parseInt(req.query.page, 10) || 1;
const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));
const invalidPagination = (settings.usePagination && (currentPage < 1 || currentPage > pageCount));
Expand Down
45 changes: 45 additions & 0 deletions src/plugins/mark-as-solved.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const topics = require.main.require('./src/topics');
const posts = require.main.require('./src/posts');
const privileges = require.main.require('./src/privileges');

module.exports = function(socket, callback) {
socket.on('plugins.markAsSolved', async (data, callback) => {
const { tid, pid } = data;

try {
// Fetch the topic and check if the user has permission to mark as solved
const topic = await topics.getTopicData(tid);
const canMarkAsSolved = await privileges.posts.canEdit(pid, socket.uid);

if (!canMarkAsSolved) {
return callback(new Error('You do not have permission to mark as solved.'));
}

// Mark the post as solved in the database
await posts.setPostField(pid, 'solved', 1);

// Optionally mark the entire topic as solved
await topics.setTopicField(tid, 'solved', 1);

// Success callback with more detailed response
callback(null, { message: 'Post and topic marked as solved.', tid, pid });
} catch (err) {
// Handle any errors during the process
callback(new Error('An error occurred while marking the post as solved.'));
}
});
};

function markAsSolved(tid, pid) {
socket.emit('plugins.markAsSolved', { tid: tid, pid: pid }, function (err, data) {
if (err) {
app.alertError(err.message);
} else {
app.alertSuccess('Post marked as solved!');

// Update UI: Hide the button and show the "Solved" label
document.querySelector(`[data-pid="${pid}"] .mark-as-solved`).style.display = 'none';
document.querySelector(`[data-pid="${pid}"] .solved-label`).style.display = 'block';
}
});
}
34 changes: 34 additions & 0 deletions src/socket.io/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ SocketTopics.getMyNextPostIndex = async function (socket, data) {
return await posts.getPidIndex(userPidsInTopic[0], data.tid, data.sort);
};

SocketTopics.markAsSolved = async function (socket, data) {
if (!socket.uid) {
throw new Error('[[error:not-logged-in]]');
}

if (!data || !data.tid || !data.pid) {
throw new Error('[[error:invalid-data]]');
}

// Fetch the topic and check if the user has permission to mark as solved
const topic = await topics.getTopicData(data.tid);
const canMarkAsSolved = await privileges.posts.canEdit(data.pid, socket.uid);

if (!canMarkAsSolved) {
throw new Error('[[error:no-privileges]]');
}

// Mark the post as solved in the database
await posts.setPostField(data.pid, 'solved', 1);

// Optionally mark the entire topic as solved
await topics.setTopicField(data.tid, 'solved', 1);

// Emit an event or log the action if necessary
events.log({
type: 'post-mark-solved',
uid: socket.uid,
tid: data.tid,
pid: data.pid,
});

return { message: 'Post marked as solved.' };
};

SocketTopics.getPostCountInTopic = async function (socket, tid) {
if (!socket.uid || !tid) {
return 0;
Expand Down