fix: restore cross-message text search

5c62cf4a6ef7d3b73d9ac746c2b08e1ab026e8a9

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +11 -7Ignore whitespace
src/endpoints/chats.js+11 -7
@@ -353,7 +353,7 @@ async function checkChatIntegrity(filePath, integritySlug) {
353353 * @param {ChatMatchFunction|null} matcher - Optional function to match messages
354354 * @returns {Promise<ChatInfo>}
355355 *
356356 * @typedef {(texttextArray: string[]) => boolean} ChatMatchFunction
357357 */
358358export async function getChatInfo(pathToFile, additionalData = {}, withMetadata = false, matcher = null) {
359359 return new Promise(async (res) => {
@@ -386,6 +386,7 @@ export async function getChatInfo(pathToFile, additionalData = {}, withMetadata
386386 let lastLine;
387387 let itemCounter = 0;
388388 let hasAnyMatch = false;
389+ let matchBuffer = [];
389390 rl.on('line', (line) => {
390391 if (withMetadata && itemCounter === 0) {
391392 const jsonData = tryParse(line);
@@ -396,8 +397,12 @@ export async function getChatInfo(pathToFile, additionalData = {}, withMetadata
396397 // Skip matching if any match was already found
397398 if (hasMatcher && !hasAnyMatch && itemCounter > 0) {
398399 const jsonData = tryParse(line);
399400 if (jsonData && matcher(jsonData.mes || '')) {
400- hasAnyMatch = true;
401+ matchBuffer.push(jsonData.mes || '');
402+ if (matcher(matchBuffer)) {
403+ hasAnyMatch = true;
404+ matchBuffer = [];
405+ }
401406 }
402407 }
403408 itemCounter++;
@@ -910,18 +915,17 @@ router.post('/search', validateAvatarUrlMiddleware, async function (request, res
910915 const fragments = query ? query.trim().toLowerCase().split(/\s+/).filter(x => x) : [];
911916
912917 /** @type {ChatMatchFunction} */
913918 const hasTextMatch = (texttextArray) => {
914919 if (fragments.length === 0) {
915920 return true;
916921 }
917- const loweredText = String(text).toLowerCase();
922+ return fragments.every(fragment => textArray.some(text => String(text ?? '').toLowerCase().includes(fragment)));
918- return fragments.every(fragment => loweredText.includes(fragment));
919923 };
920924
921925 for (const chatFile of chatFiles) {
922926 const matcher = query ? hasTextMatch : null;
923927 const chatInfo = await getChatInfo(chatFile, {}, false, matcher);
924928 const hasMatch = chatInfo.match || hasTextMatch([chatInfo.file_id ?? '']) || chatInfo.match;
925929
926930 // Skip corrupted or invalid chat files
927931 if (!chatInfo.file_name) {