diff --git a/app/controllers/concerns/paginates_answers.rb b/app/controllers/concerns/paginates_answers.rb new file mode 100644 index 00000000..83fd5293 --- /dev/null +++ b/app/controllers/concerns/paginates_answers.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module PaginatesAnswers + def paginate_answers + answer_ids = @answers.map(&:id) + answer_ids += @pinned_answers.pluck(:id) if @pinned_answers.present? + @answers_last_id = answer_ids.min + @more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero? + Subscription.where(user: current_user, answer_id: answer_ids + @pinned_answers.pluck(:id)).pluck(:answer_id) if user_signed_in? + end +end diff --git a/app/controllers/question_controller.rb b/app/controllers/question_controller.rb index 0952f8d7..77100737 100644 --- a/app/controllers/question_controller.rb +++ b/app/controllers/question_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class QuestionController < ApplicationController + include PaginatesAnswers + def show @question = Question.find(params[:id]) @answers = @question.cursored_answers(last_id: params[:last_id], current_user:) @@ -10,8 +12,8 @@ class QuestionController < ApplicationController @subscribed = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in? respond_to do |format| - format.html - format.turbo_stream { render layout: false, status: :see_other } + format.html { render locals: { subscribed_answer_ids: } } + format.turbo_stream { render layout: false, status: :see_other, locals: { subscribed_answer_ids: } } end end end diff --git a/app/controllers/timeline_controller.rb b/app/controllers/timeline_controller.rb index 7e56ba73..75c54e78 100644 --- a/app/controllers/timeline_controller.rb +++ b/app/controllers/timeline_controller.rb @@ -35,11 +35,11 @@ class TimelineController < ApplicationController timeline_ids = @timeline.map(&:id) @timeline_last_id = timeline_ids.min @more_data_available = !yield(last_id: @timeline_last_id, size: 1).count.zero? - @subscribed = Subscription.where(user: current_user, answer_id: timeline_ids).pluck(:answer_id) if user_signed_in? + subscribed_answer_ids = Subscription.where(user: current_user, answer_id: timeline_ids).pluck(:answer_id) if user_signed_in? respond_to do |format| - format.html { render "timeline/timeline" } - format.turbo_stream { render "timeline/timeline", layout: false, status: :see_other } + format.html { render "timeline/timeline", locals: { subscribed_answer_ids: } } + format.turbo_stream { render "timeline/timeline", layout: false, status: :see_other, locals: { subscribed_answer_ids: } } end end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 56262166..0fc30366 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,21 +1,20 @@ # frozen_string_literal: true class UserController < ApplicationController + include PaginatesAnswers + before_action :set_user before_action :hidden_social_graph_redirect, only: %i[followers followings] after_action :mark_notification_as_read, only: %i[show] def show @answers = @user.cursored_answers(last_id: params[:last_id]) - answer_ids = @answers.map(&:id) @pinned_answers = @user.answers.pinned.order(pinned_at: :desc).limit(10) - @answers_last_id = answer_ids.min - @more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero? - @subscribed = Subscription.where(user: current_user, answer_id: answer_ids).pluck(:answer_id) if user_signed_in? + subscribed_answer_ids = paginate_answers respond_to do |format| - format.html - format.turbo_stream + format.html { render locals: { subscribed_answer_ids: } } + format.turbo_stream { render layout: false, locals: { subscribed_answer_ids: } } end end diff --git a/app/views/actions/_answer.html.haml b/app/views/actions/_answer.html.haml index b0fe1909..e63e452b 100644 --- a/app/views/actions/_answer.html.haml +++ b/app/views/actions/_answer.html.haml @@ -1,5 +1,5 @@ .dropdown-menu.dropdown-menu-end{ role: :menu } - - if @subscribed&.include?(answer.id) + - if subscribed_answer_ids&.include?(answer.id) -# fun joke should subscribe? %a.dropdown-item{ href: "#", data: { a_id: answer.id, action: "ab-submarine", torpedo: "no" } } %i.fa.fa-fw.fa-anchor diff --git a/app/views/answerbox/_actions.html.haml b/app/views/answerbox/_actions.html.haml index 4ec1c35d..5c152abe 100644 --- a/app/views/answerbox/_actions.html.haml +++ b/app/views/answerbox/_actions.html.haml @@ -13,4 +13,4 @@ .btn-group %button.btn.btn-default.btn-sm.dropdown-toggle{ data: { bs_toggle: :dropdown }, aria: { expanded: false } } %span.caret - = render "actions/answer", answer: a + = render "actions/answer", answer: a, subscribed_answer_ids: diff --git a/app/views/application/_answerbox.html.haml b/app/views/application/_answerbox.html.haml index 36c2a90e..b84a5f17 100644 --- a/app/views/application/_answerbox.html.haml +++ b/app/views/application/_answerbox.html.haml @@ -21,7 +21,7 @@ .answerbox__answer-date = link_to(raw(t("time.distance_ago", time: time_tooltip(a))), answer_path(a.user.screen_name, a.id), data: { selection_hotkey: "l" }) .col-md-6.d-flex.d-md-block.answerbox__actions - = render "answerbox/actions", a: a, display_all: display_all + = render "answerbox/actions", a:, display_all:, subscribed_answer_ids: - else .row .col-md-6.text-start.text-muted @@ -33,7 +33,7 @@ %i.fa.fa-thumbtack = t(".pinned") .col-md-6.d-md-flex.answerbox__actions - = render "answerbox/actions", a: a, display_all: display_all + = render "answerbox/actions", a:, display_all:, subscribed_answer_ids: .card-footer{ id: "ab-comments-section-#{a.id}", class: display_all.nil? ? "d-none" : nil } %div{ id: "ab-smiles-#{a.id}" }= render "answerbox/smiles", a: a %div{ id: "ab-comments-#{a.id}" }= render "answerbox/comments", a: a diff --git a/app/views/question/show.html.haml b/app/views/question/show.html.haml index b21b70f1..102389f6 100644 --- a/app/views/question/show.html.haml +++ b/app/views/question/show.html.haml @@ -6,7 +6,7 @@ %button.d-none{ data: { hotkey: "j", action: "navigation#down" } } %button.d-none{ data: { hotkey: "k", action: "navigation#up" } } - @answers.each do |a| - = render "answerbox", a: a, show_question: false + = render "answerbox", a:, show_question: false, subscribed_answer_ids: - if @more_data_available .d-flex.justify-content-center.justify-content-sm-start#paginator diff --git a/app/views/question/show.turbo_stream.haml b/app/views/question/show.turbo_stream.haml index 34f6be75..758a5e23 100644 --- a/app/views/question/show.turbo_stream.haml +++ b/app/views/question/show.turbo_stream.haml @@ -1,6 +1,6 @@ = turbo_stream.append "answers" do - @answers.each do |a| - = render "answerbox", a: a, show_question: false + = render "answerbox", a:, show_question: false, subscribed_answer_ids: = turbo_stream.update "paginator" do - if @more_data_available diff --git a/app/views/timeline/timeline.html.haml b/app/views/timeline/timeline.html.haml index 2b3089b8..6515d9bf 100644 --- a/app/views/timeline/timeline.html.haml +++ b/app/views/timeline/timeline.html.haml @@ -2,7 +2,7 @@ %button.d-none{ data: { hotkey: "j", action: "navigation#down" } } %button.d-none{ data: { hotkey: "k", action: "navigation#up" } } - @timeline.each do |answer| - = render "answerbox", a: answer + = render "answerbox", a: answer, subscribed_answer_ids: - if @more_data_available .d-flex.justify-content-center#paginator diff --git a/app/views/user/show.html.haml b/app/views/user/show.html.haml index 98f36de8..69a03fe8 100644 --- a/app/views/user/show.html.haml +++ b/app/views/user/show.html.haml @@ -4,11 +4,11 @@ %button.d-none{ data: { hotkey: "k", action: "navigation#up" } } #pinned-answers - @pinned_answers.each do |a| - = render "answerbox", a: + = render "answerbox", a:, subscribed_answer_ids: #answers - @answers.each do |a| - = render "answerbox", a: + = render "answerbox", a:, subscribed_answer_ids: - if @more_data_available .d-flex.justify-content-center.justify-content-sm-start#paginator diff --git a/app/views/user/show.turbo_stream.haml b/app/views/user/show.turbo_stream.haml index c341e74b..a3477c47 100644 --- a/app/views/user/show.turbo_stream.haml +++ b/app/views/user/show.turbo_stream.haml @@ -1,6 +1,6 @@ = turbo_stream.append "answers" do - @answers.each do |a| - = render 'answerbox', a: a + = render "answerbox", a:, subscribed_answer_ids: = turbo_stream.update "paginator" do - if @more_data_available