Adds hardcoded suggested list (#178)

* Adds hardcoded suggested list

* Update suggested-actors-view to support page sizes smaller than the hardcoded list

---------

Co-authored-by: Paul Frazee <pfrazee@gmail.com>
This commit is contained in:
Aryan Goharzad
2023-02-09 17:33:35 -05:00
committed by GitHub
parent 70b0dc070a
commit 04b3ebbf63
4 changed files with 142 additions and 14 deletions
+72 -9
View File
@@ -1,12 +1,30 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AppBskyActorGetSuggestions as GetSuggestions} from '@atproto/api'
import {
AppBskyActorGetSuggestions as GetSuggestions,
AppBskyActorProfile as Profile,
} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {cleanError} from '../../lib/strings'
import {bundleAsync} from '../../lib/async/bundle'
import {
devSuggestedFollows,
productionSuggestedFollows,
stagingSuggestedFollows,
} from '../../lib/suggestedFollows'
const PAGE_SIZE = 30
export type SuggestedActor = GetSuggestions.Actor
export type SuggestedActor = GetSuggestions.Actor | Profile.View
const getSuggestionList = ({serviceUrl}: {serviceUrl: string}) => {
if (serviceUrl.includes('localhost')) {
return devSuggestedFollows
} else if (serviceUrl.includes('staging')) {
return stagingSuggestedFollows
} else {
return productionSuggestedFollows
}
}
export class SuggestedActorsViewModel {
// state
@@ -18,6 +36,8 @@ export class SuggestedActorsViewModel {
hasMore = true
loadMoreCursor?: string
private hardCodedSuggestions: SuggestedActor[] | undefined
// data
suggestions: SuggestedActor[] = []
@@ -57,6 +77,9 @@ export class SuggestedActorsViewModel {
if (!replace && !this.hasMore) {
return
}
if (replace) {
this.hardCodedSuggestions = undefined
}
this._xLoading(replace)
try {
let items: SuggestedActor[] = this.suggestions
@@ -66,13 +89,27 @@ export class SuggestedActorsViewModel {
}
let res
do {
res = await this.rootStore.api.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)
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.api.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
@@ -83,6 +120,32 @@ export class SuggestedActorsViewModel {
}
})
private async fetchHardcodedSuggestions() {
if (!this.hardCodedSuggestions) {
try {
const suggestionsList = getSuggestionList({
serviceUrl: this.rootStore.session.currentSession?.service || '',
})
const res = await this.rootStore.api.app.bsky.actor.getProfiles({
actors: suggestionsList,
})
runInAction(() => {
this.hardCodedSuggestions = res.data.profiles.filter(
profile => !profile.myState?.follow,
)
})
} catch (e) {
this.rootStore.log.error(
'Failed to getProfiles() for suggested follows',
{e},
)
runInAction(() => {
this.hardCodedSuggestions = []
})
}
}
}
// state transitions
// =