Turn links to posts into quote posts (#262)

* Turn links to posts into quote posts

* Fix lint
This commit is contained in:
Paul Frazee 2023-03-03 16:30:33 -06:00 committed by GitHub
parent d74ff9c219
commit 8e22ce8e2a
3 changed files with 73 additions and 10 deletions

View file

@ -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,
},
}
}

View file

@ -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 {