inbox: refactor show method, fix #959

This commit is contained in:
Georg Gadinger 2023-01-19 00:13:48 +01:00
parent 31ed9ba738
commit 30dec68e1b
3 changed files with 33 additions and 43 deletions

View file

@ -1,50 +1,21 @@
# frozen_string_literal: true
class InboxController < ApplicationController
before_action :authenticate_user!
before_action :find_author, only: %i[show]
def show
@inbox = current_user.cursored_inbox(last_id: params[:last_id])
@inbox = current_user.cursored_inbox(last_id: params[:last_id]).then(&method(:filter_author_chain))
@inbox_last_id = @inbox.map(&:id).min
@more_data_available = !current_user.cursored_inbox(last_id: @inbox_last_id, size: 1).count.zero?
@inbox_count = current_user.inboxes.count
@more_data_available = !current_user.cursored_inbox(last_id: @inbox_last_id, size: 1).then(&method(:filter_author_chain)).count.zero?
@inbox_count = current_user.inboxes.then(&method(:filter_author_chain)).count
if params[:author].present?
begin
@author = true
@target_user = User.where('LOWER(screen_name) = ?', params[:author].downcase).first!
@inbox_author = @inbox.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
@inbox_author_count = current_user.inboxes
.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
.count
if @inbox_author.empty?
@empty = true
flash.now[:info] = t(".author.info", author: params[:author])
else
@inbox = @inbox_author
@inbox_count = @inbox_author_count
@inbox_last_id = @inbox.map(&:id).min
@more_data_available = !current_user.cursored_inbox(last_id: @inbox_last_id, size: 1)
.joins(:question)
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
.count
.zero?
end
rescue => e
Sentry.capture_exception(e)
flash.now[:error] = t(".author.error", author: params[:author])
@not_found = true
end
end
if @empty or @not_found
@delete_id = "ib-delete-all"
elsif @author
@delete_id = "ib-delete-all-author"
else
@delete_id = "ib-delete-all"
end
@delete_id = if @author_user && @inbox_count.positive?
"ib-delete-all-author"
else
"ib-delete-all"
end
@disabled = true if @inbox.empty?
respond_to do |format|
@ -75,4 +46,23 @@ class InboxController < ApplicationController
format.html { redirect_to inbox_path }
end
end
private
def find_author
return if params[:author].blank?
@author = params[:author]
@author_user = User.where("LOWER(screen_name) = ?", @author.downcase).first
flash.now[:error] = t(".author.error", author: @author) unless @author_user
end
def filter_author_chain(query)
return query unless @author_user
query
.joins(:question)
.where(questions: { user: @author_user, author_is_anonymous: false })
end
end

View file

@ -7,4 +7,4 @@
- if @more_data_available
.d-flex.justify-content-center#paginator
= button_to t("voc.load"), inbox_path, class: "btn btn-light", method: :get, params: { last_id: @inbox_last_id }, form: { data: { turbo_stream: true } }
= button_to t("voc.load"), inbox_path, class: "btn btn-light", method: :get, params: { last_id: @inbox_last_id, author: @author }.compact, form: { data: { turbo_stream: true } }

View file

@ -4,4 +4,4 @@
= turbo_stream.update "paginator" do
- if @more_data_available
= button_to t("voc.load"), inbox_path, class: "btn btn-light", method: :get, params: { last_id: @inbox_last_id }, form: { data: { turbo_stream: true } }
= button_to t("voc.load"), inbox_path, class: "btn btn-light", method: :get, params: { last_id: @inbox_last_id, author: @author }.compact, form: { data: { turbo_stream: true } }