From 8cf4d52cb6970bf0ff6245265da48c85bea03d93 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 10 Dec 2015 11:39:33 +1100 Subject: [PATCH] FEATURE: split up group PMS on user page --- .../discourse/controllers/user.js.es6 | 14 +++++++++++++- .../javascripts/discourse/models/user.js.es6 | 1 - .../discourse/routes/app-route-map.js.es6 | 2 +- .../routes/user-private-messages-group.js.es6 | 13 +++++++++++++ .../routes/user-private-messages-groups.js.es6 | 3 --- .../discourse/templates/user/user.hbs | 11 +++++++---- app/controllers/list_controller.rb | 5 +++-- app/controllers/user_actions_controller.rb | 1 + app/models/user_action.rb | 18 +++++++++++++----- config/routes.rb | 6 +++++- lib/topic_query.rb | 9 +++++++-- 11 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 app/assets/javascripts/discourse/routes/user-private-messages-group.js.es6 delete mode 100644 app/assets/javascripts/discourse/routes/user-private-messages-groups.js.es6 diff --git a/app/assets/javascripts/discourse/controllers/user.js.es6 b/app/assets/javascripts/discourse/controllers/user.js.es6 index 538ffe6f120..49889e5c023 100644 --- a/app/assets/javascripts/discourse/controllers/user.js.es6 +++ b/app/assets/javascripts/discourse/controllers/user.js.es6 @@ -67,7 +67,19 @@ 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'), + + @computed('model.private_messages_stats.groups', 'groupFilter', 'pmView') + groupPMStats(stats,filter,pmView) { + if (stats) { + return stats.map(g => { + return { + name: g.name, + count: g.count, + active: (g.name === filter && 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 96c3cf13dd0..97c611ab89e 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -16,7 +16,6 @@ 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 47979037707..d67e0a78660 100644 --- a/app/assets/javascripts/discourse/routes/app-route-map.js.es6 +++ b/app/assets/javascripts/discourse/routes/app-route-map.js.es6 @@ -71,7 +71,7 @@ export default function() { this.resource('userPrivateMessages', { path: '/messages' }, function() { this.route('mine'); this.route('unread'); - this.route('groups'); + this.route('group', { path: 'group/:name'}); }); this.resource('preferences', function() { diff --git a/app/assets/javascripts/discourse/routes/user-private-messages-group.js.es6 b/app/assets/javascripts/discourse/routes/user-private-messages-group.js.es6 new file mode 100644 index 00000000000..80f498273dd --- /dev/null +++ b/app/assets/javascripts/discourse/routes/user-private-messages-group.js.es6 @@ -0,0 +1,13 @@ +import createPMRoute from "discourse/routes/build-user-topic-list-route"; + +export default createPMRoute('groups', 'private-messages-groups').extend({ + model(params) { + return this.store.findFiltered("topicList", { filter: "topics/private-messages-group/" + this.modelFor("user").get("username_lower") + "/" + params.name }); + }, + + setupController(controller,model) { + this._super.apply(this, arguments); + const filter = _.last(model.get("filter").split('/')); + this.controllerFor("user").set("groupFilter", filter); + } +}); 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 deleted file mode 100644 index 3f0a88e373a..00000000000 --- a/app/assets/javascripts/discourse/routes/user-private-messages-groups.js.es6 +++ /dev/null @@ -1,3 +0,0 @@ -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 5ca435ba812..ae0d50b06ab 100644 --- a/app/assets/javascripts/discourse/templates/user/user.hbs +++ b/app/assets/javascripts/discourse/templates/user/user.hbs @@ -197,12 +197,15 @@ {{#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}} + {{#each groupPMStats as |group|}} +
  • + {{#link-to 'userPrivateMessages.group' group.name}} + + {{group.name}} + ({{group.count}}) {{/link-to}}
  • + {{/each}} {{/if}} diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 443ae47dbd7..1e7a3245ff4 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, :private_messages_groups].each do |action| + [:topics_by, :private_messages, :private_messages_sent, :private_messages_unread, :private_messages_group].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?)) @@ -252,7 +252,8 @@ class ListController < ApplicationController filter: params[:filter], state: params[:state], search: params[:search], - q: params[:q] + q: params[:q], + group_name: params[:group_name] } options[:no_subcategories] = true if params[:no_subcategories] == 'true' options[:slow_platform] = true if slow_platform? diff --git a/app/controllers/user_actions_controller.rb b/app/controllers/user_actions_controller.rb index 1299c8d9836..6ff74ec995d 100644 --- a/app/controllers/user_actions_controller.rb +++ b/app/controllers/user_actions_controller.rb @@ -34,6 +34,7 @@ class UserActionsController < ApplicationController def private_messages # DO NOT REMOVE + # TODO should preload messages to avoid extra http req end end diff --git a/app/models/user_action.rb b/app/models/user_action.rb index f79e7c54cd7..320b94d078c 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -95,16 +95,24 @@ SQL all, mine, unread = exec_sql(sql, user_id: user_id).values[0].map(&:to_i) sql = <<-SQL - SELECT COUNT(*) "groups" - FROM topics + SELECT g.name, COUNT(*) "count" + FROM topics t + JOIN topic_allowed_groups tg ON topic_id = t.id + JOIN group_users gu ON gu.user_id = :user_id AND gu.group_id = tg.group_id + JOIN groups g ON g.id = gu.group_id WHERE deleted_at IS NULL AND archetype = 'private_message' - AND id IN (SELECT topic_id FROM topic_allowed_groups WHERE group_id IN (SELECT group_id FROM group_users WHERE user_id = :user_id)) + GROUP BY g.name SQL - groups = exec_sql(sql, user_id: user_id).values[0][0].to_i + result = { all: all, mine: mine, unread: unread} + + exec_sql(sql, user_id: user_id).each do |row| + (result[:groups] ||= []) << {name: row["name"], count: row["count"].to_i} + end + + result - { all: all, mine: mine, unread: unread, groups: groups } end def self.stream_item(action_id, guardian) diff --git a/config/routes.rb b/config/routes.rb index 221215a100c..e28edf8e580 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -278,6 +278,7 @@ Discourse::Application.routes.draw do get "users/:username/private-messages/:filter" => "user_actions#private_messages", constraints: {username: USERNAME_ROUTE_FORMAT} get "users/:username/messages" => "user_actions#private_messages", constraints: {username: USERNAME_ROUTE_FORMAT} get "users/:username/messages/:filter" => "user_actions#private_messages", constraints: {username: USERNAME_ROUTE_FORMAT} + get "users/:username/messages/group/:group_name" => "user_actions#private_messages", constraints: {username: USERNAME_ROUTE_FORMAT, group_name: USERNAME_ROUTE_FORMAT} get "users/:username.json" => "users#show", constraints: {username: USERNAME_ROUTE_FORMAT}, defaults: {format: :json} get "users/:username" => "users#show", as: 'user', constraints: {username: USERNAME_ROUTE_FORMAT} put "users/:username" => "users#update", constraints: {username: USERNAME_ROUTE_FORMAT} @@ -466,7 +467,10 @@ Discourse::Application.routes.draw do get "topics/private-messages/:username" => "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 "topics/private-messages-group/:username/:group_name.json" => "list#private_messages_group", as: "topics_private_messages_group", constraints: { + username: USERNAME_ROUTE_FORMAT, + group_name: 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 32c557c1f0b..3fb32b380d9 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -28,6 +28,7 @@ class TopicQuery search slow_platform filter + group_name q) # Maps `order` to a columns in `topics` @@ -130,7 +131,7 @@ class TopicQuery create_list(:private_messages, {}, list) end - def list_private_messages_groups(user) + def list_private_messages_group(user) list = private_messages_for(user, :group) create_list(:private_messages, {}, list) end @@ -240,7 +241,11 @@ class TopicQuery 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}))") + 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}) AND + group_id IN (SELECT id FROM groups WHERE name ilike ?) + )", @options[:group_name]) 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})")