diff --git a/assets/javascripts/discourse/templates/user-activity-assigned.hbs b/assets/javascripts/discourse/templates/user-activity-assigned.hbs index c67894b..d1b5e19 100644 --- a/assets/javascripts/discourse/templates/user-activity-assigned.hbs +++ b/assets/javascripts/discourse/templates/user-activity-assigned.hbs @@ -1,11 +1,18 @@ -{{#if canUnassignAll}} -
+
+ {{#if model.topic_list.assigned_messages_count}} + {{#link-to 'userPrivateMessages.assigned' user.model class="btn assign-messages-assigned"}} + {{d-icon "envelope" class="glyph"}} + {{i18n 'user.messages.assigned_title' count=model.topic_list.assigned_messages_count}} + {{/link-to}} + {{/if}} + + {{#if canUnassignAll}} {{d-button action=(action "unassignAll") icon="times" label="discourse_assign.unassign_all.title" class="btn-danger"}} -
-{{/if}} + {{/if}} +
{{outlet}} diff --git a/assets/stylesheets/assigns.scss b/assets/stylesheets/assigns.scss index d3f35f4..dcd7573 100644 --- a/assets/stylesheets/assigns.scss +++ b/assets/stylesheets/assigns.scss @@ -3,6 +3,10 @@ display: flex; justify-content: flex-end; margin-bottom: 0.5em; + + .assign-messages-assigned { + margin-right: 0.5em; + } } } diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 80a3a68..192a6d7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -28,4 +28,5 @@ en: help: "Assign topic to yourself" user: messages: + assigned_title: "Assigned Messages (%{count})" assigned: "Assigned" diff --git a/plugin.rb b/plugin.rb index 1b8be3e..8409bec 100644 --- a/plugin.rb +++ b/plugin.rb @@ -123,13 +123,21 @@ after_initialize do end add_to_class(:topic_query, :list_private_messages_assigned) do |user| - list = private_messages_for(user, :all) - list = list.where("topics.id IN ( - SELECT topic_id FROM topic_custom_fields WHERE name = 'assigned_to_id' AND value = ? - )", user.id.to_s) + list = private_messages_assigned_query(user) create_list(:private_messages, {}, list) end + add_to_class(:topic_query, :private_messages_assigned_query) do |user| + list = private_messages_for(user, :all) + + list = list.where(" + topics.id IN ( + SELECT topic_id FROM topic_custom_fields + WHERE name = 'assigned_to_id' + AND value = ?) + ", user.id.to_s) + end + add_to_class(:topic, :assigned_to_user) do @assigned_to_user || if user_id = custom_fields["assigned_to_id"] @@ -145,6 +153,22 @@ after_initialize do (SiteSetting.assigns_public || scope.is_staff?) && object.assigned_to_user end + add_to_serializer(:topic_list, :assigned_messages_count) do + TopicQuery.new(object.current_user, guardian: scope, limit: false) + .private_messages_assigned_query(object.current_user) + .count + end + + add_to_serializer(:topic_list, 'include_assigned_messages_count?') do + return unless SiteSetting.assigns_public + options = object.instance_variable_get(:@opts) + + if assigned_username = options[:assigned]&.downcase + assigned_username == object.current_user&.username_lower + end + end + + add_to_serializer(:topic_view, :assigned_to_user, false) do DiscourseAssign::Helpers.build_assigned_to_user(assigned_to_user_id, object.topic) end diff --git a/spec/serializers/topic_list_serializer_spec.rb b/spec/serializers/topic_list_serializer_spec.rb new file mode 100644 index 0000000..1ef0cb8 --- /dev/null +++ b/spec/serializers/topic_list_serializer_spec.rb @@ -0,0 +1,60 @@ +require 'rails_helper' + +RSpec.describe TopicListSerializer do + let(:user) { Fabricate(:user) } + + let(:private_message_topic) do + Fabricate(:private_message_topic, + posts: [Fabricate(:post)], + topic_allowed_users: [ + Fabricate.build(:topic_allowed_user, user: user) + ] + ) + end + + let(:assigned_topic) do + topic = Fabricate(:private_message_topic, + posts: [Fabricate(:post)], + topic_allowed_users: [ + Fabricate.build(:topic_allowed_user, user: user) + ] + ) + + TopicAssigner.new(topic, user).assign(user) + topic + end + + let(:guardian) { Guardian.new(user) } + let(:serializer) { TopicListSerializer.new(topic_list, scope: guardian) } + + before do + SiteSetting.assign_enabled = true + SiteSetting.assigns_public = true + end + + describe '#assigned_messages_count' do + let(:topic_list) do + TopicQuery.new(user, assigned: user.username).list_private_messages_assigned(user) + end + + before do + assigned_topic + end + + it 'should include right attribute' do + expect(serializer.as_json[:topic_list][:assigned_messages_count]) + .to eq(1) + end + + describe 'when assigns are not public' do + before do + SiteSetting.assigns_public = false + end + + it 'should not include the attributes' do + expect(serializer.as_json[:topic_list][:assigned_messages_count]) + .to eq(nil) + end + end + end +end