about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMary <pineapplecreamcheese@skiff.com>2024-01-12 09:56:14 +0700
committerMary <pineapplecreamcheese@skiff.com>2024-01-12 09:56:14 +0700
commit7f4d3dc999b130cd8552ed97c0c90cf83cbdd13c (patch)
tree32066cda290a232129d94801ecbd5ce0f65ec586
parent17f7c36edfcacfad5a56b72628e3762de04f5d0d (diff)
downloadvoidsky-7f4d3dc999b130cd8552ed97c0c90cf83cbdd13c.tar.zst
feat: from:me search syntax
-rw-r--r--src/lib/strings/helpers.ts24
-rw-r--r--src/view/screens/Search/Search.tsx9
2 files changed, 31 insertions, 2 deletions
diff --git a/src/lib/strings/helpers.ts b/src/lib/strings/helpers.ts
index 381ae32f3..e2abe9019 100644
--- a/src/lib/strings/helpers.ts
+++ b/src/lib/strings/helpers.ts
@@ -37,3 +37,27 @@ export function countLines(str: string | undefined): number {
   if (!str) return 0
   return str.match(/\n/g)?.length ?? 0
 }
+
+// Augments search query with additional syntax like `from:me`
+export function augmentSearchQuery(query: string, {did}: {did?: string}) {
+  // Don't do anything if there's no DID
+  if (!did) {
+    return query
+  }
+
+  // We don't want to replace substrings that are being "quoted" because those
+  // are exact string matches, so what we'll do here is to split them apart
+
+  // Even-indexed strings are unquoted, odd-indexed strings are quoted
+  const splits = query.split(/("(?:[^"\\]|\\.)*")/g)
+
+  return splits
+    .map((str, idx) => {
+      if (idx % 2 === 0) {
+        return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`)
+      }
+
+      return str
+    })
+    .join('')
+}
diff --git a/src/view/screens/Search/Search.tsx b/src/view/screens/Search/Search.tsx
index 8e7e204ad..28df298e0 100644
--- a/src/view/screens/Search/Search.tsx
+++ b/src/view/screens/Search/Search.tsx
@@ -47,6 +47,7 @@ import {useSetMinimalShellMode, useSetDrawerSwipeDisabled} from '#/state/shell'
 import {isWeb} from '#/platform/detection'
 import {listenSoftReset} from '#/state/events'
 import {s} from '#/lib/styles'
+import {augmentSearchQuery} from '#/lib/strings/helpers'
 
 function Loader() {
   const pal = usePalette('default')
@@ -315,9 +316,13 @@ export function SearchScreenInner({
   const pal = usePalette('default')
   const setMinimalShellMode = useSetMinimalShellMode()
   const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
-  const {hasSession} = useSession()
+  const {hasSession, currentAccount} = useSession()
   const {isDesktop} = useWebMediaQueries()
 
+  const augmentedQuery = React.useMemo(() => {
+    return augmentSearchQuery(query || '', {did: currentAccount?.did})
+  }, [query, currentAccount])
+
   const onPageSelected = React.useCallback(
     (index: number) => {
       setMinimalShellMode(false)
@@ -338,7 +343,7 @@ export function SearchScreenInner({
         )}
         initialPage={0}>
         <View>
-          <SearchScreenPostResults query={query} />
+          <SearchScreenPostResults query={augmentedQuery} />
         </View>
         <View>
           <SearchScreenUserResults query={query} />