More custom-feed behavior fixes [APP-678] (#831)
* Remove extraneous custom-feed health check * Fixes to custom feed preference sync * Fix lint * Remove dead code (client-side suggested posts constructor) * Enforce the feed-fetch limit in the client if the generator fails to observe the parameter * Bump the number of items fetched in the multifeed per feed from 5 to 10 * Reset the currently active feed when the pinned feeds change * Some fixes to icons * Add a prompt to load latest to the multifeed * Remove debug
This commit is contained in:
parent
e9c84a192b
commit
3217c7ff32
8 changed files with 88 additions and 186 deletions
|
@ -1,137 +0,0 @@
|
|||
import {RootStoreModel} from 'state/index'
|
||||
import {
|
||||
AppBskyFeedDefs,
|
||||
AppBskyFeedGetAuthorFeed as GetAuthorFeed,
|
||||
} from '@atproto/api'
|
||||
type ReasonRepost = AppBskyFeedDefs.ReasonRepost
|
||||
|
||||
async function getMultipleAuthorsPosts(
|
||||
rootStore: RootStoreModel,
|
||||
authors: string[],
|
||||
cursor: string | undefined = undefined,
|
||||
limit: number = 10,
|
||||
) {
|
||||
const responses = await Promise.all(
|
||||
authors.map((actor, index) =>
|
||||
rootStore.agent
|
||||
.getAuthorFeed({
|
||||
actor,
|
||||
limit,
|
||||
cursor: cursor ? cursor.split(',')[index] : undefined,
|
||||
})
|
||||
.catch(_err => ({success: false, headers: {}, data: {feed: []}})),
|
||||
),
|
||||
)
|
||||
return responses
|
||||
}
|
||||
|
||||
function mergePosts(
|
||||
responses: GetAuthorFeed.Response[],
|
||||
{repostsOnly, bestOfOnly}: {repostsOnly?: boolean; bestOfOnly?: boolean},
|
||||
) {
|
||||
let posts: AppBskyFeedDefs.FeedViewPost[] = []
|
||||
|
||||
if (bestOfOnly) {
|
||||
for (const res of responses) {
|
||||
if (res.success) {
|
||||
// filter the feed down to the post with the most likes
|
||||
res.data.feed = res.data.feed.reduce(
|
||||
(acc: AppBskyFeedDefs.FeedViewPost[], v) => {
|
||||
if (
|
||||
!acc?.[0] &&
|
||||
!v.reason &&
|
||||
!v.reply &&
|
||||
isRecentEnough(v.post.indexedAt)
|
||||
) {
|
||||
return [v]
|
||||
}
|
||||
if (
|
||||
acc &&
|
||||
!v.reason &&
|
||||
!v.reply &&
|
||||
(v.post.likeCount || 0) > (acc[0]?.post.likeCount || 0) &&
|
||||
isRecentEnough(v.post.indexedAt)
|
||||
) {
|
||||
return [v]
|
||||
}
|
||||
return acc
|
||||
},
|
||||
[],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// merge into one array
|
||||
for (const res of responses) {
|
||||
if (res.success) {
|
||||
posts = posts.concat(res.data.feed)
|
||||
}
|
||||
}
|
||||
|
||||
// filter down to reposts of other users
|
||||
const uris = new Set()
|
||||
posts = posts.filter(p => {
|
||||
if (repostsOnly && !isARepostOfSomeoneElse(p)) {
|
||||
return false
|
||||
}
|
||||
if (uris.has(p.post.uri)) {
|
||||
return false
|
||||
}
|
||||
uris.add(p.post.uri)
|
||||
return true
|
||||
})
|
||||
|
||||
// sort by index time
|
||||
posts.sort((a, b) => {
|
||||
return (
|
||||
Number(new Date(b.post.indexedAt)) - Number(new Date(a.post.indexedAt))
|
||||
)
|
||||
})
|
||||
|
||||
return posts
|
||||
}
|
||||
|
||||
function isARepostOfSomeoneElse(post: AppBskyFeedDefs.FeedViewPost): boolean {
|
||||
return (
|
||||
post.reason?.$type === 'app.bsky.feed.defs#reasonRepost' &&
|
||||
post.post.author.did !== (post.reason as ReasonRepost).by.did
|
||||
)
|
||||
}
|
||||
|
||||
function getCombinedCursors(responses: GetAuthorFeed.Response[]) {
|
||||
let hasCursor = false
|
||||
const cursors = responses.map(r => {
|
||||
if (r.data.cursor) {
|
||||
hasCursor = true
|
||||
return r.data.cursor
|
||||
}
|
||||
return ''
|
||||
})
|
||||
if (!hasCursor) {
|
||||
return undefined
|
||||
}
|
||||
const combinedCursors = cursors.join(',')
|
||||
return combinedCursors
|
||||
}
|
||||
|
||||
function isCombinedCursor(cursor: string) {
|
||||
return cursor.includes(',')
|
||||
}
|
||||
|
||||
const TWO_DAYS_AGO = Date.now() - 1e3 * 60 * 60 * 48
|
||||
function isRecentEnough(date: string) {
|
||||
try {
|
||||
const d = Number(new Date(date))
|
||||
return d > TWO_DAYS_AGO
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
getMultipleAuthorsPosts,
|
||||
mergePosts,
|
||||
getCombinedCursors,
|
||||
isCombinedCursor,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue