This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
home_controller.rb
188 lines (165 loc) · 6.71 KB
/
home_controller.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# coding: utf-8
class HomeController < ApplicationController
before_filter :require_user_text, :only => [:update_in_place]
before_filter :require_user, :except => [:about,:index]
def index
@per_page = 20
if current_user
@notifies = {}
@notifications = current_user.notifications.unread.includes(:log)
@notifications.each do |notify|
@notifies[notify.target_id] ||= {}
@notifies[notify.target_id][:items] ||= []
case notify.action
when "FOLLOW" then @notifies[notify.target_id][:type] = "USER"
when "THANK_ANSWER" then @notifies[notify.target_id][:type] = "THANK_ANSWER"
when "INVITE_TO_ANSWER" then @notifies[notify.target_id][:type] = "INVITE_TO_ANSWER"
when "NEW_TO_USER" then @notifies[notify.target_id][:type] = "ASK_USER"
else
@notifies[notify.target_id][:type] = "ASK"
end
@notifies[notify.target_id][:items] << notify
end
if current_user.following_ids.size == 0 and current_user.followed_ask_ids.size == 0 and current_user.followed_topic_ids.size == 0
redirect_to newbie_path and return
else
# TODO: 这里需要过滤掉烂问题
@logs = Log.any_of({:user_id.in => current_user.following_ids},
{:target_id.in => current_user.followed_ask_ids})
.and(:action.in => ["NEW", "AGREE", "EDIT"], :_type.in => ["AskLog", "AnswerLog", "CommentLog", "UserLog"])
.excludes(:user_id => current_user.id).desc("$natural")
.paginate(:page => params[:page], :per_page => @per_page)
if @logs.count < 1
@asks = Ask.normal.any_of({:topics.in => current_user.followed_topics.map{|t| t.name}}).not_in(:follower_ids => [current_user.id])
@asks = @asks.includes(:user,:last_answer,:last_answer_user,:topics)
.exclude_ids(current_user.muted_ask_ids)
.desc(:answers_count,:answered_at)
.paginate(:page => params[:page], :per_page => @per_page)
if params[:format] == "js"
render "/asks/index.js"
end
else
if params[:format] == "js"
render "/logs/index.js"
else
render "/logs/index"
end
end
end
else
@asks = Ask.normal.recent.includes(:user,:last_answer,:last_answer_user,:topics)
.paginate(:page => params[:page], :per_page => @per_page)
if params[:format] == "js"
render "/asks/index.js"
end
end
end
def newbie
ask_logs = Log.any_of({:_type => "AskLog"}, {:_type => "UserLog", :action.in => ["FOLLOW_ASK", "UNFOLLOW_ASK"]}).where(:created_at.gte => (Time.now - 12.hours))
answer_logs = Log.any_of({:_type => "AnswerLog"}, {:_type => "UserLog", :action => "AGREE"}).where(:created_at.gte => (Time.now - 12.hours))
@asks = Ask.normal.any_of({:_id.in => ask_logs.map {|l| l.target_id}.uniq}, {:_id.in => answer_logs.map {|l| l.target_parent_id}.uniq}).order_by(:answers_count.asc, :views_count.asc)
h = {}
# 将回答次数*topic,以加入回答次数
@hot_topics = @asks.inject([]) { |memo, ask|
memo += ask.topics
}
@hot_topics.delete("者也")
@hot_topics.delete("知乎")
@hot_topics.delete("反馈")
@hot_topics.delete("zheye")
@hot_topics.delete("Quora")
@hot_topics.delete("quora")
@hot_topics.each { |str|
h[str] = (h[str] || 0) + 1
}
@hot_topics = h.sort{|a, b|b[1]<=>a[1]}.collect{|tmp|tmp[0]}[0..8]
end
def timeline
@per_page = 20
# @logs = Log.any_in(:user_id => curr)
end
def followed
@per_page = 20
@asks = current_user ? current_user.followed_asks.normal : Ask.normal
@asks = @asks.includes(:user,:last_answer,:last_answer_user,:topics)
.exclude_ids(current_user.muted_ask_ids)
.desc(:answered_at,:id)
.paginate(:page => params[:page], :per_page => @per_page)
if params[:format] == "js"
render "/asks/index.js"
else
render "index"
end
end
def recommended
@per_page = 20
@asks = current_user ? Ask.normal.any_of({:topics.in => current_user.followed_topics.map{|t| t.name}}).not_in(:follower_ids => [current_user.id]).and(:answers_count.lt => 1) : Ask.normal
@asks = @asks.includes(:user,:last_answer,:last_answer_user,:topics)
.exclude_ids(current_user.muted_ask_ids)
.desc(:answers_count,:answered_at)
.paginate(:page => params[:page], :per_page => @per_page)
if params[:format] == "js"
render "/asks/recommended.js"
end
end
# 查看用户不感兴趣的问题
def muted
@per_page = 20
@asks = Ask.normal.includes(:user,:last_answer,:last_answer_user,:topics)
.only_ids(current_user.muted_ask_ids)
.desc(:answered_at,:id)
.paginate(:page => params[:page], :per_page => @per_page)
set_seo_meta("我屏蔽掉的问题")
if params[:format] == "js"
render "/asks/index.js"
else
render "index"
end
end
def update_in_place
# TODO: Here need to chack permission
klass, field, id = params[:id].split('__')
puts params[:id]
# 验证权限,用户是否有修改制定信息的权限
case klass
when "user" then return if current_user.id.to_s != id
end
params[:value] = simple_format(params[:value].to_s.strip) if params[:did_editor_content_formatted] == "no"
object = klass.camelize.constantize.find(id)
update_hash = {field => params[:value]}
if ["ask","topic"].include?(klass) and current_user
update_hash[:current_user_id] = current_user.id
end
if object.update_attributes(update_hash)
render :text => object.send(field).to_s
else
Rails.logger.info "object.errors.full_messages: #{object.errors.full_messages}"
render :text => object.errors.full_messages.join("\n"), :status => 422
end
end
def about
set_seo_meta("关于")
@users = User.any_in(:email => Setting.admin_emails)
end
def mark_notifies_as_read
if !params[:ids]
render :text => "0"
else
notifications = current_user.notifications.any_in(:_id => params[:ids].split(","))
notifications.each do |notify|
# Rails.logger.info "mark_notifies_as_read\n"
notify.update_attribute(:has_read, true)
end
render :text => "1"
end
end
def report
name = "访客"
if current_user
name = current_user.name
end
ReportSpam.add(params[:url],params[:desc],name)
flash[:notice] = "举报信息已经提交,谢谢你。"
redirect_to params[:url]
end
end