2015-05-13 11:39:04 -07:00
|
|
|
class DiscoverController < ApplicationController
|
2020-04-18 16:45:50 -07:00
|
|
|
before_action :authenticate_user!
|
2015-05-14 21:55:26 -07:00
|
|
|
|
2015-05-13 11:56:51 -07:00
|
|
|
def index
|
2020-04-19 04:55:13 -07:00
|
|
|
unless APP_CONFIG.dig(:features, :discover, :enabled) || current_user.mod?
|
|
|
|
return redirect_to root_path
|
|
|
|
end
|
|
|
|
|
2015-05-13 12:58:00 -07:00
|
|
|
top_x = 10 # only display the top X items
|
2015-05-13 11:56:51 -07:00
|
|
|
|
2021-12-30 13:15:59 -08:00
|
|
|
@popular_answers = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:smile_count).reverse_order.limit(top_x).includes(:question, :user, :comments)
|
|
|
|
@most_discussed = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:comment_count).reverse_order.limit(top_x).includes(:question, :user, :comments)
|
|
|
|
@popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x).includes(:user)
|
|
|
|
@new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x).includes(:profile)
|
2015-05-13 12:58:00 -07:00
|
|
|
|
2023-05-07 07:39:02 -07:00
|
|
|
answer_ids = @popular_answers.map(&:id) + @most_discussed.map(&:id)
|
|
|
|
@subscribed_answer_ids = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id)
|
|
|
|
|
2015-05-13 12:58:00 -07:00
|
|
|
# .user = the user
|
|
|
|
# .question_count = how many questions did the user ask
|
|
|
|
@users_with_most_questions = Question.select('user_id, COUNT(*) AS question_count').
|
|
|
|
where("created_at > ?", Time.now.ago(1.week)).
|
|
|
|
where(author_is_anonymous: false).
|
|
|
|
group(:user_id).
|
|
|
|
order('question_count').
|
2015-05-17 15:57:47 -07:00
|
|
|
reverse_order.limit(top_x)
|
|
|
|
|
|
|
|
# .user = the user
|
|
|
|
# .answer_count = how many questions did the user answer
|
|
|
|
@users_with_most_answers = Answer.select('user_id, COUNT(*) AS answer_count').
|
|
|
|
where("created_at > ?", Time.now.ago(1.week)).
|
|
|
|
group(:user_id).
|
|
|
|
order('answer_count').
|
|
|
|
reverse_order.limit(top_x)
|
2015-05-13 11:56:51 -07:00
|
|
|
end
|
2015-05-13 11:39:04 -07:00
|
|
|
end
|