From c06761b39696bbe552415abca9b8562657b88b7c Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 17 Feb 2017 16:40:43 -0500 Subject: [PATCH] allow to unassign a topic default select assigned user display icon for assigned user --- .../assign-button.js.es6 | 7 ++- .../topic-list-custom-tags/assign-to.raw.hbs | 2 +- .../topic-list-tags/assigned-to.raw.hbs | 2 +- .../discourse/controllers/assign-user.js.es6 | 26 +++++---- .../initializers/extend-for-assigns.js.es6 | 18 ++++++- .../discourse/templates/modal/assign-user.hbs | 2 +- assets/stylesheets/assigns.scss | 8 +++ config/locales/server.en.yml | 1 + plugin.rb | 54 ++++++++++++++++--- 9 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 assets/stylesheets/assigns.scss diff --git a/assets/javascripts/discourse/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6 b/assets/javascripts/discourse/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6 index b3586ba..49ffd66 100644 --- a/assets/javascripts/discourse/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6 +++ b/assets/javascripts/discourse/connectors/topic-footer-main-buttons-before-create/assign-button.js.es6 @@ -7,7 +7,12 @@ export default { actions: { assign(){ - showModal("assign-user", { model: this.topic }); + showModal("assign-user", { + model: { + topic: this.topic, + username: this.topic.get('assigned_to_user.username') + } + }); } } }; diff --git a/assets/javascripts/discourse/connectors/topic-list-custom-tags/assign-to.raw.hbs b/assets/javascripts/discourse/connectors/topic-list-custom-tags/assign-to.raw.hbs index de5cdf9..4858d25 100644 --- a/assets/javascripts/discourse/connectors/topic-list-custom-tags/assign-to.raw.hbs +++ b/assets/javascripts/discourse/connectors/topic-list-custom-tags/assign-to.raw.hbs @@ -1,3 +1,3 @@ {{#if context.topic.assigned_to_user}} - @{{context.topic.assigned_to_user.username}} +{{context.topic.assigned_to_user.username}} {{/if}} diff --git a/assets/javascripts/discourse/connectors/topic-list-tags/assigned-to.raw.hbs b/assets/javascripts/discourse/connectors/topic-list-tags/assigned-to.raw.hbs index bda33c2..3bd6cac 100644 --- a/assets/javascripts/discourse/connectors/topic-list-tags/assigned-to.raw.hbs +++ b/assets/javascripts/discourse/connectors/topic-list-tags/assigned-to.raw.hbs @@ -2,7 +2,7 @@ {{#if context.topic.assigned_to_user}}
- @{{context.topic.assigned_to_user.username}} + {{context.topic.assigned_to_user.username}}
{{/if}} {{/unless}} diff --git a/assets/javascripts/discourse/controllers/assign-user.js.es6 b/assets/javascripts/discourse/controllers/assign-user.js.es6 index 3cb4ce0..cc134b2 100644 --- a/assets/javascripts/discourse/controllers/assign-user.js.es6 +++ b/assets/javascripts/discourse/controllers/assign-user.js.es6 @@ -1,21 +1,29 @@ -import { default as computed } from 'ember-addons/ember-computed-decorators'; +//import { default as computed } from 'ember-addons/ember-computed-decorators'; import { ajax } from 'discourse/lib/ajax'; import { popupAjaxError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend({ - @computed("username") - disabled(username) { - return Ember.isEmpty(username); - }, + // @computed("username") + // disabled(username) { + // return Ember.isEmpty(username); + // }, actions: { assign(){ - return ajax('/assign/assign',{ + + let path = '/assign/assign'; + + if (Ember.isEmpty(this.get('model.username'))) { + path = '/assign/unassign'; + this.set('model.assigned_to_user', null); + } + + return ajax(path,{ type: 'PUT', - data: { username: this.get('username'), topic_id: this.get('model.id') } - }).then((user)=>{ - console.log(user); + data: { username: this.get('model.username'), topic_id: this.get('model.topic.id') } + }).then(()=>{ + //console.log(user); this.send('closeModal'); }).catch(popupAjaxError); } diff --git a/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 index a52a946..34d5af3 100644 --- a/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 +++ b/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 @@ -1,4 +1,5 @@ import { withPluginApi } from 'discourse/lib/plugin-api'; +import { h } from 'virtual-dom'; // should this be in API ? import Topic from 'discourse/models/topic'; @@ -10,13 +11,28 @@ function initialize(api, container) { Topic.reopen({ assignedToUserPath: function(){ return siteSettings.assigns_user_url_path.replace("{username}", this.get("assigned_to_user.username")); - }.property('owner') + }.property('assigned_to_user') }); api.addPostSmallActionIcon('assigned','user-plus'); api.addDiscoveryQueryParam('assigned', {replace: true, refreshModel: true}); + api.decorateWidget('header-topic-info:after-tags', dec => { + + const topic = dec.attrs.topic; + const assignedTo = topic.get('assigned_to_user.username'); + if (assignedTo) { + const assignedPath = topic.get('assignedToUserPath'); + return h('div.list-tags.assigned', + h('a.assigned-to.discourse-tag.simple', {href: assignedPath}, [ + h('i.fa.fa-user-plus'), + assignedTo + ]) + ); + } + }); + api.decorateWidget('post-contents:after-cooked', dec => { if (dec.attrs.post_number === 1) { const postModel = dec.getModel(); diff --git a/assets/javascripts/discourse/templates/modal/assign-user.hbs b/assets/javascripts/discourse/templates/modal/assign-user.hbs index ae62d1b..74855b1 100644 --- a/assets/javascripts/discourse/templates/modal/assign-user.hbs +++ b/assets/javascripts/discourse/templates/modal/assign-user.hbs @@ -7,7 +7,7 @@ excludeCurrentUser=false includeMentionableGroups=false hasGroups=false - usernames=username + usernames=model.username placeholderKey=placeholderKey autocomplete="off"}} {{/d-modal-body}} diff --git a/assets/stylesheets/assigns.scss b/assets/stylesheets/assigns.scss new file mode 100644 index 0000000..999143e --- /dev/null +++ b/assets/stylesheets/assigns.scss @@ -0,0 +1,8 @@ +a.assigned-to .fa.fa-user-plus { + margin-right: 2px; + color: #999; +} + +.list-tags.assigned { + margin-left: 5px; +} diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 29b29ff..b3eed72 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4,3 +4,4 @@ en: assigns_user_url_path: "Path to link all user links for assigned (use: {username} to subtitue username)" discourse_assign: assigned_to: "Topic assigned to @%{username}" + unassigned: "Topic was unassigned" diff --git a/plugin.rb b/plugin.rb index 29f4e2f..163db67 100644 --- a/plugin.rb +++ b/plugin.rb @@ -3,6 +3,8 @@ # version: 0.1 # authors: Sam Saffron +register_asset 'stylesheets/assigns.scss' + after_initialize do module ::DiscourseAssign @@ -16,7 +18,42 @@ after_initialize do before_filter :ensure_logged_in def unassign - _topic_id = params.require(:topic_id) + topic_id = params.require(:topic_id) + topic = Topic.find(topic_id.to_i) + if assigned_to_id = topic.custom_fields["assigned_to_id"] + topic.custom_fields["assigned_to_id"] = nil + topic.custom_fields["assigned_by_id"] = nil + topic.save! + + post = topic.posts.where(post_number: 1).first + post.publish_change_to_clients!(:revised, { reload_topic: true }) + + assigned_user = User.find_by(id: assigned_to_id) + + UserAction.where( + action_type: UserAction::ASSIGNED, + target_post_id: post.id + ).destroy_all + + # yank notification + Notification.where( + notification_type: Notification.types[:custom], + user_id: assigned_user.try(:id), + topic_id: topic.id, + post_number: 1 + ).where("data like '%discourse_assign.assign_notification%'") + .destroy_all + + + post_type = SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper] + topic.add_moderator_post(current_user, + I18n.t('discourse_assign.unassigned'), + { bump: false, + post_type: post_type, + action_code: "assigned"}) + end + + render json: success_json end def assign @@ -28,11 +65,14 @@ after_initialize do raise Discourse::NotFound unless assign_to - topic.custom_fields["assigned_to_id"] = assign_to.id - topic.custom_fields["assigned_by_id"] = current_user.id - topic.save! + #Scheduler::Defer.later "assign topic" do + + topic.custom_fields["assigned_to_id"] = assign_to.id + topic.custom_fields["assigned_by_id"] = current_user.id + topic.save! + + topic.posts.first.publish_change_to_clients!(:revised, { reload_topic: true }) - #Scheduler::Defer.later "add moderator post" do UserAction.log_action!(action_type: UserAction::ASSIGNED, user_id: assign_to.id, @@ -49,7 +89,7 @@ after_initialize do post_type: post_type, action_code: "assigned"}) - unless false && current_user.id == assign_to.id + unless current_user.id == assign_to.id Notification.create!(notification_type: Notification.types[:custom], user_id: assign_to.id, @@ -63,7 +103,6 @@ after_initialize do ) end - #end render json: success_json end @@ -167,6 +206,7 @@ after_initialize do DiscourseAssign::Engine.routes.draw do put "/assign" => "assign#assign" + put "/unassign" => "assign#unassign" end Discourse::Application.routes.append do