Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export async function setPostCategoryExpertAttributes(
response.topic_needs_category_expert_approval,
expert_post_group_names: response.topic_expert_post_group_names,
});
appEvents.trigger("post-stream:refresh", { id: post.id });
} catch (error) {
popupAjaxError(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { next } from "@ember/runloop";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { iconNode } from "discourse/lib/icon-library";
import { withPluginApi } from "discourse/lib/plugin-api";
import CategoryExpertPostIndicator from "../components/category-expert-post-indicator";
import CategoryExpertsApproveButton from "../components/category-experts-approve-button";
Expand Down Expand Up @@ -41,43 +38,6 @@ function customizePost(api) {
"post-meta-data-poster-name",
CategoryExpertPostIndicator
);

withSilencedDeprecations("discourse.post-stream-widget-overrides", () =>
customizeWidgetPost(api)
);
}

function customizeWidgetPost(api) {
api.decorateWidget("post:after", (helper) => {
const post = helper.getModel();
next(() => {
const article = document.querySelector(
`article[data-post-id="${post.id}"]`
);
if (!article) {
return;
}

if (post.category_expert_approved_group) {
article.classList.add("category-expert-post");
article.classList.add(
`category-expert-${post.category_expert_approved_group}`
);
} else if (post.needs_category_expert_approval) {
article.classList.remove("category-expert-post");
}
});
});

api.decorateWidget("poster-name:after", (helper) => {
const post = helper.getModel();
if (post && post.category_expert_approved_group) {
return helper.h(
`span.category-expert-indicator.category-expert-${post.category_expert_approved_group}`,
iconNode("circle-check")
);
}
});
}

function customizePostMenu(api) {
Expand Down
240 changes: 115 additions & 125 deletions test/javascripts/acceptance/category-experts-post-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,128 +10,118 @@ import {

const groupName = "some-group";

["enabled", "disabled"].forEach((postStreamMode) => {
acceptance(
`Discourse Category Experts - Posts - Auto-approved (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
enable_category_experts: true,
category_experts_posts_require_approval: false,
glimmer_post_stream_mode: postStreamMode,
});

needs.pretender((server, helper) => {
// deep clone
let topicResponse = JSON.parse(
JSON.stringify(topicFixtures["/t/2480/1.json"])
);
topicResponse.post_stream.posts[2].category_expert_approved_group =
groupName;
server.get("/t/2480.json", () => helper.response(topicResponse));

let cardResponse = JSON.parse(
JSON.stringify(userFixtures["/u/charlie/card.json"])
);
cardResponse.user.username = "normal_user";
cardResponse.user.category_expert_endorsements = [];
cardResponse.user.topic_post_count = { 2480: 2 };
server.get("/u/normal_user/card.json", () =>
helper.response(cardResponse)
);
});

test("Posts with category_expert_approved have the correct classes", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const lastArticle = articles[articles.length - 1];

assert.ok(lastArticle.classList.contains("category-expert-post"));
assert.ok(
lastArticle.classList.contains(`category-expert-${groupName}`)
);

await click(lastArticle.querySelector("button.show-more-actions"));

assert.notOk(exists(".post-controls .unapprove-category-expert-post"));
});

test("Filter posts by user works", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");
await click("article#post_2 .trigger-user-card a");
await click(".usercard-controls .btn-default");
assert.equal(queryAll(".topic-post").length, 3);
});
}
);

acceptance(
`Discourse Category Experts - Posts - Need approved (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
enable_category_experts: true,
category_experts_posts_require_approval: true,
glimmer_post_stream_mode: postStreamMode,
});

needs.pretender((server, helper) => {
// deep clone
let topicResponse = JSON.parse(
JSON.stringify(topicFixtures["/t/2480/1.json"])
);
topicResponse.post_stream.posts[1].needs_category_expert_approval = true;
topicResponse.post_stream.posts[1].can_manage_category_expert_posts = true;

topicResponse.post_stream.posts[2].category_expert_approved_group =
groupName;
topicResponse.post_stream.posts[2].can_manage_category_expert_posts = true;

server.get("/t/2480.json", () => helper.response(topicResponse));
server.post("/category-experts/unapprove", () => helper.response({}));
server.post("/category-experts/approve", () =>
helper.response({
group_name: groupName,
})
);
});

test("The unapprove button is present and works for approved posts", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const lastArticle = articles[articles.length - 1];

assert.ok(lastArticle.classList.contains("category-expert-post"));
assert.ok(
lastArticle.classList.contains(`category-expert-${groupName}`)
);

await click(lastArticle.querySelector("button.show-more-actions"));
await click(".post-controls .unapprove-category-expert-post");

assert.notOk(lastArticle.classList.contains("category-expert-post"));
});

test("The approve button is present and works for unapproved posts by category experts", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const article = articles[articles.length - 2];

assert.notOk(article.classList.contains("category-expert-post"));
assert.notOk(
article.classList.contains(`category-expert-${groupName}`)
);

await click(
article.querySelector(".post-controls .approve-category-expert-post")
);

assert.ok(article.classList.contains("category-expert-post"));
assert.ok(article.classList.contains(`category-expert-${groupName}`));
});
}
);
});
acceptance(
`Discourse Category Experts - Posts - Auto-approved`,
function (needs) {
needs.user();
needs.settings({
enable_category_experts: true,
category_experts_posts_require_approval: false,
});

needs.pretender((server, helper) => {
// deep clone
let topicResponse = JSON.parse(
JSON.stringify(topicFixtures["/t/2480/1.json"])
);
topicResponse.post_stream.posts[2].category_expert_approved_group =
groupName;
server.get("/t/2480.json", () => helper.response(topicResponse));

let cardResponse = JSON.parse(
JSON.stringify(userFixtures["/u/charlie/card.json"])
);
cardResponse.user.username = "normal_user";
cardResponse.user.category_expert_endorsements = [];
cardResponse.user.topic_post_count = { 2480: 2 };
server.get("/u/normal_user/card.json", () =>
helper.response(cardResponse)
);
});

test("Posts with category_expert_approved have the correct classes", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const lastArticle = articles[articles.length - 1];

assert.ok(lastArticle.classList.contains("category-expert-post"));
assert.ok(lastArticle.classList.contains(`category-expert-${groupName}`));

await click(lastArticle.querySelector("button.show-more-actions"));

assert.notOk(exists(".post-controls .unapprove-category-expert-post"));
});

test("Filter posts by user works", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");
await click("article#post_2 .trigger-user-card a");
await click(".usercard-controls .btn-default");
assert.equal(queryAll(".topic-post").length, 3);
});
}
);

