Skip to content

Commit 032b34c

Browse files
DEV: Update modifyClass calls to native class syntax (#613)
1 parent e603f06 commit 032b34c

File tree

2 files changed

+161
-144
lines changed

2 files changed

+161
-144
lines changed

assets/javascripts/discourse/initializers/extend-for-assigns.js

Lines changed: 103 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getOwner } from "@ember/application";
2+
import { action } from "@ember/object";
23
import { htmlSafe } from "@ember/template";
34
import { isEmpty } from "@ember/utils";
45
import { hbs } from "ember-cli-htmlbars";
@@ -24,8 +25,6 @@ import EditTopicAssignments from "../components/modal/edit-topic-assignments";
2425
import TopicLevelAssignMenu from "../components/topic-level-assign-menu";
2526
import { extendTopicModel } from "../models/topic";
2627

27-
const PLUGIN_ID = "discourse-assign";
28-
2928
const DEPENDENT_KEYS = [
3029
"topic.assigned_to_user",
3130
"topic.assigned_to_group",
@@ -361,18 +360,21 @@ function initialize(api) {
361360
return getURL(`/g/${assignedToGroup.name}/assigned/everyone`);
362361
}
363362

364-
api.modifyClass("model:bookmark", {
365-
pluginId: PLUGIN_ID,
363+
api.modifyClass(
364+
"model:bookmark",
365+
(Superclass) =>
366+
class extends Superclass {
367+
@discourseComputed("assigned_to_user")
368+
assignedToUserPath(assignedToUser) {
369+
return assignedToUserPath(assignedToUser);
370+
}
366371

367-
@discourseComputed("assigned_to_user")
368-
assignedToUserPath(assignedToUser) {
369-
return assignedToUserPath(assignedToUser);
370-
},
371-
@discourseComputed("assigned_to_group")
372-
assignedToGroupPath(assignedToGroup) {
373-
return assignedToGroupPath(assignedToGroup);
374-
},
375-
});
372+
@discourseComputed("assigned_to_group")
373+
assignedToGroupPath(assignedToGroup) {
374+
return assignedToGroupPath(assignedToGroup);
375+
}
376+
}
377+
);
376378

377379
api.modifyClass(
378380
"component:topic-notifications-button",
@@ -590,83 +592,91 @@ function initialize(api) {
590592
},
591593
});
592594

