use xrpc dispatcher

This commit is contained in:
Matthieu Sieben
2024-05-02 13:29:33 +02:00
parent be26e9b017
commit e3e1a5c7a9
5 changed files with 26 additions and 20 deletions
+2 -2
View File
@@ -156,7 +156,7 @@ class Mocker {
} }
async createUser(name: string) { 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( const inviteRes = await agent.api.com.atproto.server.createInviteCode(
{useCount: 1}, {useCount: 1},
@@ -332,7 +332,7 @@ class Mocker {
} }
async createInvite(forAccount: string) { 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( await agent.api.com.atproto.server.createInviteCode(
{useCount: 1, forAccount}, {useCount: 1, forAccount},
{ {
+11 -14
View File
@@ -1,7 +1,6 @@
import { import {
AppBskyFeedDefs, AppBskyFeedDefs,
AppBskyFeedGetFeed as GetCustomFeed, AppBskyFeedGetFeed as GetCustomFeed,
AtpAgent,
BskyAgent, BskyAgent,
} from '@atproto/api' } from '@atproto/api'
@@ -51,7 +50,7 @@ export class CustomFeedAPI implements FeedAPI {
const agent = this.agent const agent = this.agent
const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed) const isBlueskyOwned = isBlueskyOwnedFeed(this.params.feed)
const res = agent.session const res = agent.did
? await this.agent.app.bsky.feed.getFeed( ? await this.agent.app.bsky.feed.getFeed(
{ {
...this.params, ...this.params,
@@ -106,34 +105,32 @@ async function loggedOutFetch({
let contentLangs = getContentLanguages().join(',') let contentLangs = getContentLanguages().join(',')
// manually construct fetch call so we can add the `lang` cache-busting param // 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}${ `https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : '' cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}&lang=${contentLangs}`, }&limit=${limit}&lang=${contentLangs}`,
'GET', {method: 'GET', headers: {'Accept-Language': contentLangs}},
{'Accept-Language': contentLangs},
undefined,
) )
if (res.body?.feed?.length) { let data = res.ok ? await res.json() : null
if (data?.feed?.length) {
return { return {
success: true, success: true,
data: res.body, data,
} }
} }
// no data, try again with language headers removed // 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}${ `https://api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=${feed}${
cursor ? `&cursor=${cursor}` : '' cursor ? `&cursor=${cursor}` : ''
}&limit=${limit}`, }&limit=${limit}`,
'GET', {method: 'GET', headers: {'Accept-Language': ''}},
{'Accept-Language': ''},
undefined,
) )
if (res.body?.feed?.length) { data = res.ok ? await res.json() : null
if (data?.feed?.length) {
return { return {
success: true, success: true,
data: res.body, data,
} }
} }
+1 -1
View File
@@ -280,7 +280,7 @@ export async function createThreadgate(
const postUrip = new AtUri(postUri) const postUrip = new AtUri(postUri)
await agent.api.com.atproto.repo.putRecord({ await agent.api.com.atproto.repo.putRecord({
repo: agent.session!.did, repo: agent.accountDid,
collection: 'app.bsky.feed.threadgate', collection: 'app.bsky.feed.threadgate',
rkey: postUrip.rkey, rkey: postUrip.rkey,
record: { record: {
+2 -2
View File
@@ -37,14 +37,14 @@ export function usePreferencesQuery() {
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
queryKey: preferencesQueryKey, queryKey: preferencesQueryKey,
queryFn: async () => { queryFn: async () => {
if (agent.session?.did === undefined) { if (!agent.did) {
return DEFAULT_LOGGED_OUT_PREFERENCES return DEFAULT_LOGGED_OUT_PREFERENCES
} else { } else {
const res = await agent.getPreferences() const res = await agent.getPreferences()
// save to local storage to ensure there are labels on initial requests // save to local storage to ensure there are labels on initial requests
saveLabelers( saveLabelers(
agent.session.did, agent.did,
res.moderationPrefs.labelers.map(l => l.did), res.moderationPrefs.labelers.map(l => l.did),
) )
+10 -1
View File
@@ -1,5 +1,14 @@
<<<<<<< HEAD
import {AtpSessionData, AtpSessionEvent, BskyAgent} from '@atproto/api' import {AtpSessionData, AtpSessionEvent, BskyAgent} from '@atproto/api'
import {TID} from '@atproto/common-web' import {TID} from '@atproto/common-web'
=======
import {
AtpSessionData,
AtpSessionEvent,
BskyAgent,
SessionDispatcher,
} from '@atproto/api'
>>>>>>> 1a535b0b7 (use xrpc dispatcher)
import {networkRetry} from '#/lib/async/retry' import {networkRetry} from '#/lib/async/retry'
import { import {
@@ -35,7 +44,7 @@ export async function createAgentAndResume(
) { ) {
const agent = new BskyAgent({service: storedAccount.service}) const agent = new BskyAgent({service: storedAccount.service})
if (storedAccount.pdsUrl) { if (storedAccount.pdsUrl) {
agent.pdsUrl = agent.api.xrpc.uri = new URL(storedAccount.pdsUrl) agent.sessionManager.pdsUrl = new URL(storedAccount.pdsUrl)
} }
const gates = tryFetchGates(storedAccount.did, 'prefer-low-latency') const gates = tryFetchGates(storedAccount.did, 'prefer-low-latency')
const moderation = configureModerationForAccount(agent, storedAccount) const moderation = configureModerationForAccount(agent, storedAccount)