about summary refs log tree commit diff
path: root/src/lib/link-meta/link-meta.ts
diff options
context:
space:
mode:
authorOllie H <renahlee@outlook.com>2023-05-30 18:08:49 -0700
committerGitHub <noreply@github.com>2023-05-30 20:08:49 -0500
commitc0ca27b7ce6518758323e2922e66a3e3a008dbce (patch)
treedd3611dff125cc3013e690a3fd19c20703dae5bb /src/lib/link-meta/link-meta.ts
parent2018558585fd7b955598c8584751bbdd9c6ff227 (diff)
downloadvoidsky-c0ca27b7ce6518758323e2922e66a3e3a008dbce.tar.zst
Use proxy for fetching link meta (#716)
* Use proxy for fetching link meta

* Remove link meta test due to hitting proxy

* setup different staging and prod proxy URLs

---------

Co-authored-by: Ansh Nanda <anshnanda10@gmail.com>
Co-authored-by: Paul Frazee <pfrazee@gmail.com>
Diffstat (limited to 'src/lib/link-meta/link-meta.ts')
-rw-r--r--src/lib/link-meta/link-meta.ts38
1 files changed, 20 insertions, 18 deletions
diff --git a/src/lib/link-meta/link-meta.ts b/src/lib/link-meta/link-meta.ts
index 6c4ad5384..6863798b4 100644
--- a/src/lib/link-meta/link-meta.ts
+++ b/src/lib/link-meta/link-meta.ts
@@ -1,8 +1,7 @@
-import he from 'he'
 import {isBskyAppUrl} from '../strings/url-helpers'
 import {RootStoreModel} from 'state/index'
 import {extractBskyMeta} from './bsky'
-import {extractHtmlMeta} from './html'
+import {LINK_META_PROXY} from 'lib/constants'
 
 export enum LikelyType {
   HTML,
@@ -54,26 +53,29 @@ export async function getLinkMeta(
   try {
     const controller = new AbortController()
     const to = setTimeout(() => controller.abort(), timeout || 5e3)
-    const httpRes = await fetch(url, {
-      headers: {accept: 'text/html'},
-      signal: controller.signal,
-    })
-    const httpResBody = await httpRes.text()
+
+    const response = await fetch(
+      `${LINK_META_PROXY(
+        store.session.currentSession?.service || '',
+      )}${encodeURIComponent(url)}`,
+    )
+
+    const body = await response.json()
     clearTimeout(to)
-    const httpResMeta = extractHtmlMeta({
-      html: httpResBody,
-      hostname: urlp?.hostname,
-      pathname: urlp?.pathname,
-    })
-    meta.title = httpResMeta.title ? he.decode(httpResMeta.title) : undefined
-    meta.description = httpResMeta.description
-      ? he.decode(httpResMeta.description)
-      : undefined
-    meta.image = httpResMeta.image
+
+    const {description, error, image, title} = body
+
+    if (error !== '') {
+      throw new Error(error)
+    }
+
+    meta.description = description
+    meta.image = image
+    meta.title = title
   } catch (e) {
     // failed
     console.error(e)
-    meta.error = 'Failed to fetch link'
+    meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link'
   }
 
   return meta