Skip to content
Extraits de code Groupes Projets
Valider f959ca0c rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

refactor streams to be in a Stream module, hopefully they will auto-reload now

parent 44b0887e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 23 ajouts et 423 suppressions
......@@ -2,7 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, "lib", 'stream', "aspect_stream")
require File.join(Rails.root, "lib", 'stream', "aspect")
class AspectsController < ApplicationController
before_filter :authenticate_user!
......@@ -15,7 +15,7 @@ class AspectsController < ApplicationController
def index
aspect_ids = (session[:a_ids] ? session[:a_ids] : [])
@stream = AspectStream.new(current_user, aspect_ids,
@stream = Stream::Aspect.new(current_user, aspect_ids,
:order => sort_order,
:max_time => params[:max_time].to_i)
......
require File.join(Rails.root, 'lib', 'stream', 'featured_users_stream')
require File.join(Rails.root, 'lib', 'stream', 'featured_users')
class FeaturedUsersController < ApplicationController
def index
default_stream_action(FeaturedUsersStream)
default_stream_action(Stream::FeaturedUsers)
end
end
......@@ -2,10 +2,10 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib','stream', 'mention_stream')
require File.join(Rails.root, 'lib','stream', 'mention')
class MentionsController < ApplicationController
def index
default_stream_action(MentionStream)
default_stream_action(Stream::Mention)
end
end
......@@ -2,7 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib', 'stream', 'public_stream')
require File.join(Rails.root, 'lib', 'stream', 'public')
class PostsController < ApplicationController
before_filter :authenticate_user!, :except => :show
......@@ -63,7 +63,7 @@ class PostsController < ApplicationController
end
def index
default_stream_action(PublicStream)
default_stream_action(Stream::Public)
end
def set_format_if_malformed_from_status_net
......
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require File.join(Rails.root, 'lib', 'stream', 'public_stream')
require File.join(Rails.root, 'lib', 'stream', 'public')
class PublicsController < ApplicationController
require File.join(Rails.root, '/lib/diaspora/parser')
......
require File.join(Rails.root, 'lib', 'stream', 'soup_stream')
require File.join(Rails.root, 'lib', 'stream', 'soup')
class SoupsController < ApplicationController
before_filter :redirect_unless_admin
......
......@@ -2,13 +2,13 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#
require File.join(Rails.root, 'lib', 'stream', 'tag_stream')
require File.join(Rails.root, 'lib', 'stream', 'followed_tag')
class TagFollowingsController < ApplicationController
before_filter :authenticate_user!
def index
default_stream_action(TagStream)
default_stream_action(Stream::FollowedTag)
end
# POST /tag_followings
......
......@@ -43,7 +43,7 @@ class Post < ActiveRecord::Base
def self.for_a_stream(max_time, order)
by_max_time(max_time, order).
includes_for_a_stream.
where(:type => BaseStream::TYPES_OF_POST_IN_STREAM).
where(:type => Stream::Base::TYPES_OF_POST_IN_STREAM).
limit(15)
end
......
class BaseStream
TYPES_OF_POST_IN_STREAM = ['StatusMessage', 'Reshare', 'ActivityStreams::Photo']
attr_accessor :max_time, :order, :user
def initialize(user, opts={})
self.user = user
self.max_time = opts[:max_time]
self.order = opts[:order]
end
def random_featured_user
@random_featured_user ||= Person.find_by_diaspora_handle(featured_diaspora_id)
end
def has_featured_users?
random_featured_user.present?
end
#requied to implement said stream
def link(opts={})
'change me in lib/base_stream.rb!'
end
def can_comment?(post)
true
end
def title
'a title'
end
def posts
[]
end
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
def people
people_ids = posts.map{|x| x.author_id}
Person.where(:id => people_ids).includes(:profile)
end
def contacts_link_title
I18n.translate('aspects.selected_contacts.view_all_contacts')
end
def contacts_title
'change me in lib/base_stream.rb!'
end
def contacts_link
'#'
end
#helpers
def ajax_stream?
false
end
def for_all_aspects?
true
end
#NOTE: MBS bad bad methods the fact we need these means our views are foobared. please kill them and make them
#private methods on the streams that need them
def aspects
@user.aspects
end
def aspect
aspects.first
end
def aspect_ids
aspects.map{|x| x.id}
end
def max_time=(time_string)
@max_time = Time.at(time_string.to_i) unless time_string.blank?
@max_time ||= (Time.now + 1)
end
def order=(order_string)
@order = order_string
@order ||= 'created_at'
end
private
def featured_diaspora_id
@featured_diaspora_id ||= AppConfig[:featured_users].try(:sample, 1)
end
end
......@@ -89,7 +89,7 @@ class RedisCache
# exposing the need to tie cache to a stream
# @return [Array<String>] Acceptable Post types for the given cache
def self.acceptable_types
BaseStream::TYPES_OF_POST_IN_STREAM
Stream::Base::TYPES_OF_POST_IN_STREAM
end
# Instantiate a redis connection
......
......@@ -126,7 +126,7 @@ module Diaspora
# @return [Hash]
def prep_opts(opts)
defaults = {
:type => BaseStream::TYPES_OF_POST_IN_STREAM,
:type => Stream::Base::TYPES_OF_POST_IN_STREAM,
:order => 'created_at DESC',
:limit => 15,
:hidden => false
......
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class AspectStream < BaseStream
# @param user [User]
# @param inputted_aspect_ids [Array<Integer>] Ids of aspects for given stream
# @param aspect_ids [Array<Integer>] Aspects this stream is responsible for
# @opt max_time [Integer] Unix timestamp of stream's post ceiling
# @opt order [String] Order of posts (i.e. 'created_at', 'updated_at')
# @return [void]
def initialize(user, inputted_aspect_ids, opts={})
super(user, opts)
@inputted_aspect_ids = inputted_aspect_ids
end
# Filters aspects given the stream's aspect ids on initialization and the user.
# Will disclude aspects from inputted aspect ids if user is not associated with their
# target aspects.
#
# @return [ActiveRecord::Association<Aspect>] Filtered aspects given the stream's user
def aspects
@aspects ||= lambda do
a = user.aspects
a = a.where(:id => @inputted_aspect_ids) if @inputted_aspect_ids.any?
a
end.call
end
# Maps ids into an array from #aspects
#
# @return [Array<Integer>] Aspect ids
def aspect_ids
@aspect_ids ||= aspects.map { |a| a.id }
end
# @return [ActiveRecord::Association<Post>] AR association of posts
def posts
# NOTE(this should be something like Post.all_for_stream(@user, aspect_ids, {}) that calls visible_posts
@posts ||= user.visible_posts(:all_aspects? => for_all_aspects?,
:by_members_of => aspect_ids,
:type => TYPES_OF_POST_IN_STREAM,
:order => "#{order} DESC",
:max_time => max_time
).for_a_stream(max_time, order)
end
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
def people
@people ||= Person.all_from_aspects(aspect_ids, user).includes(:profile)
end
# @return [String] URL
def link(opts={})
Rails.application.routes.url_helpers.aspects_path(opts)
end
# The first aspect in #aspects, given the stream is not for all aspects, or #aspects size is 1
# @note aspects.first is used for mobile. NOTE(this is a hack and should be fixed)
# @return [Aspect,Symbol]
def aspect
if !for_all_aspects? || aspects.size == 1
aspects.first
end
end
# Only ajax in the stream if all aspects are present.
# In this case, we know we're on the first page of the stream,
# as the default view for aspects/index is showing posts from
# all a user's aspects.
#
# @return [Boolean] see #for_all_aspects?
def ajax_stream?
for_all_aspects?
end
# The title that will display at the top of the stream's
# publisher box.
#
# @return [String]
def title
if self.for_all_aspects?
I18n.t('aspects.aspect_stream.stream')
else
self.aspects.to_sentence
end
end
# Determine whether or not the stream is displaying across
# all of the user's aspects.
#
# @return [Boolean]
def for_all_aspects?
@all_aspects ||= aspect_ids.length == user.aspects.size
end
# Provides a translated title for contacts box on the right pane.
#
# @return [String]
def contacts_title
if self.for_all_aspects? || self.aspect_ids.size > 1
I18n.t('_contacts')
else
"#{self.aspect.name} (#{self.people.size})"
end
end
# Provides a link to the user to the contacts page that corresponds with
# the stream's active aspects.
#
# @return [String] Link to contacts
def contacts_link
if for_all_aspects? || aspect_ids.size > 1
Rails.application.routes.url_helpers.contacts_path
else
Rails.application.routes.url_helpers.contacts_path(:a_id => aspect.id)
end
end
end
class FeaturedUsersStream < BaseStream
def title
"Featured users doing cool stuff!"
end
def link(opts={})
Rails.application.routes.url_helpers.featured_path(opts)
end
def contacts_title
"This week's featured users"
end
def contacts_link
Rails.application.routes.url_helpers.featured_users_path
end
def contacts_link_title
I18n.translate('aspects.selected_contacts.view_all_featured_users')
end
def posts
Post.all_public.where(:author_id => people.map{|x| x.id}).for_a_stream(max_time, order)
end
def people
Person.featured_users
end
end
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class MentionStream< BaseStream
def link(opts={})
Rails.application.routes.url_helpers.mentions_path(opts)
end
def title
I18n.translate("streams.mentions.title")
end
# @return [ActiveRecord::Association<Post>] AR association of posts
def posts
@posts ||= StatusMessage.where_person_is_mentioned(self.user.person).for_a_stream(max_time, order)
end
def contacts_title
I18n.translate('streams.mentions.contacts_title')
end
end
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class PublicStream < BaseStream
def link(opts={})
Rails.application.routes.url_helpers.public_stream_path(opts)
end
def title
I18n.translate("streams.public.title")
end
# @return [ActiveRecord::Association<Post>] AR association of posts
def posts
@posts ||= Post.all_public.for_a_stream(max_time, order)
end
def contacts_title
I18n.translate("streams.public.contacts_title")
end
def can_comment?(post)
post.author.local?
end
end
class SoupStream < BaseStream
def link(opts)
Rails.application.routes.url_helpers.soup_path
end
def title
I18n.t('streams.soup.title')
end
def contacts_title
I18n.t('streams.soup.contacts_title')
end
def posts
post_ids = aspect_posts_ids + followed_tag_ids + mentioned_post_ids
post_ids += featured_user_post_ids
Post.where(:id => post_ids).for_a_stream(max_time, order)
end
private
def aspect_posts_ids
user.visible_post_ids(:limit => 15, :order => order, :max_time => max_time)
end
def followed_tag_ids
StatusMessage.tag_stream(user, tag_array, max_time, order).map{|x| x.id}
end
def mentioned_post_ids
ids(StatusMessage.where_person_is_mentioned(user.person).for_a_stream(max_time, order))
end
def featured_user_post_ids
ids(Post.all_public.where(:author_id => featured_user_ids).for_a_stream(max_time, order))
end
#worthless helpers
def featured_user_ids
ids(Person.featured_users)
end
def tag_array
user.followed_tags.map{|x| x.name}
end
def ids(enumerable)
enumerable.map{|x| x.id}
end
end
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class TagStream < BaseStream
def link(opts={})
Rails.application.routes.url_helpers.tag_followings_path(opts)
end
def title
I18n.t('aspects.index.tags_following')
end
# @return [ActiveRecord::Association<Post>] AR association of posts
def posts
return [] if tag_string.empty?
@posts ||= StatusMessage.tag_stream(user, tag_array, max_time, order)
end
def contacts_title
I18n.translate('streams.tags.contacts_title')
end
def can_comment?(post)
@can_comment_cache ||= {}
@can_comment_cache[post.id] ||= contacts_in_stream.find{|contact| contact.person_id == post.author.id}.present?
@can_comment_cache[post.id] ||= user.person.id == post.author.id
@can_comment_cache[post.id]
end
def contacts_in_stream
@contacts_in_stream ||= Contact.where(:user_id => user.id, :person_id => people.map{|x| x.id}).all
end
private
def tag_string
@tag_string ||= tags.join(', '){|tag| tag.name}.to_s
end
def tag_array
tags.map{|x| x.name}
end
def tags
@tags = user.followed_tags
end
end
......@@ -68,8 +68,9 @@ describe AspectsController do
describe "#index" do
context 'jasmine fixtures' do
before do
AspectStream.any_instance.stub(:ajax_stream?).and_return(false)
Stream::Aspect.any_instance.stub(:ajax_stream?).and_return(false)
end
it "generates a jasmine fixture", :fixture => true do
get :index
save_fixture(html_for("body"), "aspects_index")
......@@ -152,26 +153,26 @@ describe AspectsController do
response.should render_template('shared/_stream')
end
it 'assigns an AspectStream' do
it 'assigns an Stream::Aspect' do
get :index
assigns(:stream).class.should == AspectStream
assigns(:stream).class.should == Stream::Aspect
end
describe 'filtering by aspect' do
before do
@aspect1 = alice.aspects.create(:name => "test aspect")
@stream = AspectStream.new(alice, [])
@stream = Stream::Aspect.new(alice, [])
@stream.stub(:posts).and_return([])
end
it 'respects a single aspect' do
AspectStream.should_receive(:new).with(alice, [@aspect1.id], anything).and_return(@stream)
Stream::Aspect.should_receive(:new).with(alice, [@aspect1.id], anything).and_return(@stream)
get :index, :a_ids => [@aspect1.id]
end
it 'respects multiple aspects' do
aspect2 = alice.aspects.create(:name => "test aspect two")
AspectStream.should_receive(:new).with(alice, [@aspect1.id, aspect2.id], anything).and_return(@stream)
Stream::Aspect.should_receive(:new).with(alice, [@aspect1.id, aspect2.id], anything).and_return(@stream)
get :index, :a_ids => [@aspect1.id, aspect2.id]
end
end
......
require 'spec_helper'
require File.join(Rails.root, 'spec', 'shared_behaviors', 'stream')
describe BaseStream do
before do
@stream = BaseStream.new(stub)
end
describe 'shared behaviors' do
it_should_behave_like 'it is a stream'
end
end
......@@ -207,7 +207,7 @@ describe RedisCache do
describe '.acceptable_types' do
#exposing the need to tie cache to a stream
it 'returns the types from the aspect stream' do
RedisCache.acceptable_types.should =~ AspectStream::TYPES_OF_POST_IN_STREAM
RedisCache.acceptable_types.should =~ Stream::Base::TYPES_OF_POST_IN_STREAM
end
end
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter