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

Keyword Search Backend - Khadija and Retaj #40

Open
wants to merge 2 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 modified dump.rdb
Binary file not shown.

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

1 change: 1 addition & 0 deletions public/src/client/chats/user-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ define('forum/chats/user-search', [
function doSearch() {
const chatsListEl = $('[component="chat/search/list"]');
const username = components.get('chat/search').val();
console.log('in this file');
if (!username) {
return clearInputAndResults(chatsListEl);
}
Expand Down
94 changes: 94 additions & 0 deletions public/src/client/topic/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

define('forum/topics/list', [
'forum/infinitescroll', 'benchpress', 'api', 'bootbox', 'alerts', 'utils', 'socket',
], function (infinitescroll, Benchpress, api, bootbox, alerts, utils, socket) { // Added 'utils' and 'socket'
const Topics = {};

Topics.init = function () {
infinitescroll.init(Topics.loadMoreTopics);

// Topic creation
$('button[data-action="new"]').on('click', function () {
bootbox.prompt('[[topics:new-topic.topic-name]]', function (title) {
if (title && title.length) {
api.post('/topics', {
title: title,
}).then((res) => {
ajaxify.go('topic/' + res.slug);
}).catch(alerts.error);
}
});
});

const params = utils.params();
$('#search-sort').val(params.sort || 'alpha');

// Topic searching
$('#search-text').on('keyup', Topics.search);
$('#submit-search-button').on('click', Topics.search);
$('#search-sort').on('change', function () {
ajaxify.go('topics?sort=' + $('#search-sort').val());
});
};

Topics.loadMoreTopics = function (direction) {
if (direction < 0) {
return;
}

infinitescroll.loadMore('/topics', {
sort: $('#search-sort').val(),
after: $('[component="topics/container"]').attr('data-nextstart'),
}, function (data, done) {
if (data && data.topics.length) {
Benchpress.render('partials/topics/list', {
topics: data.topics,
}).then(function (html) {
$('#topics-list').append(html);
done();
});
} else {
done();
}

if (data && data.nextStart) {
$('[component="topics/container"]').attr('data-nextstart', data.nextStart);
}
});
};

Topics.search = function () {
const topicsEl = $('#topics-list');
const queryEl = $('#search-text');
const sortEl = $('#search-sort');

socket.emit('topics.search', {
query: queryEl.val(),
options: {
sort: sortEl.val(),
filterHidden: true,
showMembers: true,
hideEphemeralTopics: true,
},
}, function (err, topics) {
if (err) {
return alerts.error(err);
}

// Filter out topics you don't want to display
topics = topics.filter(function (topic) {
return topic.title !== 'off-topic' && topic.title !== 'guests';
});

Benchpress.render('partials/topics/list', {
topics: topics,
}).then(function (html) {
topicsEl.empty().append(html);
});
});
return false;
};

return Topics;
});
1 change: 1 addition & 0 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ usersController.index = async function (req, res, next) {
}
};

// potentially do something similar in controller/topics.js
usersController.search = async function (req, res) {
const searchData = await api.users.search(req, req.query);

Expand Down
41 changes: 41 additions & 0 deletions src/posts/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const db = require('../database');

module.exports = function (Posts) {
Posts.search = async function (query, options) {
if (!query) {
return [];
}

query = String(query).toLowerCase();

const postIds = await db.getSortedSetRangeByScore('posts:pid', 0, -1);
let posts = await Posts.getPostsData(postIds);

posts = posts.filter(post => post.content.toLowerCase().includes(query));

posts = posts.slice(0, 100);

if (options && options.sort) {
posts = Posts.sort(options.sort, posts);
}

return posts;
};

Posts.sort = function (strategy, posts) {
switch (strategy) {
case 'date':
posts.sort((a, b) => b.timestamp - a.timestamp);
break;
case 'alpha':
posts.sort((a, b) => (a.content > b.content ? 1 : -1));
break;
default:
posts.sort((a, b) => b.timestamp - a.timestamp);
}

return posts;
};
};
Empty file added src/topics/search.js
Empty file.