diff --git a/app/controllers/discourse_assign/assign_controller.rb b/app/controllers/discourse_assign/assign_controller.rb index c9accfd..5b2dfec 100644 --- a/app/controllers/discourse_assign/assign_controller.rb +++ b/app/controllers/discourse_assign/assign_controller.rb @@ -44,11 +44,25 @@ module DiscourseAssign end def assign - reassign_or_assign_target(action: "assign") - end + target_id = params.require(:target_id) + target_type = params.require(:target_type) + username = params.permit(:username)['username'] + group_name = params.permit(:group_name)['group_name'] - def reassign - reassign_or_assign_target(action: "reassign") + assign_to = username.present? ? User.find_by(username_lower: username.downcase) : Group.where("LOWER(name) = ?", group_name.downcase).first + + raise Discourse::NotFound unless assign_to + raise Discourse::NotFound if !Assignment.valid_type?(target_type) + target = target_type.constantize.where(id: target_id).first + raise Discourse::NotFound unless target + + assign = Assigner.new(target, current_user).assign(assign_to) + + if assign[:success] + render json: success_json + else + render json: translate_failure(assign[:reason], assign_to), status: 400 + end end def assigned @@ -169,29 +183,5 @@ module DiscourseAssign def ensure_assign_allowed raise Discourse::InvalidAccess.new unless current_user.can_assign? end - - def reassign_or_assign_target(action:) - target_id = params.require(:target_id) - target_type = params.require(:target_type) - username = params.permit(:username)['username'] - group_name = params.permit(:group_name)['group_name'] - - assign_to = username.present? ? User.find_by(username_lower: username.downcase) : Group.where("LOWER(name) = ?", group_name.downcase).first - - raise Discourse::NotFound unless assign_to - raise Discourse::NotFound if !Assignment.valid_type?(target_type) - target = target_type.constantize.where(id: target_id).first - raise Discourse::NotFound unless target - - # perhaps? - #Scheduler::Defer.later "assign topic" do - assign = Assigner.new(target, current_user).send(action, assign_to) - - if assign[:success] - render json: success_json - else - render json: translate_failure(assign[:reason], assign_to), status: 400 - end - end end end diff --git a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 index 841fef4..7d6b826 100644 --- a/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 +++ b/assets/javascripts/discourse-assign/controllers/assign-user.js.es6 @@ -38,12 +38,14 @@ export default Controller.extend({ }); }, - reassignOrAssignTarget(assign_action) { + @action + assign() { if (this.isBulkAction) { this.bulkAction(this.model.username); return; } - let path = "/assign/" + assign_action; + + let path = "/assign/assign"; if (isEmpty(this.get("model.username"))) { this.model.target.set("assigned_to_user", null); @@ -103,49 +105,8 @@ export default Controller.extend({ } }, - @action - assign() { - this.reassignOrAssignTarget("assign"); - }, - - @action - reassign() { - this.reassignOrAssignTarget("reassign"); - }, - - @action - reassignUser(name) { - if (this.isBulkAction) { - this.bulkAction(name); - return; - } - - if (this.allowedGroupsForAssignment.includes(name)) { - this.setProperties({ - "model.username": null, - "model.group_name": name, - "model.allowedGroups": this.taskActions.allowedGroups, - }); - } else { - this.setProperties({ - "model.username": name, - "model.group_name": null, - "model.allowedGroups": this.taskActions.allowedGroups, - }); - } - - if (name) { - return this.reassign(); - } - }, - @action assignUsername(selected) { this.assignUser(selected.firstObject); }, - - @action - reassignUsername(selected) { - this.reassignUser(selected.firstObject); - }, }); diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 index bec3747..1c009f6 100644 --- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 +++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 @@ -86,11 +86,16 @@ function registerTopicFooterButtons(api) { break; } case "reassign": { - taskActions.reassign(this.topic).set("model.onSuccess", () => { - this.appEvents.trigger("post-stream:refresh", { - id: this.topic.postStream.firstPostId, + taskActions + .assign(this.topic, { + targetType: "Topic", + isAssigned: this.topic.isAssigned(), + }) + .set("model.onSuccess", () => { + this.appEvents.trigger("post-stream:refresh", { + id: this.topic.postStream.firstPostId, + }); }); - }); break; } } @@ -387,11 +392,16 @@ function registerTopicFooterButtons(api) { const taskActions = getOwner(this).lookup("service:task-actions"); - taskActions.reassign(this.topic).set("model.onSuccess", () => { - this.appEvents.trigger("post-stream:refresh", { - id: this.topic.postStream.firstPostId, + taskActions + .assign(this.topic, { + targetType: "Topic", + isAssigned: this.topic.isAssigned(), + }) + .set("model.onSuccess", () => { + this.appEvents.trigger("post-stream:refresh", { + id: this.topic.postStream.firstPostId, + }); }); - }); }, dropdown() { return this.currentUser?.can_assign && this.topic.isAssigned(); @@ -460,7 +470,10 @@ function initialize(api) { }); api.attachWidgetAction("post", "assignPost", function () { const taskActions = getOwner(this).lookup("service:task-actions"); - taskActions.assign(this.model, "Post"); + taskActions.assign(this.model, { + isAssigned: false, + targetType: "Post", + }); }); api.attachWidgetAction("post", "unassignPost", function () { const taskActions = getOwner(this).lookup("service:task-actions"); diff --git a/assets/javascripts/discourse/services/task-actions.js.es6 b/assets/javascripts/discourse/services/task-actions.js.es6 index b3e9f9d..c7cae11 100644 --- a/assets/javascripts/discourse/services/task-actions.js.es6 +++ b/assets/javascripts/discourse/services/task-actions.js.es6 @@ -22,24 +22,28 @@ export default Service.extend({ }); }, - assign(target, targetType = "Topic") { + assign(target, options = { isAssigned: false, targetType: "Topic" }) { return showModal("assign-user", { - title: "discourse_assign.assign" + this.i18nSuffix(targetType) + ".title", + title: + "discourse_assign.assign" + + this.i18nSuffix(options.targetType) + + `.${options.isAssigned ? "reassign_title" : "title"}`, model: { description: - "discourse_assign.assign" + - this.i18nSuffix(targetType) + + `discourse_assign.${options.isAssigned ? "reassign" : "assign"}` + + this.i18nSuffix(options.targetType) + ".description", + reassign: options.isAssigned, username: target.assigned_to_user?.username, group_name: target.assigned_to_group?.name, target, - targetType, + targetType: options.targetType, }, }); }, reassignUserToTopic(user, target, targetType = "Topic") { - return ajax("/assign/reassign", { + return ajax("/assign/assign", { type: "PUT", data: { username: user.username, @@ -48,24 +52,4 @@ export default Service.extend({ }, }); }, - - reassign(target, targetType = "Topic") { - return showModal("assign-user", { - title: - "discourse_assign.assign" + - this.i18nSuffix(targetType) + - ".reassign_title", - model: { - description: - "discourse_assign.reassign" + - this.i18nSuffix(targetType) + - ".description", - reassign: true, - username: target.assigned_to_user?.username, - group_name: target.assigned_to_group?.name, - target, - targetType, - }, - }); - }, }); diff --git a/assets/javascripts/discourse/templates/modal/assign-user.hbs b/assets/javascripts/discourse/templates/modal/assign-user.hbs index 37e4ad5..8884cf6 100644 --- a/assets/javascripts/discourse/templates/modal/assign-user.hbs +++ b/assets/javascripts/discourse/templates/modal/assign-user.hbs @@ -4,7 +4,7 @@ {{email-group-user-chooser autocomplete="off" value=assigneeName - onChange=(action (if model.reassign "reassignUsername" "assignUsername")) + onChange=(action "assignUsername") autofocus="autofocus" options=(hash placementStrategy="absolute" @@ -19,7 +19,7 @@ }}