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 8b46635..de5cdf9 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.owner}} - @{{context.topic.owner.username}} +{{#if context.topic.assigned_to_user}} + @{{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 1d4836c..bda33c2 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 @@ -1,7 +1,8 @@ {{#unless context.topic.tags}} -{{#if context.topic.owner}} + +{{#if context.topic.assigned_to_user}}
{{/if}} {{/unless}} diff --git a/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 index c4d3ef3..334e9a6 100644 --- a/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 +++ b/assets/javascripts/discourse/initializers/extend-for-assigns.js.es6 @@ -1,6 +1,18 @@ import { withPluginApi } from 'discourse/lib/plugin-api'; -function initialize(api) { +// should this be in API ? +import Topic from 'discourse/models/topic'; + +function initialize(api, container) { + + const siteSettings = container.lookup('site-settings:main'); + + Topic.reopen({ + assignedToUserPath: function(){ + return siteSettings.assigns_user_url_path.replace("{username}", this.get("assigned_to_user.username")); + }.property('owner') + }); + api.addPostSmallActionIcon('assigned','user-plus'); api.decorateWidget('post-contents:after-cooked', dec => { @@ -9,18 +21,22 @@ function initialize(api) { if (postModel) { const assignedToUser = postModel.get('topic.assigned_to_user'); if (assignedToUser) { - const html = I18n.t('discourse_assign.assign_html', assignedToUser); - //const topic = postModel.get('topic'); + const path = postModel.get('topic.assignedToUserPath'); + const userLink = `${assignedToUser.username}`; + const html = I18n.t('discourse_assign.assign_html', {userLink}); return dec.rawHtml(html); } } } + }); }; export default { name: 'extend-for-assign', - initialize() { - withPluginApi('0.8', initialize); + initialize(container) { + withPluginApi('0.8', api => { + initialize(api, container); + }); } }; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index bf20203..d260fe8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3,7 +3,7 @@ en: action_codes: assigned: "assigned" discourse_assign: - assign_html: "Assigned to: @{{username}}
" + assign_html: "Assigned to {{userLink}}
" assign_notification: "{{username}} {{description}}
" assign: title: "Assign" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 89ac865..29b29ff 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1,3 +1,6 @@ en: + site_settings: + assigns_public: "Allow general public to see topic assignments" + 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}" diff --git a/config/settings.yml b/config/settings.yml new file mode 100644 index 0000000..e2f47b5 --- /dev/null +++ b/config/settings.yml @@ -0,0 +1,5 @@ +plugins: + assigns_public: false + assigns_user_url_path: + client: true + default: "/latest?assigned={username}" diff --git a/plugin.rb b/plugin.rb index 455c9c8..2510b0f 100644 --- a/plugin.rb +++ b/plugin.rb @@ -40,11 +40,13 @@ after_initialize do target_post_id: topic.posts.find_by(post_number: 1).id, target_topic_id: topic.id) + post_type = SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper] + topic.add_moderator_post(current_user, I18n.t('discourse_assign.assigned_to', username: assign_to.username), { bump: false, - post_type: Post.types[:small_action], + post_type: post_type, action_code: "assigned"}) unless false && current_user.id == assign_to.id @@ -67,22 +69,25 @@ after_initialize do end class ::Topic - def owner - @owner || + def assigned_to_user + @assigned_to_user || if user_id = custom_fields["assigned_to_id"] - @owner = User.find_by(id: user_id) + @assigned_to_user = User.find_by(id: user_id) end end - def preload_owner(owner) - @owner = owner + def preload_assigned_to_user(assigned_to_user) + @assigned_to_user = assigned_to_user end end TopicList.preloaded_custom_fields << "assigned_to_id" - TopicList.on_preload do |topics| - if topics.length > 0 + TopicList.on_preload do |topics, topic_list| + is_staff = topic_list.current_user && topic_list.current_user.staff? + allowed_access = SiteSetting.assigns_public || is_staff + + if allowed_access && topics.length > 0 users = User.where("id in ( SELECT value::int FROM topic_custom_fields @@ -95,7 +100,7 @@ after_initialize do topics.each do |t| if id = t.custom_fields['assigned_to_id'] - t.preload_owner(map[id.to_i]) + t.preload_assigned_to_user(map[id.to_i]) end end end @@ -103,7 +108,11 @@ after_initialize do require_dependency 'listable_topic_serializer' class ::ListableTopicSerializer - has_one :owner, serializer: BasicUserSerializer, embed: :objects + has_one :assigned_to_user, serializer: BasicUserSerializer, embed: :objects + + def include_assigned_to_user? + (SiteSetting.assigns_public || scope.is_staff?) && object.assigned_to_user + end end require_dependency 'topic_view_serializer' @@ -128,7 +137,9 @@ after_initialize do end def include_assigned_to_user? - assigned_to_user_id + if SiteSetting.assigns_public || scope.is_staff? + assigned_to_user_id + end end def assigned_to_user_id