Update hardcoded suggested actors (#524)

* Update the hardcoded suggested follows

* Update the suggested actors to just use hardcoded list since there's now the foaf system
This commit is contained in:
Paul Frazee 2023-04-24 18:49:11 -05:00 committed by GitHub
parent c5222db38b
commit df1791bde2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 186 deletions

View file

@ -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,56 +57,10 @@ 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 = [
@ -117,20 +68,14 @@ export class SuggestedActorsModel {
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
// =