diff --git a/app/assets/javascripts/discourse/controllers/user.js.es6 b/app/assets/javascripts/discourse/controllers/user.js.es6 index b6cea5d0a..538ffe6f1 100644 --- a/app/assets/javascripts/discourse/controllers/user.js.es6 +++ b/app/assets/javascripts/discourse/controllers/user.js.es6 @@ -67,6 +67,7 @@ export default Ember.Controller.extend(CanCheckEmails, { privateMessagesActive: Em.computed.equal('pmView', 'index'), privateMessagesMineActive: Em.computed.equal('pmView', 'mine'), privateMessagesUnreadActive: Em.computed.equal('pmView', 'unread'), + privateMessagesGroupsActive: Em.computed.equal('pmView', 'groups'), actions: { expandProfile() { diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 97c611ab8..96c3cf13d 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -16,6 +16,7 @@ const User = RestModel.extend({ hasPMs: Em.computed.gt("private_messages_stats.all", 0), hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0), hasUnreadPMs: Em.computed.gt("private_messages_stats.unread", 0), + hasGroupsPMs: Em.computed.gt("private_messages_stats.groups", 0), hasPosted: Em.computed.gt("post_count", 0), hasNotPosted: Em.computed.not("hasPosted"), canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"), diff --git a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 index e76b33296..9e9c0a770 100644 --- a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 +++ b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 @@ -70,6 +70,7 @@ export default function() { this.resource('userPrivateMessages', { path: '/messages' }, function() { this.route('mine'); this.route('unread'); + this.route('groups'); }); this.resource('preferences', function() { diff --git a/app/assets/javascripts/discourse/routes/user-private-messages-groups.js.es6 b/app/assets/javascripts/discourse/routes/user-private-messages-groups.js.es6 new file mode 100644 index 000000000..3f0a88e37 --- /dev/null +++ b/app/assets/javascripts/discourse/routes/user-private-messages-groups.js.es6 @@ -0,0 +1,3 @@ +import createPMRoute from "discourse/routes/build-user-topic-list-route"; + +export default createPMRoute('groups', 'private-messages-groups'); diff --git a/app/assets/javascripts/discourse/templates/user/user.hbs b/app/assets/javascripts/discourse/templates/user/user.hbs index 1f61a722d..5ca435ba8 100644 --- a/app/assets/javascripts/discourse/templates/user/user.hbs +++ b/app/assets/javascripts/discourse/templates/user/user.hbs @@ -197,6 +197,12 @@ {{#if model.hasUnreadPMs}}{{model.private_messages_stats.unread}}{{/if}} {{/link-to}} +
  • + {{#link-to 'userPrivateMessages.groups' model}} + {{i18n 'user.messages.groups'}} + {{#if model.hasGroupsPMs}}({{model.private_messages_stats.groups}}){{/if}} + {{/link-to}} +
  • {{/if}} diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 03d78cb21..443ae47db 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -102,7 +102,7 @@ class ListController < ApplicationController end end - [:topics_by, :private_messages, :private_messages_sent, :private_messages_unread].each do |action| + [:topics_by, :private_messages, :private_messages_sent, :private_messages_unread, :private_messages_groups].each do |action| define_method("#{action}") do list_opts = build_topic_list_options target_user = fetch_user_from_params(include_inactive: current_user.try(:staff?)) diff --git a/app/models/user_action.rb b/app/models/user_action.rb index fa87a744b..f79e7c54c 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -78,23 +78,33 @@ SQL def self.private_messages_stats(user_id, guardian) return unless guardian.can_see_private_messages?(user_id) - # list the stats for: all/mine/unread (topic-based) - sql = < "list#private_messages", as: "topics_private_messages", constraints: {username: USERNAME_ROUTE_FORMAT} get "topics/private-messages-sent/:username" => "list#private_messages_sent", as: "topics_private_messages_sent", constraints: {username: USERNAME_ROUTE_FORMAT} get "topics/private-messages-unread/:username" => "list#private_messages_unread", as: "topics_private_messages_unread", constraints: {username: USERNAME_ROUTE_FORMAT} + get "topics/private-messages-groups/:username" => "list#private_messages_groups", as: "topics_private_messages_groups", constraints: {username: USERNAME_ROUTE_FORMAT} get 'embed/comments' => 'embed#comments' get 'embed/count' => 'embed#count' diff --git a/lib/topic_query.rb b/lib/topic_query.rb index b02c3148e..32c557c1f 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -114,22 +114,27 @@ class TopicQuery end def list_private_messages(user) - list = private_messages_for(user) + list = private_messages_for(user, :user) create_list(:private_messages, {}, list) end def list_private_messages_sent(user) - list = private_messages_for(user) + list = private_messages_for(user, :user) list = list.where(user_id: user.id) create_list(:private_messages, {}, list) end def list_private_messages_unread(user) - list = private_messages_for(user) + list = private_messages_for(user, :user) list = list.where("tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.highest_post_number") create_list(:private_messages, {}, list) end + def list_private_messages_groups(user) + list = private_messages_for(user, :group) + create_list(:private_messages, {}, list) + end + def list_category_topic_ids(category) query = default_results(category: category.id) pinned_ids = query.where('pinned_at IS NOT NULL AND category_id = ?', category.id) @@ -227,17 +232,23 @@ class TopicQuery @options[:slow_platform] ? 15 : 30 end - - def private_messages_for(user) + def private_messages_for(user, type) options = @options options.reverse_merge!(per_page: per_page_setting) - # Start with a list of all topics - result = Topic.includes(:allowed_users) - .where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})") - .joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})") - .order("topics.bumped_at DESC") - .private_messages + result = Topic + + if type == :group + result = result.includes(:allowed_groups) + result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_groups WHERE group_id IN (SELECT group_id FROM group_users WHERE user_id = #{user.id.to_i}))") + elsif type == :user + result = result.includes(:allowed_users) + result = result.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})") + end + + result = result.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})") + .order("topics.bumped_at DESC") + .private_messages result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular?