From 6bcf06f57847145a7b9261683083a1e73a8b7cbf Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 1 Feb 2012 12:00:16 -0800 Subject: [PATCH] Refactor multistream query --- lib/diaspora/user/querying.rb | 97 +++++++++++++++-------------------- 1 file changed, 41 insertions(+), 56 deletions(-) diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 2249faaed..5ba58fd71 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -23,64 +23,53 @@ module Diaspora opts end - class MultiStream - def initialize(user, order, max_time, include_spotlight) - @user = user - @order = order - @max_time = max_time - @aspect_ids = aspect_ids! - @include_spotlight = include_spotlight - end - - def aspect_ids! - @user.aspects.map(&:id) - end - - def make_relation! - post_ids = aspects_post_ids + followed_tags_post_ids + mentioned_post_ids - post_ids += community_spotlight_post_ids if @include_spotlight - Post.where(:id => post_ids) - end - - def aspects_post_ids - @aspects_post_ids ||= @user.visible_shareable_ids(Post, :limit => 15, :order => "#{@order} DESC", :max_time => @max_time, :all_aspects? => true, :by_members_of => @aspect_ids) - end - - def followed_tags_post_ids - @followed_tags_ids ||= ids(StatusMessage.public_tag_stream(tag_ids)) - end - - def mentioned_post_ids - @mentioned_post_ids ||= ids(StatusMessage.where_person_is_mentioned(@user.person)) - end - - def community_spotlight_post_ids - @community_spotlight_post_ids ||= ids(Post.all_public.where(:author_id => community_spotlight_person_ids)) - end - - def community_spotlight_person_ids - @community_spotlight_person_ids ||= Person.community_spotlight.select('id').map{|x| x.id} - end - - def tag_ids - @user.followed_tags.map{|x| x.id} - end - - def ids(query) - Post.connection.select_values(query.for_a_stream(@max_time, @order).select('posts.id').to_sql) + class Base + def fetch_ids!(relation, id_column) + #the relation should be ordered and limited by here + @class.connection.select_values(relation.select(id_column).to_sql) end end - class Base - def initialize(user, klass) - @querent = user - @class = klass + class MultiStream < Base + def initialize(user, order, max_time, include_spotlight) + @user = user + @class = Post + @order = order + @max_time = max_time + @include_spotlight = include_spotlight + end + + def make_relation! + post_ids = aspects_post_ids! + ids!(followed_tags_posts!) + ids!(mentioned_posts) + post_ids += ids!(community_spotlight_posts!) if @include_spotlight + Post.where(:id => post_ids) + end + + def aspects_post_ids! + @user.visible_shareable_ids(Post, :limit => 15, :order => "#{@order} DESC", :max_time => @max_time, :all_aspects? => true, :by_members_of => @user.aspect_ids) + end + + def followed_tag_posts! + StatusMessage.public_tag_stream(@user.followed_tag_ids) + end + + def mentioned_posts + StatusMessage.where_person_is_mentioned(@user.person) + end + + def community_spotlight_posts! + Post.all_public.where(:author_id => fetch_ids!(Person.community_spotlight, 'id')) + end + + def ids!(query) + fetch_ids!(query.for_a_stream(@max_time, @order), 'posts.id') end end class VisibleShareableById < Base def initialize(user, klass, key, id, conditions={}) - super(user, klass) + @querent = user + @class = klass @key = key @id = id @conditions = conditions @@ -108,7 +97,8 @@ module Diaspora class ShareablesFromPerson < Base def initialize(querent, klass, person) - super(querent, klass) + @querent = querent + @class = klass @person = person end @@ -129,11 +119,6 @@ module Diaspora protected - def fetch_ids!(relation, id_column) - #the relation should be ordered and limited by here - @class.connection.select_values(relation.select(id_column).to_sql) - end - def table_name @class.table_name end