Skip to content

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

FIX: Marking posts as solutions in nested topic (#40451)

Mark VanLandingham26c12a14d738
diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.js
index 75d213602b9..b63e1f84e2e 100644
--- a/frontend/discourse/app/controllers/nested.js
+++ b/frontend/discourse/app/controllers/nested.js
@@ -481,6 +481,7 @@ export default class NestedController extends Controller {
#onPostRegistered(post) {
if (post?.post_number != null) {
+ this.topic?.postStream?.storePost(post);
this.postRegistry.set(post.post_number, post);
}
}
@@ -534,8 +535,7 @@ export default class NestedController extends Controller {
return;
}
- const post = this.store.createRecord("post", postData);
- post.topic = this.topic;
+ const { post } = this.#processNode({ ...postData, children: [] });
const replyTo = postData.reply_to_post_number;
const isRoot = !replyTo || replyTo === 1;
@@ -646,10 +646,7 @@ export default class NestedController extends Controller {
const newNodes = [];
for (const result of results) {
if (result.status === "fulfilled") {
- const postData = result.value;
- const post = this.store.createRecord("post", postData);
- post.topic = this.topic;
- newNodes.push({ post, children: [] });
+ newNodes.push(this.#processNode({ ...result.value, children: [] }));
}
}
diff --git a/frontend/discourse/app/lib/process-node.js b/frontend/discourse/app/lib/process-node.js
index 6ec1dd4aebf..323a6de09c0 100644
--- a/frontend/discourse/app/lib/process-node.js
+++ b/frontend/discourse/app/lib/process-node.js
@@ -1,6 +1,11 @@
export default function processNode(store, topic, nodeData) {
- const post = store.createRecord("post", nodeData);
- post.topic = topic;
+ const createdPost = store.createRecord("post", nodeData);
+ const post = topic.postStream?.storePost(createdPost) || createdPost;
+
+ if (post.topic !== topic) {
+ post.topic = topic;
+ }
+
const children = (nodeData.children || []).map((child) =>
processNode(store, topic, child)
);
diff --git a/frontend/discourse/app/models/post-stream.js b/frontend/discourse/app/models/post-stream.js
index 8bff5e60ce4..f3e9fadbf44 100644
--- a/frontend/discourse/app/models/post-stream.js
+++ b/frontend/discourse/app/models/post-stream.js
@@ -680,6 +680,10 @@ export default class PostStream extends RestModel {
return this._identityMap[id];
}
+ get loadedPosts() {
+ return Object.values(this._identityMap).filter(Boolean);
+ }
+
loadPostByPostNumber(postNumber) {
const url = `/posts/by_number/${this.topic.id}/${postNumber}`;
diff --git a/frontend/discourse/tests/unit/lib/process-node-test.js b/frontend/discourse/tests/unit/lib/process-node-test.js
index 4e49fd05336..85a99995146 100644
--- a/frontend/discourse/tests/unit/lib/process-node-test.js
+++ b/frontend/discourse/tests/unit/lib/process-node-test.js
@@ -25,6 +25,28 @@ module("Unit | Lib | process-node", function (hooks) {
assert.strictEqual(result.post.topic, topic);
});
+ test("stores post records in the topic post stream when available", function (assert) {
+ const store = getStore(this);
+ const storedPosts = [];
+ const topic = {
+ id: 42,
+ slug: "test",
+ postStream: {
+ storePost(post) {
+ storedPosts.push(post);
+ return post;
+ },
+ },
+ };
+ const nodeData = { id: 100, post_number: 2 };
+
+ const result = processNode(store, topic, nodeData);
+
+ assert.strictEqual(storedPosts.length, 1);
+ assert.strictEqual(storedPosts[0], result.post);
+ assert.strictEqual(result.post.topic, topic);
+ });
+
test("returns empty children when node has no children", function (assert) {
const store = getStore(this);
const topic = { id: 42, slug: "test" };
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/lib/set-accepted-solutions.js b/plugins/discourse-solved/assets/javascripts/discourse/lib/set-accepted-solutions.js
index b48e71f7459..93fd928ce0b 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/lib/set-accepted-solutions.js
+++ b/plugins/discourse-solved/assets/javascripts/discourse/lib/set-accepted-solutions.js
@@ -8,7 +8,10 @@ export default function setAcceptedSolutions(topic, acceptedAnswers) {
topic.accepted_answers = acceptedAnswers;
topic.has_accepted_answer = topicHasAcceptedAnswer;
- topic.postStream?.posts?.forEach((post) => {
+ const posts = new Set(topic.postStream?.posts);
+ topic.postStream?.loadedPosts?.forEach((post) => posts.add(post));
+
+ posts.forEach((post) => {
if (post.post_number === 1) {
return;
}
diff --git a/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-post-menu-test.js b/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-post-menu-test.js
index 166b912f2c3..da550a734a6 100644
--- a/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-post-menu-test.js
+++ b/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-post-menu-test.js
@@ -4,12 +4,48 @@ import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { i18n } from "discourse-i18n";
import { postStreamWithAcceptedAnswerExcerpt } from "../helpers/discourse-solved-helpers";
+function nestedTopicWithUnacceptedAnswer() {
+ const topicView = postStreamWithAcceptedAnswerExcerpt(null);
+ const [opPost, answerPost] = topicView.post_stream.posts;
+ const topic = { ...topicView };
+ delete topic.post_stream;
+ delete topic.timeline_lookup;
+
+ return {
+ topic: {
+ ...topic,
+ accepted_answers: [],
+ has_accepted_answer: false,
+ },
+ op_post: {
+ ...opPost,
+ topic_accepted_answer: false,
+ },
+ roots: [
+ {
+ ...answerPost,
+ accepted_answer: false,
+ topic_accepted_answer: false,
+ reply_to_post_number: 1,
+ direct_reply_count: 0,
+ total_descendant_count: 0,
+ children: [],
+ },
+ ],
+ page: 0,
+ has_more_roots: false,
+ sort: "top",
+ message_bus_last_id: 0,
+ };
+}
+
acceptance("Post Menu | Accept and Unaccept", function (needs) {
needs.user({ admin: true });
needs.settings({
solved_enabled: true,
allow_solved_on_all_topics: true,
+ nested_replies_enabled: true,
});
needs.pretender((server, helper) => {
@@ -23,6 +59,9 @@ acceptance("Post Menu | Accept and Unaccept", function (needs) {
server.get("/t/12.json", () =>
helper.response(postStreamWithAcceptedAnswerExcerpt(null))
);
+ server.get("/n/test-solved/23.json", () =>
+ helper.response(nestedTopicWithUnacceptedAnswer())
+ );
});
test("accepting and unaccepting a post works", async function (assert) {
@@ -46,6 +85,26 @@ acceptance("Post Menu | Accept and Unaccept", function (needs) {
.exists("Unaccept button is visible again");
});
+ test("accepting a post in nested view updates the post menu button", async function (assert) {
+ await visit("/n/test-solved/23");
+
+ assert
+ .dom(
+ '.nested-post__article[data-post-number="2"] .post-action-menu__solved-unaccepted'
+ )
+ .exists("Accept button is visible");
+
+ await click(
+ '.nested-post__article[data-post-number="2"] .post-action-menu__solved-unaccepted'
+ );
+
+ assert
+ .dom(
+ '.nested-post__article[data-post-number="2"] .post-action-menu__solved-accepted'
+ )
+ .exists("Unaccept button is visible after accepting the solution");
+ });
+
test("hides/shows the accepting user based on setting", async function (assert) {
await visit("/t/without-excerpt/12");
diff --git a/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js b/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
index 7f7e9a15d20..9d5d3ae2287 100644
--- a/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
+++ b/plugins/discourse-solved/test/javascripts/acceptance/no-answer-prompt-test.js
@@ -18,12 +18,14 @@ acceptance("No Answer Prompt", function (needs) {
needs.hooks.beforeEach(function () {
pretender.get("/t/100.json", () => response(topicWithNoAnswer(1)));
pretender.post("/solution/accept", () =>
- response({
- success: "OK",
- post_number: 2,
- username: "helper",
- excerpt: "<p>Here is a potential answer</p>",
- })
+ response([
+ {
+ id: 2,
+ post_number: 2,
+ username: "helper",
+ cooked: "<p>Here is a potential answer</p>",
+ },
+ ])
);
});