Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 7142c32

Browse files
committed
Support exact phrases
1 parent 0a6c1f1 commit 7142c32

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

cloudflare-worker/bsky-feedgen.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { loginWithEnv } from "./bsky-auth";
77

88
// let's be nice
99
const DEFAULT_LIMIT = 40;
10+
const QUOTED_PHRASE_REGEX = /"([^"]+)"/g;
1011

1112
export async function feedGeneratorWellKnown(request) {
1213
let host = request.headers.get("Host");
@@ -105,11 +106,40 @@ function fromUser(query, queryIdx, response, params) {
105106
return docs;
106107
}
107108

108-
function fromSearch(queryIdx, response, searchParams) {
109+
/**
110+
* Returns a set of normalized (lowercase) quoted phrases
111+
* @param query
112+
* @returns {any[]}
113+
*/
114+
function getNormalizedQuotedPhrases(query) {
115+
let phrases = new Set();
116+
let match;
117+
while ((match = QUOTED_PHRASE_REGEX.exec(query.value)) !== null) {
118+
phrases.add(match[1].toLowerCase());
119+
}
120+
return Array.from(phrases);
121+
}
122+
123+
function fromSearch(query, queryIdx, response, searchParams) {
109124
let docs = [];
125+
let normalizedQuotedPhrases = getNormalizedQuotedPhrases(query);
110126
if (Array.isArray(response)) {
111127
for (let itemIdx = 0; itemIdx < response.length; itemIdx++) {
112128
let searchResult = response[itemIdx];
129+
if (normalizedQuotedPhrases.length > 0) {
130+
// perform a case-insensitive search for all quoted phrases
131+
let matches = true;
132+
let normalizedPostText = searchResult.post.text.toLowerCase();
133+
for (let phrase of normalizedQuotedPhrases) {
134+
if (!normalizedPostText.includes(phrase)) {
135+
matches = false;
136+
break;
137+
}
138+
}
139+
if (!matches) {
140+
continue;
141+
}
142+
}
113143
let did = searchResult.user.did;
114144
let rkey = searchResult.tid.split("/").slice(-1)[0];
115145
let timestamp = searchResult.post.createdAt;
@@ -289,7 +319,7 @@ export async function getFeedSkeleton(request, env) {
289319
};
290320
let response = await searchPost(query.value, searchParams);
291321
if (response !== null) {
292-
items.push(...fromSearch(queryIdx, response, searchParams));
322+
items.push(...fromSearch(query, queryIdx, response, searchParams));
293323
}
294324
} else if (query.type === "user") {
295325
let cursor = objSafeGet(queryCursor, "cursor", null);

0 commit comments

Comments
 (0)