Skip to content

Commit

Permalink
Include hydrated responses for other records
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Oct 21, 2024
1 parent 6df004a commit 0a7640d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
54 changes: 27 additions & 27 deletions src/lib/api/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AppBskyFeedDefs,
AppBskyGraphStarterpack,
AppBskyGraphDefs,
ComAtprotoRepoStrongRef,
} from '@atproto/api'
import {AtUri} from '@atproto/api'
Expand Down Expand Up @@ -43,21 +43,33 @@ type ResolvedPostRecord = {
meta: AppBskyFeedDefs.PostView
}

type ResolvedOtherRecord = {
type ResolvedFeedRecord = {
type: 'record'
record: ComAtprotoRepoStrongRef.Main
kind: 'other'
meta: {
// We should replace this with a hydrated record (e.g. feed, list, starter pack)
// and change the composer preview to use the actual post embed components:
title: string
}
kind: 'feed'
meta: AppBskyFeedDefs.GeneratorView
}

type ResolvedListRecord = {
type: 'record'
record: ComAtprotoRepoStrongRef.Main
kind: 'list'
meta: AppBskyGraphDefs.ListView
}

type ResolvedStarterPackRecord = {
type: 'record'
record: ComAtprotoRepoStrongRef.Main
kind: 'starter-pack'
meta: AppBskyGraphDefs.StarterPackView
}

export type ResolvedLink =
| ResolvedExternalLink
| ResolvedPostRecord
| ResolvedOtherRecord
| ResolvedFeedRecord
| ResolvedListRecord
| ResolvedStarterPackRecord

export class EmbeddingDisabledError extends Error {
constructor() {
Expand Down Expand Up @@ -102,11 +114,8 @@ export async function resolveLink(
uri: res.data.view.uri,
cid: res.data.view.cid,
},
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: res.data.view.displayName,
},
kind: 'feed',
meta: res.data.view,
}
}
if (isBskyListUrl(uri)) {
Expand All @@ -121,11 +130,8 @@ export async function resolveLink(
uri: res.data.list.uri,
cid: res.data.list.cid,
},
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: res.data.list.name,
},
kind: 'list',
meta: res.data.list,
}
}
if (isBskyStartUrl(uri) || isBskyStarterPackUrl(uri)) {
Expand All @@ -138,20 +144,14 @@ export async function resolveLink(
const did = await fetchDid(parsed.name)
const starterPack = createStarterPackUri({did, rkey: parsed.rkey})
const res = await agent.app.bsky.graph.getStarterPack({starterPack})
const record = res.data.starterPack.record
return {
type: 'record',
record: {
uri: res.data.starterPack.uri,
cid: res.data.starterPack.cid,
},
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: AppBskyGraphStarterpack.isRecord(record)
? record.name
: 'Starter Pack',
},
kind: 'starter-pack',
meta: res.data.starterPack,
}
}
return resolveExternal(agent, uri)
Expand Down
28 changes: 22 additions & 6 deletions src/view/com/composer/ExternalEmbed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import {StyleProp, View, ViewStyle} from 'react-native'
import {AppBskyGraphStarterpack} from '@atproto/api'

import {ResolvedLink} from '#/lib/api/resolve'
import {cleanError} from '#/lib/strings/errors'
import {
useResolveGifQuery,
Expand Down Expand Up @@ -75,12 +77,7 @@ export const ExternalEmbedLink = ({
const linkInfo = React.useMemo(
() =>
data && {
title:
data.type === 'external'
? data.title
: data.kind === 'other'
? data.meta.title
: uri,
title: getExternalLinkTitle(data) ?? uri,
uri,
description: data.type === 'external' ? data.description : '',
thumb: data.type === 'external' ? data.thumb?.source.path : undefined,
Expand Down Expand Up @@ -137,3 +134,22 @@ function Container({
</View>
)
}

function getExternalLinkTitle(link: ResolvedLink): string | undefined {
if (link.type === 'external') {
return link.title
}
switch (link.kind) {
// These are currently treated as external.
// TODO: Display them as embeds instead.
case 'feed':
return link.meta.displayName
case 'list':
return link.meta.name
case 'starter-pack':
const record = link.meta.record
return AppBskyGraphStarterpack.isRecord(record)
? record.name
: 'Starter Pack'
}
}

0 comments on commit 0a7640d

Please sign in to comment.