Add a hotness thread sort (#6649)

* Add a hotness thread sort

* Bump @atproto/api
This commit is contained in:
dan
2024-11-23 03:27:17 +00:00
committed by GitHub
parent ac5b2cf31f
commit 631277dc3d
6 changed files with 67 additions and 4 deletions
+20 -1
View File
@@ -237,7 +237,11 @@ export function sortThread(
}
}
if (opts.sort === 'oldest') {
if (opts.sort === 'hotness') {
const aHotness = getHotness(a.post)
const bHotness = getHotness(b.post)
return bHotness - aHotness
} else if (opts.sort === 'oldest') {
return a.post.indexedAt.localeCompare(b.post.indexedAt)
} else if (opts.sort === 'newest') {
return b.post.indexedAt.localeCompare(a.post.indexedAt)
@@ -269,6 +273,21 @@ export function sortThread(
// internal methods
// =
// Inspired by https://join-lemmy.org/docs/contributors/07-ranking-algo.html
// We want to give recent comments a real chance (and not bury them deep below the fold)
// while also surfacing well-liked comments from the past. In the future, we can explore
// something more sophisticated, but we don't have much data on the client right now.
function getHotness(post: AppBskyFeedDefs.PostView) {
const hoursAgo =
(new Date().getTime() - new Date(post.indexedAt).getTime()) /
(1000 * 60 * 60)
const likeCount = post.likeCount ?? 0
const likeOrder = Math.log(3 + likeCount)
const timePenaltyExponent = 1.5 + 1.5 / (1 + Math.log(1 + likeCount))
const timePenalty = Math.pow(hoursAgo + 2, timePenaltyExponent)
return likeOrder / timePenalty
}
function responseToThreadNodes(
node: ThreadViewNode,
depth = 0,
+1 -1
View File
@@ -15,7 +15,7 @@ export const DEFAULT_HOME_FEED_PREFS: UsePreferencesQueryResponse['feedViewPrefs
}
export const DEFAULT_THREAD_VIEW_PREFS: ThreadViewPreferences = {
sort: 'newest',
sort: 'hotness',
prioritizeFollowedUsers: true,
lab_treeViewEnabled: false,
}
+1 -1
View File
@@ -22,6 +22,6 @@ export type ThreadViewPreferences = Pick<
BskyThreadViewPreference,
'prioritizeFollowedUsers'
> & {
sort: 'oldest' | 'newest' | 'most-likes' | 'random' | string
sort: 'hotness' | 'oldest' | 'newest' | 'most-likes' | 'random' | string
lab_treeViewEnabled?: boolean
}