2013-02-06 03:16:51 +08:00
|
|
|
class Admin::FlagsController < Admin::AdminController
|
2013-02-07 23:45:24 +08:00
|
|
|
def index
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
sql = SqlBuilder.new "select p.id, t.title, p.cooked, p.user_id, p.topic_id, p.post_number, p.hidden, t.visible topic_visible
|
2013-02-07 23:45:24 +08:00
|
|
|
from posts p
|
2013-02-06 03:16:51 +08:00
|
|
|
join topics t on t.id = topic_id
|
|
|
|
join (
|
2013-02-07 23:45:24 +08:00
|
|
|
select
|
|
|
|
post_id,
|
|
|
|
count(*) as cnt,
|
|
|
|
max(created_at) max,
|
|
|
|
min(created_at) min
|
2013-02-06 03:16:51 +08:00
|
|
|
from post_actions
|
|
|
|
/*where2*/
|
|
|
|
group by post_id
|
|
|
|
) as a on a.post_id = p.id
|
|
|
|
/*where*/
|
2013-02-07 23:45:24 +08:00
|
|
|
/*order_by*/
|
2013-02-06 03:16:51 +08:00
|
|
|
limit 100
|
|
|
|
"
|
|
|
|
|
2013-04-15 11:09:52 +08:00
|
|
|
sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.notify_flag_types.values
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-06 09:13:41 +08:00
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
# it may make sense to add a view that shows flags on deleted posts,
|
2013-02-06 09:13:41 +08:00
|
|
|
# we don't clear the flags on post deletion, just supress counts
|
|
|
|
# they may have deleted_at on the action not set
|
2013-02-06 03:16:51 +08:00
|
|
|
if params[:filter] == 'old'
|
|
|
|
sql.where2 "deleted_at is not null"
|
|
|
|
else
|
2013-02-06 09:13:41 +08:00
|
|
|
sql.where "p.deleted_at is null and t.deleted_at is null"
|
2013-02-06 03:16:51 +08:00
|
|
|
sql.where2 "deleted_at is null"
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:filter] == 'old'
|
|
|
|
sql.order_by "max desc"
|
|
|
|
else
|
|
|
|
sql.order_by "cnt desc, max asc"
|
|
|
|
end
|
|
|
|
|
|
|
|
posts = sql.exec.to_a
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
if posts.length == 0
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {users: [], posts: []}
|
2013-02-07 23:45:24 +08:00
|
|
|
return
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
map = {}
|
2013-02-06 03:16:51 +08:00
|
|
|
users = Set.new
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
posts.each{ |p|
|
2013-02-06 03:16:51 +08:00
|
|
|
users << p["user_id"]
|
|
|
|
p["excerpt"] = Post.excerpt(p["cooked"])
|
|
|
|
p.delete "cooked"
|
|
|
|
p[:topic_slug] = Slug.for(p["title"])
|
|
|
|
map[p["id"]] = p
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
sql = SqlBuilder.new "select a.id, a.user_id, post_action_type_id, a.created_at, post_id, a.message
|
2013-02-06 03:16:51 +08:00
|
|
|
from post_actions a
|
2013-02-07 23:45:24 +08:00
|
|
|
/*where*/
|
|
|
|
"
|
2013-04-15 11:09:52 +08:00
|
|
|
sql.where("post_action_type_id in (:flag_types)", flag_types: PostActionType.notify_flag_types.values)
|
2013-02-06 03:16:51 +08:00
|
|
|
sql.where("post_id in (:posts)", posts: posts.map{|p| p["id"].to_i})
|
|
|
|
|
|
|
|
if params[:filter] == 'old'
|
|
|
|
sql.where('deleted_at is not null')
|
|
|
|
else
|
|
|
|
sql.where('deleted_at is null')
|
|
|
|
end
|
|
|
|
|
2013-02-07 09:32:29 +08:00
|
|
|
sql.exec.each do |action|
|
2013-02-07 23:45:24 +08:00
|
|
|
p = map[action["post_id"]]
|
2013-02-06 03:16:51 +08:00
|
|
|
p[:post_actions] ||= []
|
|
|
|
p[:post_actions] << action
|
|
|
|
|
|
|
|
users << action["user_id"]
|
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
sql =
|
2013-02-06 03:16:51 +08:00
|
|
|
"select id, username, name, email from users
|
|
|
|
where id in (?)"
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
users = User.exec_sql(sql, users.to_a).to_a
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
users.each { |u|
|
2013-02-06 03:16:51 +08:00
|
|
|
u["avatar_template"] = User.avatar_template(u["email"])
|
|
|
|
u.delete("email")
|
|
|
|
}
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
render json: MultiJson.dump({users: users, posts: posts})
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
p = Post.find(params[:id])
|
|
|
|
PostAction.clear_flags!(p, current_user.id)
|
2013-02-07 23:45:24 +08:00
|
|
|
p.hidden = false
|
2013-02-06 03:16:51 +08:00
|
|
|
p.hidden_reason_id = nil
|
|
|
|
p.save
|
2013-02-07 23:45:24 +08:00
|
|
|
render nothing: true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|