Refactor multistream query

This commit is contained in:
danielgrippi 2012-02-01 12:00:16 -08:00
parent 3ed68fc036
commit 6bcf06f578

View file

@ -23,64 +23,53 @@ module Diaspora
opts opts
end end
class MultiStream class Base
def initialize(user, order, max_time, include_spotlight) def fetch_ids!(relation, id_column)
@user = user #the relation should be ordered and limited by here
@order = order @class.connection.select_values(relation.select(id_column).to_sql)
@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)
end end
end end
class Base class MultiStream < Base
def initialize(user, klass) def initialize(user, order, max_time, include_spotlight)
@querent = user @user = user
@class = klass @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
end end
class VisibleShareableById < Base class VisibleShareableById < Base
def initialize(user, klass, key, id, conditions={}) def initialize(user, klass, key, id, conditions={})
super(user, klass) @querent = user
@class = klass
@key = key @key = key
@id = id @id = id
@conditions = conditions @conditions = conditions
@ -108,7 +97,8 @@ module Diaspora
class ShareablesFromPerson < Base class ShareablesFromPerson < Base
def initialize(querent, klass, person) def initialize(querent, klass, person)
super(querent, klass) @querent = querent
@class = klass
@person = person @person = person
end end
@ -129,11 +119,6 @@ module Diaspora
protected 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 def table_name
@class.table_name @class.table_name
end end