593-
api.modifyClass("model:group", {
594-
pluginId: PLUGIN_ID,
595-
596-
asJSON() {
597-
return Object.assign({}, this._super(...arguments), {
598-
assignable_level: this.assignable_level,
599-
});
600-
},
601-
});
602-
603-
api.modifyClass("controller:topic", {
604-
pluginId: PLUGIN_ID,
605-
606-
subscribe() {
607-
this._super(...arguments);
608-
609-
this.messageBus.subscribe("/staff/topic-assignment", (data) => {
610-
const topic = this.model;
611-
const topicId = topic.id;
612-
613-
if (data.topic_id === topicId) {
614-
let post;
615-
if (data.post_id) {
616-
post = topic.postStream.posts.find((p) => p.id === data.post_id);
617-
}
618-
const target = post || topic;
619-
620-
target.set("assignment_note", data.assignment_note);
621-
target.set("assignment_status", data.assignment_status);
622-
if (data.assigned_type === "User") {
623-
target.set(
624-
"assigned_to_user_id",
625-
data.type === "assigned" ? data.assigned_to.id : null
626-
);
627-
target.set("assigned_to_user", data.assigned_to);
628-
}
629-
if (data.assigned_type === "Group") {
630-
target.set(
631-
"assigned_to_group_id",
632-
data.type === "assigned" ? data.assigned_to.id : null
633-
);
634-
target.set("assigned_to_group", data.assigned_to);
635-
}
595+
api.modifyClass(
596+
"model:group",
597+
(Superclass) =>
598+
class extends Superclass {
599+
asJSON() {
600+
return Object.assign({}, super.asJSON(...arguments), {
601+
assignable_level: this.assignable_level,
602+
});
603+
}
604+
}
605+
);
636606

637-
if (data.post_id) {
638-
if (data.type === "unassigned") {
639-
delete topic.indirectly_assigned_to[data.post_number];
607+
api.modifyClass(
608+
"controller:topic",
609+
(Superclass) =>
610+
class extends Superclass {
611+
subscribe() {
612+
super.subscribe(...arguments);
613+
614+
this.messageBus.subscribe("/staff/topic-assignment", (data) => {
615+
const topic = this.model;
616+
const topicId = topic.id;
617+
618+
if (data.topic_id === topicId) {
619+
let post;
620+
if (data.post_id) {
621+
post = topic.postStream.posts.find(
622+
(p) => p.id === data.post_id
623+
);
624+
}
625+
const target = post || topic;
626+
627+
target.set("assignment_note", data.assignment_note);
628+
target.set("assignment_status", data.assignment_status);
629+
if (data.assigned_type === "User") {
630+
target.set(
631+
"assigned_to_user_id",
632+
data.type === "assigned" ? data.assigned_to.id : null
633+
);
634+
target.set("assigned_to_user", data.assigned_to);
635+
}
636+
if (data.assigned_type === "Group") {
637+
target.set(
638+
"assigned_to_group_id",
639+
data.type === "assigned" ? data.assigned_to.id : null
640+
);
641+
target.set("assigned_to_group", data.assigned_to);
642+
}
643+
644+
if (data.post_id) {
645+
if (data.type === "unassigned") {
646+
delete topic.indirectly_assigned_to[data.post_number];
647+
}
648+
649+
this.appEvents.trigger("post-stream:refresh", {
650+
id: topic.postStream.posts[0].id,
651+
});
652+
this.appEvents.trigger("post-stream:refresh", {
653+
id: data.post_id,
654+
});
655+
}
656+
if (topic.closed) {
657+
this.appEvents.trigger("post-stream:refresh", {
658+
id: topic.postStream.posts[0].id,
659+
});
660+
}
640661
}
641-
642-
this.appEvents.trigger("post-stream:refresh", {
643-
id: topic.postStream.posts[0].id,
644-
});
645-
this.appEvents.trigger("post-stream:refresh", { id: data.post_id });
646-
}
647-
if (topic.closed) {
662+
this.appEvents.trigger("header:update-topic", topic);
648663
this.appEvents.trigger("post-stream:refresh", {
649664
id: topic.postStream.posts[0].id,
650665
});
651-
}
666+
});
652667
}
653-
this.appEvents.trigger("header:update-topic", topic);
654-
this.appEvents.trigger("post-stream:refresh", {
655-
id: topic.postStream.posts[0].id,
656-
});
657-
});
658-
},
659668

660-
unsubscribe() {
661-
this._super(...arguments);
669+
unsubscribe() {
670+
super.unsubscribe(...arguments);
662671

663-
if (!this.model?.id) {
664-
return;
665-
}
672+
if (!this.model?.id) {
673+
return;
674+
}
666675

667-
this.messageBus.unsubscribe("/staff/topic-assignment");
668-
},
669-
});
676+
this.messageBus.unsubscribe("/staff/topic-assignment");
677+
}
678+
}
679+
);
670680

671681
api.decorateWidget("post-contents:after-cooked", (dec) => {
672682
const postModel = dec.getModel();
@@ -710,16 +720,17 @@ function initialize(api) {
710720
"group-plus"
711721
);
712722

713-
api.modifyClass("controller:preferences/notifications", {
714-
pluginId: PLUGIN_ID,
715-
716-
actions: {
717-
save() {
718-
this.saveAttrNames.push("custom_fields");
719-
this._super(...arguments);
720-
},
721-
},
722-
});
723+
api.modifyClass(
724+
"controller:preferences/notifications",
725+
(Superclass) =>
726+
class extends Superclass {
727+
@action
728+
save() {
729+
this.saveAttrNames.push("custom_fields");
730+
super.save(...arguments);
731+
}
732+
}
733+
);
723734

724735
api.addKeyboardShortcut("g a", "", { path: "/my/activity/assigned" });
725736
}
@@ -834,7 +845,7 @@ export default {
834845
}
835846

836847
withPluginApi("1.34.0", (api) => {
837-
extendTopicModel(api, PLUGIN_ID);
848+
extendTopicModel(api);
838849
initialize(api);
839850
registerTopicFooterButtons(api);
840851

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,73 @@
11
import { Assignment } from "./assignment";
22

3-
export function extendTopicModel(api, pluginId) {
4-
api.modifyClass("model:topic", {
5-
pluginId,
3+
export function extendTopicModel(api) {
4+
api.modifyClass(
5+
"model:topic",
6+
(Superclass) =>
7+
class extends Superclass {
8+
assignees() {
9+
const result = [];
610

7-
assignees() {
8-
const result = [];
11+
if (this.assigned_to_user) {
12+
result.push(this.assigned_to_user);
13+
}
914

10-
if (this.assigned_to_user) {
11-
result.push(this.assigned_to_user);
12-
}
15+
const postAssignees = this.assignedPosts().map((p) => p.assigned_to);
16+
result.push(...postAssignees);
17+
return result;
18+
}
1319

14-
const postAssignees = this.assignedPosts().map((p) => p.assigned_to);
15-
result.push(...postAssignees);
16-
return result;
17-
},
20+
uniqueAssignees() {
21+
const map = new Map();
22+
this.assignees().forEach((user) => map.set(user.username, user));
23+
return [...map.values()];
24+
}
1825

19-
uniqueAssignees() {
20-
const map = new Map();
21-
this.assignees().forEach((user) => map.set(user.username, user));
22-
return [...map.values()];
23-
},
26+
assignedPosts() {
27+
if (!this.indirectly_assigned_to) {
28+
return [];
29+
}
2430

25-
assignedPosts() {
26-
if (!this.indirectly_assigned_to) {
27-
return [];
28-
}
31+
return Object.entries(this.indirectly_assigned_to).map(
32+
([key, value]) => {
33+
value.postId = key;
34+
return value;
35+
}
36+
);
37+
}
2938

30-
return Object.entries(this.indirectly_assigned_to).map(([key, value]) => {
31-
value.postId = key;
32-
return value;
33-
});
34-
},
39+
assignments() {
40+
return [this.topicAssignment(), ...this.postAssignments()].compact();
41+
}
3542

36-
assignments() {
37-
return [this.topicAssignment(), ...this.postAssignments()].compact();
38-
},
43+
postAssignments() {
44+
if (!this.indirectly_assigned_to) {
45+
return [];
46+
}
3947

40-
postAssignments() {
41-
if (!this.indirectly_assigned_to) {
42-
return [];
43-
}
48+
return Object.entries(this.indirectly_assigned_to).map(
49+
([key, value]) => {
50+
value.postId = key;
51+
return Assignment.fromPost(value);
52+
}
53+
);
54+
}
4455

45-
return Object.entries(this.indirectly_assigned_to).map(([key, value]) => {
46-
value.postId = key;
47-
return Assignment.fromPost(value);
48-
});
49-
},
56+
topicAssignment() {
57+
return Assignment.fromTopic(this);
58+
}
5059

51-
topicAssignment() {
52-
return Assignment.fromTopic(this);
53-
},
60+
isAssigned() {
61+
return this.assigned_to_user || this.assigned_to_group;
62+
}
5463

55-
isAssigned() {
56-
return this.assigned_to_user || this.assigned_to_group;
57-
},
64+
isAssignedTo(user) {
65+
return this.assigned_to_user?.username === user.username;
66+
}
5867

59-
isAssignedTo(user) {
60-
return this.assigned_to_user?.username === user.username;
61-
},
62-
63-
hasAssignedPosts() {
64-
return !!this.postAssignments().length;
65-
},
66-
});
68+
hasAssignedPosts() {
69+
return !!this.postAssignments().length;
70+
}
71+
}
72+
);
6773
}

0 commit comments

Comments
 (0)