DEV: Update modifyClass calls to native class syntax (#613)

This commit is contained in:
David Taylor 2024-12-02 13:22:02 +00:00 committed by GitHub
parent e603f06394
commit 032b34ce2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 176 additions and 159 deletions

View File

@ -1,4 +1,5 @@
import { getOwner } from "@ember/application"; import { getOwner } from "@ember/application";
import { action } from "@ember/object";
import { htmlSafe } from "@ember/template"; import { htmlSafe } from "@ember/template";
import { isEmpty } from "@ember/utils"; import { isEmpty } from "@ember/utils";
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
@ -24,8 +25,6 @@ import EditTopicAssignments from "../components/modal/edit-topic-assignments";
import TopicLevelAssignMenu from "../components/topic-level-assign-menu"; import TopicLevelAssignMenu from "../components/topic-level-assign-menu";
import { extendTopicModel } from "../models/topic"; import { extendTopicModel } from "../models/topic";
const PLUGIN_ID = "discourse-assign";
const DEPENDENT_KEYS = [ const DEPENDENT_KEYS = [
"topic.assigned_to_user", "topic.assigned_to_user",
"topic.assigned_to_group", "topic.assigned_to_group",
@ -361,18 +360,21 @@ function initialize(api) {
return getURL(`/g/${assignedToGroup.name}/assigned/everyone`); return getURL(`/g/${assignedToGroup.name}/assigned/everyone`);
} }
api.modifyClass("model:bookmark", { api.modifyClass(
pluginId: PLUGIN_ID, "model:bookmark",
(Superclass) =>
class extends Superclass {
@discourseComputed("assigned_to_user") @discourseComputed("assigned_to_user")
assignedToUserPath(assignedToUser) { assignedToUserPath(assignedToUser) {
return assignedToUserPath(assignedToUser); return assignedToUserPath(assignedToUser);
}, }
@discourseComputed("assigned_to_group") @discourseComputed("assigned_to_group")
assignedToGroupPath(assignedToGroup) { assignedToGroupPath(assignedToGroup) {
return assignedToGroupPath(assignedToGroup); return assignedToGroupPath(assignedToGroup);
}, }
}); }
);
api.modifyClass( api.modifyClass(
"component:topic-notifications-button", "component:topic-notifications-button",
@ -590,21 +592,24 @@ function initialize(api) {
}, },
}); });
api.modifyClass("model:group", { api.modifyClass(
pluginId: PLUGIN_ID, "model:group",
(Superclass) =>
class extends Superclass {
asJSON() { asJSON() {
return Object.assign({}, this._super(...arguments), { return Object.assign({}, super.asJSON(...arguments), {
assignable_level: this.assignable_level, assignable_level: this.assignable_level,
}); });
}, }
}); }
);
api.modifyClass("controller:topic", {
pluginId: PLUGIN_ID,
api.modifyClass(
"controller:topic",
(Superclass) =>
class extends Superclass {
subscribe() { subscribe() {
this._super(...arguments); super.subscribe(...arguments);
this.messageBus.subscribe("/staff/topic-assignment", (data) => { this.messageBus.subscribe("/staff/topic-assignment", (data) => {
const topic = this.model; const topic = this.model;
@ -613,7 +618,9 @@ function initialize(api) {
if (data.topic_id === topicId) { if (data.topic_id === topicId) {
let post; let post;
if (data.post_id) { if (data.post_id) {
post = topic.postStream.posts.find((p) => p.id === data.post_id); post = topic.postStream.posts.find(
(p) => p.id === data.post_id
);
} }
const target = post || topic; const target = post || topic;
@ -642,7 +649,9 @@ function initialize(api) {
this.appEvents.trigger("post-stream:refresh", { this.appEvents.trigger("post-stream:refresh", {
id: topic.postStream.posts[0].id, id: topic.postStream.posts[0].id,
}); });
this.appEvents.trigger("post-stream:refresh", { id: data.post_id }); this.appEvents.trigger("post-stream:refresh", {
id: data.post_id,
});
} }
if (topic.closed) { if (topic.closed) {
this.appEvents.trigger("post-stream:refresh", { this.appEvents.trigger("post-stream:refresh", {
@ -655,18 +664,19 @@ function initialize(api) {
id: topic.postStream.posts[0].id, id: topic.postStream.posts[0].id,
}); });
}); });
}, }
unsubscribe() { unsubscribe() {
this._super(...arguments); super.unsubscribe(...arguments);
if (!this.model?.id) { if (!this.model?.id) {
return; return;
} }
this.messageBus.unsubscribe("/staff/topic-assignment"); this.messageBus.unsubscribe("/staff/topic-assignment");
}, }
}); }
);
api.decorateWidget("post-contents:after-cooked", (dec) => { api.decorateWidget("post-contents:after-cooked", (dec) => {
const postModel = dec.getModel(); const postModel = dec.getModel();
@ -710,16 +720,17 @@ function initialize(api) {
"group-plus" "group-plus"
); );
api.modifyClass("controller:preferences/notifications", { api.modifyClass(
pluginId: PLUGIN_ID, "controller:preferences/notifications",
(Superclass) =>
actions: { class extends Superclass {
@action
save() { save() {
this.saveAttrNames.push("custom_fields"); this.saveAttrNames.push("custom_fields");
this._super(...arguments); super.save(...arguments);
}, }
}, }
}); );
api.addKeyboardShortcut("g a", "", { path: "/my/activity/assigned" }); api.addKeyboardShortcut("g a", "", { path: "/my/activity/assigned" });
} }
@ -834,7 +845,7 @@ export default {
} }
withPluginApi("1.34.0", (api) => { withPluginApi("1.34.0", (api) => {
extendTopicModel(api, PLUGIN_ID); extendTopicModel(api);
initialize(api); initialize(api);
registerTopicFooterButtons(api); registerTopicFooterButtons(api);

View File

@ -1,9 +1,10 @@
import { Assignment } from "./assignment"; import { Assignment } from "./assignment";
export function extendTopicModel(api, pluginId) { export function extendTopicModel(api) {
api.modifyClass("model:topic", { api.modifyClass(
pluginId, "model:topic",
(Superclass) =>
class extends Superclass {
assignees() { assignees() {
const result = []; const result = [];
@ -14,54 +15,59 @@ export function extendTopicModel(api, pluginId) {
const postAssignees = this.assignedPosts().map((p) => p.assigned_to); const postAssignees = this.assignedPosts().map((p) => p.assigned_to);
result.push(...postAssignees); result.push(...postAssignees);
return result; return result;
}, }
uniqueAssignees() { uniqueAssignees() {
const map = new Map(); const map = new Map();
this.assignees().forEach((user) => map.set(user.username, user)); this.assignees().forEach((user) => map.set(user.username, user));
return [...map.values()]; return [...map.values()];
}, }
assignedPosts() { assignedPosts() {
if (!this.indirectly_assigned_to) { if (!this.indirectly_assigned_to) {
return []; return [];
} }
return Object.entries(this.indirectly_assigned_to).map(([key, value]) => { return Object.entries(this.indirectly_assigned_to).map(
([key, value]) => {
value.postId = key; value.postId = key;
return value; return value;
}); }
}, );
}
assignments() { assignments() {
return [this.topicAssignment(), ...this.postAssignments()].compact(); return [this.topicAssignment(), ...this.postAssignments()].compact();
}, }
postAssignments() { postAssignments() {
if (!this.indirectly_assigned_to) { if (!this.indirectly_assigned_to) {
return []; return [];
} }
return Object.entries(this.indirectly_assigned_to).map(([key, value]) => { return Object.entries(this.indirectly_assigned_to).map(
([key, value]) => {
value.postId = key; value.postId = key;
return Assignment.fromPost(value); return Assignment.fromPost(value);
}); }
}, );
}
topicAssignment() { topicAssignment() {
return Assignment.fromTopic(this); return Assignment.fromTopic(this);
}, }
isAssigned() { isAssigned() {
return this.assigned_to_user || this.assigned_to_group; return this.assigned_to_user || this.assigned_to_group;
}, }
isAssignedTo(user) { isAssignedTo(user) {
return this.assigned_to_user?.username === user.username; return this.assigned_to_user?.username === user.username;
}, }
hasAssignedPosts() { hasAssignedPosts() {
return !!this.postAssignments().length; return !!this.postAssignments().length;
}, }
}); }
);
} }