Skip to content

Commit

Permalink
Fetch link previews from RQ (#5608)
Browse files Browse the repository at this point in the history
Co-authored-by: Mary <[email protected]>
Co-authored-by: Hailey <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent dd8be2e commit c06040c
Show file tree
Hide file tree
Showing 17 changed files with 490 additions and 516 deletions.
61 changes: 32 additions & 29 deletions src/App.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from '#/state/session'
import {readLastActiveAccount} from '#/state/session/util'
import {Provider as ShellStateProvider} from '#/state/shell'
import {Provider as ComposerProvider} from '#/state/shell/composer'
import {Provider as LoggedOutViewProvider} from '#/state/shell/logged-out'
import {Provider as ProgressGuideProvider} from '#/state/shell/progress-guide'
import {Provider as SelectedFeedProvider} from '#/state/shell/selected-feed'
Expand Down Expand Up @@ -125,35 +126,37 @@ function InnerApp() {
// Resets the entire tree below when it changes:
key={currentAccount?.did}>
<QueryProvider currentDid={currentAccount?.did}>
<StatsigProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<HiddenRepliesProvider>
<UnreadNotifsProvider>
<BackgroundNotificationPreferencesProvider>
<MutedThreadsProvider>
<ProgressGuideProvider>
<GestureHandlerRootView
style={s.h100pct}>
<TestCtrls />
<Shell />
<NuxDialogs />
</GestureHandlerRootView>
</ProgressGuideProvider>
</MutedThreadsProvider>
</BackgroundNotificationPreferencesProvider>
</UnreadNotifsProvider>
</HiddenRepliesProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
<ComposerProvider>
<StatsigProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<HiddenRepliesProvider>
<UnreadNotifsProvider>
<BackgroundNotificationPreferencesProvider>
<MutedThreadsProvider>
<ProgressGuideProvider>
<GestureHandlerRootView
style={s.h100pct}>
<TestCtrls />
<Shell />
<NuxDialogs />
</GestureHandlerRootView>
</ProgressGuideProvider>
</MutedThreadsProvider>
</BackgroundNotificationPreferencesProvider>
</UnreadNotifsProvider>
</HiddenRepliesProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
</ComposerProvider>
</QueryProvider>
</React.Fragment>
</VideoVolumeProvider>
Expand Down
57 changes: 30 additions & 27 deletions src/App.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from '#/state/session'
import {readLastActiveAccount} from '#/state/session/util'
import {Provider as ShellStateProvider} from '#/state/shell'
import {Provider as ComposerProvider} from '#/state/shell/composer'
import {useComposerKeyboardShortcut} from '#/state/shell/composer/useComposerKeyboardShortcut'
import {Provider as LoggedOutViewProvider} from '#/state/shell/logged-out'
import {Provider as ProgressGuideProvider} from '#/state/shell/progress-guide'
Expand Down Expand Up @@ -116,33 +117,35 @@ function InnerApp() {
// Resets the entire tree below when it changes:
key={currentAccount?.did}>
<QueryProvider currentDid={currentAccount?.did}>
<StatsigProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<HiddenRepliesProvider>
<UnreadNotifsProvider>
<BackgroundNotificationPreferencesProvider>
<MutedThreadsProvider>
<SafeAreaProvider>
<ProgressGuideProvider>
<Shell />
<NuxDialogs />
</ProgressGuideProvider>
</SafeAreaProvider>
</MutedThreadsProvider>
</BackgroundNotificationPreferencesProvider>
</UnreadNotifsProvider>
</HiddenRepliesProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
<ComposerProvider>
<StatsigProvider>
<MessagesProvider>
{/* LabelDefsProvider MUST come before ModerationOptsProvider */}
<LabelDefsProvider>
<ModerationOptsProvider>
<LoggedOutViewProvider>
<SelectedFeedProvider>
<HiddenRepliesProvider>
<UnreadNotifsProvider>
<BackgroundNotificationPreferencesProvider>
<MutedThreadsProvider>
<SafeAreaProvider>
<ProgressGuideProvider>
<Shell />
<NuxDialogs />
</ProgressGuideProvider>
</SafeAreaProvider>
</MutedThreadsProvider>
</BackgroundNotificationPreferencesProvider>
</UnreadNotifsProvider>
</HiddenRepliesProvider>
</SelectedFeedProvider>
</LoggedOutViewProvider>
</ModerationOptsProvider>
</LabelDefsProvider>
</MessagesProvider>
</StatsigProvider>
</ComposerProvider>
</QueryProvider>
<ToastContainer />
</React.Fragment>
Expand Down
47 changes: 38 additions & 9 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import {
ComAtprotoRepoStrongRef,
RichText,
} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'

import {isNetworkError} from '#/lib/strings/errors'
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
import {logger} from '#/logger'
import {ComposerImage, compressImage} from '#/state/gallery'
import {writePostgateRecord} from '#/state/queries/postgate'
import {
fetchResolveGifQuery,
fetchResolveLinkQuery,
} from '#/state/queries/resolve-link'
import {
createThreadgateRecord,
ThreadgateAllowUISetting,
Expand All @@ -27,7 +32,6 @@ import {
import {ComposerState, EmbedDraft} from '#/view/com/composer/state/composer'
import {createGIFDescription} from '../gif-alt-text'
import {LinkMeta} from '../link-meta/link-meta'
import {resolveGif, resolveLink} from './resolve'
import {uploadBlob} from './upload-blob'

export {uploadBlob}
Expand All @@ -51,7 +55,11 @@ interface PostOpts {
langs?: string[]
}

export async function post(agent: BskyAgent, opts: PostOpts) {
export async function post(
agent: BskyAgent,
queryClient: QueryClient,
opts: PostOpts,
) {
let reply
let rt = new RichText({text: opts.rawText.trimEnd()}, {cleanNewlines: true})

Expand All @@ -64,6 +72,7 @@ export async function post(agent: BskyAgent, opts: PostOpts) {

const embed = await resolveEmbed(
agent,
queryClient,
opts.composerState,
opts.onStateChange,
)
Expand Down Expand Up @@ -178,6 +187,7 @@ export async function post(agent: BskyAgent, opts: PostOpts) {

async function resolveEmbed(
agent: BskyAgent,
queryClient: QueryClient,
draft: ComposerState,
onStateChange: ((state: string) => void) | undefined,
): Promise<
Expand All @@ -190,8 +200,8 @@ async function resolveEmbed(
> {
if (draft.embed.quote) {
const [resolvedMedia, resolvedQuote] = await Promise.all([
resolveMedia(agent, draft.embed, onStateChange),
resolveRecord(agent, draft.embed.quote.uri),
resolveMedia(agent, queryClient, draft.embed, onStateChange),
resolveRecord(agent, queryClient, draft.embed.quote.uri),
])
if (resolvedMedia) {
return {
Expand All @@ -208,12 +218,21 @@ async function resolveEmbed(
record: resolvedQuote,
}
}
const resolvedMedia = await resolveMedia(agent, draft.embed, onStateChange)
const resolvedMedia = await resolveMedia(
agent,
queryClient,
draft.embed,
onStateChange,
)
if (resolvedMedia) {
return resolvedMedia
}
if (draft.embed.link) {
const resolvedLink = await resolveLink(agent, draft.embed.link.uri)
const resolvedLink = await fetchResolveLinkQuery(
queryClient,
agent,
draft.embed.link.uri,
)
if (resolvedLink.type === 'record') {
return {
$type: 'app.bsky.embed.record',
Expand All @@ -226,6 +245,7 @@ async function resolveEmbed(

async function resolveMedia(
agent: BskyAgent,
queryClient: QueryClient,
embedDraft: EmbedDraft,
onStateChange: ((state: string) => void) | undefined,
): Promise<
Expand Down Expand Up @@ -286,7 +306,11 @@ async function resolveMedia(
}
if (embedDraft.media?.type === 'gif') {
const gifDraft = embedDraft.media
const resolvedGif = await resolveGif(agent, gifDraft.gif)
const resolvedGif = await fetchResolveGifQuery(
queryClient,
agent,
gifDraft.gif,
)
let blob: BlobRef | undefined
if (resolvedGif.thumb) {
onStateChange?.('Uploading link thumbnail...')
Expand All @@ -305,7 +329,11 @@ async function resolveMedia(
}
}
if (embedDraft.link) {
const resolvedLink = await resolveLink(agent, embedDraft.link.uri)
const resolvedLink = await fetchResolveLinkQuery(
queryClient,
agent,
embedDraft.link.uri,
)
if (resolvedLink.type === 'external') {
let blob: BlobRef | undefined
if (resolvedLink.thumb) {
Expand All @@ -330,9 +358,10 @@ async function resolveMedia(

async function resolveRecord(
agent: BskyAgent,
queryClient: QueryClient,
uri: string,
): Promise<ComAtprotoRepoStrongRef.Main> {
const resolvedLink = await resolveLink(agent, uri)
const resolvedLink = await fetchResolveLinkQuery(queryClient, agent, uri)
if (resolvedLink.type !== 'record') {
throw Error('Expected uri to resolve to a record')
}
Expand Down
49 changes: 43 additions & 6 deletions src/lib/api/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ComAtprotoRepoStrongRef} from '@atproto/api'
import {AppBskyActorDefs, ComAtprotoRepoStrongRef} from '@atproto/api'
import {AtUri} from '@atproto/api'
import {BskyAgent} from '@atproto/api'

Expand Down Expand Up @@ -33,12 +33,32 @@ type ResolvedExternalLink = {
thumb: ComposerImage | undefined
}

type ResolvedRecord = {
type ResolvedPostRecord = {
type: 'record'
record: ComAtprotoRepoStrongRef.Main
kind: 'post'
meta: {
text: string
indexedAt: string
author: AppBskyActorDefs.ProfileViewBasic
}
}

type ResolvedLink = ResolvedExternalLink | ResolvedRecord
type ResolvedOtherRecord = {
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
}
}

export type ResolvedLink =
| ResolvedExternalLink
| ResolvedPostRecord
| ResolvedOtherRecord

export async function resolveLink(
agent: BskyAgent,
Expand All @@ -57,30 +77,47 @@ export async function resolveLink(
cid: result.cid,
uri: result.uri,
},
kind: 'post',
meta: result,
}
}
if (isBskyCustomFeedUrl(uri)) {
// TODO: Remove this abstraction.
const result = await getFeedAsEmbed(agent, fetchDid, uri)
return {
type: 'record',
record: result.embed!.record, // TODO: Fix types.
record: result.embed!.record,
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: result.meta!.title!,
},
}
}
if (isBskyListUrl(uri)) {
// TODO: Remove this abstraction.
const result = await getListAsEmbed(agent, fetchDid, uri)
return {
type: 'record',
record: result.embed!.record, // TODO: Fix types.
record: result.embed!.record,
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: result.meta!.title!,
},
}
}
if (isBskyStartUrl(uri) || isBskyStarterPackUrl(uri)) {
// TODO: Remove this abstraction.
const result = await getStarterPackAsEmbed(agent, fetchDid, uri)
return {
type: 'record',
record: result.embed!.record, // TODO: Fix types.
record: result.embed!.record,
kind: 'other',
meta: {
// TODO: Include hydrated content instead.
title: result.meta!.title!,
},
}
}
return resolveExternal(agent, uri)
Expand Down
Loading

0 comments on commit c06040c

Please sign in to comment.