diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/link-meta/bsky.ts | 13 | ||||
-rw-r--r-- | src/lib/strings/helpers.ts | 24 | ||||
-rw-r--r-- | src/lib/strings/rich-text-helpers.ts | 4 |
3 files changed, 35 insertions, 6 deletions
diff --git a/src/lib/link-meta/bsky.ts b/src/lib/link-meta/bsky.ts index 322b02332..c1fbb34b3 100644 --- a/src/lib/link-meta/bsky.ts +++ b/src/lib/link-meta/bsky.ts @@ -5,6 +5,7 @@ import {LikelyType, LinkMeta} from './link-meta' import {convertBskyAppUrlIfNeeded, makeRecordUri} from '../strings/url-helpers' import {ComposerOptsQuote} from 'state/shell/composer' import {useGetPost} from '#/state/queries/post' +import {useFetchDid} from '#/state/queries/handle' // TODO // import {Home} from 'view/screens/Home' @@ -120,11 +121,13 @@ export async function getPostAsQuote( export async function getFeedAsEmbed( agent: BskyAgent, + fetchDid: ReturnType<typeof useFetchDid>, url: string, ): Promise<apilib.ExternalEmbedDraft> { url = convertBskyAppUrlIfNeeded(url) - const [_0, user, _1, rkey] = url.split('/').filter(Boolean) - const feed = makeRecordUri(user, 'app.bsky.feed.generator', rkey) + const [_0, handleOrDid, _1, rkey] = url.split('/').filter(Boolean) + const did = await fetchDid(handleOrDid) + const feed = makeRecordUri(did, 'app.bsky.feed.generator', rkey) const res = await agent.app.bsky.feed.getFeedGenerator({feed}) return { isLoading: false, @@ -146,11 +149,13 @@ export async function getFeedAsEmbed( export async function getListAsEmbed( agent: BskyAgent, + fetchDid: ReturnType<typeof useFetchDid>, url: string, ): Promise<apilib.ExternalEmbedDraft> { url = convertBskyAppUrlIfNeeded(url) - const [_0, user, _1, rkey] = url.split('/').filter(Boolean) - const list = makeRecordUri(user, 'app.bsky.graph.list', rkey) + const [_0, handleOrDid, _1, rkey] = url.split('/').filter(Boolean) + const did = await fetchDid(handleOrDid) + const list = makeRecordUri(did, 'app.bsky.graph.list', rkey) const res = await agent.app.bsky.graph.getList({list}) return { isLoading: false, 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/lib/strings/rich-text-helpers.ts b/src/lib/strings/rich-text-helpers.ts index 08971ca03..662004599 100644 --- a/src/lib/strings/rich-text-helpers.ts +++ b/src/lib/strings/rich-text-helpers.ts @@ -1,7 +1,7 @@ import {AppBskyRichtextFacet, RichText} from '@atproto/api' import {linkRequiresWarning} from './url-helpers' -export function richTextToString(rt: RichText): string { +export function richTextToString(rt: RichText, loose: boolean): string { const {text, facets} = rt if (!facets?.length) { @@ -19,7 +19,7 @@ export function richTextToString(rt: RichText): string { const requiresWarning = linkRequiresWarning(href, text) - result += !requiresWarning ? href : `[${text}](${href})` + result += !requiresWarning ? href : loose ? `[${text}](${href})` : text } else { result += segment.text } |