2020-09-18 02:09:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Search
|
|
|
|
# This is an abstract class used for storing/searching recently viewed
|
|
|
|
# items. The #type and #finder methods are the only ones needed to be
|
|
|
|
# implemented by classes inheriting from this.
|
|
|
|
class RecentItems
|
2020-10-05 11:08:56 -04:00
|
|
|
ITEMS_LIMIT = 100 # How much history to remember from the user
|
|
|
|
SEARCH_LIMIT = 5 # How many matching items to return from search
|
2020-09-18 02:09:31 -04:00
|
|
|
EXPIRES_AFTER = 7.days
|
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
attr_reader :user
|
|
|
|
|
2020-09-18 02:09:31 -04:00
|
|
|
def initialize(user:, items_limit: ITEMS_LIMIT, expires_after: EXPIRES_AFTER)
|
|
|
|
@user = user
|
|
|
|
@items_limit = items_limit
|
|
|
|
@expires_after = expires_after
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_view(item)
|
|
|
|
with_redis do |redis|
|
|
|
|
redis.zadd(key, Time.now.to_f, item.id)
|
|
|
|
redis.expire(key, @expires_after)
|
|
|
|
|
|
|
|
# There is a race condition here where we could end up removing an
|
|
|
|
# item from 2 places concurrently but this is fine since worst case
|
|
|
|
# scenario we remove an extra item from the end of the list.
|
|
|
|
if redis.zcard(key) > @items_limit
|
|
|
|
redis.zremrangebyrank(key, 0, 0) # Remove least recent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(term)
|
2020-10-05 11:08:56 -04:00
|
|
|
finder.new(user, search: term, in: 'title')
|
|
|
|
.execute
|
|
|
|
.limit(SEARCH_LIMIT).reorder(nil).id_in_ordered(latest_ids) # rubocop: disable CodeReuse/ActiveRecord
|
2020-09-18 02:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
def latest_ids
|
|
|
|
with_redis do |redis|
|
|
|
|
redis.zrevrange(key, 0, @items_limit - 1)
|
|
|
|
end.map(&:to_i)
|
|
|
|
end
|
|
|
|
|
2020-09-18 02:09:31 -04:00
|
|
|
def with_redis(&blk)
|
|
|
|
Gitlab::Redis::SharedState.with(&blk) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
2020-10-05 11:08:56 -04:00
|
|
|
"recent_items:#{type.name.downcase}:#{user.id}"
|
2020-09-18 02:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def finder
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|