Skip to content

Commit

Permalink
Upgrade API, implement XRPC rework (#4857)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthieu Sieben <[email protected]>
  • Loading branch information
haileyok and matthieusieben authored Aug 12, 2024
1 parent ae883e2 commit 7df2327
Show file tree
Hide file tree
Showing 19 changed files with 542 additions and 359 deletions.
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import 'react-native-gesture-handler' // must be first
import {LogBox} from 'react-native'

import '#/platform/polyfills'
import {IS_TEST} from '#/env'

import {LogBox} from 'react-native'
import {registerRootComponent} from 'expo'
import {doPolyfill} from '#/lib/api/api-polyfill'

import App from '#/App'

doPolyfill()
import {IS_TEST} from '#/env'

if (IS_TEST) {
LogBox.ignoreAllLogs() // suppress all logs in tests
Expand Down
5 changes: 2 additions & 3 deletions index.web.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import '#/platform/markBundleStartTime'

import '#/platform/polyfills'

import {registerRootComponent} from 'expo'
import {doPolyfill} from '#/lib/api/api-polyfill'

import App from '#/App'

doPolyfill()
registerRootComponent(App)
4 changes: 2 additions & 2 deletions jest/test-pds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Mocker {
}

async createUser(name: string) {
const agent = new BskyAgent({service: this.agent.service})
const agent = new BskyAgent({service: this.service})

const inviteRes = await agent.api.com.atproto.server.createInviteCode(
{useCount: 1},
Expand Down Expand Up @@ -332,7 +332,7 @@ class Mocker {
}

async createInvite(forAccount: string) {
const agent = new BskyAgent({service: this.agent.service})
const agent = new BskyAgent({service: this.service})
await agent.api.com.atproto.server.createInviteCode(
{useCount: 1, forAccount},
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"open-analyzer": "EXPO_PUBLIC_OPEN_ANALYZER=1 yarn build-web"
},
"dependencies": {
"@atproto/api": "0.12.29",
"@atproto/api": "0.13.0",
"@bam.tech/react-native-image-resizer": "^3.0.4",
"@braintree/sanitize-url": "^6.0.2",
"@discord/bottom-sheet": "bluesky-social/react-native-bottom-sheet",
Expand Down Expand Up @@ -208,7 +208,7 @@
"zod": "^3.20.2"
},
"devDependencies": {
"@atproto/dev-env": "^0.3.5",
"@atproto/dev-env": "^0.3.39",
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
Expand Down
28 changes: 0 additions & 28 deletions patches/@atproto+lexicon+0.4.0.patch

This file was deleted.

85 changes: 0 additions & 85 deletions src/lib/api/api-polyfill.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/lib/api/api-polyfill.web.ts

This file was deleted.

25 changes: 11 additions & 14 deletions src/lib/api/feed/custom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
AppBskyFeedDefs,
AppBskyFeedGetFeed as GetCustomFeed,
AtpAgent,
BskyAgent,
} from '@atproto/api'

Expand Down Expand Up @@ -51,7 +50,7 @@ export class CustomFeedAPI implements FeedAPI {
const agent = this.agent
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)

const res = agent.session
const res = agent.did
? await this.agent.app.bsky.feed.getFeed(
{
...this.params,
Expand Down Expand Up @@ -106,34 +105,32 @@ async function loggedOutFetch({
let contentLangs = getContentLanguages().join(',')

// manually construct fetch call so we can add the `lang` cache-busting param
let res = await AtpAgent.fetch!(
let res = await fetch(
`https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}&lang=${contentLangs}`,
'GET',
{'Accept-Language': contentLangs},
undefined,
{method: 'GET', headers: {'Accept-Language': contentLangs}},
)
if (res.body?.feed?.length) {
let data = res.ok ? await res.json() : null
if (data?.feed?.length) {
return {
success: true,
data: res.body,
data,
}
}

// no data, try again with language headers removed
res = await AtpAgent.fetch!(
res = await fetch(
`https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}`,
'GET',
{'Accept-Language': ''},
undefined,
{method: 'GET', headers: {'Accept-Language': ''}},
)
if (res.body?.feed?.length) {
data = res.ok ? await res.json() : null
if (data?.feed?.length) {
return {
success: true,
data: res.body,
data,
}
}

Expand Down
39 changes: 5 additions & 34 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AppBskyFeedThreadgate,
BskyAgent,
ComAtprotoLabelDefs,
ComAtprotoRepoUploadBlob,
RichText,
} from '@atproto/api'
import {AtUri} from '@atproto/api'
Expand All @@ -15,10 +14,13 @@ import {logger} from '#/logger'
import {ThreadgateSetting} from '#/state/queries/threadgate'
import {isNetworkError} from 'lib/strings/errors'
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
import {isNative, isWeb} from 'platform/detection'
import {isNative} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {LinkMeta} from '../link-meta/link-meta'
import {safeDeleteAsync} from '../media/manip'
import {uploadBlob} from './upload-blob'

export {uploadBlob}

export interface ExternalEmbedDraft {
uri: string
Expand All @@ -28,25 +30,6 @@ export interface ExternalEmbedDraft {
localThumb?: ImageModel
}

export async function uploadBlob(
agent: BskyAgent,
blob: string,
encoding: string,
): Promise<ComAtprotoRepoUploadBlob.Response> {
if (isWeb) {
// `blob` should be a data uri
return agent.uploadBlob(convertDataURIToUint8Array(blob), {
encoding,
})
} else {
// `blob` should be a path to a file in the local FS
return agent.uploadBlob(
blob, // this will be special-cased by the fetch monkeypatch in /src/state/lib/api.ts
{encoding},
)
}
}

interface PostOpts {
rawText: string
replyTo?: string
Expand Down Expand Up @@ -301,7 +284,7 @@ export async function createThreadgate(

const postUrip = new AtUri(postUri)
await agent.api.com.atproto.repo.putRecord({
repo: agent.session!.did,
repo: agent.accountDid,
collection: 'app.bsky.feed.threadgate',
rkey: postUrip.rkey,
record: {
Expand All @@ -312,15 +295,3 @@ export async function createThreadgate(
},
})
}

// helpers
// =

function convertDataURIToUint8Array(uri: string): Uint8Array {
var raw = window.atob(uri.substring(uri.indexOf(';base64,') + 8))
var binary = new Uint8Array(new ArrayBuffer(raw.length))
for (let i = 0; i < raw.length; i++) {
binary[i] = raw.charCodeAt(i)
}
return binary
}
Loading

0 comments on commit 7df2327

Please sign in to comment.