diff options
Diffstat (limited to 'src/state')
-rw-r--r-- | src/state/models/discovery/suggested-actors.ts | 83 |
1 files changed, 11 insertions, 72 deletions
diff --git a/src/state/models/discovery/suggested-actors.ts b/src/state/models/discovery/suggested-actors.ts index dca81dc90..dc9cac817 100644 --- a/src/state/models/discovery/suggested-actors.ts +++ b/src/state/models/discovery/suggested-actors.ts @@ -19,10 +19,7 @@ export class SuggestedActorsModel { isRefreshing = false hasLoaded = false error = '' - hasMore = true - loadMoreCursor?: string - - hardCodedSuggestions: SuggestedActor[] | undefined + hasMore = false // data suggestions: SuggestedActor[] = [] @@ -60,77 +57,25 @@ export class SuggestedActorsModel { } loadMore = bundleAsync(async (replace: boolean = false) => { - if (!replace && !this.hasMore) { + if (this.suggestions.length && !replace) { return } - if (replace) { - this.hardCodedSuggestions = undefined - } this._xLoading(replace) try { - let items: SuggestedActor[] = this.suggestions - if (replace) { - items = [] - this.loadMoreCursor = undefined - } - let res - do { - await this.fetchHardcodedSuggestions() - if (this.hardCodedSuggestions && this.hardCodedSuggestions.length > 0) { - // pull from the hard-coded suggestions - const newItems = this.hardCodedSuggestions.splice(0, this.pageSize) - items = items.concat(newItems) - this.hasMore = true - this.loadMoreCursor = undefined - } else { - // pull from the PDS' algo - res = await this.rootStore.agent.app.bsky.actor.getSuggestions({ - limit: this.pageSize, - cursor: this.loadMoreCursor, - }) - this.loadMoreCursor = res.data.cursor - this.hasMore = !!this.loadMoreCursor - items = items.concat( - res.data.actors.filter( - actor => !items.find(i => i.did === actor.did), - ), - ) - } - } while (items.length < this.pageSize && this.hasMore) - runInAction(() => { - this.suggestions = items - }) - this._xIdle() - } catch (e: any) { - this._xIdle(e) - } - }) - - async fetchHardcodedSuggestions() { - if (this.hardCodedSuggestions) { - return - } - try { // clone the array so we can mutate it const actors = [ ...SUGGESTED_FOLLOWS( this.rootStore.session.currentSession?.service || '', ), ] - - // fetch the profiles in chunks of 25 (the limit allowed by `getProfiles`) - let profiles: AppBskyActorDefs.ProfileView[] = [] - do { - const res = await this.rootStore.agent.getProfiles({ - actors: actors.splice(0, 25), - }) - profiles = profiles.concat(res.data.profiles) - } while (actors.length) - + const res = await this.rootStore.agent.getProfiles({ + actors: shuffle(actors).splice(0, 25), + }) + const {profiles} = res.data this.rootStore.me.follows.hydrateProfiles(profiles) runInAction(() => { - profiles = profiles.filter(profile => { + this.suggestions = profiles.filter(profile => { if (profile.viewer?.following) { return false } @@ -139,18 +84,12 @@ export class SuggestedActorsModel { } return true }) - this.hardCodedSuggestions = shuffle(profiles) - }) - } catch (e) { - this.rootStore.log.error( - 'Failed to getProfiles() for suggested follows', - {e}, - ) - runInAction(() => { - this.hardCodedSuggestions = [] }) + this._xIdle() + } catch (e: any) { + this._xIdle(e) } - } + }) // state transitions // = |