gitlab-org--gitlab-foss/app/models/user_interacted_project.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class UserInteractedProject < ApplicationRecord
extend SuppressCompositePrimaryKeyWarning
belongs_to :user
belongs_to :project
validates :project_id, presence: true
validates :user_id, presence: true
2018-02-23 14:56:43 -05:00
CACHE_EXPIRY_TIME = 1.day
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).
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
where(attributes).exists? || UserInteractedProject.insert_all([attributes], unique_by: %w(project_id user_id))
true
2018-02-23 14:56:43 -05:00
end
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)
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
end
end