Skip to content

Commit 631277d

Browse files
authored
Add a hotness thread sort (bluesky-social#6649)
* Add a hotness thread sort * Bump @atproto/api
1 parent ac5b2cf commit 631277d

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"icons:optimize": "svgo -f ./assets/icons"
5555
},
5656
"dependencies": {
57-
"@atproto/api": "^0.13.11",
57+
"@atproto/api": "^0.13.18",
5858
"@braintree/sanitize-url": "^6.0.2",
5959
"@discord/bottom-sheet": "bluesky-social/react-native-bottom-sheet",
6060
"@emoji-mart/react": "^1.1.1",

src/screens/Settings/ThreadPreferences.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export function ThreadPreferencesScreen({}: Props) {
5656
values={sortReplies ? [sortReplies] : []}
5757
onChange={values => setThreadViewPrefs({sort: values[0]})}>
5858
<View style={[a.gap_sm, a.flex_1]}>
59+
<Toggle.Item name="hotness" label={_(msg`Hot replies first`)}>
60+
<Toggle.Radio />
61+
<Toggle.LabelText>
62+
<Trans>Hot replies first</Trans>
63+
</Toggle.LabelText>
64+
</Toggle.Item>
5965
<Toggle.Item
6066
name="oldest"
6167
label={_(msg`Oldest replies first`)}>

src/state/queries/post-thread.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ export function sortThread(
237237
}
238238
}
239239

240-
if (opts.sort === 'oldest') {
240+
if (opts.sort === 'hotness') {
241+
const aHotness = getHotness(a.post)
242+
const bHotness = getHotness(b.post)
243+
return bHotness - aHotness
244+
} else if (opts.sort === 'oldest') {
241245
return a.post.indexedAt.localeCompare(b.post.indexedAt)
242246
} else if (opts.sort === 'newest') {
243247
return b.post.indexedAt.localeCompare(a.post.indexedAt)
@@ -269,6 +273,21 @@ export function sortThread(
269273
// internal methods
270274
// =
271275

276+
// Inspired by https://join-lemmy.org/docs/contributors/07-ranking-algo.html
277+
// We want to give recent comments a real chance (and not bury them deep below the fold)
278+
// while also surfacing well-liked comments from the past. In the future, we can explore
279+
// something more sophisticated, but we don't have much data on the client right now.
280+
function getHotness(post: AppBskyFeedDefs.PostView) {
281+
const hoursAgo =
282+
(new Date().getTime() - new Date(post.indexedAt).getTime()) /
283+
(1000 * 60 * 60)
284+
const likeCount = post.likeCount ?? 0
285+
const likeOrder = Math.log(3 + likeCount)
286+
const timePenaltyExponent = 1.5 + 1.5 / (1 + Math.log(1 + likeCount))
287+
const timePenalty = Math.pow(hoursAgo + 2, timePenaltyExponent)
288+
return likeOrder / timePenalty
289+
}
290+
272291
function responseToThreadNodes(
273292
node: ThreadViewNode,
274293
depth = 0,

src/state/queries/preferences/const.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const DEFAULT_HOME_FEED_PREFS: UsePreferencesQueryResponse['feedViewPrefs
1515
}
1616

1717
export const DEFAULT_THREAD_VIEW_PREFS: ThreadViewPreferences = {
18-
sort: 'newest',
18+
sort: 'hotness',
1919
prioritizeFollowedUsers: true,
2020
lab_treeViewEnabled: false,
2121
}

src/state/queries/preferences/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export type ThreadViewPreferences = Pick<
2222
BskyThreadViewPreference,
2323
'prioritizeFollowedUsers'
2424
> & {
25-
sort: 'oldest' | 'newest' | 'most-likes' | 'random' | string
25+
sort: 'hotness' | 'oldest' | 'newest' | 'most-likes' | 'random' | string
2626
lab_treeViewEnabled?: boolean
2727
}

yarn.lock

+38
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@
7272
tlds "^1.234.0"
7373
zod "^3.23.8"
7474

75+
"@atproto/api@^0.13.18":
76+
version "0.13.18"
77+
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.13.18.tgz#cc537cc3b4c8d03f258a373f4d893fea11a77cdd"
78+
integrity sha512-rrl5HhzGYWZ7fiC965TPBUOVItq9M4dxMb6qz8IvAVQliSkrJrKc7UD0QWL89QiiXaOBuX8w+4i5r4wrfBGddg==
79+
dependencies:
80+
"@atproto/common-web" "^0.3.1"
81+
"@atproto/lexicon" "^0.4.3"
82+
"@atproto/syntax" "^0.3.1"
83+
"@atproto/xrpc" "^0.6.4"
84+
await-lock "^2.2.2"
85+
multiformats "^9.9.0"
86+
tlds "^1.234.0"
87+
zod "^3.23.8"
88+
7589
"@atproto/aws@^0.2.7":
7690
version "0.2.7"
7791
resolved "https://registry.yarnpkg.com/@atproto/aws/-/aws-0.2.7.tgz#2f3e05c897ef49b4c46452ec8c36870193952998"
@@ -269,6 +283,17 @@
269283
multiformats "^9.9.0"
270284
zod "^3.23.8"
271285

286+
"@atproto/lexicon@^0.4.3":
287+
version "0.4.3"
288+
resolved "https://registry.yarnpkg.com/@atproto/lexicon/-/lexicon-0.4.3.tgz#d69f6bb363a6326df7766c48132bfa30e22622d9"
289+
integrity sha512-lFVZXe1S1pJP0dcxvJuHP3r/a+EAIBwwU7jUK+r8iLhIja+ml6NmYv8KeFHmIJATh03spEQ9s02duDmFVdCoXg==
290+
dependencies:
291+
"@atproto/common-web" "^0.3.1"
292+
"@atproto/syntax" "^0.3.1"
293+
iso-datestring-validator "^2.2.2"
294+
multiformats "^9.9.0"
295+
zod "^3.23.8"
296+
272297
"@atproto/oauth-provider@^0.2.5":
273298
version "0.2.5"
274299
resolved "https://registry.yarnpkg.com/@atproto/oauth-provider/-/oauth-provider-0.2.5.tgz#7358398125f840404bcc02c966fd2f333869ad2f"
@@ -411,6 +436,11 @@
411436
resolved "https://registry.yarnpkg.com/@atproto/syntax/-/syntax-0.3.0.tgz#fafa2dbea9add37253005cb663e7373e05e618b3"
412437
integrity sha512-Weq0ZBxffGHDXHl9U7BQc2BFJi/e23AL+k+i5+D9hUq/bzT4yjGsrCejkjq0xt82xXDjmhhvQSZ0LqxyZ5woxA==
413438

439+
"@atproto/syntax@^0.3.1":
440+
version "0.3.1"
441+
resolved "https://registry.yarnpkg.com/@atproto/syntax/-/syntax-0.3.1.tgz#4346418728f9643d783d2ffcf7c77e132e1f53d4"
442+
integrity sha512-fzW0Mg1QUOVCWUD3RgEsDt6d1OZ6DdFmbKcDdbzUfh0t4rhtRAC05KbZYmxuMPWDAiJ4BbbQ5dkAc/mNypMXkw==
443+
414444
"@atproto/xrpc-server@^0.7.1":
415445
version "0.7.1"
416446
resolved "https://registry.yarnpkg.com/@atproto/xrpc-server/-/xrpc-server-0.7.1.tgz#e9750aab7bb531c3a82dc6048d47179de18dbdd9"
@@ -437,6 +467,14 @@
437467
"@atproto/lexicon" "^0.4.2"
438468
zod "^3.23.8"
439469

470+
"@atproto/xrpc@^0.6.4":
471+
version "0.6.4"
472+
resolved "https://registry.yarnpkg.com/@atproto/xrpc/-/xrpc-0.6.4.tgz#4cf59774f7c72e5bc821bc5f1d57f0a6ae2014db"
473+
integrity sha512-9ZAJ8nsXTqC4XFyS0E1Wlg7bAvonhXQNQ3Ocs1L1LIwFLXvsw/4fNpIHXxvXvqTCVeyHLbImOnE9UiO1c/qIYA==
474+
dependencies:
475+
"@atproto/lexicon" "^0.4.3"
476+
zod "^3.23.8"
477+
440478
"@aws-crypto/[email protected]":
441479
version "3.0.0"
442480
resolved "https://registry.yarnpkg.com/@aws-crypto/crc32/-/crc32-3.0.0.tgz#07300eca214409c33e3ff769cd5697b57fdd38fa"

0 commit comments

Comments
 (0)