load conversations and visibilities in one query with correct order
This commit is contained in:
parent
735d692ac0
commit
0ffb83d351
3 changed files with 27 additions and 21 deletions
|
|
@ -5,35 +5,41 @@ class ConversationsController < ApplicationController
|
|||
respond_to :html, :mobile, :json, :js
|
||||
|
||||
def index
|
||||
@conversations = current_user.conversations.paginate(
|
||||
:page => params[:page], :per_page => 15)
|
||||
@visibilities = ConversationVisibility.includes(:conversation)
|
||||
.order("conversations.updated_at DESC")
|
||||
.where(person_id: current_user.person_id)
|
||||
.paginate(page: params[:page], per_page: 15)
|
||||
|
||||
@visibilities = current_user.conversation_visibilities.paginate(
|
||||
:page => params[:page], :per_page => 15)
|
||||
|
||||
@conversation = Conversation.joins(:conversation_visibilities).where(
|
||||
:conversation_visibilities => {:person_id => current_user.person_id, :conversation_id => params[:conversation_id]}).first
|
||||
|
||||
@unread_counts = {}
|
||||
@visibilities.each { |v| @unread_counts[v.conversation_id] = v.unread }
|
||||
|
||||
@first_unread_message_id = @conversation.try(:first_unread_message, current_user).try(:id)
|
||||
if params[:conversation_id]
|
||||
@conversation = Conversation.joins(:conversation_visibilities)
|
||||
.where(conversation_visibilities: {
|
||||
person_id: current_user.person_id,
|
||||
conversation_id: params[:conversation_id]
|
||||
}).first
|
||||
|
||||
if @conversation
|
||||
@first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
|
||||
@conversation.set_read(current_user)
|
||||
end
|
||||
end
|
||||
|
||||
@conversations = []
|
||||
@unread_counts = {}
|
||||
@authors = {}
|
||||
@conversations.each { |c| @authors[c.id] = c.last_author }
|
||||
|
||||
@ordered_participants = {}
|
||||
@conversations.each { |c| @ordered_participants[c.id] = (c.messages.map{|m| m.author}.reverse + c.participants).uniq }
|
||||
@visibilities.each {|v|
|
||||
@unread_counts[v.conversation_id] = v.unread
|
||||
c = v.conversation
|
||||
@conversations << c
|
||||
@authors[c.id] = c.last_author
|
||||
@ordered_participants[c.id] = (c.messages.map(&:author).reverse + c.participants).uniq
|
||||
}
|
||||
|
||||
gon.contacts = contacts_data
|
||||
|
||||
respond_with do |format|
|
||||
format.html
|
||||
format.json { render :json => @conversations, :status => 200 }
|
||||
format.json { render json: @conversations, status: 200 }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -67,8 +67,8 @@ class User < ActiveRecord::Base
|
|||
has_many :blocks
|
||||
has_many :ignored_people, :through => :blocks, :source => :person
|
||||
|
||||
has_many :conversation_visibilities, -> { order 'updated_at DESC' }, through: :person
|
||||
has_many :conversations, -> { order 'updated_at DESC' }, through: :conversation_visibilities
|
||||
has_many :conversation_visibilities, through: :person
|
||||
has_many :conversations, through: :conversation_visibilities
|
||||
|
||||
has_many :notifications, :foreign_key => :recipient_id
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
- else
|
||||
#no_conversations
|
||||
= t('.no_messages')
|
||||
= will_paginate @conversations, :previous_label => '«', :next_label => '»', :inner_window => 1, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer
|
||||
= will_paginate @visibilities, :previous_label => "«", :next_label => "»", :inner_window => 1, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer
|
||||
|
||||
.span8
|
||||
- if @conversation
|
||||
|
|
|
|||
Loading…
Reference in a new issue