Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from:me search syntax #2044

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/bsky/src/api/app/bsky/feed/searchPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const skeleton = async (
ctx: Context,
): Promise<SkeletonState> => {
const res = await ctx.searchAgent.api.app.bsky.unspecced.searchPostsSkeleton({
q: params.q,
q: augmentSearchQuery(params.q, { viewer: params.viewer }),
cursor: params.cursor,
limit: params.limit,
})
Expand Down Expand Up @@ -111,6 +111,33 @@ const presentation = (state: HydrationState, ctx: Context) => {
return { posts: postViews, cursor: state.cursor, hitsTotal: state.hitsTotal }
}

// Adds additional search syntax like `from:me`
const augmentSearchQuery = (
query: string,
{ viewer }: { viewer: string | null },
) => {
// Don't do anything if there's no viewer
if (!viewer) {
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${viewer}$2`)
}

return str
})
.join('')
}

type Context = {
db: Database
feedService: FeedService
Expand Down
Loading