Skip to content

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

DEV: Various changes to support nested posts experiment (#37785)

Mark VanLandinghamff700ad8c391
diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb
index e9a593cf2e7..a21afc1a7ca 100644
--- a/app/serializers/topic_view_serializer.rb
+++ b/app/serializers/topic_view_serializer.rb
@@ -187,7 +187,7 @@ class TopicViewSerializer < ApplicationSerializer
end
def include_has_deleted?
- object.guardian.can_see_deleted_posts?
+ !object.skip_post_loading && object.guardian.can_see_deleted_posts?
end
def expandable_first_post
diff --git a/frontend/discourse/app/components/topic-metadata.gjs b/frontend/discourse/app/components/topic-metadata.gjs
new file mode 100644
index 00000000000..d878e3df372
--- /dev/null
+++ b/frontend/discourse/app/components/topic-metadata.gjs
@@ -0,0 +1,66 @@
+import { hash } from "@ember/helper";
+import DButton from "discourse/components/d-button";
+import PluginOutlet from "discourse/components/plugin-outlet";
+import lazyHash from "discourse/helpers/lazy-hash";
+import CategoryChooser from "discourse/select-kit/components/category-chooser";
+import MiniTagChooser from "discourse/select-kit/components/mini-tag-chooser";
+
+<template>
+ {{#if @showCategoryChooser}}
+ <div class="edit-category__wrapper">
+ <PluginOutlet
+ @name="edit-topic-category"
+ @outletArgs={{lazyHash model=@model buffered=@buffered}}
+ >
+ <CategoryChooser
+ @value={{@buffered.category_id}}
+ @onChange={{@topicCategoryChanged}}
+ class="small"
+ />
+ </PluginOutlet>
+ </div>
+ {{/if}}
+
+ {{#if @canEditTags}}
+ <div class="edit-tags__wrapper">
+ <PluginOutlet
+ @name="edit-topic-tags"
+ @outletArgs={{lazyHash model=@model buffered=@buffered}}
+ >
+ <MiniTagChooser
+ @value={{@buffered.tags}}
+ @onChange={{@topicTagsChanged}}
+ @options={{hash
+ filterable=true
+ minimum=@minimumRequiredTags
+ filterPlaceholder="tagging.choose_for_topic"
+ useHeaderFilter=true
+ }}
+ />
+ </PluginOutlet>
+ </div>
+ {{/if}}
+
+ <PluginOutlet
+ @name="edit-topic"
+ @connectorTagName="div"
+ @outletArgs={{lazyHash model=@model buffered=@buffered}}
+ />
+
+ <div class="edit-controls">
+ <DButton
+ @action={{@onSave}}
+ @icon="check"
+ @ariaLabel="composer.save_edit"
+ class="btn-primary submit-edit"
+ />
+ <DButton
+ @action={{@onCancel}}
+ @icon="xmark"
+ @ariaLabel="composer.cancel"
+ class="btn-default cancel-edit"
+ />
+ {{yield}}
+ </div>
+</template>
diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.js
index 408cd410ea8..9df69ce6fe0 100644
--- a/frontend/discourse/app/lib/registry/transformers.js
+++ b/frontend/discourse/app/lib/registry/transformers.js
@@ -88,6 +88,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"post-meta-data-infos",
"post-meta-data-poster-name-suppress-similar-name",
"post-notice-component",
+ "post-share-url",
"post-show-topic-map",
"post-small-action-class",
"post-small-action-custom-component",
@@ -97,6 +98,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"poster-name-user-title",
"preferences-save-attributes",
"quote-params",
+ "route-to-url",
"small-user-attrs",
"tag-separator",
"topic-list-class",
@@ -106,6 +108,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"topic-list-item-expand-pinned",
"topic-list-item-mobile-layout",
"topic-list-item-style",
+ "topic-url-for-post-number",
"user-field-components",
"user-menu-notification-item-acting-user-avatar",
"user-notes-modal-subtitle",
diff --git a/frontend/discourse/app/lib/url.js b/frontend/discourse/app/lib/url.js
index eab4a85e4d5..d08dab012fb 100644
--- a/frontend/discourse/app/lib/url.js
+++ b/frontend/discourse/app/lib/url.js
@@ -9,6 +9,7 @@ import { isTesting } from "discourse/lib/environment";
import getURL, { withoutPrefix } from "discourse/lib/get-url";
import LockOn from "discourse/lib/lock-on";
import offsetCalculator from "discourse/lib/offset-calculator";
+import { applyValueTransformer } from "discourse/lib/transformer";
import { defaultHomepage } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import Session from "discourse/models/session";
@@ -211,6 +212,11 @@ class DiscourseURL extends EmberObject {
return;
}
+ path = applyValueTransformer("route-to-url", path, { opts });
+ if (isEmpty(path)) {
+ return;
+ }
+
// In embed mode, open all navigation in new tabs except same-topic navigation
if (EmbedMode.enabled) {
const currentTopicMatch = TOPIC_URL_REGEXP.exec(window.location.pathname);
diff --git a/frontend/discourse/app/models/post.js b/frontend/discourse/app/models/post.js
index e1ff952b38c..40338b82856 100644
--- a/frontend/discourse/app/models/post.js
+++ b/frontend/discourse/app/models/post.js
@@ -14,6 +14,7 @@ import {
defineTrackedProperty,
enumerateTrackedKeys,
} from "discourse/lib/tracked-tools";
+import { applyValueTransformer } from "discourse/lib/transformer";
import { userPath } from "discourse/lib/url";
import { postUrl } from "discourse/lib/utilities";
import ActionSummary from "discourse/models/action-summary";
@@ -219,7 +220,8 @@ export default class Post extends RestModel {
}
get shareUrl() {
- return this.customShare || resolveShareUrl(this.url, this.currentUser);
+ const url = this.customShare || resolveShareUrl(this.url, this.currentUser);
+ return applyValueTransformer("post-share-url", url, { post: this });
}
@computed("name", "username")
diff --git a/frontend/discourse/app/models/topic.js b/frontend/discourse/app/models/topic.js
index 04f0da2ff52..bb83e43ea09 100644
--- a/frontend/discourse/app/models/topic.js
+++ b/frontend/discourse/app/models/topic.js
@@ -22,6 +22,7 @@ import {
autoTrackedArray,
defineTrackedProperty,
} from "discourse/lib/tracked-tools";
+import { applyValueTransformer } from "discourse/lib/transformer";
import DiscourseURL, { userPath } from "discourse/lib/url";
import ActionSummary from "discourse/models/action-summary";
import Bookmark from "discourse/models/bookmark";
@@ -581,7 +582,10 @@ export default class Topic extends RestModel {
if (postNumber > 0) {
url += `/${postNumber}`;
}
- return url;
+ return applyValueTransformer("topic-url-for-post-number", url, {
+ topic: this,
+ postNumber,
+ });
}
@computed("unread_posts", "new_posts")
diff --git a/frontend/discourse/app/templates/topic.gjs b/frontend/discourse/app/templates/topic.gjs
index 18638ddc553..82c9b1e70e6 100644
--- a/frontend/discourse/app/templates/topic.gjs
+++ b/frontend/discourse/app/templates/topic.gjs
@@ -27,6 +27,7 @@ import TopicCategory from "discourse/components/topic-category";
import TopicFooterButtons from "discourse/components/topic-footer-buttons";
import TopicLocalizedContentToggle from "discourse/components/topic-localized-content-toggle";
import TopicMap from "discourse/components/topic-map/index";
+import TopicMetadata from "discourse/components/topic-metadata";
import TopicNavigation from "discourse/components/topic-navigation";
import TopicProgress from "discourse/components/topic-progress";
import TopicSkipLinks from "discourse/components/topic-skip-links";
@@ -42,8 +43,6 @@ import hideApplicationFooter from "discourse/helpers/hide-application-footer";
import hideScrollableContent from "discourse/helpers/hide-scrollable-content";
import lazyHash from "discourse/helpers/lazy-hash";
import routeAction from "discourse/helpers/route-action";
-import CategoryChooser from "discourse/select-kit/components/category-chooser";
-import MiniTagChooser from "discourse/select-kit/components/mini-tag-chooser";
import { and, eq } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
import booleanString from "../helpers/boolean-string";
@@ -121,71 +120,17 @@ export default <template>
@buffered={{@controller.buffered}}
/>
- {{#if @controller.showCategoryChooser}}
- <div class="edit-category__wrapper">
- <PluginOutlet
- @name="edit-topic-category"
- @outletArgs={{lazyHash
- }}
- >
- <CategoryChooser
- @value={{@controller.buffered.category_id}}
- @onChange={{@controller.topicCategoryChanged}}
- class="small"
- />
- </PluginOutlet>
- </div>
- {{/if}}
-
- {{#if @controller.canEditTags}}
- <div class="edit-tags__wrapper">
- <PluginOutlet
- @name="edit-topic-tags"
- @outletArgs={{lazyHash
- }}
- >
- <MiniTagChooser
- @value={{@controller.buffered.tags}}
- @onChange={{@controller.topicTagsChanged}}
- @options={{hash
- filterable=true
- filterPlaceholder="tagging.choose_for_topic"
- useHeaderFilter=true
- }}
- />
- </PluginOutlet>
- </div>
- {{/if}}
-
- <PluginOutlet
- @name="edit-topic"
- @connectorTagName="div"
- @outletArgs={{lazyHash
- }}
- />
-
- <div class="edit-controls">
- <DButton
- @action={{@controller.finishedEditingTopic}}
- @icon="check"
- @ariaLabel="composer.save_edit"
- class="btn-primary submit-edit"
- />
- <DButton
- @action={{@controller.cancelEditingTopic}}
- @icon="xmark"
- @ariaLabel="composer.cancel"
- class="btn-default cancel-edit"
- />
-
+ <TopicMetadata
+ @buffered={{@controller.buffered}}
+ @model={{@controller.model}}
+ @showCategoryChooser={{@controller.showCategoryChooser}}
+ @canEditTags={{@controller.canEditTags}}
+ @minimumRequiredTags={{@controller.minimumRequiredTags}}
+ @onSave={{@controller.finishedEditingTopic}}
+ @onCancel={{@controller.cancelEditingTopic}}
+ @topicCategoryChanged={{@controller.topicCategoryChanged}}
+ @topicTagsChanged={{@controller.topicTagsChanged}}
+ >
{{#if @controller.canRemoveTopicFeaturedLink}}
<a
href
@@ -197,7 +142,7 @@ export default <template>
{{@controller.featuredLinkDomain}}
</a>
{{/if}}
- </div>
+ </TopicMetadata>
</div>
{{else}}
diff --git a/frontend/discourse/tests/acceptance/transformers/post-share-url-test.js b/frontend/discourse/tests/acceptance/transformers/post-share-url-test.js
new file mode 100644
index 00000000000..0e1d840c1da
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/transformers/post-share-url-test.js
@@ -0,0 +1,51 @@
+import { getOwner } from "@ember/owner";
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { withPluginApi } from "discourse/lib/plugin-api";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("post-share-url transformer", function (needs) {
+ needs.user();
+
+ test("applying a value transformation", async function (assert) {
+ withPluginApi((api) => {
+ api.registerValueTransformer("post-share-url", ({ value }) => {
+ return "/custom" + value;
+ });
+ });
+
+ await visit("/t/internationalization-localization/280");
+
+ const topicController = getOwner(this).lookup("controller:topic");
+ const post = topicController.model.postStream.posts[0];
+
+ assert.true(
+ post.shareUrl.startsWith("/custom"),
+ "it transforms the share url"
+ );
+ });
+
+ test("transformer receives the post in context", async function (assert) {
+ let receivedPost;
+
+ withPluginApi((api) => {
+ api.registerValueTransformer("post-share-url", ({ value, context }) => {
+ receivedPost = context.post;
+ return value;
+ });
+ });
+
+ await visit("/t/internationalization-localization/280");
+
+ const topicController = getOwner(this).lookup("controller:topic");
+ const post = topicController.model.postStream.posts[0];
+ // access shareUrl to trigger the transformer
+ post.shareUrl;
+
+ assert.strictEqual(
+ receivedPost.post_number,
+ 1,
+ "transformer receives the correct post in context"
+ );
+ });
+});
diff --git a/frontend/discourse/tests/acceptance/transformers/topic-url-for-post-number-test.js b/frontend/discourse/tests/acceptance/transformers/topic-url-for-post-number-test.js
new file mode 100644
index 00000000000..1005361835c
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/transformers/topic-url-for-post-number-test.js
@@ -0,0 +1,58 @@
+import { getOwner } from "@ember/owner";
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { withPluginApi } from "discourse/lib/plugin-api";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("topic-url-for-post-number transformer", function (needs) {
+ needs.user();
+
+ test("applying a value transformation", async function (assert) {
+ withPluginApi((api) => {
+ api.registerValueTransformer("topic-url-for-post-number", ({ value }) => {
+ return "/custom" + value;
+ });
+ });
+
+ await visit("/t/internationalization-localization/280");
+
+ const topicController = getOwner(this).lookup("controller:topic");
+ const topic = topicController.model;
+
+ assert.true(
+ topic.urlForPostNumber(1).startsWith("/custom"),
+ "it transforms the topic url for post number"
+ );
+ });
+
+ test("transformer receives the topic and postNumber in context", async function (assert) {
+ let receivedContext;
+
+ withPluginApi((api) => {
+ api.registerValueTransformer(
+ "topic-url-for-post-number",
+ ({ value, context }) => {
+ receivedContext = context;
+ return value;
+ }
+ );
+ });
+
+ await visit("/t/internationalization-localization/280");
+
+ const topicController = getOwner(this).lookup("controller:topic");
+ const topic = topicController.model;
+ topic.urlForPostNumber(3);
+
+ assert.strictEqual(
+ receivedContext.topic,
+ topic,
+ "transformer receives the topic in context"
+ );
+ assert.strictEqual(
+ receivedContext.postNumber,
+ 3,
+ "transformer receives the postNumber in context"
+ );
+ });
+});
diff --git a/frontend/discourse/tests/unit/lib/url-test.js b/frontend/discourse/tests/unit/lib/url-test.js
index 805a90a766e..cd95b89a426 100644
--- a/frontend/discourse/tests/unit/lib/url-test.js
+++ b/frontend/discourse/tests/unit/lib/url-test.js
@@ -2,6 +2,7 @@ import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import { setPrefix } from "discourse/lib/get-url";
+import { withPluginApi } from "discourse/lib/plugin-api";
import DiscourseURL, {
getCanonicalUrl,
getCategoryAndTagUrl,
@@ -192,6 +193,75 @@ module("Unit | Utility | url", function (hooks) {
);
});
+ test("routeTo applies route-to-url value transformer", async function (assert) {
+ sinon.stub(DiscourseURL, "origin").get(() => "http://example.com");
+ sinon.stub(DiscourseURL, "handleURL");
+ sinon.stub(DiscourseURL, "router").get(() => {
+ return { currentURL: "/bar" };
+ });
+
+ withPluginApi((api) => {
+ api.registerValueTransformer("route-to-url", ({ value }) => {
+ if (value === "/t/some-topic/123") {
+ return "/nested/some-topic/123";
+ }
+ return value;
+ });
+ });
+
+ DiscourseURL.routeTo("/t/some-topic/123");
+ assert.true(
+ DiscourseURL.handleURL.calledWith("/nested/some-topic/123"),
+ "the transformed URL is used for routing"
+ );
+ });
+
+ test("routeTo passes untransformed URLs through", async function (assert) {
+ sinon.stub(DiscourseURL, "origin").get(() => "http://example.com");
+ sinon.stub(DiscourseURL, "handleURL");
+ sinon.stub(DiscourseURL, "router").get(() => {
+ return { currentURL: "/bar" };
+ });
+
+ withPluginApi((api) => {
+ api.registerValueTransformer("route-to-url", ({ value }) => {
+ if (value.startsWith("/t/redirect-me")) {
+ return "/other/path";
+ }
+ return value;
+ });
+ });
+
+ DiscourseURL.routeTo("/some/other/path");
+ assert.true(
+ DiscourseURL.handleURL.calledWith("/some/other/path"),
+ "non-matching URLs pass through unchanged"
+ );
+ });
+
+ test("routeTo aborts when transformer returns null", async function (assert) {
+ sinon.stub(DiscourseURL, "origin").get(() => "http://example.com");
+ sinon.stub(DiscourseURL, "handleURL");
+ sinon.stub(DiscourseURL, "redirectTo");
+ sinon.stub(DiscourseURL, "router").get(() => {
+ return { currentURL: "/bar" };
+ });
+
+ withPluginApi((api) => {
+ api.registerValueTransformer("route-to-url", () => null);
+ });
+
+ DiscourseURL.routeTo("/t/some-topic/123");
+ assert.false(
+ DiscourseURL.handleURL.called,
+ "handleURL is not called when transformer returns null"
+ );
+ assert.false(
+ DiscourseURL.redirectTo.called,
+ "redirectTo is not called when transformer returns null"
+ );
+ });
+
test("prefixProtocol", async function (assert) {
assert.strictEqual(
prefixProtocol("mailto:[email protected]"),
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 51c02a45c2a..11226e4c31c 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -4,6 +4,20 @@ class TopicView
MEGA_TOPIC_POSTS_COUNT = 10_000
MIN_POST_READ_TIME = 4.0
+ include PostDependentCache
+
+ memoize_for_posts :all_post_actions
+ memoize_for_posts :reviewable_counts
+ memoize_for_posts :post_custom_fields
+ memoize_for_posts :user_custom_fields
+ memoize_for_posts :category_group_moderator_user_ids
+ memoize_for_posts :mentioned_users
+ memoize_for_posts :link_counts
+ memoize_for_posts :read_posts_set
+ memoize_for_posts :primary_group_names, :@group_names
+ memoize_for_posts :post_user_badges
+ memoize_for_posts :last_post
+
def self.on_preload(&blk)
(@preload ||= Set.new) << blk
end
@@ -31,6 +45,7 @@ class TopicView
:personal_message,
:can_review_topic,
:page,
+ :skip_post_loading,
)
alias queued_posts_enabled? queued_posts_enabled
@@ -131,21 +146,27 @@ class TopicView
@page = @page.to_i > 1 ? @page.to_i : calculate_page
- setup_filtered_posts
- @filtered_posts = apply_default_scope(@filtered_posts)
- filter_posts(options)
+ if @skip_post_loading
+ @filtered_posts = @topic.posts.none
+ @predelete_filtered_posts = @filtered_posts
+ @posts = []
+ else
+ setup_filtered_posts
+ @filtered_posts = apply_default_scope(@filtered_posts)
+ filter_posts(options)
- if @posts && !@skip_custom_fields
- if (added_fields = User.allowed_user_custom_fields(@guardian)).present?
- @user_custom_fields = User.custom_fields_for_ids(@posts.map(&:user_id), added_fields)
- end
+ if @posts && !@skip_custom_fields
+ if (added_fields = User.allowed_user_custom_fields(@guardian)).present?
+ @user_custom_fields = User.custom_fields_for_ids(@posts.map(&:user_id), added_fields)
+ end
- if (allowed_fields = TopicView.allowed_post_custom_fields(@user, @topic)).present?
- @post_custom_fields = Post.custom_fields_for_ids(@posts.map(&:id), allowed_fields)
+ if (allowed_fields = TopicView.allowed_post_custom_fields(@user, @topic)).present?
+ @post_custom_fields = Post.custom_fields_for_ids(@posts.map(&:id), allowed_fields)
+ end
end
- end
- TopicView.preload(self)
+ TopicView.preload(self)
+ end
@draft_key = @topic.draft_key
@draft_sequence = DraftSequence.current(@user, @draft_key)
diff --git a/lib/topic_view/post_dependent_cache.rb b/lib/topic_view/post_dependent_cache.rb
new file mode 100644
index 00000000000..724b8416cc1
--- /dev/null
+++ b/lib/topic_view/post_dependent_cache.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+# Extracted from TopicView to make post-dependent memoization self-maintaining.
+#
+# Any method in TopicView (or a plugin) that caches data derived from @posts
+# can register itself with `memoize_for_posts`. When @posts is replaced via
+# `reset_post_collection`, all registered caches are automatically cleared —
+# no hardcoded ivar list required.
+module TopicView::PostDependentCache
+ extend ActiveSupport::Concern
+
+ included do
+ # Stores ivar names (as symbols like :@all_post_actions) that should
+ # be cleared whenever @posts is replaced.
+ class_attribute :post_dependent_ivars, instance_writer: false, default: []
+ end
+
+ class_methods do
+ # Register a memoized ivar as post-dependent.
+ #
+ # memoize_for_posts :all_post_actions
+ # memoize_for_posts :primary_group_names, :@group_names
+ #
+ # The ivar name defaults to `@<method_name>` but can be overridden
+ # for cases where the ivar doesn't match the method name.
+ def memoize_for_posts(method_name, ivar_name = nil)
+ ivar_name ||= :"@#{method_name}"
+ # Use += to avoid mutating a parent class's array
+ self.post_dependent_ivars = post_dependent_ivars + [ivar_name]
+ end
+ end
+
+ # Replaces @posts with a new collection and clears all registered
+ # post-dependent caches. Use this instead of writing to @posts directly
+ # when you need to swap in a different set of posts (e.g. a plugin that
+ # builds its own post tree) after the TopicView has been initialized.
+ def reset_post_collection(posts:)
+ @posts = posts
+ self.class.post_dependent_ivars.each do |ivar|
+ remove_instance_variable(ivar) if instance_variable_defined?(ivar)
+ end
+ end
+end
diff --git a/spec/lib/topic_view_spec.rb b/spec/lib/topic_view_spec.rb
index e88f2c4e93c..63af922ea5f 100644
--- a/spec/lib/topic_view_spec.rb
+++ b/spec/lib/topic_view_spec.rb
@@ -28,6 +28,120 @@ RSpec.describe TopicView do
end
end
+ describe "#reset_post_collection" do
+ fab!(:post1) { Fabricate(:post, topic: topic) }
+ fab!(:post2) { Fabricate(:post, topic: topic) }
+ fab!(:post3) { Fabricate(:post, topic: topic) }
+
+ it "replaces the posts collection" do
+ tv = TopicView.new(topic.id, evil_trout)
+ original_posts = tv.posts.to_a
+
+ new_posts = [post3]
+ tv.reset_post_collection(posts: new_posts)
+
+ expect(tv.posts).to eq(new_posts)
+ expect(tv.posts).not_to eq(original_posts)
+ end
+
+ it "clears memoized state derived from the previous posts" do
+ tv = TopicView.new(topic.id, evil_trout)
+
+ tv.all_post_actions
+ tv.reviewable_counts
+ tv.mentioned_users
+ tv.category_group_moderator_user_ids
+ tv.primary_group_names
+ tv.last_post
+
+ tv.reset_post_collection(posts: [post2])
+
+ expect(tv.all_post_actions).to be_a(Hash)
+ expect(tv.last_post).to eq(post2)
+ expect(tv.mentioned_users).to eq({})
+ expect(tv.primary_group_names).to be_a(Hash)
+ end
+
+ it "allows preload hooks to run on the new posts" do
+ tv = TopicView.new(topic.id, evil_trout)
+ preloaded_post_ids = nil
+ preloader = lambda { |view| preloaded_post_ids = view.posts.map(&:id) }
+
+ TopicView.on_preload(&preloader)
+
+ tv.reset_post_collection(posts: [post2, post3])
+ TopicView.preload(tv)
+
+ expect(preloaded_post_ids).to contain_exactly(post2.id, post3.id)
+ ensure
+ TopicView.cancel_preload(&preloader)
+ end
+
+ it "skips post loading when skip_post_loading is true" do
+ tv = TopicView.new(topic.id, evil_trout, skip_post_loading: true)
+
+ expect(tv.posts).to eq([])
+ expect(tv.filtered_posts.count).to eq(0)
+ expect(tv.topic).to eq(topic)
+
+ tv.reset_post_collection(posts: [post1, post2])
+ expect(tv.posts).to eq([post1, post2])
+ end
+ end
+
+ describe "#reset_post_collection (memoize_for_posts)" do
+ fab!(:post1) { Fabricate(:post, topic: topic) }
+ fab!(:post2) { Fabricate(:post, topic: topic) }
+
+ it "clears all registered post-dependent caches when posts are replaced" do
+ tv = TopicView.new(topic.id, evil_trout)
+
+ # Force memoization of a registered cache
+ tv.all_post_actions
+ expect(tv.instance_variable_defined?(:@all_post_actions)).to eq(true)
+
+ tv.reset_post_collection(posts: [post1])
+
+ expect(tv.instance_variable_defined?(:@all_post_actions)).to eq(false)
+ end
+
+ it "clears caches with custom ivar names" do
+ tv = TopicView.new(topic.id, evil_trout)
+
+ # primary_group_names is registered as `memoize_for_posts :primary_group_names, :@group_names`
+ tv.primary_group_names
+ expect(tv.instance_variable_defined?(:@group_names)).to eq(true)
+
+ tv.reset_post_collection(posts: [post1])
+
+ expect(tv.instance_variable_defined?(:@group_names)).to eq(false)
+ end
+
+ it "allows plugins to register their own post-dependent caches" do
+ original_ivars = TopicView.post_dependent_ivars.dup
+ TopicView.memoize_for_posts(:test_plugin_cache)
+
+ tv = TopicView.new(topic.id, evil_trout)
+ tv.instance_variable_set(:@test_plugin_cache, { some: "data" })
+
+ tv.reset_post_collection(posts: [post1])
+
+ expect(tv.instance_variable_defined?(:@test_plugin_cache)).to eq(false)
+ ensure
+ TopicView.post_dependent_ivars = original_ivars
+ end
+
+ it "replaces @posts with the new collection" do
+ tv = TopicView.new(topic.id, evil_trout)
+
+ tv.reset_post_collection(posts: [post2])
+ expect(tv.posts).to eq([post2])
+
+ tv.reset_post_collection(posts: [post1, post2])
+ expect(tv.posts).to eq([post1, post2])
+ end
+ end
+
it "raises a not found error if the topic doesn't exist" do
expect { TopicView.new(1_231_232, evil_trout) }.to raise_error(Discourse::NotFound)
end