FEATURE: new 'My Groups' messages filter in user page

This commit is contained in:
Régis Hanol 2015-12-07 18:37:03 +01:00
parent 1cde276656
commit a37d575d7d
10 changed files with 60 additions and 25 deletions

View file

@ -67,6 +67,7 @@ export default Ember.Controller.extend(CanCheckEmails, {
privateMessagesActive: Em.computed.equal('pmView', 'index'), privateMessagesActive: Em.computed.equal('pmView', 'index'),
privateMessagesMineActive: Em.computed.equal('pmView', 'mine'), privateMessagesMineActive: Em.computed.equal('pmView', 'mine'),
privateMessagesUnreadActive: Em.computed.equal('pmView', 'unread'), privateMessagesUnreadActive: Em.computed.equal('pmView', 'unread'),
privateMessagesGroupsActive: Em.computed.equal('pmView', 'groups'),
actions: { actions: {
expandProfile() { expandProfile() {

View file

@ -16,6 +16,7 @@ const User = RestModel.extend({
hasPMs: Em.computed.gt("private_messages_stats.all", 0), hasPMs: Em.computed.gt("private_messages_stats.all", 0),
hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0), hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0),
hasUnreadPMs: Em.computed.gt("private_messages_stats.unread", 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), hasPosted: Em.computed.gt("post_count", 0),
hasNotPosted: Em.computed.not("hasPosted"), hasNotPosted: Em.computed.not("hasPosted"),
canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"), canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"),

View file

@ -70,6 +70,7 @@ export default function() {
this.resource('userPrivateMessages', { path: '/messages' }, function() { this.resource('userPrivateMessages', { path: '/messages' }, function() {
this.route('mine'); this.route('mine');
this.route('unread'); this.route('unread');
this.route('groups');
}); });
this.resource('preferences', function() { this.resource('preferences', function() {

View file

@ -0,0 +1,3 @@
import createPMRoute from "discourse/routes/build-user-topic-list-route";
export default createPMRoute('groups', 'private-messages-groups');

View file

@ -197,6 +197,12 @@
{{#if model.hasUnreadPMs}}<span class='badge-notification unread-private-messages'>{{model.private_messages_stats.unread}}</span>{{/if}} {{#if model.hasUnreadPMs}}<span class='badge-notification unread-private-messages'>{{model.private_messages_stats.unread}}</span>{{/if}}
{{/link-to}} {{/link-to}}
</li> </li>
<li {{bind-attr class=":noGlyph privateMessagesGroupsActive:active"}}>
{{#link-to 'userPrivateMessages.groups' model}}
{{i18n 'user.messages.groups'}}
{{#if model.hasGroupsPMs}}<span class='count'>({{model.private_messages_stats.groups}})</span>{{/if}}
{{/link-to}}
</li>
</ul> </ul>
{{/if}} {{/if}}

View file

@ -102,7 +102,7 @@ class ListController < ApplicationController
end end
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 define_method("#{action}") do
list_opts = build_topic_list_options list_opts = build_topic_list_options
target_user = fetch_user_from_params(include_inactive: current_user.try(:staff?)) target_user = fetch_user_from_params(include_inactive: current_user.try(:staff?))

View file

@ -78,23 +78,33 @@ SQL
def self.private_messages_stats(user_id, guardian) def self.private_messages_stats(user_id, guardian)
return unless guardian.can_see_private_messages?(user_id) return unless guardian.can_see_private_messages?(user_id)
# list the stats for: all/mine/unread (topic-based)
sql = <<SQL # list the stats for: all/mine/unread/groups (topic-based)
SELECT COUNT(*) "all",
SUM(CASE WHEN t.user_id = :user_id THEN 1 ELSE 0 END) mine,
SUM(CASE WHEN tu.last_read_post_number IS NULL OR tu.last_read_post_number < t.highest_post_number THEN 1 ELSE 0 END) unread
FROM topics t
LEFT JOIN topic_users tu ON t.id = tu.topic_id AND tu.user_id = :user_id
WHERE t.deleted_at IS NULL AND
t.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = :user_id) AND
t.archetype = 'private_message'
SQL sql = <<-SQL
SELECT COUNT(*) "all"
, SUM(CASE WHEN t.user_id = :user_id THEN 1 ELSE 0 END) "mine"
, SUM(CASE WHEN tu.last_read_post_number IS NULL OR tu.last_read_post_number < t.highest_post_number THEN 1 ELSE 0 END) "unread"
FROM topics t
LEFT JOIN topic_users tu ON t.id = tu.topic_id AND tu.user_id = :user_id
WHERE t.deleted_at IS NULL
AND t.archetype = 'private_message'
AND t.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = :user_id)
SQL
all,mine,unread = exec_sql(sql, user_id: user_id).values[0].map(&:to_i) all, mine, unread = exec_sql(sql, user_id: user_id).values[0].map(&:to_i)
{ all: all, mine: mine, unread: unread } sql = <<-SQL
SELECT COUNT(*) "groups"
FROM topics
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))
SQL
groups = exec_sql(sql, user_id: user_id).values[0][0].to_i
{ all: all, mine: mine, unread: unread, groups: groups }
end end
def self.stream_item(action_id, guardian) def self.stream_item(action_id, guardian)

View file

@ -486,6 +486,7 @@ en:
all: "All" all: "All"
mine: "Mine" mine: "Mine"
unread: "Unread" unread: "Unread"
groups: "My Groups"
change_password: change_password:
success: "(email sent)" success: "(email sent)"

View file

@ -465,6 +465,7 @@ 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/: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-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-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/comments' => 'embed#comments'
get 'embed/count' => 'embed#count' get 'embed/count' => 'embed#count'

View file

@ -114,22 +114,27 @@ class TopicQuery
end end
def list_private_messages(user) def list_private_messages(user)
list = private_messages_for(user) list = private_messages_for(user, :user)
create_list(:private_messages, {}, list) create_list(:private_messages, {}, list)
end end
def list_private_messages_sent(user) def list_private_messages_sent(user)
list = private_messages_for(user) list = private_messages_for(user, :user)
list = list.where(user_id: user.id) list = list.where(user_id: user.id)
create_list(:private_messages, {}, list) create_list(:private_messages, {}, list)
end end
def list_private_messages_unread(user) 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") 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) create_list(:private_messages, {}, list)
end 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) def list_category_topic_ids(category)
query = default_results(category: category.id) query = default_results(category: category.id)
pinned_ids = query.where('pinned_at IS NOT NULL AND category_id = ?', 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 @options[:slow_platform] ? 15 : 30
end end
def private_messages_for(user, type)
def private_messages_for(user)
options = @options options = @options
options.reverse_merge!(per_page: per_page_setting) options.reverse_merge!(per_page: per_page_setting)
# Start with a list of all topics result = Topic
result = Topic.includes(:allowed_users)
.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = #{user.id.to_i})") if type == :group
.joins("LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = #{user.id.to_i})") result = result.includes(:allowed_groups)
.order("topics.bumped_at DESC") 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}))")
.private_messages 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.limit(options[:per_page]) unless options[:limit] == false
result = result.visible if options[:visible] || @user.nil? || @user.regular? result = result.visible if options[:visible] || @user.nil? || @user.regular?