forked from diaspora/diaspora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevil_query.rb
153 lines (122 loc) · 4.03 KB
/
evil_query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module EvilQuery
class Base
def fetch_ids!(relation, id_column)
#the relation should be ordered and limited by here
@class.connection.select_values(id_sql(relation, id_column))
end
def id_sql(relation, id_column)
relation.select(id_column).to_sql
end
end
class Participation < Base
def initialize(user)
@user = user
@class = Post
end
def posts
Post.joins(:participations).where(:participations => {:author_id => @user.person.id}).order("posts.interacted_at DESC")
end
end
class LikedPosts < Base
def initialize(user)
@user = user
end
def posts
Post.liked_by(@user.person)
end
end
class CommentedPosts < Base
def initialize(user)
@user = user
end
def posts
Post.commented_by(@user.person)
end
end
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_tags_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, 'people.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={})
@querent = user
@class = klass
@key = key
@id = id
@conditions = conditions
end
def post!
#small optimization - is this optimal order??
querent_is_contact.first || querent_is_author.first || public_post.first
end
protected
def querent_is_contact
@class.where(@key => @id).joins(:contacts).where(:contacts => {:user_id => @querent.id}).where(@conditions).select(@class.table_name+".*")
end
def querent_is_author
@class.where(@key => @id, :author_id => @querent.person.id).where(@conditions)
end
def public_post
@class.where(@key => @id, :public => true).where(@conditions)
end
end
class ShareablesFromPerson < Base
def initialize(querent, klass, person)
@querent = querent
@class = klass
@person = person
end
def make_relation!
return querents_posts if @person == @querent.person
# persons_private_visibilities and persons_public_posts have no limit which is making shareable_ids gigantic.
# perhaps they should the arrays should be merged and sorted
# then the query at the bottom of this method can be paginated or something?
shareable_ids = contact.present? ? fetch_ids!(persons_private_visibilities, "share_visibilities.shareable_id") : []
shareable_ids += fetch_ids!(persons_public_posts, table_name + ".id")
@class.where(:id => shareable_ids, :pending => false).
select('DISTINCT '+table_name+'.*').
order(table_name+".created_at DESC")
end
protected
def table_name
@class.table_name
end
def contact
@contact ||= @querent.contact_for(@person)
end
def querents_posts
@querent.person.send(table_name).where(:pending => false).order("#{table_name}.created_at DESC")
end
def persons_private_visibilities
contact.share_visibilities.where(:hidden => false, :shareable_type => @class.to_s)
end
def persons_public_posts
@person.send(table_name).where(:public => true).select(table_name+'.id')
end
end
end