Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Hot algorithm for nested replies (simple) (#41742)

Mark VanLandinghamdc2d170a9fc2
diff --git a/app/jobs/scheduled/recalculate_nested_hot_scores.rb b/app/jobs/scheduled/recalculate_nested_hot_scores.rb
new file mode 100644
index 00000000000..b481b2342d0
--- /dev/null
+++ b/app/jobs/scheduled/recalculate_nested_hot_scores.rb
@@ -0,0 +1,120 @@
+# frozen_string_literal: true
+
+module Jobs
+ class RecalculateNestedHotScores < ::Jobs::Scheduled
+ every 1.minute
+
+ cluster_concurrency 1
+
+ MIN_STATEMENT_TIME_MS = 100
+
+ def execute(_args = {})
+ return unless SiteSetting.nested_replies_enabled
+ return unless SiteSetting.nested_replies_hot_sort_enabled
+
+ started_at = monotonic_time
+ topics_inspected = 0
+ topics_rebuilt = 0
+ posts_rebuilt = 0
+ failures = 0
+
+ while topics_inspected < SiteSetting.nested_replies_hot_max_topics_per_run
+ timeout_ms = remaining_database_time_ms(started_at)
+ break if timeout_ms < MIN_STATEMENT_TIME_MS
+
+ topic_id = NestedReplies::HotScoreQueue.pop
+ break if topic_id.blank?
+
+ topics_inspected += 1
+ topic = Topic.includes(:nested_topic).find_by(id: topic_id)
+ next unless eligible?(topic)
+ next if NestedReplies::HotScoreCache.fresh?(topic)
+
+ begin
+ rebuilt_posts =
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id, timeout_ms: timeout_ms)
+ NestedReplies::HotScoreQueue.clear_cooldown(topic.id)
+ topics_rebuilt += 1
+ posts_rebuilt += rebuilt_posts
+ rescue ActiveRecord::QueryCanceled, PG::QueryCanceled => error
+ failures += 1
+ cooldown(topic.id)
+ Discourse.warn_exception(
+ error,
+ message: "Timed out refreshing nested hot scores for topic #{topic.id}",
+ )
+ break
+ rescue => error
+ failures += 1
+ cooldown(topic.id)
+ Discourse.warn_exception(
+ error,
+ message: "Failed to refresh nested hot scores for topic #{topic.id}",
+ )
+ end
+ end
+
+ purge_result = purge_expired_cache(started_at)
+ return if topics_inspected.zero? && purge_result.blank?
+
+ DiscourseEvent.trigger(
+ :nested_replies_hot_scores_processed,
+ {
+ topics_inspected: topics_inspected,
+ topics_rebuilt: topics_rebuilt,
+ posts_rebuilt: posts_rebuilt,
+ failures: failures,
+ cooldowns_started: failures,
+ queue_depth: NestedReplies::HotScoreQueue.size,
+ oldest_queued_age: NestedReplies::HotScoreQueue.oldest_age,
+ scores_purged: purge_result&.fetch(:scores_removed, 0).to_i,
+ snapshots_purged: purge_result&.fetch(:snapshots_removed, 0).to_i,
+ duration_seconds: monotonic_time - started_at,
+ },
+ continue_on_error: true,
+ )
+ end
+
+ private
+
+ def eligible?(topic)
+ topic.present? && topic.deleted_at.nil? && topic.regular? && topic.nested_view? &&
+ topic.posts_count.to_i > NestedReplies::HotScoreCache.small_topic_post_limit &&
+ NestedReplies::HotScoreCache.active_topic?(topic)
+ end
+
+ def cooldown(topic_id)
+ NestedReplies::HotScoreQueue.cooldown(
+ topic_id,
+ duration: SiteSetting.nested_replies_hot_failure_cooldown_minutes.minutes,
+ )
+ end
+
+ def purge_expired_cache(started_at)
+ return unless NestedReplies::HotScoreQueue.claim_cleanup
+
+ remaining_ms = remaining_database_time_ms(started_at)
+ return if remaining_ms < MIN_STATEMENT_TIME_MS * 2
+
+ NestedReplies::HotScoreCache.purge_expired(
+ timeout_ms: [remaining_ms / 2, MIN_STATEMENT_TIME_MS].max,
+ )
+ rescue ActiveRecord::QueryCanceled, PG::QueryCanceled => error
+ Discourse.warn_exception(error, message: "Timed out purging nested hot score cache")
+ nil
+ rescue => error
+ Discourse.warn_exception(error, message: "Failed to purge nested hot score cache")
+ nil
+ end
+
+ def remaining_database_time_ms(started_at)
+ elapsed_ms = (monotonic_time - started_at) * 1_000
+ max_job_runtime_ms = SiteSetting.nested_replies_hot_max_job_runtime_ms
+ (max_job_runtime_ms - elapsed_ms).floor.clamp(0, max_job_runtime_ms)
+ end
+
+ def monotonic_time
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ end
+ end
+end
diff --git a/app/services/nested_topic/list_children.rb b/app/services/nested_topic/list_children.rb
index 8570d7ad0fc..1c184220590 100644
--- a/app/services/nested_topic/list_children.rb
+++ b/app/services/nested_topic/list_children.rb
@@ -70,7 +70,7 @@ class NestedTopic::ListChildren
.where(reply_to_post_number: params.parent_post_number)
.where(post_number: 2..)
scope = loader.apply_visibility(scope)
- scope = NestedReplies::Sort.apply(scope, params.sort)
+ scope = loader.apply_sort(scope, params.sort)
scope.offset(params.page * per_page).limit(per_page)
end
diff --git a/app/services/nested_topic/list_roots.rb b/app/services/nested_topic/list_roots.rb
index 0cb775f3314..34c4d9ee5cf 100644
--- a/app/services/nested_topic/list_roots.rb
+++ b/app/services/nested_topic/list_roots.rb
@@ -125,6 +125,7 @@ class NestedTopic::ListRoots
response[:topic] = serializer.serialize_topic
response[:op_post] = serializer.serialize_post(loader.op_post, reply_counts, descendant_counts)
response[:sort] = params.sort
+ response[:effective_sort] = loader.effective_sort(params.sort)
response[:message_bus_last_id] = topic_view.message_bus_last_id
response[:pinned_post_ids] = pinned_post_ids if pinned_post_ids.present?
end
diff --git a/app/services/nested_topic/show_context.rb b/app/services/nested_topic/show_context.rb
index 0d62f84ac7e..f5d00021353 100644
--- a/app/services/nested_topic/show_context.rb
+++ b/app/services/nested_topic/show_context.rb
@@ -134,6 +134,7 @@ class NestedTopic::ShowContext
end
def serialize_context(
+ params:,
loader:,
serializer:,
target_post:,
@@ -157,6 +158,7 @@ class NestedTopic::ShowContext
end,
target_post:
serializer.serialize_tree(target_post, children_map, reply_counts, descendant_counts),
+ effective_sort: loader.effective_sort(params.sort),
message_bus_last_id: topic_view.message_bus_last_id,
}
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 70fddf76673..6ae9d3841ce 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4254,6 +4254,7 @@ en:
sort:
label: "Sort by"
top: "Top"
+ hot: "Hot"
new: "New"
old: "Old"
load_more_children:
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f90a7efd4b2..755e87684dc 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2929,6 +2929,25 @@ en:
content_localization_allow_author_localization: "Allow authors update localizations of their own topics and posts via the post menu."
nested_replies_enabled: "Enable the nested replies view for topics."
nested_replies_default: "Use nested replies as the default view for all topics."
+ nested_replies_hot_sort_enabled: "Allow people to sort nested replies by a demand-driven hot score. Hot scores are calculated in the background only for topics where hot sorting is requested."
+ nested_replies_hot_like_weight: "Weight assigned to each point of a post's like score when calculating nested reply hotness."
+ nested_replies_hot_reply_weight: "Weight assigned to each direct reply when calculating nested reply hotness."
+ nested_replies_hot_freshness_max_bonus: "Maximum freshness bonus added to a newly created nested reply's hot score."
+ nested_replies_hot_freshness_half_life_hours: "Number of hours in which the nested reply freshness bonus is reduced by half."
+ nested_replies_hot_child_penalty: "Hot score subtracted for each ancestor when descendant hotness propagates upward."
+ nested_replies_hot_small_topic_post_limit: "Maximum topic post count that uses top sorting without calculating nested reply hot scores."
+ nested_replies_hot_max_inactivity_days: "Maximum number of days since a topic's latest public post during which hot sorting may be used. Older topics use top sorting without calculating or refreshing hot scores."
+ nested_replies_hot_snapshot_ttl_minutes: "Number of minutes a nested reply hot score snapshot remains fresh before a request may queue a refresh."
+ nested_replies_hot_max_stale_age_days: "Maximum age in days at which an existing nested reply hot score snapshot may still be served."
+ nested_replies_hot_refresh_requests_per_minute: "Maximum nested reply hot score refresh requests accepted from one requester per minute."
+ nested_replies_hot_max_topics_per_run: "Maximum number of queued topics inspected by each nested reply hot score job run."
+ nested_replies_hot_max_job_runtime_ms: "Maximum elapsed runtime in milliseconds for each nested reply hot score job run."
+ nested_replies_hot_max_statement_timeout_ms: "Maximum PostgreSQL statement timeout in milliseconds for one nested reply hot score calculation."
+ nested_replies_hot_lock_timeout_ms: "Maximum PostgreSQL lock wait in milliseconds for nested reply hot score database work."
+ nested_replies_hot_failure_cooldown_minutes: "Number of minutes before a failed nested reply hot score calculation may be queued again."
+ nested_replies_hot_max_pending_topics: "Maximum number of topics waiting in the nested reply hot score queue."
+ nested_replies_hot_max_queue_age_minutes: "Maximum number of minutes a nested reply hot score request may wait in the queue before it is discarded."
+ nested_replies_hot_preload_depth_penalty: "Priority subtracted for each level of depth when choosing nested replies to pre-expand in hot view."
nested_replies_default_sort: "Default sort algorithm for nested replies view."
nested_replies_max_depth: "Maximum visual nesting depth for threaded replies. Posts deeper than this show a 'Continue this thread' link."
nested_replies_backfill_batch_size: "Number of topics to backfill nested reply stats for per scheduled run."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index f4add921efa..42b46f7e673 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1968,12 +1968,128 @@ nested_replies:
depends_on:
- "nested_replies_enabled"
depends_behavior: "hidden"
+ nested_replies_hot_sort_enabled:
+ default: false
+ client: true
+ area: "posts_and_topics"
+ depends_on:
+ - "nested_replies_enabled"
+ depends_behavior: "hidden"
+ nested_replies_hot_like_weight:
+ default: 1.0
+ type: float
+ min: 0
+ max: 100
+ hidden: true
+ nested_replies_hot_reply_weight:
+ default: 2.0
+ type: float
+ min: 0
+ max: 100
+ hidden: true
+ nested_replies_hot_freshness_max_bonus:
+ default: 3.0
+ type: float
+ min: 0
+ max: 100
+ hidden: true
+ nested_replies_hot_freshness_half_life_hours:
+ default: 168
+ type: float
+ min: 1
+ max: 8760
+ hidden: true
+ nested_replies_hot_child_penalty:
+ default: 0.25
+ type: float
+ min: 0.01
+ max: 100
+ hidden: true
+ nested_replies_hot_small_topic_post_limit:
+ default: 5
+ type: integer
+ min: 0
+ max: 10000
+ hidden: true
+ nested_replies_hot_max_inactivity_days:
+ default: 30
+ type: integer
+ min: 1
+ max: 3650
+ hidden: true
+ nested_replies_hot_snapshot_ttl_minutes:
+ default: 30
+ type: integer
+ min: 1
+ max: 10080
+ hidden: true
+ nested_replies_hot_max_stale_age_days:
+ default: 30
+ type: integer
+ min: 1
+ max: 3650
+ hidden: true
+ nested_replies_hot_refresh_requests_per_minute:
+ default: 20
+ type: integer
+ min: 1
+ max: 10000
+ hidden: true
+ nested_replies_hot_max_topics_per_run:
+ default: 10
+ type: integer
+ min: 1
+ max: 1000
+ hidden: true
+ nested_replies_hot_max_job_runtime_ms:
+ default: 10000
+ type: integer
+ min: 100
+ max: 60000
+ hidden: true
+ nested_replies_hot_max_statement_timeout_ms:
+ default: 10000
+ type: integer
+ min: 100
+ max: 60000
+ hidden: true
+ nested_replies_hot_lock_timeout_ms:
+ default: 1000
+ type: integer
+ min: 1
+ max: 10000
+ hidden: true
+ nested_replies_hot_failure_cooldown_minutes:
+ default: 60
+ type: integer
+ min: 1
+ max: 10080
+ hidden: true
+ nested_replies_hot_max_pending_topics:
+ default: 1000
+ type: integer
+ min: 1
+ max: 100000
+ hidden: true
+ nested_replies_hot_max_queue_age_minutes:
+ default: 60
+ type: integer
+ min: 1
+ max: 10080
+ hidden: true
+ nested_replies_hot_preload_depth_penalty:
+ default: 0.25
+ type: float
+ min: 0
+ max: 100
+ hidden: true
nested_replies_default_sort:
default: "top"
client: true
type: enum
choices:
- top
+ - hot
- new
- old
area: "posts_and_topics"
diff --git a/db/migrate/20260715134306_create_nested_hot_score_cache.rb b/db/migrate/20260715134306_create_nested_hot_score_cache.rb
new file mode 100644
index 00000000000..8fa67aa95fe
--- /dev/null
+++ b/db/migrate/20260715134306_create_nested_hot_score_cache.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class CreateNestedHotScoreCache < ActiveRecord::Migration[8.0]
+ def up
+ create_table :nested_hot_score_snapshots, id: false do |table|
+ table.bigint :topic_id, null: false
+ table.datetime :calculated_at, null: false
+ end
+
+ add_index :nested_hot_score_snapshots, :topic_id, unique: true
+ add_index :nested_hot_score_snapshots, :calculated_at
+
+ create_table :nested_hot_post_scores, id: false do |table|
+ table.bigint :post_id, null: false
+ table.bigint :topic_id, null: false
+ table.float :hot_score, null: false
+ table.float :thread_hot_score, null: false
+ end
+
+ add_index :nested_hot_post_scores, :post_id, unique: true
+ add_index :nested_hot_post_scores, :topic_id
+ end
+
+ def down
+ drop_table :nested_hot_post_scores
+ drop_table :nested_hot_score_snapshots
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 7a19a89e6de..2527876e2ae 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -6948,6 +6948,28 @@ CREATE SEQUENCE public.muted_users_id_seq
ALTER SEQUENCE public.muted_users_id_seq OWNED BY public.muted_users.id;
+--
+-- Name: nested_hot_post_scores; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.nested_hot_post_scores (
+ post_id bigint NOT NULL,
+ topic_id bigint NOT NULL,
+ hot_score double precision NOT NULL,
+ thread_hot_score double precision NOT NULL
+);
+
+
+--
+-- Name: nested_hot_score_snapshots; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.nested_hot_score_snapshots (
+ topic_id bigint NOT NULL,
+ calculated_at timestamp(6) without time zone NOT NULL
+);
+
+
--
-- Name: nested_topics; Type: TABLE; Schema: public; Owner: -
--
@@ -19960,6 +19982,34 @@ CREATE UNIQUE INDEX index_muted_users_on_muted_user_id_and_user_id ON public.mut
CREATE UNIQUE INDEX index_muted_users_on_user_id_and_muted_user_id ON public.muted_users USING btree (user_id, muted_user_id);
+--
+-- Name: index_nested_hot_post_scores_on_post_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX index_nested_hot_post_scores_on_post_id ON public.nested_hot_post_scores USING btree (post_id);
+
+
+--
+-- Name: index_nested_hot_post_scores_on_topic_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_nested_hot_post_scores_on_topic_id ON public.nested_hot_post_scores USING btree (topic_id);
+
+
+--
+-- Name: index_nested_hot_score_snapshots_on_calculated_at; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_nested_hot_score_snapshots_on_calculated_at ON public.nested_hot_score_snapshots USING btree (calculated_at);
+
+
+--
+-- Name: index_nested_hot_score_snapshots_on_topic_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX index_nested_hot_score_snapshots_on_topic_id ON public.nested_hot_score_snapshots USING btree (topic_id);
+
+
--
-- Name: index_nested_topics_on_topic_id; Type: INDEX; Schema: public; Owner: -
--
@@ -22651,6 +22701,7 @@ SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20260715183411'),
+('20260715134306'),
('20260715090434'),
('20260715090355'),
('20260715064155'),
diff --git a/frontend/discourse/app/components/nested.gjs b/frontend/discourse/app/components/nested.gjs
index 5169829c795..ee1a4183ac9 100644
--- a/frontend/discourse/app/components/nested.gjs
+++ b/frontend/discourse/app/components/nested.gjs
@@ -761,7 +761,7 @@ export default class Nested extends Component {
@topic={{@topic}}
@depth={{0}}
@path={{this.ancestorPath}}
- @sort={{@sort}}
+ @sort={{@effectiveSort}}
@replyToPost={{@replyToPost}}
@editPost={{@editPost}}
@deletePost={{@deletePost}}
@@ -901,7 +901,7 @@ export default class Nested extends Component {
@topic={{@topic}}
@depth={{0}}
@path={{this.emptyPath}}
- @sort={{@sort}}
+ @sort={{@effectiveSort}}
@isPinned={{includes @pinnedPostIds node.post.id}}
@replyToPost={{@replyToPost}}
@editPost={{@editPost}}
diff --git a/frontend/discourse/app/components/nested/sort-selector.gjs b/frontend/discourse/app/components/nested/sort-selector.gjs
index 87a5e04928f..6b60ea3273f 100644
--- a/frontend/discourse/app/components/nested/sort-selector.gjs
+++ b/frontend/discourse/app/components/nested/sort-selector.gjs
@@ -1,6 +1,7 @@
import Component from "@glimmer/component";
import { fn } from "@ember/helper";
import { action } from "@ember/object";
+import { service } from "@ember/service";
import DMenu from "discourse/float-kit/components/d-menu";
import { eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
@@ -10,12 +11,21 @@ import { i18n } from "discourse-i18n";
const SORT_OPTIONS = [
{ value: "top", labelKey: "nested_replies.sort.top" },
+ { value: "hot", labelKey: "nested_replies.sort.hot" },
{ value: "new", labelKey: "nested_replies.sort.new" },
{ value: "old", labelKey: "nested_replies.sort.old" },
];
export default class NestedSortSelector extends Component {
- sortOptions = SORT_OPTIONS;
+ @service siteSettings;
+
+ get sortOptions() {
+ return SORT_OPTIONS.filter(
+ (option) =>
+ option.value !== "hot" ||
+ this.siteSettings.nested_replies_hot_sort_enabled
+ );
+ }
get currentSortLabel() {
const current = this.sortOptions.find(
diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.js
index 316c0e56973..044f30dd61a 100644
--- a/frontend/discourse/app/controllers/nested.js
+++ b/frontend/discourse/app/controllers/nested.js
@@ -43,6 +43,7 @@ export default class NestedController extends Controller {
@tracked hasMoreRoots = false;
@tracked loadingMore = false;
@tracked sort;
+ @tracked effectiveSort;
@tracked messageBusLastId;
@tracked postNumber;
@tracked context = null;
@@ -258,7 +259,7 @@ export default class NestedController extends Controller {
const nextPage = this.page + 1;
const query = new URLSearchParams({
page: nextPage,
- sort: this.sort || "top",
+ sort: this.effectiveSort || this.sort || "top",
});
const data = await ajax(
`/n/${this.topic.slug}/${this.topic.id}.json?${query}`
@@ -366,6 +367,7 @@ export default class NestedController extends Controller {
page: this.page,
hasMoreRoots: this.hasMoreRoots,
sort: this.sort,
+ effectiveSort: this.effectiveSort,
messageBusLastId: this.messageBusLastId,
pinnedPostIds: this.pinnedPostIds,
postNumber: this.postNumber,
diff --git a/frontend/discourse/app/lib/nested-topic-model.js b/frontend/discourse/app/lib/nested-topic-model.js
index 8f3aca38adc..9d398bf81bb 100644
--- a/frontend/discourse/app/lib/nested-topic-model.js
+++ b/frontend/discourse/app/lib/nested-topic-model.js
@@ -23,6 +23,11 @@ export function processNestedRootResponse({
page: data.page || 0,
hasMoreRoots: data.has_more_roots || false,
sort: data.sort || siteSettings.nested_replies_default_sort || "top",
+ effectiveSort:
+ data.effective_sort ||
+ data.sort ||
+ siteSettings.nested_replies_default_sort ||
+ "top",
messageBusLastId: data.message_bus_last_id,
pinnedPostIds: data.pinned_post_ids || [],
postNumber: params.post_number ? Number(params.post_number) : null,
@@ -80,6 +85,7 @@ export function processNestedContextResponse({
topic,
opPost,
sort,
+ effectiveSort: data.effective_sort || sort,
pinnedPostIds: [],
messageBusLastId: data.message_bus_last_id,
postNumber: Number(params.post_number),
diff --git a/frontend/discourse/app/lib/nested-view-cache-snapshot.js b/frontend/discourse/app/lib/nested-view-cache-snapshot.js
index 8c27ad41bd0..f192522d0a5 100644
--- a/frontend/discourse/app/lib/nested-view-cache-snapshot.js
+++ b/frontend/discourse/app/lib/nested-view-cache-snapshot.js
@@ -1,6 +1,6 @@
import { enumerateTrackedEntries } from "discourse/lib/tracked-tools";
-export const NESTED_VIEW_CACHE_FORMAT_VERSION = 2;
+export const NESTED_VIEW_CACHE_FORMAT_VERSION = 3;
const EXCLUDED_RECORD_KEYS = new Set([
"__munge",
@@ -24,6 +24,7 @@ export function snapshotNestedModelData(modelData) {
page: modelData.page,
hasMoreRoots: modelData.hasMoreRoots,
sort: modelData.sort,
+ effectiveSort: modelData.effectiveSort || modelData.sort,
messageBusLastId: modelData.messageBusLastId,
pinnedPostIds: snapshotValue(modelData.pinnedPostIds),
postNumber: modelData.postNumber,
@@ -59,6 +60,7 @@ export function hydrateNestedModelData(store, snapshot) {
page: snapshot.page,
hasMoreRoots: snapshot.hasMoreRoots,
sort: snapshot.sort,
+ effectiveSort: snapshot.effectiveSort || snapshot.sort,
messageBusLastId: snapshot.messageBusLastId,
pinnedPostIds: snapshotValue(snapshot.pinnedPostIds) || [],
postNumber: snapshot.postNumber,
diff --git a/frontend/discourse/app/templates/topic.gjs b/frontend/discourse/app/templates/topic.gjs
index a9de9cbcb70..33c2244b578 100644
--- a/frontend/discourse/app/templates/topic.gjs
+++ b/frontend/discourse/app/templates/topic.gjs
@@ -112,6 +112,7 @@ export default <template>
@loadingMore={{nested.loadingMore}}
@loadMoreRoots={{nested.loadMoreRoots}}
@sort={{nested.sort}}
+ @effectiveSort={{nested.effectiveSort}}
@changeSort={{nested.changeSort}}
@replyToPost={{nested.replyToPost}}
@editPost={{nested.editPost}}
diff --git a/frontend/discourse/tests/unit/controllers/nested-test.js b/frontend/discourse/tests/unit/controllers/nested-test.js
index 814251f6ed1..4395ad60f4c 100644
--- a/frontend/discourse/tests/unit/controllers/nested-test.js
+++ b/frontend/discourse/tests/unit/controllers/nested-test.js
@@ -234,6 +234,37 @@ module("Unit | Controller | nested", function (hooks) {
);
});
+ test("root pagination uses the effective sort", async function (assert) {
+ const topic = buildTopic(this.store, 724);
+ let requestedSort;
+
+ pretender.get(`/n/${topic.slug}/${topic.id}.json`, (request) => {
+ requestedSort = request.queryParams.sort;
+ return response({ roots: [], page: 1, has_more_roots: false });
+ });
+
+ this.controller.setProperties({
+ topic,
+ page: 0,
+ hasMoreRoots: true,
+ sort: "hot",
+ effectiveSort: "top",
+ });
+
+ await this.controller.loadMoreRoots();
+
+ assert.strictEqual(
+ requestedSort,
+ "top",
+ "continues the ordering selected by the initial response"
+ );
+ assert.strictEqual(
+ this.controller.sort,
+ "hot",
+ "keeps the requested sort selected"
+ );
+ });
+
test("deletePost delegates first post deletion to the topic controller", function (assert) {
const topic = buildTopic(this.store, 724);
const op = buildPost(this.store, topic, 1001, 1);
@@ -393,4 +424,22 @@ module("Unit | Controller | nested", function (hooks) {
);
assert.strictEqual(cached.modelData.context, 0, "preserves context depth");
});
+
+ test("cache snapshots preserve the effective sort", function (assert) {
+ const topic = buildTopic(this.store, 726);
+ const cacheKey = this.nestedViewCache.buildKey(topic.id, { sort: "hot" });
+
+ this.controller.setProperties({
+ topic,
+ sort: "hot",
+ effectiveSort: "top",
+ });
+ this.controller.saveToCache();
+
+ assert.strictEqual(
+ this.nestedViewCache.get(cacheKey).modelData.effectiveSort,
+ "top",
+ "restored pagination continues using the original effective sort"
+ );
+ });
});
diff --git a/frontend/discourse/tests/unit/lib/nested-topic-model-test.js b/frontend/discourse/tests/unit/lib/nested-topic-model-test.js
new file mode 100644
index 00000000000..c0c77f4cc5c
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/nested-topic-model-test.js
@@ -0,0 +1,29 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { processNestedRootResponse } from "discourse/lib/nested-topic-model";
+
+module("Unit | Lib | nested-topic-model", function (hooks) {
+ setupTest(hooks);
+
+ test("preserves requested and effective sorts", function (assert) {
+ const model = processNestedRootResponse({
+ data: {
+ topic: { id: 1, slug: "nested-topic" },
+ roots: [],
+ sort: "hot",
+ effective_sort: "top",
+ },
+ params: {},
+ site: this.owner.lookup("service:site"),
+ siteSettings: this.owner.lookup("service:site-settings"),
+ store: this.owner.lookup("service:store"),
+ });
+
+ assert.strictEqual(model.sort, "hot", "keeps Hot selected");
+ assert.strictEqual(
+ model.effectiveSort,
+ "top",
+ "records the ordering used by the server"
+ );
+ });
+});
diff --git a/lib/backup_restore/creator.rb b/lib/backup_restore/creator.rb
index 2411683959f..f91ac0cf95c 100644
--- a/lib/backup_restore/creator.rb
+++ b/lib/backup_restore/creator.rb
@@ -184,6 +184,8 @@ module BackupRestore
"pg_dump", # the pg_dump command
"--schema=public", # only public schema
"-T public.pg_*", # exclude tables and views whose name starts with "pg_"
+ "--exclude-table-data=public.nested_hot_post_scores",
+ "--exclude-table-data=public.nested_hot_score_snapshots",
"--file='#{@dump_filename}'", # output to the dump.sql file
"--no-owner", # do not output commands to set ownership of objects
"--no-privileges", # prevent dumping of access privileges
diff --git a/lib/nested_replies.rb b/lib/nested_replies.rb
index de55d1fcddc..19a80e65613 100644
--- a/lib/nested_replies.rb
+++ b/lib/nested_replies.rb
@@ -5,6 +5,9 @@ module NestedReplies
end
require_relative "nested_replies/ancestor_walker"
+require_relative "nested_replies/hot_score_calculator"
+require_relative "nested_replies/hot_score_queue"
+require_relative "nested_replies/hot_score_cache"
require_relative "nested_replies/sort"
require_relative "nested_replies/tree_loader"
require_relative "nested_replies/post_preloader"
diff --git a/lib/nested_replies/hot_score_cache.rb b/lib/nested_replies/hot_score_cache.rb
new file mode 100644
index 00000000000..b0f9fe7a5ea
--- /dev/null
+++ b/lib/nested_replies/hot_score_cache.rb
@@ -0,0 +1,163 @@
+# frozen_string_literal: true
+
+module NestedReplies
+ module HotScoreCache
+ Decision = Struct.new(:effective_sort, :mode, :enqueue_result, keyword_init: true)
+
+ PURGE_SCORE_BATCH_SIZE = 10_000
+ PURGE_SNAPSHOT_BATCH_SIZE = 100
+ MAX_PURGE_STATEMENT_TIMEOUT_MS = 1_000
+
+ def self.small_topic_post_limit
+ SiteSetting.nested_replies_hot_small_topic_post_limit
+ end
+
+ def self.snapshot_ttl
+ SiteSetting.nested_replies_hot_snapshot_ttl_minutes.minutes
+ end
+
+ def self.max_stale_age
+ SiteSetting.nested_replies_hot_max_stale_age_days.days
+ end
+
+ def self.active_topic?(topic)
+ topic.last_posted_at.present? &&
+ topic.last_posted_at >= SiteSetting.nested_replies_hot_max_inactivity_days.days.ago
+ end
+
+ def self.resolve(topic, requested_sort, requester: nil)
+ return Decision.new(effective_sort: requested_sort, mode: :not_hot) if requested_sort != "hot"
+
+ if !SiteSetting.nested_replies_enabled || !SiteSetting.nested_replies_hot_sort_enabled
+ return record(Decision.new(effective_sort: "top", mode: :disabled))
+ end
+
+ if topic.deleted_at.present? || !topic.regular? || !topic.nested_view?
+ return record(Decision.new(effective_sort: "top", mode: :ineligible))
+ end
+
+ if topic.posts_count.to_i <= small_topic_post_limit
+ return record(Decision.new(effective_sort: "top", mode: :small_topic))
+ end
+
+ unless active_topic?(topic)
+ return record(Decision.new(effective_sort: "top", mode: :inactive_topic))
+ end
+
+ snapshot = snapshot(topic.id)
+ return fallback_with_refresh(topic.id, requester, mode: :missing) if snapshot.blank?
+
+ calculated_at = snapshot.calculated_at
+ if calculated_at.blank? || calculated_at < max_stale_age.ago
+ return fallback_with_refresh(topic.id, requester, mode: :expired)
+ end
+
+ if calculated_at < snapshot_ttl.ago
+ enqueue_result = request_refresh(topic.id, requester)
+ return(
+ record(Decision.new(effective_sort: "hot", mode: :stale, enqueue_result: enqueue_result))
+ )
+ end
+
+ record(Decision.new(effective_sort: "hot", mode: :fresh))
+ end
+
+ def self.effective_sort(topic, requested_sort, requester: nil)
+ resolve(topic, requested_sort, requester: requester).effective_sort
+ end
+
+ def self.fresh?(topic)
+ snapshot = snapshot(topic.id)
+ snapshot.present? && snapshot.calculated_at.present? &&
+ snapshot.calculated_at >= snapshot_ttl.ago
+ end
+
+ def self.snapshot(topic_id)
+ DB.query(<<~SQL, topic_id: topic_id).first
+ SELECT calculated_at
+ FROM nested_hot_score_snapshots
+ WHERE topic_id = :topic_id
+ SQL
+ end
+
+ def self.purge_expired(cutoff: nil, timeout_ms: MAX_PURGE_STATEMENT_TIMEOUT_MS)
+ cutoff ||= max_stale_age.ago
+ timeout_ms = timeout_ms.to_i.clamp(1, MAX_PURGE_STATEMENT_TIMEOUT_MS)
+ ActiveRecord::Base.transaction do
+ DB.exec "SET LOCAL statement_timeout = #{timeout_ms}"
+ DB.exec(
+ "SET LOCAL lock_timeout = #{[timeout_ms, SiteSetting.nested_replies_hot_lock_timeout_ms].min}",
+ )
+
+ scores_removed = DB.exec(<<~SQL, cutoff: cutoff, batch_size: PURGE_SCORE_BATCH_SIZE)
+ WITH scores_to_remove AS MATERIALIZED (
+ SELECT scores.post_id
+ FROM nested_hot_post_scores scores
+ LEFT JOIN nested_hot_score_snapshots snapshots
+ ON snapshots.topic_id = scores.topic_id
+ WHERE snapshots.topic_id IS NULL
+ OR snapshots.calculated_at < :cutoff
+ ORDER BY snapshots.calculated_at NULLS FIRST, scores.post_id
+ LIMIT :batch_size
+ )
+ DELETE FROM nested_hot_post_scores scores
+ USING scores_to_remove
+ WHERE scores.post_id = scores_to_remove.post_id
+ SQL
+
+ snapshots_removed = DB.exec(<<~SQL, cutoff: cutoff, batch_size: PURGE_SNAPSHOT_BATCH_SIZE)
+ WITH snapshots_to_remove AS MATERIALIZED (
+ SELECT snapshots.topic_id
+ FROM nested_hot_score_snapshots snapshots
+ WHERE snapshots.calculated_at < :cutoff
+ AND NOT EXISTS (
+ SELECT 1
+ FROM nested_hot_post_scores scores
+ WHERE scores.topic_id = snapshots.topic_id
+ )
+ ORDER BY snapshots.calculated_at
+ LIMIT :batch_size
+ )
+ DELETE FROM nested_hot_score_snapshots snapshots
+ USING snapshots_to_remove
+ WHERE snapshots.topic_id = snapshots_to_remove.topic_id
+ SQL
+
+ { scores_removed: scores_removed, snapshots_removed: snapshots_removed }
+ end
+ end
+
+ def self.fallback_with_refresh(topic_id, requester, mode:)
+ enqueue_result = request_refresh(topic_id, requester)
+ record(Decision.new(effective_sort: "top", mode: mode, enqueue_result: enqueue_result))
+ end
+ private_class_method :fallback_with_refresh
+
+ def self.request_refresh(topic_id, requester)
+ allowed =
+ RateLimiter.new(
+ requester,
+ "nested-hot-score-refresh",
+ SiteSetting.nested_replies_hot_refresh_requests_per_minute,
+ 1.minute,
+ apply_limit_to_staff: true,
+ ).performed!(raise_error: false)
+ return :requester_limited unless allowed
+
+ HotScoreQueue.enqueue(topic_id)
+ rescue Redis::BaseError
+ :unavailable
+ end
+ private_class_method :request_refresh
+
+ def self.record(decision)
+ DiscourseEvent.trigger(
+ :nested_replies_hot_sort_resolved,
+ { mode: decision.mode, enqueue_result: decision.enqueue_result },
+ continue_on_error: true,
+ )
+ decision
+ end
+ private_class_method :record
+ end
+end
diff --git a/lib/nested_replies/hot_score_calculator.rb b/lib/nested_replies/hot_score_calculator.rb
new file mode 100644
index 00000000000..29de67b015b
--- /dev/null
+++ b/lib/nested_replies/hot_score_calculator.rb
@@ -0,0 +1,321 @@
+# frozen_string_literal: true
+
+module NestedReplies
+ class HotScoreCalculator
+ class InvalidTree < StandardError
+ end
+
+ class MissingOriginalPost < StandardError
+ end
+
+ HOT_SCORE_FLOOR = 0.0
+
+ def self.formula_settings
+ {
+ like_weight: SiteSetting.nested_replies_hot_like_weight.to_f,
+ reply_weight: SiteSetting.nested_replies_hot_reply_weight.to_f,
+ freshness_max_bonus: SiteSetting.nested_replies_hot_freshness_max_bonus.to_f,
+ freshness_half_life_seconds:
+ SiteSetting.nested_replies_hot_freshness_half_life_hours.to_f.hours.to_f,
+ child_penalty: SiteSetting.nested_replies_hot_child_penalty.to_f,
+ }
+ end
+
+ def self.public_post_types
+ [Post.types[:regular], Post.types[:moderator_action]]
+ end
+
+ def self.carrier_post_sql(table_name)
+ "#{table_name}.post_type IN (#{public_post_types.join(", ")})"
+ end
+
+ def self.public_post_sql(table_name)
+ <<~SQL.squish
+ #{carrier_post_sql(table_name)}
+ AND #{table_name}.deleted_at IS NULL
+ AND NOT #{table_name}.hidden
+ AND NOT #{table_name}.user_deleted
+ SQL
+ end
+
+ def self.hot_score_sql(
+ table_name,
+ reply_count_sql: "0",
+ now_sql: "CURRENT_TIMESTAMP",
+ formula: formula_settings
+ )
+ engagement_sql =
+ "COALESCE(#{table_name}.like_score, 0) * #{formula.fetch(:like_weight)} + " \
+ "COALESCE(#{reply_count_sql}, 0) * #{formula.fetch(:reply_weight)}"
+ age_seconds_sql = "GREATEST(EXTRACT(EPOCH FROM #{now_sql} - #{table_name}.created_at), 0)"
+ freshness_sql =
+ "#{formula.fetch(:freshness_max_bonus)} * " \
+ "POWER(0.5, #{age_seconds_sql} / #{formula.fetch(:freshness_half_life_seconds)})"
+
+ <<~SQL.squish
+ CASE
+ WHEN #{public_post_sql(table_name)}
+ THEN LN(1 + GREATEST(#{engagement_sql}, 0)) + #{freshness_sql}
+ ELSE #{HOT_SCORE_FLOOR}
+ END
+ SQL
+ end
+
+ def self.score_for(post, direct_reply_count: 0, now: Time.current)
+ return HOT_SCORE_FLOOR unless public_post?(post)
+
+ formula = formula_settings
+ engagement =
+ post.like_score.to_f * formula.fetch(:like_weight) +
+ direct_reply_count.to_f * formula.fetch(:reply_weight)
+ age_seconds = [now.to_f - post.created_at.to_f, 0.0].max
+ freshness =
+ formula.fetch(:freshness_max_bonus) *
+ 0.5**(age_seconds / formula.fetch(:freshness_half_life_seconds))
+ Math.log(1 + [engagement, 0.0].max) + freshness
+ end
+
+ def self.public_post?(post)
+ public_post_types.include?(post.post_type) && post.deleted_at.nil? && !post.hidden? &&
+ !post.user_deleted?
+ end
+
+ def self.recalculate_topic(topic_id, timeout_ms: nil)
+ return 0 if topic_id.blank?
+
+ calculated_at = Time.current
+ formula = formula_settings
+ max_timeout_ms = SiteSetting.nested_replies_hot_max_statement_timeout_ms
+ timeout_ms = (timeout_ms || max_timeout_ms).to_i.clamp(1, max_timeout_ms)
+ result =
+ ActiveRecord::Base.transaction do
+ DB.exec "SET LOCAL statement_timeout = #{timeout_ms}"
+ DB.exec(
+ "SET LOCAL lock_timeout = #{[timeout_ms, SiteSetting.nested_replies_hot_lock_timeout_ms].min}",
+ )
+ DB.query(refresh_sql, topic_id: topic_id, calculated_at: calculated_at, **formula).first
+ end
+
+ raise InvalidTree, "Cycle in nested replies for topic #{topic_id}" if result.invalid_tree
+ unless result.snapshot_written
+ raise MissingOriginalPost, "Missing original post for topic #{topic_id}"
+ end
+
+ result.rows_written.to_i
+ end
+
+ def self.refresh_sql
+ @refresh_sql ||=
+ begin
+ own_score_sql =
+ hot_score_sql(
+ "topic_posts",
+ reply_count_sql: "direct_reply_counts.direct_reply_count",
+ now_sql: ":calculated_at::timestamp",
+ formula: {
+ like_weight: ":like_weight",
+ reply_weight: ":reply_weight",
+ freshness_max_bonus: ":freshness_max_bonus",
+ freshness_half_life_seconds: ":freshness_half_life_seconds",
+ },
+ )
+ public_topic_post_sql = public_post_sql("posts")
+ carrier_topic_post_sql = carrier_post_sql("posts")
+ carrier_parent_post_sql = carrier_post_sql("parent")
+
+ <<~SQL
+ WITH RECURSIVE
+ topic_posts AS MATERIALIZED (
+ SELECT posts.id,
+ posts.post_number,
+ posts.reply_to_post_number,
+ posts.post_type,
+ posts.deleted_at,
+ posts.hidden,
+ posts.user_deleted,
+ posts.like_score,
+ posts.created_at,
+ (#{public_topic_post_sql}) AS is_public,
+ (#{carrier_topic_post_sql}) AS is_carrier
+ FROM posts
+ WHERE posts.topic_id = :topic_id
+ ),
+ direct_reply_counts AS (
+ SELECT child.reply_to_post_number,
+ COUNT(*) FILTER (WHERE child.is_public) AS direct_reply_count
+ FROM topic_posts child
+ WHERE child.post_number > 1
+ AND child.reply_to_post_number IS NOT NULL
+ GROUP BY child.reply_to_post_number
+ ),
+ scored_posts AS MATERIALIZED (
+ SELECT topic_posts.id,
+ topic_posts.post_number,
+ topic_posts.reply_to_post_number,
+ topic_posts.is_public,
+ topic_posts.is_carrier,
+ CASE
+ WHEN topic_posts.post_number = 1 THEN NULL
+ ELSE #{own_score_sql}
+ END AS hot_score
+ FROM topic_posts
+ LEFT JOIN direct_reply_counts
+ ON direct_reply_counts.reply_to_post_number = topic_posts.post_number
+ ),
+ cycle_candidates AS (
+ SELECT topic_posts.post_number,
+ topic_posts.reply_to_post_number
+ FROM topic_posts
+ WHERE topic_posts.post_number > 1
+ AND topic_posts.reply_to_post_number >= topic_posts.post_number
+ ),
+ cycle_walk (
+ current_post_number,
+ parent_post_number,
+ path,
+ cycle
+ ) AS (
+ SELECT cycle_candidates.post_number,
+ cycle_candidates.reply_to_post_number,
+ ARRAY[cycle_candidates.post_number]::integer[],
+ false
+ FROM cycle_candidates
+
+ UNION ALL
+
+ SELECT parent.post_number,
+ parent.reply_to_post_number,
+ cycle_walk.path || parent.post_number,
+ parent.post_number = ANY(cycle_walk.path)
+ FROM cycle_walk
+ JOIN LATERAL (
+ SELECT posts.post_number,
+ posts.reply_to_post_number
+ FROM posts
+ WHERE posts.topic_id = :topic_id
+ AND posts.post_number = cycle_walk.parent_post_number
+ LIMIT 1
+ ) parent ON true
+ WHERE cycle_walk.parent_post_number > 1
+ AND NOT cycle_walk.cycle
+ ),
+ tree_validation AS MATERIALIZED (
+ SELECT NOT EXISTS (
+ SELECT 1
+ FROM cycle_walk
+ WHERE cycle_walk.cycle
+ ) AS valid
+ ),
+ original_post AS MATERIALIZED (
+ SELECT topic_posts.id
+ FROM topic_posts
+ WHERE topic_posts.post_number = 1
+ LIMIT 1
+ ),
+ propagation (
+ target_post_id,
+ parent_post_number,
+ propagated_score,
+ path
+ ) AS (
+ SELECT scored_posts.id,
+ scored_posts.reply_to_post_number,
+ scored_posts.hot_score,
+ ARRAY[scored_posts.post_number]::integer[]
+ FROM scored_posts
+ CROSS JOIN tree_validation
+ WHERE scored_posts.post_number > 1
+ AND scored_posts.is_public
+ AND tree_validation.valid
+
+ UNION ALL
+
+ SELECT parent.id,
+ parent.reply_to_post_number,
+ propagation.propagated_score - :child_penalty,
+ propagation.path || parent.post_number
+ FROM propagation
+ JOIN posts parent
+ ON parent.topic_id = :topic_id
+ AND parent.post_number = propagation.parent_post_number
+ WHERE propagation.parent_post_number > 1
+ AND (#{carrier_parent_post_sql})
+ AND propagation.propagated_score > :child_penalty
+ AND NOT parent.post_number = ANY(propagation.path)
+ ),
+ thread_scores AS (
+ SELECT propagation.target_post_id AS post_id,
+ MAX(propagation.propagated_score) AS thread_hot_score
+ FROM propagation
+ GROUP BY propagation.target_post_id
+ ),
+ calculated_scores AS MATERIALIZED (
+ SELECT scored_posts.id AS post_id,
+ scored_posts.hot_score,
+ GREATEST(
+ scored_posts.hot_score,
+ COALESCE(thread_scores.thread_hot_score, #{HOT_SCORE_FLOOR})
+ ) AS thread_hot_score
+ FROM scored_posts
+ LEFT JOIN thread_scores ON thread_scores.post_id = scored_posts.id
+ WHERE scored_posts.post_number > 1
+ AND scored_posts.is_carrier
+ ),
+ upserted_scores AS (
+ INSERT INTO nested_hot_post_scores (
+ post_id,
+ topic_id,
+ hot_score,
+ thread_hot_score
+ )
+ SELECT calculated_scores.post_id,
+ :topic_id,
+ calculated_scores.hot_score,
+ calculated_scores.thread_hot_score
+ FROM calculated_scores
+ CROSS JOIN tree_validation
+ WHERE tree_validation.valid
+ AND EXISTS (SELECT 1 FROM original_post)
+ ON CONFLICT (post_id) DO UPDATE SET
+ topic_id = EXCLUDED.topic_id,
+ hot_score = EXCLUDED.hot_score,
+ thread_hot_score = EXCLUDED.thread_hot_score
+ RETURNING post_id
+ ),
+ removed_scores AS (
+ DELETE FROM nested_hot_post_scores cached_scores
+ WHERE cached_scores.topic_id = :topic_id
+ AND (SELECT valid FROM tree_validation)
+ AND EXISTS (SELECT 1 FROM original_post)
+ AND NOT EXISTS (
+ SELECT 1
+ FROM calculated_scores
+ WHERE calculated_scores.post_id = cached_scores.post_id
+ )
+ RETURNING post_id
+ ),
+ upserted_snapshot AS (
+ INSERT INTO nested_hot_score_snapshots (
+ topic_id,
+ calculated_at
+ )
+ SELECT :topic_id,
+ :calculated_at::timestamp
+ FROM tree_validation
+ WHERE tree_validation.valid
+ AND EXISTS (SELECT 1 FROM original_post)
+ ON CONFLICT (topic_id) DO UPDATE SET
+ calculated_at = EXCLUDED.calculated_at
+ RETURNING topic_id
+ )
+ SELECT (SELECT COUNT(*) FROM upserted_scores) AS rows_written,
+ (SELECT COUNT(*) FROM removed_scores) AS rows_removed,
+ NOT tree_validation.valid AS invalid_tree,
+ EXISTS (SELECT 1 FROM upserted_snapshot) AS snapshot_written
+ FROM tree_validation
+ SQL
+ end
+ end
+ private_class_method :refresh_sql
+ end
+end
diff --git a/lib/nested_replies/hot_score_queue.rb b/lib/nested_replies/hot_score_queue.rb
new file mode 100644
index 00000000000..e3dd57d2b69
--- /dev/null
+++ b/lib/nested_replies/hot_score_queue.rb
@@ -0,0 +1,127 @@
+# frozen_string_literal: true
+
+module NestedReplies
+ module HotScoreQueue
+ QUEUE_KEY = "nested_replies:hot_score_queue"
+ COOLDOWN_KEY = "nested_replies:hot_score_cooldowns"
+ CLEANUP_CLAIM_KEY = "nested_replies:hot_score_cleanup_claim"
+ CLEANUP_INTERVAL = 1.hour
+
+ ENQUEUE_SCRIPT = DiscourseRedis::EvalHelper.new <<~LUA
+ local topic_id = ARGV[1]
+ local now = tonumber(ARGV[2])
+ local capacity = tonumber(ARGV[3])
+ local oldest_allowed = tonumber(ARGV[4])
+
+ redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", oldest_allowed)
+ redis.call("ZREMRANGEBYSCORE", KEYS[2], "-inf", now)
+
+ if redis.call("ZSCORE", KEYS[1], topic_id) then
+ return 0
+ end
+
+ if redis.call("ZSCORE", KEYS[2], topic_id) then
+ return -2
+ end
+
+ if redis.call("ZCARD", KEYS[1]) >= capacity then
+ return -1
+ end
+
+ redis.call("ZADD", KEYS[1], now, topic_id)
+ return 1
+ LUA
+
+ POP_SCRIPT = DiscourseRedis::EvalHelper.new <<~LUA
+ redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", ARGV[1])
+
+ local members = redis.call("ZRANGE", KEYS[1], 0, 0)
+ if #members == 0 then
+ return nil
+ end
+
+ redis.call("ZREM", KEYS[1], members[1])
+ return members[1]
+ LUA
+
+ def self.enqueue(topic_id, requested_at: Time.current)
+ topic_id = topic_id.to_i
+ return :invalid unless topic_id.positive?
+
+ result =
+ ENQUEUE_SCRIPT.eval(
+ Discourse.redis,
+ [redis_key(QUEUE_KEY), redis_key(COOLDOWN_KEY)],
+ [
+ topic_id,
+ requested_at.to_f,
+ SiteSetting.nested_replies_hot_max_pending_topics,
+ requested_at.to_f - SiteSetting.nested_replies_hot_max_queue_age_minutes.minutes.to_i,
+ ],
+ )
+ return :unavailable if result.nil?
+
+ { -2 => :cooldown, -1 => :full, 0 => :duplicate, 1 => :queued }.fetch(
+ result.to_i,
+ :unavailable,
+ )
+ rescue Redis::BaseError
+ :unavailable
+ end
+
+ def self.pop(now: Time.current)
+ POP_SCRIPT.eval(
+ Discourse.redis,
+ [redis_key(QUEUE_KEY)],
+ [now.to_f - SiteSetting.nested_replies_hot_max_queue_age_minutes.minutes.to_i],
+ )&.to_i
+ rescue Redis::BaseError
+ nil
+ end
+
+ def self.cooldown(topic_id, duration:, now: Time.current)
+ topic_id = topic_id.to_i
+ return if !topic_id.positive? || duration.to_i <= 0
+
+ Discourse.redis.zadd(COOLDOWN_KEY, now.to_f + duration.to_i, topic_id)
+ rescue Redis::BaseError
+ nil
+ end
+
+ def self.clear_cooldown(topic_id)
+ Discourse.redis.zrem(COOLDOWN_KEY, topic_id.to_i)
+ rescue Redis::BaseError
+ nil
+ end
+
+ def self.size
+ Discourse.redis.zcard(QUEUE_KEY).to_i
+ rescue Redis::BaseError
+ 0
+ end
+
+ def self.oldest_age(now: Time.current)
+ first_entry = Discourse.redis.zrange(QUEUE_KEY, 0, 0, with_scores: true).first
+ return 0.0 if first_entry.blank?
+
+ [now.to_f - first_entry.last.to_f, 0.0].max
+ rescue Redis::BaseError
+ 0.0
+ end
+
+ def self.claim_cleanup
+ !!Discourse.redis.set(CLEANUP_CLAIM_KEY, "1", nx: true, ex: CLEANUP_INTERVAL.to_i)
+ rescue Redis::BaseError
+ false
+ end
+
+ def self.clear
+ Discourse.redis.del(QUEUE_KEY, COOLDOWN_KEY, CLEANUP_CLAIM_KEY)
+ end
+
+ def self.redis_key(key)
+ Discourse.redis.namespace_key(key)
+ end
+ private_class_method :redis_key
+ end
+end
diff --git a/lib/nested_replies/sort.rb b/lib/nested_replies/sort.rb
index a6d7634c124..0c08046b429 100644
--- a/lib/nested_replies/sort.rb
+++ b/lib/nested_replies/sort.rb
@@ -2,38 +2,73 @@
module NestedReplies
module Sort
- ALGORITHMS = %w[top new old].freeze
+ ALGORITHMS = %w[top hot new old].freeze
- def self.sql_order_expression(algorithm)
+ def self.sql_order_expression(algorithm, posts_table: "posts")
raise ArgumentError, "Invalid sort algorithm: #{algorithm}" unless valid?(algorithm)
case algorithm
when "top"
- "like_count DESC, post_number ASC"
+ "#{posts_table}.like_count DESC, #{posts_table}.post_number ASC"
+ when "hot"
+ "#{hot_score_expression(posts_table, :thread_hot_score)} DESC, " \
+ "#{hot_score_expression(posts_table, :hot_score)} DESC, " \
+ "#{posts_table}.post_number ASC"
when "new"
- "created_at DESC"
+ "#{posts_table}.created_at DESC"
when "old"
- "post_number ASC"
+ "#{posts_table}.post_number ASC"
end
end
def self.apply(scope, algorithm)
+ scope = scope.joins(hot_score_join_sql) if algorithm == "hot"
scope.order(Arel.sql(sql_order_expression(algorithm)))
end
- def self.sort_in_memory(posts, algorithm)
+ def self.hot_score_join_sql(posts_table: "posts")
+ <<~SQL.squish
+ LEFT JOIN nested_hot_post_scores
+ ON nested_hot_post_scores.post_id = #{posts_table}.id
+ SQL
+ end
+
+ def self.hot_score_expression(posts_table, column)
+ fallback = HotScoreCalculator.hot_score_sql(posts_table)
+ "COALESCE(nested_hot_post_scores.#{column}, #{fallback})"
+ end
+
+ def self.sort_in_memory(posts, algorithm, hot_scores: nil)
raise ArgumentError, "Invalid sort algorithm: #{algorithm}" unless valid?(algorithm)
case algorithm
when "top"
- posts.sort_by { |p| [-p.like_count, p.post_number] }
+ posts.sort_by { |post| [-post.like_count, post.post_number] }
+ when "hot"
+ hot_scores ||= {}
+ posts.sort_by do |post|
+ thread_hot_score, hot_score = hot_score_values(post, hot_scores[post.id])
+ [-thread_hot_score, -hot_score, post.post_number]
+ end
when "new"
- posts.sort_by { |p| -p.created_at.to_i }
+ posts.sort_by { |post| -post.created_at.to_i }
when "old"
posts.sort_by(&:post_number)
end
end
+ def self.hot_score_values(post, scores)
+ if scores.present?
+ thread_hot_score, hot_score = scores
+ hot_score = (hot_score || thread_hot_score || 0.0).to_f
+ thread_hot_score = (thread_hot_score || hot_score).to_f
+ [thread_hot_score, hot_score]
+ else
+ hot_score = HotScoreCalculator.score_for(post).to_f
+ [hot_score, hot_score]
+ end
+ end
+
def self.valid?(algorithm)
ALGORITHMS.include?(algorithm)
end
diff --git a/lib/nested_replies/tree_loader.rb b/lib/nested_replies/tree_loader.rb
index 641a15fd89d..11eeeaf2071 100644
--- a/lib/nested_replies/tree_loader.rb
+++ b/lib/nested_replies/tree_loader.rb
@@ -6,6 +6,8 @@ module NestedReplies
ROOTS_PER_PAGE = 20
CHILDREN_PER_PAGE = 50
PRELOAD_CHILDREN_PER_PARENT = 3
+ HOT_PRELOAD_POST_BUDGET = 60
+ HOT_PRELOAD_PER_ROOT_BUDGET = 15
SIBLINGS_PER_ANCESTOR = 5
POST_INCLUDES = [
@@ -40,10 +42,19 @@ module NestedReplies
scope =
topic
.posts
- .where("reply_to_post_number IS NULL OR reply_to_post_number = 1")
+ .where("posts.reply_to_post_number IS NULL OR posts.reply_to_post_number = 1")
.where(post_number: 2..) # exclude OP itself
scope = apply_visibility(scope)
- NestedReplies::Sort.apply(scope, sort)
+ apply_sort(scope, sort)
+ end
+
+ def apply_sort(scope, sort)
+ NestedReplies::Sort.apply(scope, effective_sort(sort))
+ end
+
+ def effective_sort(sort)
+ @effective_sorts ||= {}
+ @effective_sorts[sort] ||= HotScoreCache.effective_sort(topic, sort, requester: guardian.user)
end
def promote_pinned_roots(roots, pinned_post_ids)
@@ -96,6 +107,9 @@ module NestedReplies
end
def batch_preload_tree(starting_posts, sort, max_depth:)
+ sort = effective_sort(sort)
+ return batch_preload_hot_tree(starting_posts, max_depth: max_depth) if sort == "hot"
+
all_posts = starting_posts.dup
children_map = {}
@@ -110,16 +124,21 @@ module NestedReplies
child_ids =
DB.query_single(
<<~SQL,
- SELECT id FROM (
- SELECT id,
- ROW_NUMBER() OVER (PARTITION BY reply_to_post_number ORDER BY #{order_expr}) AS rn
+ SELECT ranked.id FROM (
+ SELECT posts.id,
+ posts.reply_to_post_number,
+ ROW_NUMBER() OVER (
+ PARTITION BY posts.reply_to_post_number
+ ORDER BY #{order_expr}
+ ) AS rn
FROM posts
- WHERE topic_id = :topic_id
- AND reply_to_post_number IN (:parent_numbers)
- AND post_type IN (:post_types)
- AND post_number > 1
+ WHERE posts.topic_id = :topic_id
+ AND posts.reply_to_post_number IN (:parent_numbers)
+ AND posts.post_type IN (:post_types)
+ AND posts.post_number > 1
) ranked
WHERE rn <= :limit
+ ORDER BY ranked.reply_to_post_number, ranked.rn
SQL
topic_id: topic.id,
parent_numbers: parent_numbers,
@@ -130,12 +149,13 @@ module NestedReplies
break if child_ids.empty?
all_children = load_posts_for_tree(topic.posts.with_deleted.where(id: child_ids)).to_a
+ child_positions = child_ids.each_with_index.to_h
next_level = []
all_children
.group_by(&:reply_to_post_number)
.each do |parent_number, child_posts|
- sorted = NestedReplies::Sort.sort_in_memory(child_posts, sort)
+ sorted = child_posts.sort_by { |child| child_positions.fetch(child.id) }
children_map[parent_number] = sorted
all_posts.concat(sorted)
next_level.concat(sorted) unless last_level
@@ -147,7 +167,201 @@ module NestedReplies
{ children_map: children_map, all_posts: all_posts }
end
+ def batch_preload_hot_tree(starting_posts, max_depth:)
+ all_posts = starting_posts.dup
+ children_map = {}
+ max_preload_depth = [max_depth, configured_max_depth, PRELOAD_DEPTH].min
+ visible_starting_posts =
+ starting_posts.select do |post|
+ visible_post_types.include?(post.post_type) &&
+ (post.post_type != Post.types[:whisper] || post.action_code.blank?)
+ end
+
+ if visible_starting_posts.empty? || max_preload_depth <= 0 || HOT_PRELOAD_POST_BUDGET <= 0 ||
+ HOT_PRELOAD_PER_ROOT_BUDGET <= 0 || PRELOAD_CHILDREN_PER_PARENT <= 0
+ return { children_map: children_map, all_posts: all_posts }
+ end
+
+ # Discover only the three-wide, three-deep score frontier; hydrate posts after the global
+ # and per-root budgets choose which branches should arrive expanded.
+ candidate_rows = hot_preload_candidate_rows(visible_starting_posts, max_preload_depth)
+ root_rows = candidate_rows.select { |row| row.depth.zero? }.index_by(&:post_number)
+ rows_by_parent =
+ candidate_rows.select { |row| row.depth.positive? }.group_by(&:reply_to_post_number)
+ candidates =
+ visible_starting_posts.filter_map do |post|
+ row = root_rows[post.post_number]
+ next if row.blank?
+
+ {
+ post_number: post.post_number,
+ branch_post_number: post.post_number,
+ depth: 0,
+ priority: hot_preload_priority(row.thread_hot_score, 0),
+ thread_hot_score: row.thread_hot_score.to_f,
+ hot_score: row.hot_score.to_f,
+ }
+ end
+
+ preloaded_count = 0
+ branch_preloaded_counts = Hash.new(0)
+ selected_rows_by_parent = {}
+
+ while candidates.present? && preloaded_count < HOT_PRELOAD_POST_BUDGET
+ candidates.sort_by! do |candidate|
+ [
+ -candidate[:priority],
+ -candidate[:thread_hot_score],
+ -candidate[:hot_score],
+ candidate[:post_number],
+ ]
+ end
+
+ candidate = candidates.shift
+ parent_post_number = candidate[:post_number]
+ branch_post_number = candidate[:branch_post_number]
+
+ next if candidate[:depth] >= max_preload_depth
+ next if selected_rows_by_parent.key?(parent_post_number)
+ next if branch_preloaded_counts[branch_post_number] >= HOT_PRELOAD_PER_ROOT_BUDGET
+
+ remaining_total_budget = HOT_PRELOAD_POST_BUDGET - preloaded_count
+ remaining_branch_budget =
+ HOT_PRELOAD_PER_ROOT_BUDGET - branch_preloaded_counts[branch_post_number]
+ limit = [PRELOAD_CHILDREN_PER_PARENT, remaining_total_budget, remaining_branch_budget].min
+ next if limit <= 0
+
+ child_rows = rows_by_parent.fetch(parent_post_number, []).first(limit)
+ next if child_rows.empty?
+
+ selected_rows_by_parent[parent_post_number] = child_rows
+ preloaded_count += child_rows.size
+ branch_preloaded_counts[branch_post_number] += child_rows.size
+
+ child_depth = candidate[:depth] + 1
+ next if child_depth >= max_preload_depth
+
+ child_rows.each do |child|
+ candidates << {
+ post_number: child.post_number,
+ branch_post_number: branch_post_number,
+ depth: child_depth,
+ priority: hot_preload_priority(child.thread_hot_score, child_depth),
+ thread_hot_score: child.thread_hot_score.to_f,
+ hot_score: child.hot_score.to_f,
+ }
+ end
+ end
+
+ selected_ids = selected_rows_by_parent.values.flatten.map(&:id).uniq
+ posts_by_id =
+ if selected_ids.empty?
+ {}
+ else
+ load_posts_for_tree(topic.posts.with_deleted.where(id: selected_ids)).to_a.index_by(&:id)
+ end
+
+ selected_rows_by_parent.each do |selected_parent_post_number, rows|
+ children_map[selected_parent_post_number] = rows.filter_map { |row| posts_by_id[row.id] }
+ end
+ all_posts.concat(selected_ids.filter_map { |post_id| posts_by_id[post_id] })
+
+ { children_map: children_map, all_posts: all_posts }
+ end
+
+ def hot_preload_candidate_rows(starting_posts, max_depth)
+ thread_hot_score = NestedReplies::Sort.hot_score_expression("posts", :thread_hot_score)
+ hot_score = NestedReplies::Sort.hot_score_expression("posts", :hot_score)
+
+ DB.query(
+ <<~SQL,
+ WITH RECURSIVE preload_tree (
+ id,
+ post_number,
+ reply_to_post_number,
+ depth,
+ path,
+ thread_hot_score,
+ hot_score
+ ) AS (
+ SELECT posts.id,
+ posts.post_number,
+ posts.reply_to_post_number,
+ 0,
+ ARRAY[posts.post_number]::integer[],
+ #{thread_hot_score},
+ #{hot_score}
+ FROM posts
+ #{NestedReplies::Sort.hot_score_join_sql}
+ WHERE posts.topic_id = :topic_id
+ AND posts.id IN (:starting_post_ids)
+ AND posts.post_type IN (:post_types)
+ AND (
+ posts.post_type != :whisper_post_type
+ OR posts.action_code IS NULL
+ OR posts.action_code = ''
+ )
+
+ UNION ALL
+
+ SELECT children.id,
+ children.post_number,
+ children.reply_to_post_number,
+ preload_tree.depth + 1,
+ preload_tree.path || children.post_number,
+ children.thread_hot_score,
+ children.hot_score
+ FROM preload_tree
+ CROSS JOIN LATERAL (
+ SELECT posts.id,
+ posts.post_number,
+ posts.reply_to_post_number,
+ #{thread_hot_score} AS thread_hot_score,
+ #{hot_score} AS hot_score
+ FROM posts
+ #{NestedReplies::Sort.hot_score_join_sql}
+ WHERE posts.topic_id = :topic_id
+ AND posts.reply_to_post_number = preload_tree.post_number
+ AND posts.post_number > 1
+ AND posts.post_type IN (:post_types)
+ AND (
+ posts.post_type != :whisper_post_type
+ OR posts.action_code IS NULL
+ OR posts.action_code = ''
+ )
+ AND NOT posts.post_number = ANY(preload_tree.path)
+ ORDER BY #{NestedReplies::Sort.sql_order_expression("hot")}
+ LIMIT :children_per_parent
+ ) children
+ WHERE preload_tree.depth < :max_depth
+ )
+ SELECT id,
+ post_number,
+ reply_to_post_number,
+ depth,
+ thread_hot_score,
+ hot_score
+ FROM preload_tree
+ ORDER BY reply_to_post_number,
+ thread_hot_score DESC,
+ hot_score DESC,
+ post_number ASC
+ SQL
+ topic_id: topic.id,
+ starting_post_ids: starting_posts.map(&:id),
+ post_types: visible_post_types,
+ whisper_post_type: Post.types[:whisper],
+ children_per_parent: PRELOAD_CHILDREN_PER_PARENT,
+ max_depth: max_depth,
+ )
+ end
+
+ def hot_preload_priority(thread_hot_score, depth)
+ thread_hot_score.to_f - SiteSetting.nested_replies_hot_preload_depth_penalty * depth
+ end
+
def batch_load_siblings(ancestors, sort)
+ sort = effective_sort(sort)
root_ancestors, child_ancestors = ancestors.partition { |a| a.reply_to_post_number.nil? }
siblings_map = {}
@@ -156,8 +370,9 @@ module NestedReplies
parent_numbers = child_ancestors.map(&:reply_to_post_number).uniq
order_expr = NestedReplies::Sort.sql_order_expression(sort)
+ hot_join = sort == "hot" ? NestedReplies::Sort.hot_score_join_sql : ""
- visibility_conditions = +"post_type IN (:post_types) AND post_number > 1"
+ visibility_conditions = +"posts.post_type IN (:post_types) AND posts.post_number > 1"
sql_params = {
topic_id: topic.id,
parent_numbers: parent_numbers,
@@ -166,23 +381,31 @@ module NestedReplies
}
sibling_ids = DB.query_single(<<~SQL, **sql_params)
- SELECT id FROM (
- SELECT id, reply_to_post_number,
- ROW_NUMBER() OVER (PARTITION BY reply_to_post_number ORDER BY #{order_expr}) AS rn
+ SELECT ranked.id FROM (
+ SELECT posts.id, posts.reply_to_post_number,
+ ROW_NUMBER() OVER (
+ PARTITION BY posts.reply_to_post_number
+ ORDER BY #{order_expr}
+ ) AS rn
FROM posts
- WHERE topic_id = :topic_id
- AND reply_to_post_number IN (:parent_numbers)
+ #{hot_join}
+ WHERE posts.topic_id = :topic_id
+ AND posts.reply_to_post_number IN (:parent_numbers)
AND #{visibility_conditions}
) ranked
WHERE rn <= :limit
+ ORDER BY ranked.reply_to_post_number, ranked.rn
SQL
if sibling_ids.present?
loaded_siblings =
load_posts_for_tree(topic.posts.with_deleted.where(id: sibling_ids)).to_a
+ sibling_positions = sibling_ids.each_with_index.to_h
grouped = loaded_siblings.group_by(&:reply_to_post_number)
- grouped.transform_values! { |posts| NestedReplies::Sort.sort_in_memory(posts, sort) }
+ grouped.transform_values! do |posts|
+ posts.sort_by { |post| sibling_positions.fetch(post.id) }
+ end
child_ancestors.each do |ancestor|
siblings_map[ancestor.post_number] = grouped[ancestor.reply_to_post_number] || []
@@ -200,8 +423,10 @@ module NestedReplies
end
def flat_descendants_scope(parent_post_number, sort:, offset: 0, limit: CHILDREN_PER_PAGE)
+ sort = effective_sort(sort)
post_types = visible_post_types
- order_expr = NestedReplies::Sort.sql_order_expression(sort)
+ order_expr = NestedReplies::Sort.sql_order_expression(sort, posts_table: "p")
+ hot_join = sort == "hot" ? NestedReplies::Sort.hot_score_join_sql(posts_table: "p") : ""
descendant_post_numbers =
DB.query_single(
@@ -223,6 +448,7 @@ module NestedReplies
SELECT d.post_number
FROM descendants d
JOIN posts p ON p.post_number = d.post_number AND p.topic_id = :topic_id
+ #{hot_join}
WHERE p.post_type IN (:post_types)
ORDER BY #{order_expr}
OFFSET :offset
diff --git a/spec/jobs/scheduled/recalculate_nested_hot_scores_spec.rb b/spec/jobs/scheduled/recalculate_nested_hot_scores_spec.rb
new file mode 100644
index 00000000000..e978f2f2557
--- /dev/null
+++ b/spec/jobs/scheduled/recalculate_nested_hot_scores_spec.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::RecalculateNestedHotScores do
+ before do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ NestedReplies::HotScoreQueue.clear
+ end
+
+ after { NestedReplies::HotScoreQueue.clear }
+
+ def build_eligible_topic
+ topic = Fabricate(:topic)
+ op = Fabricate(:post, topic: topic, post_number: 1)
+ Fabricate(:nested_topic, topic: topic)
+ replies = 5.times.map { Fabricate(:post, topic: topic, reply_to_post_number: op.post_number) }
+ topic.update_columns(posts_count: 6, last_posted_at: Time.current)
+ [topic, replies]
+ end
+
+ def snapshot_topic_ids
+ DB.query_single("SELECT topic_id FROM nested_hot_score_snapshots ORDER BY topic_id")
+ end
+
+ it "leaves queued demand untouched while the default-off gate is disabled" do
+ topic, = build_eligible_topic
+ NestedReplies::HotScoreQueue.enqueue(topic.id)
+ SiteSetting.nested_replies_hot_sort_enabled = false
+
+ described_class.new.execute
+
+ expect(snapshot_topic_ids).to be_empty
+ expect(NestedReplies::HotScoreQueue.pop).to eq(topic.id)
+ end
+
+ it "rebuilds only requested topics regardless of their declared size" do
+ requested_topic, = build_eligible_topic
+ unrequested_topic, = build_eligible_topic
+ requested_topic.update_columns(posts_count: 1_000_000)
+ NestedReplies::HotScoreQueue.enqueue(requested_topic.id)
+
+ events =
+ DiscourseEvent.track_events(:nested_replies_hot_scores_processed) do
+ described_class.new.execute
+ end
+
+ expect(snapshot_topic_ids).to eq([requested_topic.id])
+ expect(snapshot_topic_ids).not_to include(unrequested_topic.id)
+ expect(events.dig(0, :params, 0)).to include(
+ topics_inspected: 1,
+ topics_rebuilt: 1,
+ posts_rebuilt: 5,
+ failures: 0,
+ cooldowns_started: 0,
+ queue_depth: 0,
+ )
+ end
+
+ it "discards queued requests for inactive topics" do
+ inactive_topic, = build_eligible_topic
+ inactive_topic.update_columns(last_posted_at: 31.days.ago)
+ NestedReplies::HotScoreQueue.enqueue(inactive_topic.id)
+
+ described_class.new.execute
+
+ expect(snapshot_topic_ids).to be_empty
+ expect(NestedReplies::HotScoreQueue.size).to eq(0)
+ end
+
+ it "limits the number of queued topics inspected per run" do
+ SiteSetting.nested_replies_hot_max_topics_per_run = 1
+ first_topic, = build_eligible_topic
+ second_topic, = build_eligible_topic
+ NestedReplies::HotScoreQueue.enqueue(first_topic.id)
+ NestedReplies::HotScoreQueue.enqueue(second_topic.id)
+
+ described_class.new.execute
+
+ expect(snapshot_topic_ids).to eq([first_topic.id])
+ expect(NestedReplies::HotScoreQueue.pop).to eq(second_topic.id)
+ end
+
+ it "puts a malformed topic into cooldown instead of retrying every request" do
+ SiteSetting.nested_replies_hot_failure_cooldown_minutes = 120
+ topic, replies = build_eligible_topic
+ replies.first.update_columns(reply_to_post_number: replies.second.post_number)
+ replies.second.update_columns(reply_to_post_number: replies.first.post_number)
+ NestedReplies::HotScoreQueue.enqueue(topic.id)
+ allow(Discourse).to receive(:warn_exception)
+
+ described_class.new.execute
+
+ expect(snapshot_topic_ids).to be_empty
+ expect(NestedReplies::HotScoreQueue.enqueue(topic.id)).to eq(:cooldown)
+ expect(NestedReplies::HotScoreQueue.enqueue(topic.id, requested_at: 61.minutes.from_now)).to eq(
+ :cooldown,
+ )
+ expect(
+ NestedReplies::HotScoreQueue.enqueue(topic.id, requested_at: 121.minutes.from_now),
+ ).to eq(:queued)
+ expect(Discourse).to have_received(:warn_exception) do |error, message:|
+ expect(error).to be_a(NestedReplies::HotScoreCalculator::InvalidTree)
+ expect(message).to eq("Failed to refresh nested hot scores for topic #{topic.id}")
+ end
+ end
+end
diff --git a/spec/lib/backup_restore/creator_spec.rb b/spec/lib/backup_restore/creator_spec.rb
index c8da7facf4b..a40ce60a386 100644
--- a/spec/lib/backup_restore/creator_spec.rb
+++ b/spec/lib/backup_restore/creator_spec.rb
@@ -1,6 +1,17 @@
# frozen_string_literal: true
describe BackupRestore::Creator do
+ describe "#pg_dump_command" do
+ it "excludes disposable nested hot score data" do
+ command = described_class.new(Discourse.system_user.id).send(:pg_dump_command)
+
+ expect(command).to include(
+ "--exclude-table-data=public.nested_hot_post_scores",
+ "--exclude-table-data=public.nested_hot_score_snapshots",
+ )
+ end
+ end
+
describe "#add_remote_uploads_to_archive" do
fab!(:user)
diff --git a/spec/lib/nested_replies/hot_score_cache_spec.rb b/spec/lib/nested_replies/hot_score_cache_spec.rb
new file mode 100644
index 00000000000..a3992483373
--- /dev/null
+++ b/spec/lib/nested_replies/hot_score_cache_spec.rb
@@ -0,0 +1,147 @@
+# frozen_string_literal: true
+
+RSpec.describe NestedReplies::HotScoreCache do
+ fab!(:topic)
+ fab!(:op) { Fabricate(:post, topic: topic, post_number: 1) }
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ topic.update_columns(posts_count: 6, last_posted_at: Time.current)
+ NestedReplies::HotScoreQueue.clear
+ end
+
+ after { NestedReplies::HotScoreQueue.clear }
+
+ def create_topic_with_snapshot(calculated_at:)
+ cached_topic = Fabricate(:topic)
+ cached_post = Fabricate(:post, topic: cached_topic, post_number: 1)
+ Fabricate(:nested_topic, topic: cached_topic)
+ cached_topic.update_columns(posts_count: 6, last_posted_at: Time.current)
+ DB.exec(<<~SQL, topic_id: cached_topic.id, calculated_at: calculated_at)
+ INSERT INTO nested_hot_score_snapshots (topic_id, calculated_at)
+ VALUES (:topic_id, :calculated_at)
+ SQL
+ [cached_topic, cached_post]
+ end
+
+ it "does no cache work for disabled, non-hot, ineligible, or small-topic requests" do
+ SiteSetting.nested_replies_hot_small_topic_post_limit = 6
+ non_hot = described_class.resolve(topic, "new")
+ SiteSetting.nested_replies_hot_sort_enabled = false
+ disabled = described_class.resolve(topic, "hot")
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ ineligible = described_class.resolve(Fabricate(:topic, posts_count: 6), "hot")
+ topic.update_columns(posts_count: SiteSetting.nested_replies_hot_small_topic_post_limit)
+ small_topic = described_class.resolve(topic, "hot")
+
+ expect([non_hot.effective_sort, non_hot.mode]).to eq(["new", :not_hot])
+ expect([disabled.effective_sort, disabled.mode]).to eq(["top", :disabled])
+ expect([ineligible.effective_sort, ineligible.mode]).to eq(["top", :ineligible])
+ expect([small_topic.effective_sort, small_topic.mode]).to eq(["top", :small_topic])
+ expect(NestedReplies::HotScoreQueue.size).to eq(0)
+ end
+
+ it "deduplicates demand for a missing topic without imposing a size ceiling" do
+ topic.update_columns(posts_count: 1_000_000)
+
+ first_decision = described_class.resolve(topic, "hot")
+ second_decision = described_class.resolve(topic, "hot")
+
+ expect(
+ [first_decision.effective_sort, first_decision.mode, first_decision.enqueue_result],
+ ).to eq(["top", :missing, :queued])
+ expect(second_decision.enqueue_result).to eq(:duplicate)
+ expect(NestedReplies::HotScoreQueue.size).to eq(1)
+ end
+
+ it "uses top without cache work after the topic activity window" do
+ cached_topic, = create_topic_with_snapshot(calculated_at: Time.current)
+ topic.update_columns(last_posted_at: 31.days.ago)
+ cached_topic.update_columns(last_posted_at: 31.days.ago)
+
+ decisions = [topic, cached_topic].map { |candidate| described_class.resolve(candidate, "hot") }
+
+ expect(decisions.map { |decision| [decision.effective_sort, decision.mode] }).to eq(
+ [["top", :inactive_topic], ["top", :inactive_topic]],
+ )
+ expect(NestedReplies::HotScoreQueue.size).to eq(0)
+ end
+
+ it "limits how many new refresh requests one requester can admit" do
+ requester = Fabricate(:user)
+ second_topic = Fabricate(:topic)
+ Fabricate(:post, topic: second_topic, post_number: 1)
+ Fabricate(:nested_topic, topic: second_topic)
+ second_topic.update_columns(posts_count: 6, last_posted_at: Time.current)
+ limiter =
+ RateLimiter.new(
+ requester,
+ "nested-hot-score-refresh",
+ 1,
+ 1.minute,
+ apply_limit_to_staff: true,
+ )
+ RateLimiter.enable
+ SiteSetting.nested_replies_hot_refresh_requests_per_minute = 1
+
+ first_decision = described_class.resolve(topic, "hot", requester: requester)
+ second_decision = described_class.resolve(second_topic, "hot", requester: requester)
+
+ expect(first_decision.enqueue_result).to eq(:queued)
+ expect(second_decision.enqueue_result).to eq(:requester_limited)
+ expect(NestedReplies::HotScoreQueue.size).to eq(1)
+ ensure
+ limiter&.clear!
+ RateLimiter.disable
+ end
+
+ it "uses fresh and stale snapshots while rejecting unusable snapshots" do
+ SiteSetting.nested_replies_hot_snapshot_ttl_minutes = 120
+ SiteSetting.nested_replies_hot_max_stale_age_days = 40
+ fresh_topic, = create_topic_with_snapshot(calculated_at: Time.current)
+ stale_topic, = create_topic_with_snapshot(calculated_at: 3.hours.ago)
+ expired_topic, = create_topic_with_snapshot(calculated_at: 41.days.ago)
+
+ decisions =
+ [fresh_topic, stale_topic, expired_topic].map do |cached_topic|
+ described_class.resolve(cached_topic, "hot")
+ end
+
+ expect(decisions.map { |decision| [decision.effective_sort, decision.mode] }).to eq(
+ [["hot", :fresh], ["hot", :stale], ["top", :expired]],
+ )
+ expect(NestedReplies::HotScoreQueue.size).to eq(2)
+ end
+
+ it "purges only expired cache data" do
+ SiteSetting.nested_replies_hot_max_stale_age_days = 10
+ expired_topic, expired_post = create_topic_with_snapshot(calculated_at: 11.days.ago)
+ fresh_topic, fresh_post = create_topic_with_snapshot(calculated_at: Time.current)
+ orphan_topic = Fabricate(:topic)
+ orphan_post = Fabricate(:post, topic: orphan_topic, post_number: 1)
+ DB.exec(
+ <<~SQL,
+ INSERT INTO nested_hot_post_scores (post_id, topic_id, hot_score, thread_hot_score)
+ VALUES (:expired_post_id, :expired_topic_id, 1.0, 2.0),
+ (:fresh_post_id, :fresh_topic_id, 3.0, 4.0),
+ (:orphan_post_id, :orphan_topic_id, 5.0, 6.0)
+ SQL
+ expired_post_id: expired_post.id,
+ expired_topic_id: expired_topic.id,
+ fresh_post_id: fresh_post.id,
+ fresh_topic_id: fresh_topic.id,
+ orphan_post_id: orphan_post.id,
+ orphan_topic_id: orphan_topic.id,
+ )
+
+ result = described_class.purge_expired
+
+ expect(result).to eq(scores_removed: 2, snapshots_removed: 1)
+ expect(DB.query_single("SELECT topic_id FROM nested_hot_score_snapshots")).to eq(
+ [fresh_topic.id],
+ )
+ expect(DB.query_single("SELECT post_id FROM nested_hot_post_scores")).to eq([fresh_post.id])
+ end
+end
diff --git a/spec/lib/nested_replies/hot_score_calculator_spec.rb b/spec/lib/nested_replies/hot_score_calculator_spec.rb
new file mode 100644
index 00000000000..85519805ad7
--- /dev/null
+++ b/spec/lib/nested_replies/hot_score_calculator_spec.rb
@@ -0,0 +1,142 @@
+# frozen_string_literal: true
+
+RSpec.describe NestedReplies::HotScoreCalculator do
+ fab!(:topic)
+ fab!(:op) { Fabricate(:post, topic: topic, post_number: 1) }
+
+ def set_score_inputs(post, created_at:, like_score: 0)
+ post.update_columns(created_at: created_at, like_score: like_score)
+ end
+
+ def cached_score(post)
+ DB.query(<<~SQL, post_id: post.id).first
+ SELECT hot_score,
+ thread_hot_score
+ FROM nested_hot_post_scores
+ WHERE post_id = :post_id
+ SQL
+ end
+
+ describe ".score_for" do
+ it "combines engagement and freshness while excluding non-public posts" do
+ SiteSetting.nested_replies_hot_like_weight = 1.5
+ SiteSetting.nested_replies_hot_reply_weight = 2.5
+ SiteSetting.nested_replies_hot_freshness_max_bonus = 4.0
+ SiteSetting.nested_replies_hot_freshness_half_life_hours = 48
+ now = Time.zone.local(2026, 7, 10, 12)
+ public_post = Fabricate(:post, topic: topic, reply_to_post_number: op.post_number)
+ hidden_post = Fabricate(:post, topic: topic, hidden: true)
+ set_score_inputs(public_post, created_at: now - 2.days, like_score: 3)
+ set_score_inputs(hidden_post, created_at: now, like_score: 100)
+
+ engagement =
+ 3 * SiteSetting.nested_replies_hot_like_weight +
+ 2 * SiteSetting.nested_replies_hot_reply_weight
+ freshness =
+ SiteSetting.nested_replies_hot_freshness_max_bonus *
+ 0.5**(2.days.to_f / SiteSetting.nested_replies_hot_freshness_half_life_hours.hours.to_f)
+
+ expect(described_class.score_for(public_post, direct_reply_count: 2, now: now)).to be_within(
+ 0.0001,
+ ).of(Math.log(1 + engagement) + freshness)
+ expect(described_class.score_for(hidden_post, now: now)).to eq(
+ described_class::HOT_SCORE_FLOOR,
+ )
+ end
+ end
+
+ describe ".recalculate_topic" do
+ it "publishes an isolated coherent snapshot using only public direct replies" do
+ SiteSetting.nested_replies_hot_like_weight = 1.5
+ SiteSetting.nested_replies_hot_reply_weight = 2.5
+ SiteSetting.nested_replies_hot_freshness_max_bonus = 4.0
+ SiteSetting.nested_replies_hot_freshness_half_life_hours = 48
+ SiteSetting.nested_replies_hot_child_penalty = 0.5
+ parent = Fabricate(:post, topic: topic, reply_to_post_number: op.post_number)
+ public_child = Fabricate(:post, topic: topic, reply_to_post_number: parent.post_number)
+ Fabricate(
+ :post,
+ topic: topic,
+ reply_to_post_number: parent.post_number,
+ post_type: Post.types[:whisper],
+ )
+ set_score_inputs(parent, created_at: 2.days.ago)
+ set_score_inputs(public_child, created_at: 1.day.ago, like_score: 20)
+ structural_stat = NestedViewPostStat.create!(post: parent, total_descendant_count: 42)
+
+ expect(described_class.recalculate_topic(topic.id)).to eq(2)
+
+ parent_score = cached_score(parent)
+ child_score = cached_score(public_child)
+ snapshot =
+ DB.query(
+ "SELECT calculated_at FROM nested_hot_score_snapshots WHERE topic_id = :topic_id",
+ topic_id: topic.id,
+ ).first
+ expect(parent_score.hot_score).to be_within(0.0001).of(
+ described_class.score_for(parent, direct_reply_count: 1),
+ )
+ expect(parent_score.thread_hot_score).to be_within(0.0001).of(
+ child_score.thread_hot_score - SiteSetting.nested_replies_hot_child_penalty,
+ )
+ expect(snapshot.calculated_at).to be_present
+ expect(structural_stat.reload.total_descendant_count).to eq(42)
+ expect(NestedViewPostStat.where(post: [op, public_child]).count).to eq(0)
+ end
+
+ it "propagates public descendant heat through a deleted placeholder" do
+ SiteSetting.nested_replies_hot_child_penalty = 0.5
+ deleted_root = Fabricate(:post, topic: topic, reply_to_post_number: op.post_number)
+ child =
+ Fabricate(:post, topic: topic, reply_to_post_number: deleted_root.post_number).tap do |post|
+ set_score_inputs(post, created_at: 1.hour.ago, like_score: 100)
+ end
+ deleted_root.update_columns(deleted_at: Time.current, like_score: 100)
+
+ described_class.recalculate_topic(topic.id)
+
+ deleted_root_score = cached_score(deleted_root)
+ child_score = cached_score(child)
+ expect(deleted_root_score.hot_score).to eq(described_class::HOT_SCORE_FLOOR)
+ expect(deleted_root_score.thread_hot_score).to be_within(0.0001).of(
+ child_score.thread_hot_score - SiteSetting.nested_replies_hot_child_penalty,
+ )
+ end
+
+ it "rejects a cycle without publishing partial cache rows" do
+ root = Fabricate(:post, topic: topic, reply_to_post_number: op.post_number)
+ child = Fabricate(:post, topic: topic, reply_to_post_number: root.post_number)
+ root.update_columns(reply_to_post_number: child.post_number)
+
+ expect { described_class.recalculate_topic(topic.id) }.to raise_error(
+ described_class::InvalidTree,
+ )
+ expect(
+ DB.query_single(
+ "SELECT 1 FROM nested_hot_post_scores WHERE topic_id = :topic_id",
+ topic_id: topic.id,
+ ),
+ ).to be_empty
+ expect(
+ DB.query_single(
+ "SELECT 1 FROM nested_hot_score_snapshots WHERE topic_id = :topic_id",
+ topic_id: topic.id,
+ ),
+ ).to be_empty
+ end
+
+ it "rejects a topic without an original post" do
+ op.update_columns(post_number: 99)
+
+ expect { described_class.recalculate_topic(topic.id) }.to raise_error(
+ described_class::MissingOriginalPost,
+ )
+ expect(
+ DB.query_single(
+ "SELECT 1 FROM nested_hot_score_snapshots WHERE topic_id = :topic_id",
+ topic_id: topic.id,
+ ),
+ ).to be_empty
+ end
+ end
+end
diff --git a/spec/lib/nested_replies/hot_score_queue_spec.rb b/spec/lib/nested_replies/hot_score_queue_spec.rb
new file mode 100644
index 00000000000..79615425817
--- /dev/null
+++ b/spec/lib/nested_replies/hot_score_queue_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+RSpec.describe NestedReplies::HotScoreQueue do
+ before { described_class.clear }
+ after { described_class.clear }
+
+ it "deduplicates topics and pops the oldest demand first" do
+ expect(described_class.enqueue(10, requested_at: 2.minutes.ago)).to eq(:queued)
+ expect(described_class.enqueue(20, requested_at: 1.minute.ago)).to eq(:queued)
+ expect(described_class.enqueue(10)).to eq(:duplicate)
+ expect(described_class.oldest_age).to be >= 2.minutes.to_f
+ expect([described_class.pop, described_class.pop, described_class.pop]).to eq([10, 20, nil])
+ end
+
+ it "caps pending work and honors a failure cooldown" do
+ SiteSetting.nested_replies_hot_max_pending_topics = 1
+ expect(described_class.enqueue(10)).to eq(:queued)
+ expect(described_class.enqueue(20)).to eq(:full)
+ expect(described_class.pop).to eq(10)
+
+ described_class.cooldown(10, duration: 1.hour)
+
+ expect(described_class.enqueue(10)).to eq(:cooldown)
+ expect(described_class.enqueue(10, requested_at: 2.hours.from_now)).to eq(:queued)
+ end
+
+ it "discards obsolete demand before it can run after a long pause" do
+ SiteSetting.nested_replies_hot_max_queue_age_minutes = 30
+ described_class.enqueue(10, requested_at: 31.minutes.ago)
+
+ expect(described_class.pop).to be_nil
+ end
+
+ it "treats invalid input and Redis failure as safe cache misses" do
+ allow(described_class::ENQUEUE_SCRIPT).to receive(:eval).and_raise(
+ Redis::ConnectionError,
+ "unavailable",
+ )
+
+ expect(described_class.enqueue(nil)).to eq(:invalid)
+ expect(described_class.enqueue(10)).to eq(:unavailable)
+ end
+end
diff --git a/spec/lib/nested_replies/nested_replies_n_plus_one_spec.rb b/spec/lib/nested_replies/nested_replies_n_plus_one_spec.rb
index 7fff8bcb364..0eaa57bfdd0 100644
--- a/spec/lib/nested_replies/nested_replies_n_plus_one_spec.rb
+++ b/spec/lib/nested_replies/nested_replies_n_plus_one_spec.rb
@@ -286,6 +286,35 @@ RSpec.describe "Nested replies N+1 elimination", type: :request do
end
end
+ describe "hot sorting" do
+ def build_hot_topic(root_count)
+ hot_topic = Fabricate(:topic, user: user)
+ Fabricate(:post, topic: hot_topic, user: user, post_number: 1)
+ Fabricate(:nested_topic, topic: hot_topic)
+ root_count.times { Fabricate(:post, topic: hot_topic, user: user) }
+ hot_topic.update_columns(posts_count: root_count + 1, last_posted_at: Time.current)
+ NestedReplies::HotScoreCalculator.recalculate_topic(hot_topic.id)
+ hot_topic
+ end
+
+ it "uses a constant number of cache queries as the root count grows" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ small_topic = build_hot_topic(5)
+ large_topic = build_hot_topic(50)
+ sign_in(user)
+
+ small_queries =
+ track_sql_queries { get "/n/#{small_topic.slug}/#{small_topic.id}.json?sort=hot" }
+ large_queries =
+ track_sql_queries { get "/n/#{large_topic.slug}/#{large_topic.id}.json?sort=hot" }
+ cache_query = /nested_hot_(post_scores|score_snapshots)/
+ small_cache_query_count = small_queries.grep(cache_query).size
+
+ expect(small_cache_query_count).to be_positive
+ expect(large_queries.grep(cache_query).size).to eq(small_cache_query_count)
+ end
+ end
+
describe "edge cases" do
it "handles reply to OP (no ancestors)" do
reply = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
diff --git a/spec/lib/nested_replies/sort_spec.rb b/spec/lib/nested_replies/sort_spec.rb
index e7ab9ee6ca3..8fa20d18865 100644
--- a/spec/lib/nested_replies/sort_spec.rb
+++ b/spec/lib/nested_replies/sort_spec.rb
@@ -2,10 +2,10 @@
RSpec.describe NestedReplies::Sort do
describe ".valid?" do
- it "accepts top, new, old" do
- expect(described_class.valid?("top")).to eq(true)
- expect(described_class.valid?("new")).to eq(true)
- expect(described_class.valid?("old")).to eq(true)
+ it "accepts top, hot, new, old" do
+ expect(described_class::ALGORITHMS).to all(
+ satisfy { |algorithm| described_class.valid?(algorithm) },
+ )
end
it "rejects unknown algorithms" do
@@ -17,9 +17,9 @@ RSpec.describe NestedReplies::Sort do
describe ".sort_in_memory" do
let(:posts) do
[
- Struct.new(:like_count, :post_number, :created_at).new(5, 2, 3.days.ago),
- Struct.new(:like_count, :post_number, :created_at).new(1, 3, 1.day.ago),
- Struct.new(:like_count, :post_number, :created_at).new(10, 4, 2.days.ago),
+ Struct.new(:id, :like_count, :post_number, :created_at).new(10, 5, 2, 3.days.ago),
+ Struct.new(:id, :like_count, :post_number, :created_at).new(20, 1, 3, 1.day.ago),
+ Struct.new(:id, :like_count, :post_number, :created_at).new(30, 10, 4, 2.days.ago),
]
end
@@ -38,12 +38,30 @@ RSpec.describe NestedReplies::Sort do
expect(sorted.map(&:post_number)).to eq([2, 3, 4])
end
+ it "sorts by cached thread heat before own heat" do
+ hot_scores = { 10 => [5.0, 4.0], 20 => [5.0, 6.0], 30 => [7.0, 1.0] }
+
+ sorted = described_class.sort_in_memory(posts, "hot", hot_scores: hot_scores)
+
+ expect(sorted.map(&:id)).to eq([30, 20, 10])
+ end
+
it "raises on invalid algorithm" do
expect { described_class.sort_in_memory(posts, "random") }.to raise_error(ArgumentError)
end
end
describe ".sql_order_expression" do
+ it "orders hot scores from the dedicated cache" do
+ expression = described_class.sql_order_expression("hot", posts_table: "candidate_posts")
+
+ expect(expression).to include(
+ "nested_hot_post_scores.thread_hot_score",
+ "nested_hot_post_scores.hot_score",
+ "candidate_posts.post_number ASC",
+ )
+ end
+
it "raises on invalid algorithm" do
expect { described_class.sql_order_expression("bogus") }.to raise_error(ArgumentError)
end
diff --git a/spec/requests/nested_topics_controller_spec.rb b/spec/requests/nested_topics_controller_spec.rb
index 8035fd6f892..21d7fb9e2ca 100644
--- a/spec/requests/nested_topics_controller_spec.rb
+++ b/spec/requests/nested_topics_controller_spec.rb
@@ -3,7 +3,7 @@
RSpec.describe NestedTopicsController, type: :request do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:admin)
- fab!(:topic) { Fabricate(:topic, user: user) }
+ fab!(:topic) { Fabricate(:topic, user: user, last_posted_at: Time.current) }
fab!(:op) { Fabricate(:post, topic: topic, user: user, post_number: 1) }
before { SiteSetting.nested_replies_enabled = true }
@@ -22,6 +22,15 @@ RSpec.describe NestedTopicsController, type: :request do
url
end
+ def set_cached_hot_scores(score_by_post)
+ score_by_post.each { |post, score| DB.exec(<<~SQL, post_id: post.id, score: score) }
+ UPDATE nested_hot_post_scores
+ SET hot_score = :score,
+ thread_hot_score = :score
+ WHERE post_id = :post_id
+ SQL
+ end
+
describe "GET respond" do
it "redirects crawlers to the flat topic view" do
get "/n/#{topic.slug}/#{topic.id}", headers: { "HTTP_USER_AGENT" => "Googlebot" }
@@ -145,6 +154,253 @@ RSpec.describe NestedTopicsController, type: :request do
expect(json["page"]).to eq(0)
end
+ it "keeps hot selected while a missing snapshot safely uses top and requests a refresh" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ low_root =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil, like_count: 1)
+ high_root =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil, like_count: 10)
+ 3.times { Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil) }
+ topic.update_columns(posts_count: 6)
+ NestedReplies::HotScoreQueue.clear
+ sign_in(user)
+
+ get show_url(topic, sort: "hot")
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["sort"]).to eq("hot")
+ expect(response.parsed_body["effective_sort"]).to eq("top")
+ expect(response.parsed_body["roots"].map { |root| root["id"] }.first(2)).to eq(
+ [high_root.id, low_root.id],
+ )
+ expect(NestedReplies::HotScoreQueue.pop).to eq(topic.id)
+ ensure
+ NestedReplies::HotScoreQueue.clear
+ end
+
+ it "orders a deleted placeholder by the heat of its public descendant" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ deleted_root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ hot_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: deleted_root.post_number)
+ liked_root =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil, like_count: 20)
+ 2.times { Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil) }
+ deleted_root.update_columns(deleted_at: Time.current)
+ hot_child.update_columns(like_score: 100, created_at: 1.hour.ago)
+ liked_root.update_columns(like_score: 10, created_at: 1.hour.ago)
+ topic.update_columns(posts_count: 6)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ sign_in(user)
+
+ get show_url(topic, sort: "hot")
+
+ expect(response.status).to eq(200)
+ deleted_root_json = response.parsed_body["roots"].first
+ expect(deleted_root_json["id"]).to eq(deleted_root.id)
+ expect(deleted_root_json["deleted_post_placeholder"]).to eq(true)
+ expect(deleted_root_json["children"].map { |child| child["id"] }).to eq([hot_child.id])
+ end
+
+ it "spends the hot preload budget on the strongest branch" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ hot_child = Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ cold_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ hot_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: hot_child.post_number)
+ hot_great_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: hot_grandchild.post_number)
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: cold_child.post_number)
+ topic.update_columns(posts_count: 7)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ set_cached_hot_scores(
+ root => 100.0,
+ hot_child => 100.0,
+ cold_child => 40.0,
+ hot_grandchild => 100.0,
+ hot_great_grandchild => 100.0,
+ )
+ sign_in(user)
+
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_POST_BUDGET, 4) do
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_PER_ROOT_BUDGET, 4) do
+ get show_url(topic, sort: "hot")
+ end
+ end
+
+ root_json = response.parsed_body["roots"].find { |json_root| json_root["id"] == root.id }
+ hot_child_json = root_json["children"].find { |child| child["id"] == hot_child.id }
+ cold_child_json = root_json["children"].find { |child| child["id"] == cold_child.id }
+ hot_grandchild_json =
+ hot_child_json["children"].find { |child| child["id"] == hot_grandchild.id }
+ expect(root_json["children"].map { |child| child["id"] }).to eq([hot_child.id, cold_child.id])
+ expect(hot_child_json["children"].map { |child| child["id"] }).to eq([hot_grandchild.id])
+ expect(hot_grandchild_json["children"].map { |child| child["id"] }).to eq(
+ [hot_great_grandchild.id],
+ )
+ expect(cold_child_json["children"]).to eq([])
+ end
+
+ it "uses depth penalty to return to another hot sibling branch" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ SiteSetting.nested_replies_hot_preload_depth_penalty = 1.0
+ Fabricate(:nested_topic, topic: topic)
+ root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ first_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ second_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ first_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: first_child.post_number)
+ second_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: second_child.post_number)
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: first_grandchild.post_number)
+ topic.update_columns(posts_count: 7)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ set_cached_hot_scores(
+ root => 100.0,
+ first_child => 100.0,
+ second_child => 99.9,
+ first_grandchild => 100.0,
+ second_grandchild => 99.9,
+ )
+ sign_in(user)
+
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_POST_BUDGET, 4) do
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_PER_ROOT_BUDGET, 4) do
+ get show_url(topic, sort: "hot")
+ end
+ end
+
+ root_json = response.parsed_body["roots"].find { |json_root| json_root["id"] == root.id }
+ first_child_json = root_json["children"].find { |child| child["id"] == first_child.id }
+ second_child_json = root_json["children"].find { |child| child["id"] == second_child.id }
+ first_grandchild_json =
+ first_child_json["children"].find { |child| child["id"] == first_grandchild.id }
+ expect(first_grandchild_json["children"]).to eq([])
+ expect(second_child_json["children"].map { |child| child["id"] }).to eq(
+ [second_grandchild.id],
+ )
+ end
+
+ it "caps each hot root branch and spends the remaining budget on other roots" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ hottest_root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ other_root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ hottest_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: hottest_root.post_number)
+ hottest_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: hottest_child.post_number)
+ Fabricate(
+ :post,
+ topic: topic,
+ user: user,
+ reply_to_post_number: hottest_grandchild.post_number,
+ )
+ other_child =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: other_root.post_number)
+ other_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: other_child.post_number)
+ topic.update_columns(posts_count: 8)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ set_cached_hot_scores(
+ hottest_root => 100.0,
+ hottest_child => 100.0,
+ hottest_grandchild => 100.0,
+ other_root => 90.0,
+ other_child => 90.0,
+ other_grandchild => 90.0,
+ )
+ sign_in(user)
+
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_POST_BUDGET, 4) do
+ stub_const(NestedReplies::TreeLoader, :HOT_PRELOAD_PER_ROOT_BUDGET, 2) do
+ get show_url(topic, sort: "hot")
+ end
+ end
+
+ roots = response.parsed_body["roots"]
+ hottest_root_json = roots.find { |json_root| json_root["id"] == hottest_root.id }
+ hottest_child_json =
+ hottest_root_json["children"].find { |child| child["id"] == hottest_child.id }
+ hottest_grandchild_json =
+ hottest_child_json["children"].find { |child| child["id"] == hottest_grandchild.id }
+ other_root_json = roots.find { |json_root| json_root["id"] == other_root.id }
+ other_child_json = other_root_json["children"].find { |child| child["id"] == other_child.id }
+ expect(hottest_child_json["children"].map { |child| child["id"] }).to eq(
+ [hottest_grandchild.id],
+ )
+ expect(hottest_grandchild_json["children"]).to eq([])
+ expect(other_child_json["children"].map { |child| child["id"] }).to eq([other_grandchild.id])
+ end
+
+ it "preloads replies created after the hot snapshot" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ 4.times { Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil) }
+ topic.update_columns(posts_count: 6)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ new_child = Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ sign_in(user)
+
+ get show_url(topic, sort: "hot")
+
+ root_json = response.parsed_body["roots"].find { |json_root| json_root["id"] == root.id }
+ expect(root_json["children"].map { |child| child["id"] }).to eq([new_child.id])
+ end
+
+ it "preloads visible whisper branches for whisperers when sorting by hot" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
+ Fabricate(:nested_topic, topic: topic)
+ regular_root = Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ whisper_child =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ reply_to_post_number: regular_root.post_number,
+ post_type: Post.types[:whisper],
+ )
+ whisper_root =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ reply_to_post_number: nil,
+ post_type: Post.types[:whisper],
+ )
+ whisper_grandchild =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ reply_to_post_number: whisper_root.post_number,
+ post_type: Post.types[:whisper],
+ )
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
+ topic.update_columns(posts_count: 6)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ sign_in(admin)
+
+ get show_url(topic, sort: "hot")
+
+ roots = response.parsed_body["roots"]
+ regular_root_json = roots.find { |root| root["id"] == regular_root.id }
+ whisper_root_json = roots.find { |root| root["id"] == whisper_root.id }
+ expect(regular_root_json["children"].map { |child| child["id"] }).to eq([whisper_child.id])
+ expect(whisper_root_json["children"].map { |child| child["id"] }).to eq(
+ [whisper_grandchild.id],
+ )
+ end
+
it "piggybacks suggested topics at the top level when the first page is the last page" do
Fabricate(:post, topic: topic, user: user, reply_to_post_number: nil)
suggested = Fabricate(:post).topic
@@ -933,6 +1189,33 @@ RSpec.describe NestedTopicsController, type: :request do
expect(child_ids).to eq([first.id, second.id])
end
+ it "sorts children by a fresh hot snapshot" do
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ liked_child =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: user,
+ reply_to_post_number: root.post_number,
+ like_count: 20,
+ )
+ hot_branch =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
+ hot_grandchild =
+ Fabricate(:post, topic: topic, user: user, reply_to_post_number: hot_branch.post_number)
+ liked_child.update_columns(like_score: 10, created_at: 1.hour.ago)
+ hot_grandchild.update_columns(like_score: 100, created_at: 1.hour.ago)
+ topic.update_columns(posts_count: 6)
+ NestedReplies::HotScoreCalculator.recalculate_topic(topic.id)
+ sign_in(user)
+
+ get children_url(topic, root.post_number, sort: "hot")
+
+ child_ids = response.parsed_body["children"].map { |child| child["id"] }
+ expect(child_ids).to eq([hot_branch.id, liked_child.id])
+ end
+
it "respects sort at max nesting depth" do
SiteSetting.nested_replies_max_depth = 2
child = Fabricate(:post, topic: topic, user: user, reply_to_post_number: root.post_number)
@@ -1139,6 +1422,7 @@ RSpec.describe NestedTopicsController, type: :request do
expect(json).to have_key("ancestor_chain")
expect(json).to have_key("siblings")
expect(json).to have_key("target_post")
+ expect(json["effective_sort"]).to eq("top")
expect(json).to have_key("message_bus_last_id")
end
diff --git a/spec/system/nested_view_spec.rb b/spec/system/nested_view_spec.rb
index 9fe77e970fb..f4e4f3e7f4a 100644
--- a/spec/system/nested_view_spec.rb
+++ b/spec/system/nested_view_spec.rb
@@ -255,6 +255,20 @@ RSpec.describe "Nested view" do
expect(nested_view).to have_sort_active("old")
expect(page).to have_current_path(/sort=old/)
end
+
+ it "shows hot sorting only while its rollout gate is enabled" do
+ nested_view.visit_nested(topic)
+ find(".nested-sort-selector__trigger").click
+
+ expect(page).to have_no_css(".dropdown-menu .btn", text: I18n.t("js.nested_replies.sort.hot"))
+
+ SiteSetting.nested_replies_hot_sort_enabled = true
+ nested_view.visit_nested(topic)
+ nested_view.click_sort("hot")
+
+ expect(nested_view).to have_sort_active("hot")
+ expect(page).to have_current_path(/sort=hot/)
+ end
end
describe "expand and collapse" do