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 automated tests into test/categories to test the search back-end functionality. #59

Open
wants to merge 21 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f3e5dcd
Used group search as a template for topic search file
zanzoonh Sep 26, 2024
f8f0c1c
Added socket function in src/socket.io/topics
zanzoonh Sep 26, 2024
0d59d80
Fixed syntax errors
zanzoonh Sep 26, 2024
cd76d79
branch was causing errors when starting up nodebb, so i had to reset …
zanzoonh Oct 2, 2024
3dd7123
adding missing search functions using the user search as a template
zanzoonh Oct 2, 2024
f59d486
moved client side handleSearch function from public/src/client/topic.…
zanzoonh Oct 2, 2024
ceff7fd
adding in missing functions that are needed in public/src/client/cate…
zanzoonh Oct 2, 2024
ef89581
editing the code mainly in public/src/client/category.js to try and r…
zanzoonh Oct 3, 2024
c1f2183
adding comments and console.logs to test
zanzoonh Oct 3, 2024
dd8a811
fixed routes so that they now call posts controller
zanzoonh Oct 14, 2024
ade1e7d
adding the same code to the topics folder because posts is not return…
zanzoonh Oct 19, 2024
d80cfcb
adding console logs and trying to insert search function in src/categ…
zanzoonh Oct 19, 2024
eb02604
copied search functions over to matching files for categories
zanzoonh Oct 19, 2024
b6108e0
fixed search functions and checked functionality
zanzoonh Oct 19, 2024
6b2a9d5
removing code that isnt used
zanzoonh Oct 20, 2024
6891f0c
removing most comsole.log messages
zanzoonh Oct 20, 2024
0cd5490
adding console.logs to debug tests
zanzoonh Oct 20, 2024
b0008aa
adding function that was accidentally removed
zanzoonh Oct 20, 2024
0d3367f
removing / commenting more console.log statements and other dead code
zanzoonh Oct 20, 2024
2393d0b
removed trailing spaces
zanzoonh Oct 20, 2024
186a64a
adding automated tests
zanzoonh Oct 20, 2024
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 modified dump.rdb
Binary file not shown.
6 changes: 3 additions & 3 deletions node_modules/nodebb-theme-harmony/templates/category.tpl

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.

2 changes: 1 addition & 1 deletion node_modules/nodebb-theme-harmony/templates/users.tpl

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

1 change: 0 additions & 1 deletion public/src/client/categories.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';