acceptance(
`Discourse Category Experts - Posts - Need approved`,
function (needs) {
needs.user();
needs.settings({
enable_category_experts: true,
category_experts_posts_require_approval: true,
});

needs.pretender((server, helper) => {
// deep clone
let topicResponse = JSON.parse(
JSON.stringify(topicFixtures["/t/2480/1.json"])
);
topicResponse.post_stream.posts[1].needs_category_expert_approval = true;
topicResponse.post_stream.posts[1].can_manage_category_expert_posts = true;

topicResponse.post_stream.posts[2].category_expert_approved_group =
groupName;
topicResponse.post_stream.posts[2].can_manage_category_expert_posts = true;

server.get("/t/2480.json", () => helper.response(topicResponse));
server.post("/category-experts/unapprove", () => helper.response({}));
server.post("/category-experts/approve", () =>
helper.response({
group_name: groupName,
})
);
});

test("The unapprove button is present and works for approved posts", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const lastArticle = articles[articles.length - 1];

assert.ok(lastArticle.classList.contains("category-expert-post"));
assert.ok(lastArticle.classList.contains(`category-expert-${groupName}`));

await click(lastArticle.querySelector("button.show-more-actions"));
await click(".post-controls .unapprove-category-expert-post");

assert.notOk(lastArticle.classList.contains("category-expert-post"));
});

test("The approve button is present and works for unapproved posts by category experts", async function (assert) {
await visit("/t/topic-for-group-moderators/2480");

const articles = queryAll(".topic-post article.onscreen-post");
const article = articles[articles.length - 2];

assert.notOk(article.classList.contains("category-expert-post"));
assert.notOk(article.classList.contains(`category-expert-${groupName}`));

await click(
article.querySelector(".post-controls .approve-category-expert-post")
);

assert.ok(article.classList.contains("category-expert-post"));
assert.ok(article.classList.contains(`category-expert-${groupName}`));
});
}
);