about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-03-03 16:30:33 -0600
committerGitHub <noreply@github.com>2023-03-03 16:30:33 -0600
commit8e22ce8e2a4739e5e9e90589764aff55ee73a5db (patch)
tree0a45b9dc84190af387fbb408c16bdb79daed2e56 /src/lib
parentd74ff9c2199ed9487a90413d076459638b07c470 (diff)
downloadvoidsky-8e22ce8e2a4739e5e9e90589764aff55ee73a5db.tar.zst
Turn links to posts into quote posts (#262)
* Turn links to posts into quote posts

* Fix lint
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/link-meta/bsky.ts30
-rw-r--r--src/lib/strings/url-helpers.ts12
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 {