define('forum/categories', ['categorySelector'], function (categorySelector) {
const categories = {};

Expand Down
79 changes: 78 additions & 1 deletion public/src/client/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ define('forum/category', [
'hooks',
'alerts',
'api',
], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts, api) {
'benchpress',
], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts, api, Benchpress) {
const Category = {};

$(window).on('action:ajaxify.start', function (ev, data) {
Expand All @@ -19,7 +20,10 @@ define('forum/category', [
}
});

let searchResultCount = 0;

Category.init = function () {
// console.log('entered category public client');
const cid = ajaxify.data.cid;

app.enterRoom('category_' + cid);
Expand All @@ -42,6 +46,8 @@ define('forum/category', [

handleLoadMoreSubcategories();

Category.handleSearch();

categorySelector.init($('[component="category-selector"]'), {
privilege: 'find',
parentCid: ajaxify.data.cid,
Expand Down Expand Up @@ -112,6 +118,56 @@ define('forum/category', [
});
}

Category.handleSearch = function (params) {
// console.log('entered public/src/client/category handleSearch');
searchResultCount = params && params.resultCount;
$('#search-topic').on('keyup', utils.debounce(doSearch, 250));
$('.search select, .search input[type="checkbox"]').on('change', doSearch);
};

function doSearch() {
// console.log('entered public/src/client/category doSearch');
if (!ajaxify.data.template.category) {
return;
}
$('[component="topic/search/icon"]').removeClass('fa-search').addClass('fa-spinner fa-spin');
const title = $('#search-topic').val();
// console.log(title);
const activeSection = getActiveSection();

const query = {
section: activeSection,
page: 1,
};

if (!title) {
return loadPage(query);
}

query.query = title;
query.sortBy = getSortBy();
loadPage(query);
}

function getSortBy() {
let sortBy;
const activeSection = getActiveSection();
if (activeSection === 'sort-posts') {
sortBy = 'postcount';
} else if (activeSection === 'sort-reputation') {
sortBy = 'reputation';
} else if (activeSection === 'users') {
sortBy = 'joindate';
}
return sortBy;
}

function loadPage(query) {
api.get('/api/categoriesss', query)
.then(renderSearchResults)
.catch(alerts.error);
}

Category.toTop = function () {
navigator.scrollTop(0);
};
Expand All @@ -137,5 +193,26 @@ define('forum/category', [
});
}

function getActiveSection() {
return utils.param('section') || '';
}

function renderSearchResults(data) {
Benchpress.render('partials/paginator', { pagination: data.pagination }).then(function (html) {
$('.pagination-container').replaceWith(html);
});

if (searchResultCount) {
data.users = data.users.slice(0, searchResultCount);
}

data.isAdminOrGlobalMod = app.user.isAdmin || app.user.isGlobalMod;
app.parseAndTranslate('post-queue', 'posts', data, function (html) {
$('#category-container').html(html);
html.find('.timeago').timeago();
$('[component="topic/search/icon"]').addClass('fa-search').removeClass('fa-spinner fa-spin');
});
}

return Category;
});
3 changes: 3 additions & 0 deletions public/src/client/groups/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ define('forum/groups/list', [
const queryEl = $('#search-text');
const sortEl = $('#search-sort');

// console.log('entered public/src/client/groups/list groups.search');
// console.log(queryEl);

socket.emit('groups.search', {
query: queryEl.val(),
options: {
Expand Down
2 changes: 2 additions & 0 deletions public/src/client/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ define('forum/topic', [
});

Topic.init = async function () {
// console.log('public src client topic');
const tidChanged = !tid || parseInt(tid, 10) !== parseInt(ajaxify.data.tid, 10);
tid = ajaxify.data.tid;
currentUrl = ajaxify.currentPage;
Expand Down Expand Up @@ -73,6 +74,7 @@ define('forum/topic', [
$(window).on('scroll', utils.debounce(updateTopicTitle, 250));

handleTopicSearch();
Topic.handleSearch();

hooks.fire('action:topic.loaded', ajaxify.data);
};
Expand Down
3 changes: 3 additions & 0 deletions public/src/client/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ define('forum/users', [
};

Users.handleSearch = function (params) {
// console.log('entered public/src/client/users handleSearch');
searchResultCount = params && params.resultCount;
$('#search-user').on('keyup', utils.debounce(doSearch, 250));
$('.search select, .search input[type="checkbox"]').on('change', doSearch);
};

function doSearch() {
// console.log('entered public/src/client/users doSearch');
if (!ajaxify.data.template.users) {
return;
}
Expand Down Expand Up @@ -82,6 +84,7 @@ define('forum/users', [


function loadPage(query) {
// console.log('user load page then api?');
api.get('/api/users', query)
.then(renderSearchResults)
.catch(alerts.error);
Expand Down
2 changes: 2 additions & 0 deletions public/src/modules/categorySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ define('categorySearch', ['alerts', 'bootstrap', 'api'], function (alerts, boots
});

function loadList(search, callback) {
// console.log('caategories loadlist');
// console.log('did it call search??');
api.get('/search/categories', {
search: search,
query: utils.params(),
Expand Down
103 changes: 103 additions & 0 deletions src/api/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,109 @@ categoriesAPI.get = async function (caller, data) {
return category;
};

categoriesAPI.search = async function (caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
const [allowed, isPrivileged] = await Promise.all([
privileges.global.can('search:users', caller.uid),
user.isPrivileged(caller.uid),
]);
let filters = data.filters || [];
filters = Array.isArray(filters) ? filters : [filters];
if (!allowed ||
((
data.searchBy === 'ip' ||
data.searchBy === 'email' ||
filters.includes('banned') ||
filters.includes('flagged')
) && !isPrivileged)
) {
throw new Error('[[error:no-privileges]]');
}

return await categories.searchTopics({
uid: caller.uid,
cid: '2',
start: 0,
stop: 19,
sort: data.sortBy,
settings: {
uid: 1,
showemail: false,
showfullname: false,
openOutgoingLinksInNewTab: false,
dailyDigestFreq: 'off',
usePagination: false,
topicsPerPage: 20,
postsPerPage: 20,
userLang: 'en-GB',
acpLang: 'en-GB',
topicPostSort: 'oldest_to_newest',
categoryTopicSort: 'recently_replied',
followTopicsOnCreate: true,
followTopicsOnReply: false,
upvoteNotifFreq: 'all',
restrictChat: false,
topicSearchEnabled: false,
updateUrlWithPostIndex: true,
bootswatchSkin: '',
homePageRoute: '',
scrollToMyPost: true,
categoryWatchState: 'tracking',
notificationType_upvote: 'notification',
'notificationType_new-topic': 'notification',
'notificationType_new-topic-with-tag': 'notification',
'notificationType_new-topic-in-category': 'notification',
'notificationType_new-reply': 'notification',
'notificationType_post-edit': 'notification',
notificationType_follow: 'notification',
'notificationType_new-chat': 'notification',
'notificationType_new-group-chat': 'notification',
'notificationType_new-public-chat': 'notification',
'notificationType_group-invite': 'notification',
'notificationType_group-leave': 'notification',
'notificationType_group-request-membership': 'notification',
'notificationType_new-reward': 'notification',
notificationType_mention: 'notification',
'notificationType_new-register': 'notification',
'notificationType_post-queue': 'notification',
'notificationType_new-post-flag': 'notification',
'notificationType_new-user-flag': 'notification',
},
query: data.query,
tag: undefined,
targetUid: 0,
category: {
cid: 2,
name: 'General Discussion',
description: 'A place to talk about whatever you want',
descriptionParsed: '<p>A place to talk about whatever you want</p>\n',
icon: 'fa-comments-o',
bgColor: '#59b3d0',
color: '#ffffff',
slug: '2/general-discussion',
parentCid: 0,
topic_count: 5,
post_count: 7,
disabled: 0,
order: 2,
link: '',
numRecentReplies: 1,
class: 'col-md-3 col-6',
imageClass: 'cover',
isSection: 0,
subCategoriesPerPage: 10,
minTags: 0,
maxTags: 5,
postQueue: 0,
totalPostCount: 7,
totalTopicCount: 5,
tagWhitelist: [],
},
});
};

categoriesAPI.create = async function (caller, data) {
await hasAdminPrivilege(caller.uid);

Expand Down
35 changes: 35 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const socketHelpers = require('../socket.io/helpers');

const postsAPI = module.exports;

console.log('posts api gets called');

postsAPI.get = async function (caller, data) {
const [userPrivileges, post, voted] = await Promise.all([
privileges.posts.get([data.pid], caller.uid),
Expand Down Expand Up @@ -512,3 +514,36 @@ postsAPI.getReplies = async (caller, { pid }) => {

return postData;
};

postsAPI.search = async function (caller, data) {
console.log('entered src/api/posts.js');
// console.log(caller);
// console.log(data);
if (!data) {
throw new Error('[[error:invalid-data]]');
}
const [allowed, isPrivileged] = await Promise.all([
privileges.global.can('search:users', caller.uid),
user.isPrivileged(caller.uid),
]);
let filters = data.filters || [];
filters = Array.isArray(filters) ? filters : [filters];
if (!allowed ||
((
data.searchBy === 'ip' ||
data.searchBy === 'email' ||
filters.includes('banned') ||
filters.includes('flagged')
) && !isPrivileged)
) {
throw new Error('[[error:no-privileges]]');
}
return await posts.search({
uid: caller.uid,
query: data.query,
searchBy: data.searchBy || 'title',
page: data.page || 1,
sortBy: data.sortBy || 'lastonline',
filters: filters,
});
};
3 changes: 1 addition & 2 deletions src/api/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const socketHelpers = require('../socket.io/helpers');

const topicsAPI = module.exports;


topicsAPI._checkThumbPrivileges = async function ({ tid, uid }) {
// req.params.tid could be either a tid (pushing a new thumb to an existing topic)
// or a post UUID (a new topic being composed)
Expand Down Expand Up @@ -298,5 +299,3 @@ topicsAPI.bump = async (caller, { tid }) => {
await topics.markAsUnreadForAll(tid);
topics.pushUnreadCount(caller.uid);
};


2 changes: 2 additions & 0 deletions src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ async function canDeleteUids(uids) {
}

usersAPI.search = async function (caller, data) {
// console.log(caller);
// console.log(data);
if (!data) {
throw new Error('[[error:invalid-data]]');
}
Expand Down
Loading
Loading