Skip to content

Commit

Permalink
Merge pull request #34 from CMU-17313Q/adding-search-functionality
Browse files Browse the repository at this point in the history
Working on the backend for the functionality of searching through documents - User Story 1
  • Loading branch information
zanzoonh authored Oct 20, 2024
2 parents 62f6e1c + 2393d0b commit f998872
Show file tree
Hide file tree
Showing 28 changed files with 302 additions and 20 deletions.
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

0 comments on commit f998872

Please sign in to comment.