From d24b7aa95685459e8767a3b1913071345ed03169 Mon Sep 17 00:00:00 2001 From: Rogerio Chaves Date: Tue, 21 Apr 2020 08:13:33 +0200 Subject: [PATCH] Add communities to search results --- app/lib/express.js | 12 ++++++++---- app/lib/metrics.js | 10 +++++++--- app/lib/queries.js | 39 +++++++++++++++++++++++++++++++++++---- app/views/search.ejs | 16 ++++++++++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/app/lib/express.js b/app/lib/express.js index db43117..4a21da3 100644 --- a/app/lib/express.js +++ b/app/lib/express.js @@ -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) => { diff --git a/app/lib/metrics.js b/app/lib/metrics.js index d628dd6..4e5668e 100644 --- a/app/lib/metrics.js +++ b/app/lib/metrics.js @@ -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", }), }; diff --git a/app/lib/queries.js b/app/lib/queries.js index a4abcad..4837661 100644 --- a/app/lib/queries.js +++ b/app/lib/queries.js @@ -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, diff --git a/app/views/search.ejs b/app/views/search.ejs index 152a89f..a67e6d1 100644 --- a/app/views/search.ejs +++ b/app/views/search.ejs @@ -5,12 +5,28 @@ <% } else { %>

Results for "<%= query %>"

+

People

+ + <% if (people.length == 0) { %> +

No results found

+ <% } %> <% people.map(person => { %>
<%= person.content.name %>
<% }) %> + +

Communities

+ + <% if (communities.length == 0) { %> +

No results found

+ <% } %> + <% communities.map(community => { %> + + #<%= community %> + + <% }) %> <% } %> <%- include('_footer') %> \ No newline at end of file