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 user story 9 front-end code issue #65

Closed
wants to merge 3 commits into from
Closed
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
1,779 changes: 882 additions & 897 deletions node_modules/nodebb-plugin-composer-default/static/lib/composer.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions node_modules/nodebb-theme-harmony/templates/topic.tpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/topics/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,59 @@ const intFields = [
];

module.exports = function (Topics) {
// adding search function here instead of in a separate search.js file
Topics.postSearch = async function (data) {
console.log('in Topics.postSearch in src/topics/data.js');
console.log('Topics.postSearch input data:', data);

const query = data.query || ''; // the search term
const tid = data.tid || 1; // topic id to search in
const uid = data.uid || 0; // user id of the searcher
const paginate = data.hasOwnProperty('paginate') ? data.paginate : true;

const startTime = process.hrtime();

// store posts associated with a topic
const set = `tid:${tid}:posts`;
// using topics functions to extract all posts and topic data
const topicData = await Topics.getTopicData(tid);
const postsData = await Topics.getTopicPosts(topicData, set, 0, -1, uid);

console.log('posts data:', postsData);

// if the query is empty, return all posts
if (query.trim() === '') {
const searchResult = {
matchCount: postsData.length,
pageCount: paginate ? 1 : 0, // set to 1 page if pagination is enabled
posts: postsData, // return all posts
};
console.log('searchResult when query is empty: ', searchResult);
return searchResult;
}
// filtering posts based on query
const filteredPosts = postsData.filter(post => post.content.toLowerCase().includes(query.toLowerCase()));
// the search result
const searchResult = {
matchCount: filteredPosts.length,
posts: filteredPosts, // include filtered posts in the result
};

if (paginate) {
const resultsPerPage = data.resultsPerPage || 10; // default results per page
const page = data.page || 1; // current page
const start = (page - 1) * resultsPerPage;
const stop = start + resultsPerPage;
searchResult.pageCount = Math.ceil(filteredPosts.length / resultsPerPage); // total pages
searchResult.posts = filteredPosts.slice(start, stop);
}
// timing the search
searchResult.timing = (process.hrtime(startTime)[1] / 1e6).toFixed(2); // ms timing
console.log('Final searchResult: ', searchResult);
return searchResult;
// return filteredPosts;
};

Topics.getTopicsFields = async function (tids, fields) {
if (!Array.isArray(tids) || !tids.length) {
return [];
Expand Down
Loading