app/controllers/thredded/topics_controller.rb in thredded-0.15.1 vs app/controllers/thredded/topics_controller.rb in thredded-0.15.2
- old
+ new
@@ -4,80 +4,68 @@
class TopicsController < Thredded::ApplicationController # rubocop:disable Metrics/ClassLength
include Thredded::NewTopicParams
include Thredded::NewPostParams
before_action :thredded_require_login!,
- only: %i[edit new update create destroy follow unfollow]
+ only: %i[edit new update create destroy follow unfollow unread]
+ before_action :verify_messageboard,
+ only: %i[index search unread]
+
before_action :use_topic_messageboard,
only: %i[show edit update destroy follow unfollow]
after_action :update_user_activity
- after_action :verify_authorized, except: %i[search]
+ after_action :verify_authorized, except: %i[search unread]
after_action :verify_policy_scoped, except: %i[show new create edit update destroy follow unfollow]
def index
- authorize_reading messageboard
- unless params_match?(canonical_messageboard_params)
- skip_policy_scope
- return redirect_to(canonical_messageboard_params)
- end
-
page_scope = policy_scope(messageboard.topics)
.order_sticky_first.order_recently_posted_first
+ .includes(:categories, :last_user, :user)
.page(current_page)
return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
@topics = Thredded::TopicsPageView.new(thredded_current_user, page_scope)
- Thredded::TopicForm.new(messageboard: messageboard, user: thredded_current_user).tap do |form|
- @new_topic = form if policy(form.topic).create?
- end
+ @new_topic = init_new_topic
end
- def show
- authorize topic, :read?
- return redirect_to(canonical_topic_params) unless params_match?(canonical_topic_params)
- page_scope = policy_scope(topic.posts)
- .order_oldest_first
- .includes(:user, :messageboard, :postable)
+ def unread
+ page_scope = topics_scope
+ .unread(thredded_current_user)
+ .order_followed_first(thredded_current_user).order_recently_posted_first
+ .includes(:categories, :last_user, :user)
.page(current_page)
return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
- @posts = Thredded::TopicPostsPageView.new(thredded_current_user, topic, page_scope)
-
- if thredded_signed_in?
- Thredded::UserTopicReadState.touch!(thredded_current_user.id, topic.id, page_scope.last)
- end
-
- @new_post = Thredded::PostForm.new(user: thredded_current_user, topic: topic, post_params: new_post_params)
+ @topics = Thredded::TopicsPageView.new(thredded_current_user, page_scope)
+ @new_topic = init_new_topic
end
def search
- in_messageboard = params.key?(:messageboard_id)
- if in_messageboard
- authorize_reading messageboard
- unless params_match?(canonical_messageboard_params)
- skip_policy_scope
- return redirect_to(canonical_messageboard_params)
- end
- end
@query = params[:q].to_s
- topics_scope = policy_scope(
- if in_messageboard
- messageboard.topics
- else
- Thredded::Topic.where(messageboard_id: policy_scope(Thredded::Messageboard.all).pluck(:id))
- end
- )
page_scope = topics_scope
.search_query(@query)
.order_recently_posted_first
.includes(:categories, :last_user, :user)
.page(current_page)
return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
@topics = Thredded::TopicsPageView.new(thredded_current_user, page_scope)
end
+ def show
+ authorize topic, :read?
+ return redirect_to(canonical_topic_params) unless params_match?(canonical_topic_params)
+ page_scope = policy_scope(topic.posts)
+ .order_oldest_first
+ .includes(:user, :messageboard, :postable)
+ .page(current_page)
+ return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
+ @posts = Thredded::TopicPostsPageView.new(thredded_current_user, topic, page_scope)
+ Thredded::UserTopicReadState.touch!(thredded_current_user.id, topic.id, page_scope.last) if thredded_signed_in?
+ @new_post = Thredded::PostForm.new(user: thredded_current_user, topic: topic, post_params: new_post_params)
+ end
+
def new
@new_topic = Thredded::TopicForm.new(new_topic_params)
authorize_creating @new_topic.topic
return redirect_to(canonical_messageboard_params) unless params_match?(canonical_messageboard_params)
render
@@ -150,9 +138,23 @@
Thredded::UserTopicFollow.find_by(topic_id: topic.id, user_id: thredded_current_user.id).try(:destroy)
follow_change_response(following: false)
end
private
+
+ def init_new_topic
+ return unless in_messageboard?
+ form = Thredded::TopicForm.new(messageboard: messageboard, user: thredded_current_user)
+ form if policy(form.topic).create?
+ end
+
+ def verify_messageboard
+ return unless in_messageboard?
+ authorize_reading messageboard
+ return if params_match?(canonical_messageboard_params)
+ skip_policy_scope
+ redirect_to(canonical_messageboard_params)
+ end
def canonical_messageboard_params
{ messageboard_id: messageboard.slug }
end