backend logic drafted, moving to frontend

This commit is contained in:
Daniel Avila 2023-03-17 22:20:36 -04:00
parent 4f5ee8b198
commit 610cba4a60
8 changed files with 237 additions and 72 deletions

View file

@ -1,8 +1,8 @@
const mergeSort = require('./mergeSort');
function reduceHits(hits) {
function reduceMessages(hits) {
const counts = {};
for (const hit of hits) {
if (!counts[hit.conversationId]) {
counts[hit.conversationId] = 1;
@ -10,17 +10,47 @@ function reduceHits(hits) {
counts[hit.conversationId]++;
}
}
const result = [];
for (const [conversationId, count] of Object.entries(counts)) {
result.push({
conversationId,
count
});
}
return mergeSort(result, (a, b) => b.count - a.count);
}
module.exports = reduceHits;
function reduceHits(hits, titles = []) {
const counts = {};
const titleMap = {};
const convos = [...hits, ...titles];
for (const convo of convos) {
if (!counts[convo.conversationId]) {
counts[convo.conversationId] = 1;
} else {
counts[convo.conversationId]++;
}
if (convo.title) {
titleMap[convo.conversationId] = convo._formatted.title;
}
}
const result = [];
for (const [conversationId, count] of Object.entries(counts)) {
result.push({
conversationId,
count,
title: titleMap[conversationId] ? titleMap[conversationId] : null
});
}
return mergeSort(result, (a, b) => b.count - a.count);
}
module.exports = { reduceMessages, reduceHits };