FEATURE: display indirect assignments in the first post (#227)

A link in the first post will allow going to a specific post assigned to a user or group.
This commit is contained in:
Krzysztof Kotlarek 2021-11-03 10:35:10 +11:00 committed by GitHub
parent 5447a72fd8
commit a4b1847eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 9 deletions

View File

@ -13,7 +13,7 @@ import TopicButtonAction, {
} from "discourse/controllers/topic-bulk-actions"; } from "discourse/controllers/topic-bulk-actions";
import { inject } from "@ember/controller"; import { inject } from "@ember/controller";
import I18n from "I18n"; import I18n from "I18n";
import { get } from "@ember/object"; import { isEmpty } from "@ember/utils";
const PLUGIN_ID = "discourse-assign"; const PLUGIN_ID = "discourse-assign";
@ -336,6 +336,77 @@ function initialize(api) {
}, },
}); });
api.createWidget("assigned-to-first-post", {
html(attrs) {
const topic = attrs.topic;
const [assignedToUser, assignedToGroup, indirectlyAssignedTo] = [
topic.assigned_to_user,
topic.assigned_to_group,
topic.indirectly_assigned_to,
];
const assigneeElements = [];
if (assignedToUser) {
assigneeElements.push(
h("span.assignee", [
h(
"a",
{
attributes: {
class: "assigned-to-username",
href: assignedToUserPath(assignedToUser),
},
},
assignedToUser.username
),
])
);
}
if (assignedToGroup) {
assigneeElements.push(
h("span.assignee", [
h(
"a",
{
attributes: {
class: "assigned-to-group",
href: assignedToGroupPath(assignedToGroup),
},
},
assignedToGroup.name
),
])
);
}
if (indirectlyAssignedTo) {
Object.keys(indirectlyAssignedTo).map((postNumber) => {
const assignee = indirectlyAssignedTo[postNumber];
assigneeElements.push(
h("span.assignee", [
h(
"a",
{
attributes: {
class: "assigned-indirectly",
href: `${topic.url}/${postNumber}`,
},
},
`#${postNumber} ${assignee.username || assignee.name}`
),
])
);
});
}
if (!isEmpty(assigneeElements)) {
return h("p.assigned-to", [
assignedToUser ? iconNode("user-plus") : iconNode("group-plus"),
h("span.assign-text", I18n.t("discourse_assign.assigned_to")),
assigneeElements,
]);
}
},
});
api.modifyClass("model:group", { api.modifyClass("model:group", {
pluginId: PLUGIN_ID, pluginId: PLUGIN_ID,
@ -404,10 +475,12 @@ function initialize(api) {
if (postModel) { if (postModel) {
let assignedToUser, assignedToGroup, postAssignment, href; let assignedToUser, assignedToGroup, postAssignment, href;
if (dec.attrs.post_number === 1) { if (dec.attrs.post_number === 1) {
assignedToUser = get(postModel, "topic.assigned_to_user"); return dec.widget.attach("assigned-to-first-post", {
assignedToGroup = get(postModel, "topic.assigned_to_group"); topic: postModel.topic,
});
} else { } else {
postAssignment = postModel.topic.indirectly_assigned_to?.[postModel.id]; postAssignment =
postModel.topic.indirectly_assigned_to?.[postModel.post_number];
if (postAssignment?.username) { if (postAssignment?.username) {
assignedToUser = postAssignment; assignedToUser = postAssignment;
} }

View File

@ -23,6 +23,9 @@
.composer-popup & { .composer-popup & {
margin-left: 0.5em; margin-left: 0.5em;
} }
.assignee:not(:last-child):after {
content: ", ";
}
} }
.topic-body { .topic-body {

View File

@ -413,7 +413,7 @@ after_initialize do
add_to_class(:topic, :indirectly_assigned_to) do add_to_class(:topic, :indirectly_assigned_to) do
return @indirectly_assigned_to if defined?(@indirectly_assigned_to) return @indirectly_assigned_to if defined?(@indirectly_assigned_to)
@indirectly_assigned_to = Assignment.where(topic_id: id, target_type: "Post").inject({}) do |acc, assignment| @indirectly_assigned_to = Assignment.where(topic_id: id, target_type: "Post").inject({}) do |acc, assignment|
acc[assignment.target_id] = assignment.assigned_to acc[assignment.target.post_number] = assignment.assigned_to
acc acc
end end
end end

View File

@ -26,6 +26,11 @@ acceptance("Discourse Assign | Assigned topic", function (needs) {
avatar_template: avatar_template:
"/letter_avatar/eviltrout/{size}/3_f9720745f5ce6dfc2b5641fca999d934.png", "/letter_avatar/eviltrout/{size}/3_f9720745f5ce6dfc2b5641fca999d934.png",
}; };
topic["indirectly_assigned_to"] = {
2: {
name: "Developers",
},
};
return helper.response(topic); return helper.response(topic);
}); });
@ -48,9 +53,9 @@ acceptance("Discourse Assign | Assigned topic", function (needs) {
"shows assignment in the header" "shows assignment in the header"
); );
assert.equal( assert.equal(
query("#post_1 .assigned-to-username").innerText.trim(), query("#post_1 .assigned-to").innerText,
"eviltrout", "Assigned toeviltrout#2 Developers",
"shows assignment in the first post" "shows assignment and indirect assignments in the first post"
); );
assert.ok(exists("#post_1 .assigned-to svg.d-icon-user-plus")); assert.ok(exists("#post_1 .assigned-to svg.d-icon-user-plus"));
assert.ok( assert.ok(
@ -69,7 +74,7 @@ acceptance("Discourse Assign | Assigned topic", function (needs) {
"shows assignment in the header" "shows assignment in the header"
); );
assert.equal( assert.equal(
query("#post_1 .assigned-to-username").innerText.trim(), query("#post_1 .assigned-to-group").innerText.trim(),
"Developers", "Developers",
"shows assignment in the first post" "shows assignment in the first post"
); );