@@ -7,6 +7,7 @@ import { loginWithEnv } from "./bsky-auth";
7
7
8
8
// let's be nice
9
9
const DEFAULT_LIMIT = 40 ;
10
+ const QUOTED_PHRASE_REGEX = / " ( [ ^ " ] + ) " / g;
10
11
11
12
export async function feedGeneratorWellKnown ( request ) {
12
13
let host = request . headers . get ( "Host" ) ;
@@ -105,11 +106,40 @@ function fromUser(query, queryIdx, response, params) {
105
106
return docs ;
106
107
}
107
108
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 ) {
109
124
let docs = [ ] ;
125
+ let normalizedQuotedPhrases = getNormalizedQuotedPhrases ( query ) ;
110
126
if ( Array . isArray ( response ) ) {
111
127
for ( let itemIdx = 0 ; itemIdx < response . length ; itemIdx ++ ) {
112
128
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
+ }
113
143
let did = searchResult . user . did ;
114
144
let rkey = searchResult . tid . split ( "/" ) . slice ( - 1 ) [ 0 ] ;
115
145
let timestamp = searchResult . post . createdAt ;
@@ -289,7 +319,7 @@ export async function getFeedSkeleton(request, env) {
289
319
} ;
290
320
let response = await searchPost ( query . value , searchParams ) ;
291
321
if ( response !== null ) {
292
- items . push ( ...fromSearch ( queryIdx , response , searchParams ) ) ;
322
+ items . push ( ...fromSearch ( query , queryIdx , response , searchParams ) ) ;
293
323
}
294
324
} else if ( query . type === "user" ) {
295
325
let cursor = objSafeGet ( queryCursor , "cursor" , null ) ;
0 commit comments