fix(search): fix weird scope redirection bug, and increase scope timeout.

This commit is contained in:
Neetpone 2024-10-30 04:55:22 -04:00
parent 7f2e60b5e0
commit 3f200192e1
2 changed files with 8 additions and 4 deletions

View file

@ -18,8 +18,10 @@ class SearchController < ApplicationController
@scope = SearchScope.new(params) @scope = SearchScope.new(params)
# The scope is valid if was successfully used to load the existing search params # The scope is valid if was successfully used to load the existing search params
unless @scope.scope_loaded if @scope.scope_invalid
return redirect_to "/search?scope=#{@scope.scope_key}" return redirect_to(root_path)
elsif !@scope.scope_loaded
return redirect_to(search_path(scope: @scope.scope_key))
end end
@search_params = @scope.search_params @search_params = @scope.search_params

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
class SearchScope class SearchScope
attr_reader :search_params, :scope_key, :scope_loaded attr_reader :search_params, :scope_key, :scope_loaded, :scope_invalid
def initialize(params, search_params = nil) def initialize(params, search_params = nil)
@scope_key = SecureRandom.hex 16 @scope_key = SecureRandom.hex 16
@ -24,6 +24,8 @@ class SearchScope
@scope_key = params[:scope] @scope_key = params[:scope]
@scope_loaded = true @scope_loaded = true
self.persist # refresh the expiry self.persist # refresh the expiry
else
@scope_invalid = true
end end
else else
@search_params = params.permit!.to_h.symbolize_keys @search_params = params.permit!.to_h.symbolize_keys
@ -33,6 +35,6 @@ class SearchScope
def persist def persist
# Nice long expiry so nobody's search disappears if they don't touch it for awhile. # Nice long expiry so nobody's search disappears if they don't touch it for awhile.
$redis.setex("search_scope/#{@scope_key}", 1.day.in_seconds, JSON.dump(@search_params)) $redis.setex("search_scope/#{@scope_key}", 1.week.in_seconds, JSON.dump(@search_params))
end end
end end