diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/link-meta/bsky.ts | 30 | ||||
-rw-r--r-- | src/lib/strings/url-helpers.ts | 12 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/link-meta/bsky.ts b/src/lib/link-meta/bsky.ts index fba41260d..c9c2ed31a 100644 --- a/src/lib/link-meta/bsky.ts +++ b/src/lib/link-meta/bsky.ts @@ -3,6 +3,7 @@ import {match as matchRoute} from 'view/routes' import {convertBskyAppUrlIfNeeded, makeRecordUri} from '../strings/url-helpers' import {RootStoreModel} from 'state/index' import {PostThreadViewModel} from 'state/models/post-thread-view' +import {ComposerOptsQuote} from 'state/models/shell-ui' import {Home} from 'view/screens/Home' import {Search} from 'view/screens/Search' @@ -97,3 +98,32 @@ export async function extractBskyMeta( return meta } + +export async function getPostAsQuote( + store: RootStoreModel, + url: string, +): Promise<ComposerOptsQuote> { + url = convertBskyAppUrlIfNeeded(url) + const [_0, user, _1, rkey] = url.split('/').filter(Boolean) + const threadUri = makeRecordUri(user, 'app.bsky.feed.post', rkey) + + const threadView = new PostThreadViewModel(store, { + uri: threadUri, + depth: 0, + }) + await threadView.setup() + if (!threadView.thread || threadView.notFound) { + throw new Error('Not found') + } + return { + uri: threadView.thread.post.uri, + cid: threadView.thread.post.cid, + text: threadView.thread.postRecord?.text || '', + indexedAt: threadView.thread.post.indexedAt, + author: { + handle: threadView.thread.post.author.handle, + displayName: threadView.thread.post.author.displayName, + avatar: threadView.thread.post.author.avatar, + }, + } +} diff --git a/src/lib/strings/url-helpers.ts b/src/lib/strings/url-helpers.ts index a149f49c3..1fa30a3ef 100644 --- a/src/lib/strings/url-helpers.ts +++ b/src/lib/strings/url-helpers.ts @@ -66,6 +66,18 @@ export function isBskyAppUrl(url: string): boolean { return url.startsWith('https://bsky.app/') } +export function isBskyPostUrl(url: string): boolean { + if (isBskyAppUrl(url)) { + try { + const urlp = new URL(url) + return /profile\/(?<name>[^/]+)\/post\/(?<rkey>[^/]+)/i.test( + urlp.pathname, + ) + } catch {} + } + return false +} + export function convertBskyAppUrlIfNeeded(url: string): string { if (isBskyAppUrl(url)) { try { |