A platform for community discussion. Free, open, simple.
FEATURE: filter personal messages by tags
Arpit Jalanc29660c8f147
diff --git a/app/assets/javascripts/discourse/controllers/user-private-messages.js.es6 b/app/assets/javascripts/discourse/controllers/user-private-messages.js.es6index 87d477ffd22..34b04a40574 100644--- a/app/assets/javascripts/discourse/controllers/user-private-messages.js.es6+++ b/app/assets/javascripts/discourse/controllers/user-private-messages.js.es6@@ -12,6 +12,8 @@ export default Ember.Controller.extend({currentPath: Em.computed.alias('application.currentPath'),selected: Em.computed.alias('userTopicsList.selected'),bulkSelectEnabled: Em.computed.alias('userTopicsList.bulkSelectEnabled'),+ pmTags: Em.computed.alias('userTopicsList.model.topic_list.pm_tags'),+ pmTaggingEnabled: Ember.computed.alias('site.can_tag_pms'),showNewPM: function(){return this.get('user.viewingSelf') &&diff --git a/app/assets/javascripts/discourse/templates/user/messages.hbs b/app/assets/javascripts/discourse/templates/user/messages.hbsindex 5a788282337..e9fceab0fdf 100644--- a/app/assets/javascripts/discourse/templates/user/messages.hbs+++ b/app/assets/javascripts/discourse/templates/user/messages.hbs@@ -65,7 +65,6 @@</button>{{/if}}-{{#if bulkSelectEnabled}}<button {{action "selectAll"}} class="btn btn-select-all">{{i18n "user.messages.select_all"}}@@ -75,6 +74,10 @@{{#if isGroup}}{{group-notifications-button value=group.group_user.notification_level group=group user=model}}{{/if}}++ {{#if pmTaggingEnabled}}+ {{pm-tag-drop pmTags=pmTags}}+ {{/if}}</div>{{outlet}}diff --git a/app/assets/javascripts/select-kit/components/pm-tag-drop.js.es6 b/app/assets/javascripts/select-kit/components/pm-tag-drop.js.es6new file mode 100644index 00000000000..23451ed0f62--- /dev/null+++ b/app/assets/javascripts/select-kit/components/pm-tag-drop.js.es6@@ -0,0 +1,19 @@+import TagDropComponent from "select-kit/components/tag-drop";+import DiscourseURL from "discourse/lib/url";+import { default as computed } from "ember-addons/ember-computed-decorators";++export default TagDropComponent.extend({+ @computed+ allTagsUrl() {+ return `/u/${this.currentUser.username}/messages/`;+ },++ content: Ember.computed.alias("pmTags"),++ actions: {+ onSelect(tagId) {+ const url = `/u/${this.currentUser.username}/messages/tag/${tagId}`;+ DiscourseURL.routeTo(url);+ }+ }+});diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex ba7946b1d32..06cc6e2ba14 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -134,6 +134,7 @@ class ListController < ApplicationControllerdef self.generate_message_route(action)define_method("#{action}") dolist_opts = build_topic_list_options+ list_opts[:show_pm_tags] = true if guardian.can_tag_pms?target_user = fetch_user_from_params({ include_inactive: current_user.try(:staff?) }, [:user_stat, :user_option])guardian.ensure_can_see_private_messages!(target_user.id)list = generate_list_for(action.to_s, target_user, list_opts)diff --git a/app/models/tag.rb b/app/models/tag.rbindex f26c793b390..264c123ebe4 100644--- a/app/models/tag.rb+++ b/app/models/tag.rb@@ -59,6 +59,24 @@ class Tag < ActiveRecord::Basetag_names_with_counts.map { |row| row['tag_name'] }end+ def self.pm_tags(limit_arg: nil, guardian: nil)+ return [] unless (guardian || Guardian.new).can_tag_pms?+ limit = limit_arg || SiteSetting.max_tags_in_filter_list++ tag_names = Tag.exec_sql <<~SQL+ SELECT tags.name AS tag_name+ FROM tags+ INNER JOIN topic_tags ON tags.id = topic_tags.tag_id+ INNER JOIN topics ON topics.id = topic_tags.topic_id+ AND topics.deleted_at IS NULL+ AND topics.archetype = 'private_message'+ GROUP BY tags.name+ LIMIT #{limit}+ SQL++ tag_names.values.flatten+ end+def self.include_tags?SiteSetting.tagging_enabled && SiteSetting.show_filter_by_tagenddiff --git a/app/models/topic_list.rb b/app/models/topic_list.rbindex dbb468cb489..2228f1e56e7 100644--- a/app/models/topic_list.rb+++ b/app/models/topic_list.rb@@ -35,6 +35,7 @@ class TopicList:for_period,:per_page,:top_tags,+ :pm_tags,:current_user,:tags@@ -59,6 +60,15 @@ class TopicListTag.top_tags(opts)end+ def pm_tags+ guardian = Guardian.new(@current_user)+ if @opts[:show_pm_tags] && guardian.can_tag_pms?+ Tag.pm_tags(guardian: guardian)+ else+ []+ end+ end+def preload_keyif @category"topic_list_#{@category.url.sub(/^\//, '')}/l/#{@filter}"diff --git a/app/serializers/topic_list_serializer.rb b/app/serializers/topic_list_serializer.rbindex 994ea84950c..6ad24845725 100644--- a/app/serializers/topic_list_serializer.rb+++ b/app/serializers/topic_list_serializer.rb@@ -8,6 +8,7 @@ class TopicListSerializer < ApplicationSerializer:for_period,:per_page,:top_tags,+ :pm_tags,:tagshas_many :topics, serializer: TopicListItemSerializer, embed: :objects@@ -29,6 +30,10 @@ class TopicListSerializer < ApplicationSerializerTag.include_tags?end+ def include_pm_tags?+ scope.can_tag_pms?+ end+def include_tags?SiteSetting.tagging_enabled && object.tags.present?enddiff --git a/lib/topic_query.rb b/lib/topic_query.rbindex ee65210573e..5075ae43a53 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -32,7 +32,8 @@ class TopicQuerymatch_all_tagsno_subcategoriesslow_platform- no_tags)+ no_tags+ show_pm_tags)enddef self.valid_optionsdiff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rbindex 448000e382a..79442d52e1f 100644--- a/spec/models/tag_spec.rb+++ b/spec/models/tag_spec.rb@@ -96,6 +96,28 @@ describe Tag doendend+ describe '#pm_tags' do+ before do+ @private_tags = []+ personal_message = Fabricate(:private_message_topic)+ 2.times { |i| @private_tags << Fabricate(:tag, topics: [personal_message]) }+ end++ it "returns nothing if user is not a staff" do+ expect(described_class.pm_tags.sort).to be_empty+ end++ it "returns nothing if allow_staff_to_tag_pms setting is disabled" do+ SiteSetting.allow_staff_to_tag_pms = false+ expect(described_class.pm_tags(guardian: Guardian.new(Fabricate(:admin))).sort).to be_empty+ end++ it "returns all pm tags if user is a staff and pm tagging is enabled" do+ SiteSetting.allow_staff_to_tag_pms = true+ expect(described_class.pm_tags(guardian: Guardian.new(Fabricate(:admin)))).to match_array(@private_tags.map(&:name))+ end+ end+context "topic counts" doit "should exclude private message topics" dotopicdiff --git a/spec/models/topic_list_spec.rb b/spec/models/topic_list_spec.rbindex d5022065f79..3113eecae03 100644--- a/spec/models/topic_list_spec.rb+++ b/spec/models/topic_list_spec.rb@@ -78,4 +78,28 @@ describe TopicList doendendend++ describe '#pm_tags' do+ let(:admin) { Fabricate(:admin) }+ let(:personal_message) { Fabricate(:private_message_topic) }++ before do+ SiteSetting.tagging_enabled = true+ SiteSetting.allow_staff_to_tag_pms = true+ @private_tags = []+ 2.times { |i| @private_tags << Fabricate(:tag, topics: [personal_message]) }+ end++ context 'when viewed as normal user' do+ it 'returns no tags' do+ expect(TopicList.new('liked', personal_message.user, [personal_message], show_pm_tags: true).pm_tags).to be_empty+ end+ end++ context 'when viewed as admin' do+ it 'returns pm tags' do+ expect(TopicList.new('liked', admin, [personal_message], show_pm_tags: true).pm_tags).to match_array(@private_tags.map(&:name))+ end+ end+ endenddiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex e7a92f1f8be..0d5078bc358 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -105,6 +105,8 @@ RSpec.describe ListController dosign_in(user)get "/topics/private-messages-tag/#{user.username}/#{tag.name}.json"expect(response).to be_success+ data = JSON.parse(response.body)+ expect(data["topic_list"]["pm_tags"].length).to eq(1)endendend