diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index d64245f31f0227d2e95a8d11ce70cd51e8cd7701..eba04e66c70ef76ea107fe141100e20154836b04 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -10,6 +10,8 @@ class AspectsController < ApplicationController def index @posts = current_user.visible_posts(:_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC' + @post_hashes = hashes_for_posts @posts + @aspect = :all if current_user.getting_started == true @@ -60,7 +62,8 @@ class AspectsController < ApplicationController @aspect_contacts_count = @aspect_contacts.count @posts = @aspect.posts.find_all_by__type("StatusMessage", :order => 'created_at desc').paginate :page => params[:page], :per_page => 15 - @posts_count = @posts.count + @post_hashes = hashes_for_posts @posts + @post_count = @posts.count respond_with @aspect end @@ -130,4 +133,21 @@ class AspectsController < ApplicationController end end end + + private + def hashes_for_posts posts + comment_hash = Comment.hash_from_post_ids posts.map{|p| p.id} + person_hash = Person.from_post_comment_hash comment_hash + + posts.map do |post| + {:post => post, + :person => post.person, + :comments => comment_hash[post.id].map do |comment| + {:comment => comment, + :person => person_hash[comment.person_id], + } + end, + } + end + end end diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 26e2e2d1ed013d2d7e91ecd6a870076fdc0d8527..eccd3bbbfc699f744500e179babbc03b162e9de2 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -86,6 +86,13 @@ class StatusMessagesController < ApplicationController def show @status_message = current_user.find_visible_post_by_id params[:id] + comments_hash = Comment.hash_from_post_ids [@status_message.id] + person_hash = Person.from_post_comment_hash comments_hash + @comment_hashes = comments_hash[@status_message.id].map do |comment| + {:comment => comment, + :person => person_hash[comment.person_id] + } + end respond_with @status_message end end diff --git a/app/helpers/sockets_helper.rb b/app/helpers/sockets_helper.rb index 37dc8d35f901103d9d2b870268e64f2bb26494b1..a0109425db43c85de52027f6c74ad352769e3c91 100644 --- a/app/helpers/sockets_helper.rb +++ b/app/helpers/sockets_helper.rb @@ -13,7 +13,17 @@ module SocketsHelper begin user = User.find_by_id uid if object.is_a? Post - v = render_to_string(:partial => 'shared/stream_element', :locals => {:post => object, :current_user => user, :aspects => user.aspects}) + post_hash = {:post => object, + :person => object.person, + :comments => object.comments.map{|c| + {:comment => c, + :person => c.person + } + }, + :current_user => user, + :aspects => user.aspects, + } + v = render_to_string(:partial => 'shared/stream_element', :locals => post_hash) elsif object.is_a? Person v = render_to_string(:partial => type_partial(object), :locals => {:single_aspect_form => opts[:single_aspect_form], :person => object, :aspects => user.aspects, :current_user => user}) elsif object.is_a? Comment diff --git a/app/models/comment.rb b/app/models/comment.rb index fa92d52e3f6ccaae96bca356b7665cc58783cd18..f7390a16a57c0eb511d02d2683be6a1988b46499 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -71,6 +71,19 @@ class Comment def signature_valid? verify_signature(creator_signature, person) end - - + def self.hash_from_post_ids post_ids + hash = {} + comments = self.on_posts(post_ids) + post_ids.each do |id| + hash[id] = [] + end + comments.each do |comment| + hash[comment.post_id] << comment + end + hash.each_value {|comments| comments.sort!{|c1, c2| c1.created_at <=> c2.created_at }} + hash + end + scope :on_posts, lambda { |post_ids| + where(:post_id.in => post_ids) + } end diff --git a/app/models/person.rb b/app/models/person.rb index a551c991b35019b0f063cf4fad04cdf21983aa16..7e258561b5b0eda8c733412c04d29e4d2436abc6 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -150,6 +150,15 @@ class Person } } end + + def self.from_post_comment_hash(hash) + person_ids = hash.values.flatten.map{|c| c.person_id}.uniq + people = where(:id.in => person_ids) + people_hash = {} + people.each{|p| people_hash[p.id] = p} + people_hash + end + protected def clean_url diff --git a/app/views/aspects/index.html.haml b/app/views/aspects/index.html.haml index 6af143dba40d861c73dbcd8624c39564c173ed52..f7a55dc4113a229ce6445ee7049789139c48b86b 100644 --- a/app/views/aspects/index.html.haml +++ b/app/views/aspects/index.html.haml @@ -13,9 +13,9 @@ .span-15 = render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count = render 'shared/publisher', :aspect => @aspect - = render 'aspects/no_posts_message', :post_count => @posts.count, :contact_count => @contacts.count + = render 'aspects/no_posts_message', :post_count => @post_hashes.length, :contact_count => @contacts.count - = render 'shared/stream', :posts => @posts + = render 'shared/stream', :posts => @post_hashes #pagination = will_paginate @posts diff --git a/app/views/aspects/show.html.haml b/app/views/aspects/show.html.haml index 1eb962abdd406ea664ab68aa91c7f0a9490ebd05..bcf7898537b0fc5b47d3a5ab9ead2fa493f4517e 100644 --- a/app/views/aspects/show.html.haml +++ b/app/views/aspects/show.html.haml @@ -18,9 +18,9 @@ .span-15.last = render 'shared/publisher', :aspect => @aspect = render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @aspect_contacts_count, :options => false - = render 'aspects/no_posts_message', :post_count => @posts_count, :contact_count=> @aspect_contacts_count + = render 'aspects/no_posts_message', :post_count => @post_count, :contact_count=> @aspect_contacts_count - = render 'shared/stream', :posts => @posts + = render 'shared/stream', :posts => @post_hashes #pagination = will_paginate @posts diff --git a/app/views/comments/_comments.html.haml b/app/views/comments/_comments.html.haml index 6ead23fad9ae0e207247297184ed3894bb1e0f91..7b34eb05e95e090cd3f80b0fbfabf7087632601b 100644 --- a/app/views/comments/_comments.html.haml +++ b/app/views/comments/_comments.html.haml @@ -3,8 +3,8 @@ -# the COPYRIGHT file. %ul.comments{:id => post_id, :class => ("hidden" if defined?(hidden) && hidden)} - - for comment in comments - = render 'comments/comment', :comment => comment, :person => comment.person + - for comment_hash in comment_hashes + = render 'comments/comment', comment_hash %li.comment.show = render 'comments/new_comment', :post_id => post_id diff --git a/app/views/shared/_stream.haml b/app/views/shared/_stream.haml index 5a2b151546b0487f54278c1fc69d95d255c4fa6a..df9a55f92427cea0bd3ffce7fec110d6794ea274 100644 --- a/app/views/shared/_stream.haml +++ b/app/views/shared/_stream.haml @@ -3,5 +3,5 @@ -# the COPYRIGHT file. %ul{:class => 'stream', :id => 'main_stream'} - - for post in @posts - = render 'shared/stream_element', :post => post, :aspects => @aspects + - for post_hash in @post_hashes + = render 'shared/stream_element', post_hash.merge(:aspects => @aspects) diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 5cc38106d23db4b9bfbcd9a10dcc4aa4c400d8be..d9399bc8fde08147e370a3493a9ab29c8e562d9d 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -4,11 +4,11 @@ %li.message{:data=>{:guid=>post.id}} - = person_image_link(post.person) + = person_image_link(person) .content .from - = link_to post.person.real_name, post.person + = link_to person.real_name, person - if current_user.owns?(post) .aspect @@ -29,7 +29,6 @@ .info %span.time= link_to(how_long_ago(post), object_path(post)) - = comment_toggle(post.comments.count) - - = render "comments/comments", :post_id => post.id, :comments => post.comments, :hidden => (post.comments.count == 0) + = comment_toggle(comments.length) + = render "comments/comments", :post_id => post.id, :comment_hashes => comments, :hidden => (comments.length == 0) diff --git a/app/views/status_messages/show.html.haml b/app/views/status_messages/show.html.haml index 8636202c4c6dd5d1e851256c8ce5429444134937..58a08214d1a98dcc7141878705c7cb8c2ca74368 100644 --- a/app/views/status_messages/show.html.haml +++ b/app/views/status_messages/show.html.haml @@ -22,4 +22,4 @@ %h4{:style=>"margin-bottom:5px;"}= t('_comments') %div{:class => 'stream show', :id => 'status_message_stream'} %li.message{:data=>{:guid=>@status_message.id}} - = render "comments/comments", :post_id => @status_message.id, :comments => @status_message.comments + = render "comments/comments", :post_id => @status_message.id, :comment_hashes => @comment_hashes diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index ac78d930ade69f678ad6aca129156c138a05c4da..572b9247dfb393dd14603aa2be48bc6d988b6a20 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -27,7 +27,11 @@ describe AspectsController do describe "#index" do it "assigns @contacts to all the user's contacts" do Factory.create :person + begin get :index + rescue Exception => e + raise e.original_exception + end assigns[:contacts].should == @user.contacts end context 'performance' do diff --git a/spec/models/aspect_spec.rb b/spec/models/aspect_spec.rb index 271e3d576108693e50ac2c8500f1164fda6b98d6..bb801db54610d994314cdc55b234e8f4d52ac9be 100644 --- a/spec/models/aspect_spec.rb +++ b/spec/models/aspect_spec.rb @@ -214,9 +214,6 @@ describe Aspect do before do @message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id) aspect.reload - @post_count = aspect.posts.count - @post_count1 = aspect1.posts.count - user.reload end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index f14342e720b0a544fac2775f205d679c86ee5810..ff9199712d2dd608e5219456a821693f25825897 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -20,6 +20,40 @@ describe Comment do comment.errors.full_messages.should include "Diaspora handle and person handle must match" end + describe '.hash_from_post_ids' do + before do + @hello = user.post(:status_message, :message => "Hello.", :to => aspect.id) + @hi = user.post(:status_message, :message => "hi", :to => aspect.id) + @lonely = user.post(:status_message, :message => "Hello?", :to => aspect.id) + + @c11 = user2.comment "why so formal?", :on => @hello + @c21 = user2.comment "lol hihihi", :on => @hi + @c12 = user.comment "I simply felt like issuing a greeting. Do step off.", :on => @hello + @c22 = user.comment "stfu noob", :on => @hi + + @c12.created_at = Time.now+10 + @c12.save! + @c22.created_at = Time.now+10 + @c22.save! + end + it 'returns an empty array for posts with no comments' do + Comment.hash_from_post_ids([@lonely.id]).should == + {@lonely.id => []} + end + it 'returns a hash from posts to comments' do + Comment.hash_from_post_ids([@hello.id, @hi.id]).should == + {@hello.id => [@c11, @c12], + @hi.id => [@c21, @c22] + } + end + it 'gets the people from the db' do + hash = Comment.hash_from_post_ids([@hello.id, @hi.id]) + Person.from_post_comment_hash(hash).should == { + user.person.id => user.person, + user2.person.id => user2.person, + } + end + end describe 'User#comment' do before do @status = user.post(:status_message, :message => "hello", :to => aspect.id)