Module: Diaspora::UserModules::Querying

Included in:
Diaspora::UserModules
Defined in:
lib/diaspora/user/querying.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) all_aspect_ids



89
90
91
# File 'lib/diaspora/user/querying.rb', line 89

def all_aspect_ids
  self.aspects.all.collect{|x| x.id}
end

- (Object) aspects_with_person(person)



79
80
81
# File 'lib/diaspora/user/querying.rb', line 79

def aspects_with_person person
  contact_for(person).aspects
end

- (Object) aspects_with_post(post_id)



58
59
60
# File 'lib/diaspora/user/querying.rb', line 58

def aspects_with_post(post_id)
  self.aspects.joins(:aspect_visibilities).where(:aspect_visibilities => {:post_id => post_id})
end

- (Object) contact_for(person)



54
55
56
57
# File 'lib/diaspora/user/querying.rb', line 54

def contact_for(person)
  return nil unless person
  contact_for_person_id(person.id)
end

- (Object) contact_for_person_id(person_id)



62
63
64
# File 'lib/diaspora/user/querying.rb', line 62

def contact_for_person_id(person_id)
  Contact.where(:user_id => self.id, :person_id => person_id).includes(:person => :profile).first
end

- (Object) contacts_in_aspects(aspects)



83
84
85
86
87
# File 'lib/diaspora/user/querying.rb', line 83

def contacts_in_aspects aspects
  aspects.inject([]) do |contacts,aspect|
    contacts | aspect.contacts
  end
end

- (Object) find_visible_post_by_id(id, opts = {})



9
10
11
12
13
# File 'lib/diaspora/user/querying.rb', line 9

def find_visible_post_by_id( id, opts={} )
  post = Post.where(:id => id).joins(:contacts).where(:contacts => {:user_id => self.id}).where(opts).first
  post ||= Post.where(:id => id, :author_id => self.person.id).where(opts).first
  post ||= Post.where(:id => id, :public => true).where(opts).first
end

- (Object) people_in_aspects(requested_aspects, opts = {})



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/diaspora/user/querying.rb', line 66

def people_in_aspects(requested_aspects, opts={})
  allowed_aspects = self.aspects & requested_aspects
  person_ids = contacts_in_aspects(allowed_aspects).collect{|contact| contact.person_id}
  people = Person.where(:id => person_ids)

  if opts[:type] == 'remote'
    people = people.where(:owner_id => nil)
  elsif opts[:type] == 'local'
    people = people.where('people.owner_id IS NOT NULL')
  end
  people
end

- (Object) posts_from(person)



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/diaspora/user/querying.rb', line 93

def posts_from(person)
  return self.person.posts.where(:pending => false).order("created_at DESC") if person == self.person
  con = Contact.arel_table
  p = Post.arel_table
  post_ids = []
  if contact = self.contact_for(person)
    post_ids = Post.connection.execute(contact.post_visibilities.where(:hidden => false).select('post_visibilities.post_id').to_sql).map{|r| r.first}
  end
  post_ids += Post.connection.execute(person.posts.where(:public => true).select('posts.id').to_sql).map{|r| r.first}

  Post.where(:id => post_ids, :pending => false).select('DISTINCT posts.*').order("posts.created_at DESC")
end

- (Object) visible_photos(opts = {})



50
51
52
# File 'lib/diaspora/user/querying.rb', line 50

def visible_photos(opts = {})
  visible_posts(opts.merge(:type => 'Photo'))
end

- (Object) visible_posts(opts = {})



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/diaspora/user/querying.rb', line 15

def visible_posts(opts = {})
  defaults = {
    :type => ['StatusMessage', 'Photo'],
    :order => 'updated_at DESC',
    :limit => 15,
    :hidden => false
  }
  opts = defaults.merge(opts)

  order_field = opts[:order].split.first.to_sym
  order_with_table = 'posts.' + opts[:order]

  opts[:max_time] = Time.at(opts[:max_time]) if opts[:max_time].is_a?(Integer)
  opts[:max_time] ||= Time.now + 1

  select_clause ='DISTINCT posts.id, posts.updated_at AS updated_at, posts.created_at AS created_at'

  posts_from_others = Post.joins(:contacts).where( :pending => false, :type => opts[:type], :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id})
  posts_from_self = self.person.posts.where(:pending => false, :type => opts[:type])

  if opts[:by_members_of]
    posts_from_others = posts_from_others.joins(:contacts => :aspect_memberships).where(
      :aspect_memberships => {:aspect_id => opts[:by_members_of]})
    posts_from_self = posts_from_self.joins(:aspect_visibilities).where(:aspect_visibilities => {:aspect_id => opts[:by_members_of]})
  end

  posts_from_others = posts_from_others.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time]))
  posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time]))

  all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}"
  post_ids = Post.connection.execute(all_posts).map{|r| r.first}

  Post.where(:id => post_ids).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table)
end