2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class ChatName < ApplicationRecord
|
2018-02-22 12:34:04 -05:00
|
|
|
LAST_USED_AT_INTERVAL = 1.hour
|
|
|
|
|
2022-08-12 23:09:28 -04:00
|
|
|
belongs_to :integration
|
2016-11-13 14:35:47 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user, presence: true
|
2021-05-12 08:10:24 -04:00
|
|
|
validates :integration, presence: true
|
2016-11-13 14:35:47 -05:00
|
|
|
validates :team_id, presence: true
|
|
|
|
validates :chat_id, presence: true
|
|
|
|
|
2022-08-12 23:09:28 -04:00
|
|
|
validates :user_id, uniqueness: { scope: [:integration_id] }
|
|
|
|
validates :chat_id, uniqueness: { scope: [:integration_id, :team_id] }
|
2018-02-22 12:34:04 -05:00
|
|
|
|
|
|
|
# Updates the "last_used_timestamp" but only if it wasn't already updated
|
|
|
|
# recently.
|
|
|
|
#
|
|
|
|
# The throttling this method uses is put in place to ensure that high chat
|
|
|
|
# traffic doesn't result in many UPDATE queries being performed.
|
|
|
|
def update_last_used_at
|
|
|
|
return unless update_last_used_at?
|
|
|
|
|
|
|
|
obtained = Gitlab::ExclusiveLease
|
|
|
|
.new("chat_name/last_used_at/#{id}", timeout: LAST_USED_AT_INTERVAL.to_i)
|
|
|
|
.try_obtain
|
|
|
|
|
|
|
|
touch(:last_used_at) if obtained
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_last_used_at?
|
|
|
|
last_used_at.nil? || last_used_at > LAST_USED_AT_INTERVAL.ago
|
|
|
|
end
|
2016-11-13 14:35:47 -05:00
|
|
|
end
|