2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-03 14:00:05 -05:00
|
|
|
class UserInteractedProject < ActiveRecord::Base
|
2018-02-23 14:33:16 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
|
2018-03-05 10:50:05 -05:00
|
|
|
validates :project_id, presence: true
|
|
|
|
validates :user_id, presence: true
|
2018-02-23 14:33:16 -05:00
|
|
|
|
2018-02-23 14:56:43 -05:00
|
|
|
CACHE_EXPIRY_TIME = 1.day
|
|
|
|
|
2018-02-26 10:44:35 -05:00
|
|
|
# Schema version required for this model
|
|
|
|
REQUIRED_SCHEMA_VERSION = 20180223120443
|
|
|
|
|
2018-02-26 06:24:02 -05:00
|
|
|
class << self
|
|
|
|
def track(event)
|
|
|
|
# For events without a project, we simply don't care.
|
|
|
|
# An example of this is the creation of a snippet (which
|
|
|
|
# is not related to any project).
|
2018-03-05 10:50:05 -05:00
|
|
|
return unless event.project_id
|
2018-02-26 06:24:02 -05:00
|
|
|
|
|
|
|
attributes = {
|
|
|
|
project_id: event.project_id,
|
|
|
|
user_id: event.author_id
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_exists?(attributes) do
|
2018-03-05 11:12:22 -05:00
|
|
|
transaction(requires_new: true) do
|
|
|
|
begin
|
|
|
|
where(attributes).select(1).first || create!(attributes)
|
|
|
|
true # not caching the whole record here for now
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# Note, above queries are not atomic and prone
|
|
|
|
# to race conditions (similar like #find_or_create!).
|
2018-03-06 06:45:28 -05:00
|
|
|
# In the case where we hit this, the record we want
|
|
|
|
# already exists - shortcut and return.
|
|
|
|
true
|
2018-03-05 11:12:22 -05:00
|
|
|
end
|
2018-02-26 06:24:02 -05:00
|
|
|
end
|
2018-02-23 14:56:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-26 10:44:35 -05:00
|
|
|
# Check if we can safely call .track (table exists)
|
|
|
|
def available?
|
|
|
|
@available_flag ||= ActiveRecord::Migrator.current_version >= REQUIRED_SCHEMA_VERSION # rubocop:disable Gitlab/PredicateMemoization
|
|
|
|
end
|
|
|
|
|
2018-02-26 13:39:08 -05:00
|
|
|
# Flushes cached information about schema
|
|
|
|
def reset_column_information
|
|
|
|
@available_flag = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-02-26 06:24:02 -05:00
|
|
|
private
|
2018-02-23 14:56:43 -05:00
|
|
|
|
2018-02-26 06:24:02 -05:00
|
|
|
def cached_exists?(project_id:, user_id:, &block)
|
2018-03-01 09:42:04 -05:00
|
|
|
cache_key = "user_interacted_projects:#{project_id}:#{user_id}"
|
2018-02-26 06:24:02 -05:00
|
|
|
Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRY_TIME, &block)
|
|
|
|
end
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
|
|
|
end
|