Add communities to search results

This commit is contained in:
Rogerio Chaves 2020-04-21 08:13:33 +02:00
parent 3b06e741bd
commit d24b7aa956
No known key found for this signature in database
GPG Key ID: E6AF5440509B1D94
4 changed files with 66 additions and 11 deletions

View File

@ -507,13 +507,17 @@ router.get("/search", async (req, res) => {
const query = req.query.query;
let people = [];
let results = {
people: [],
communities: [],
};
if (query.length >= 3) {
people = await queries.searchPeople(ssbServer, query);
metrics.searchResults.observe(people.length);
results = await queries.search(ssbServer, query);
metrics.searchResultsPeople.observe(results.people.length);
metrics.searchResultsCommunities.observe(results.communities.length);
}
res.render("search", { people, query });
res.render("search", { ...results, query });
});
router.get("/blob/*", (req, res) => {

View File

@ -33,8 +33,12 @@ module.exports = {
name: "social_ssb_progress_total",
help: "Tracks ssb syncing progress total",
}),
searchResults: new Summary({
name: "social_search_results",
help: "Amount of results returned from search",
searchResultsPeople: new Summary({
name: "social_search_results_people",
help: "Amount of people results returned from search",
}),
searchResultsCommunities: new Summary({
name: "social_search_results_communities",
help: "Amount of communities results returned from search",
}),
};

View File

@ -188,7 +188,7 @@ const getSecretMessages = async (ssbServer, profile) => {
return chatList;
};
const searchPeople = async (ssbServer, search) => {
const search = async (ssbServer, search) => {
debugPeople("Fetching");
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
@ -219,16 +219,47 @@ const searchPeople = async (ssbServer, search) => {
],
}),
pull.filter((msg) => {
if (!msg.value.content) return;
const normalizedName = msg.value.content.name
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
return msg.value.content && searchRegex.exec(normalizedName);
return searchRegex.exec(normalizedName);
}),
paramap(mapProfiles(ssbServer))
);
const communitiesPosts = await promisePull(
ssbServer.query.read({
reverse: true,
query: [
{
$filter: {
value: {
private: { $not: true },
content: {
type: "post",
channel: { $truthy: true },
},
},
},
},
],
limit: 3000,
})
);
const communities = Array.from(
new Set(communitiesPosts.map((p) => p.value.content.channel))
).filter((name) => {
const normalizedName = name
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
return searchRegex.exec(normalizedName);
});
debugPeople("Done");
return Object.values(mapValues(people));
return { people: Object.values(mapValues(people)), communities };
};
const getFriends = async (ssbServer, profile) => {
@ -498,7 +529,7 @@ setInterval(() => {
module.exports = {
mapProfiles,
getPosts,
searchPeople,
search,
getFriends,
getAllEntries,
getProfile,

View File

@ -5,12 +5,28 @@
<% } else { %>
<h2>Results for "<%= query %>"</h2>
<h3>People</h3>
<% if (people.length == 0) { %>
<p>No results found</p>
<% } %>
<% people.map(person => { %>
<a class="link-block" href="<%= profileUrl(person.author) %>">
<img class="link-profile-pic" src="<%= profileImageUrl(person.authorProfile) %>" />
<div><%= person.content.name %></div>
</a>
<% }) %>
<h3 style="padding-top: 20px">Communities</h3>
<% if (communities.length == 0) { %>
<p>No results found</p>
<% } %>
<% communities.map(community => { %>
<a href="/communities/<%= community %>" class="link-block" style="display: inline-block">
#<%= community %>
</a>
<% }) %>
<% } %>
<%- include('_footer') %>