From a711bcb90e49bd67c9b82198469dac41f7e0e9fc Mon Sep 17 00:00:00 2001 From: Najoud Date: Thu, 26 Sep 2024 01:59:15 +0300 Subject: [PATCH 1/4] added Topics.search function in new file src/topics/search.js --- src/topics/search.js | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/topics/search.js diff --git a/src/topics/search.js b/src/topics/search.js new file mode 100644 index 0000000000..2c47c21759 --- /dev/null +++ b/src/topics/search.js @@ -0,0 +1,48 @@ +'use strict'; + +const _ = require('lodash'); +const nconf = require('nconf'); + +const meta = require('../meta'); +const plugins = require('../plugins'); +const db = require('../database'); +const posts = require('../posts'); + + +module.exports = function (Topics) { + + Topics.search = async function (data) { + + // extracting the attributes of data object + 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 + + // storing the posts associated with a topic + const set = `tid:${tid}:posts`; + + // fetch topic attributes using getTopicData + const topicData = await Topics.getTopicData(tid); + + // using getTopicPosts function in src/topics/posts.js + // to retrieve all the posts in the topic (by making range 0 to -1) + const postsData = await Topics.getTopicPosts(topicData, set, 0, -1, uid); + + // looping through retrieved postsdata array + // and inspecting each post object for the search query + const filteredPosts = postsData.filter(post => + // comparing both in lowercase + post.content.toLowerCase().includes(query.toLowerCase()) + ); + + // returning results object containing the array of the filtered posts + // and number of filtered posts found + const result = { + matchCount: filteredPosts.length, + posts: filteredPosts // array of arrays of post objects + }; + + return result; + + }; +}; From a84ec02c9c86c1f940129e68db81b727308e45c6 Mon Sep 17 00:00:00 2001 From: Najoud Date: Thu, 26 Sep 2024 02:12:09 +0300 Subject: [PATCH 2/4] fixing lint errors in src/topics/search.js --- src/topics/search.js | 71 +++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/src/topics/search.js b/src/topics/search.js index 2c47c21759..937173a094 100644 --- a/src/topics/search.js +++ b/src/topics/search.js @@ -1,48 +1,31 @@ 'use strict'; -const _ = require('lodash'); -const nconf = require('nconf'); - -const meta = require('../meta'); -const plugins = require('../plugins'); -const db = require('../database'); -const posts = require('../posts'); - - module.exports = function (Topics) { - - Topics.search = async function (data) { - - // extracting the attributes of data object - 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 - - // storing the posts associated with a topic - const set = `tid:${tid}:posts`; - - // fetch topic attributes using getTopicData - const topicData = await Topics.getTopicData(tid); - - // using getTopicPosts function in src/topics/posts.js - // to retrieve all the posts in the topic (by making range 0 to -1) - const postsData = await Topics.getTopicPosts(topicData, set, 0, -1, uid); - - // looping through retrieved postsdata array - // and inspecting each post object for the search query - const filteredPosts = postsData.filter(post => - // comparing both in lowercase - post.content.toLowerCase().includes(query.toLowerCase()) - ); - - // returning results object containing the array of the filtered posts - // and number of filtered posts found - const result = { - matchCount: filteredPosts.length, - posts: filteredPosts // array of arrays of post objects - }; - - return result; - - }; + Topics.search = async function (data) { + // Extracting the attributes of data object + 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 + + // Storing the posts associated with a topic + const set = `tid:${tid}:posts`; + + // Getting topic attributes using getTopicData + const topicData = await Topics.getTopicData(tid); + + // Using getTopicPosts function in src/topics/posts.js + // to retrieve all the posts in the topic (by making range 0 to -1) + const postsData = await Topics.getTopicPosts(topicData, set, 0, -1, uid); + + // Looping through retrieved postsData array + // and inspecting each post object for the search query + const filteredPosts = postsData.filter(post => post.content.toLowerCase().includes(query.toLowerCase())); + + // Returning results object containing the array of filtered posts + // and number of filtered posts found + return { + matchCount: filteredPosts.length, + posts: filteredPosts, // Array of post objects + }; + }; }; From fd167c461e8d4311df054c70a68e5b93c3a9c0ad Mon Sep 17 00:00:00 2001 From: Najoud Date: Thu, 26 Sep 2024 13:43:00 +0300 Subject: [PATCH 3/4] src/topics/search.js: added pagination and same return type as user search --- src/topics/search.js | 50 ++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/topics/search.js b/src/topics/search.js index 937173a094..f95da7ef02 100644 --- a/src/topics/search.js +++ b/src/topics/search.js @@ -1,31 +1,49 @@ 'use strict'; module.exports = function (Topics) { - Topics.search = async function (data) { - // Extracting the attributes of data object - 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 + Topics.mysearch = async function (data) { + console.log('in topic search'); + console.log('data:', data); - // Storing the posts associated with a topic - const set = `tid:${tid}:posts`; + // extracting the attributes of the data object + const query = data.query || ''; // the search term + const tid = data.tid || 1; // topic id to search in + const page = data.page || 1; // pagination: current page + const uid = data.uid || 0; // user id of the searcher + const paginate = data.hasOwnProperty('paginate') ? data.paginate : true; - // Getting topic attributes using getTopicData - const topicData = await Topics.getTopicData(tid); + const startTime = process.hrtime(); - // Using getTopicPosts function in src/topics/posts.js + // storing the posts associated with a topic + const set = `tid:${tid}:posts`; + // getting topic attributes using getTopicData + const topicData = await Topics.getTopicData(tid); + // using getTopicPosts function in src/topics/posts.js // to retrieve all the posts in the topic (by making range 0 to -1) const postsData = await Topics.getTopicPosts(topicData, set, 0, -1, uid); - // Looping through retrieved postsData array + // filtering posts based on query + // looping through retrieved postsData array // and inspecting each post object for the search query - const filteredPosts = postsData.filter(post => post.content.toLowerCase().includes(query.toLowerCase())); + let filteredPosts = postsData.filter(post => post.content.toLowerCase().includes(query.toLowerCase())); - // Returning results object containing the array of filtered posts - // and number of filtered posts found - return { + const searchResult = { matchCount: filteredPosts.length, - posts: filteredPosts, // Array of post objects }; + + if (paginate) { + const resultsPerPage = data.resultsPerPage || 10; // default results per page + const start = Math.max(0, page - 1) * resultsPerPage; + const stop = start + resultsPerPage; + searchResult.pageCount = Math.ceil(filteredPosts.length / resultsPerPage); + filteredPosts = filteredPosts.slice(start, stop); + } + + // timing the search process + searchResult.timing = (process.hrtime(startTime)[1] / 1e6).toFixed(2); // ms timing + + // returning filtered posts and match count + searchResult.posts = filteredPosts; + return searchResult; }; }; From 4f4ac1539adf5616dc1b70f74b3d09561320eb7f Mon Sep 17 00:00:00 2001 From: Najoud Date: Thu, 26 Sep 2024 14:16:24 +0300 Subject: [PATCH 4/4] renaming Topics.search to Topics.postSearch and adding commented out test code in test/topics.js --- src/topics/search.js | 2 +- test/topics.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/topics/search.js b/src/topics/search.js index f95da7ef02..e05bd8b084 100644 --- a/src/topics/search.js +++ b/src/topics/search.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = function (Topics) { - Topics.mysearch = async function (data) { + Topics.postSearch = async function (data) { console.log('in topic search'); console.log('data:', data); diff --git a/test/topics.js b/test/topics.js index 8a32e445f5..8d99af493b 100644 --- a/test/topics.js +++ b/test/topics.js @@ -53,6 +53,67 @@ describe('Topic\'s', () => { }; }); + /* describe('.search()', () => { + // let topic; + let adminUid; + + before(async () => { + adminUid = await User.create({ username: 'admin' }); + const categoryObj = await categories.create({ + name: 'Test Category', + description: 'Test category created by testing script', + }); + + topic = { + userId: adminUid, + categoryId: categoryObj.cid, + title: 'Test Topic Title', + content: 'The content of test topic', + }; + + // create a topic first + topic.tid = await topics.create({ + uid: adminUid, + title: topic.title, + content: topic.content, + cid: categoryObj.cid, + }); + + // create posts in the topic for testing + const post1 = await posts.create({ uid: adminUid, tid: topic.tid, content: 'First post in topic' }); + posts.create({ uid: adminUid, tid: topic.tid, content: 'Another post about testing' }); + posts.create({ uid: adminUid, tid: topic.tid, content: 'Post with a different keyword' }); + + // checking if post has been created properly + console.log('post1:', post1); + }); + + it('should return all posts if the query is empty', async () => { + console.log('*****************************'); + // checking if topic and topic.tid created properly + console.log('topic:', topic); + console.log('topic.tid:', topic.tid); + topic.postsearch({ query: '' }, (err, searchData) => { + console.log('searchData:', searchData); + assert.ifError(err); + assert.equal(searchData.matchCount, 3); + assert.equal(searchData.posts.length, 3); + }); + }); + + it('should return all posts matching user query', async () => { + console.log('*****************************'); + console.log('topic:', topic); + console.log('topic.tid:', topic.tid); + topic.postsearch({ query: 'another' }, (err, searchData) => { + console.log('searchData:', searchData); + assert.ifError(err); + assert.equal(searchData.matchCount, 1); + assert.equal(searchData.posts.length, 1); + }); + }); + }); */ + describe('.post', () => { it('should fail to create topic with invalid data', async () => { try {