base.rb 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
class Stream::Base
  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

11
  # @return [Person]
12 13 14 15
  def random_featured_user
    @random_featured_user ||= Person.find_by_diaspora_handle(featured_diaspora_id)
  end

16
  # @return [Boolean]
17 18 19 20 21 22 23 24 25
  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

26
  # @return [Boolean]
27
  def can_comment?(post)
28 29
    return true if post.author.local?
    post_is_from_contact?(post)
30 31
  end

32
  # @return [String]
33 34 35 36
  def title
    'a title'
  end

37
  # @return [ActiveRecord::Relation<Post>]
38 39 40 41
  def posts
    []
  end

42 43 44 45 46
  # @return [String]
  def publisher_prefill_text
    ''
  end

47 48 49 50 51 52
  # @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

53
  # @return [String]
54 55 56 57
  def contacts_link_title
    I18n.translate('aspects.selected_contacts.view_all_contacts')
  end

58
  # @return [String]
59 60 61 62
  def contacts_title
    'change me in lib/base_stream.rb!'
  end

63
  # @return [String]
64 65 66 67 68
  def contacts_link
    '#'
  end

  #helpers
69
  # @return [Boolean]
70 71 72
  def ajax_stream?
    false
  end
73 74

  # @return [Boolean]
75 76 77 78 79 80 81 82
  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
83
    user.aspects
84 85
  end

86
  # @return [Aspect] The first aspect in #aspects
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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
106 107 108 109 110 111 112 113

  # Memoizes all Contacts present in the Stream
  #
  # @return [Array<Contact>]
  def contacts_in_stream
    @contacts_in_stream ||= Contact.where(:user_id => user.id, :person_id => people.map{|x| x.id}).all
  end

114 115 116
  def featured_diaspora_id
    @featured_diaspora_id ||= AppConfig[:featured_users].try(:sample, 1)
  end
117 118 119 120 121 122 123 124 125

  # @param post [Post]
  # @return [Boolean]
  def post_is_from_contact?(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
126
end