summaryrefslogtreecommitdiff
path: root/app/models/chat_name.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-02-22 18:34:04 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2018-02-23 14:37:53 +0100
commit57719d34d3fcc15f39354b0e9dc1da41bbe5d1a8 (patch)
tree255d9a3df371793748c64792a715102dca98512c /app/models/chat_name.rb
parentc679fa163148601e77803f45cf5dea8e1b3feb0a (diff)
downloadgitlab-ce-57719d34d3fcc15f39354b0e9dc1da41bbe5d1a8.tar.gz
Expose ChatName objects to slash commandsslash-commands-changes-for-chatops
Instead of only exposing a User to slash commands we now also expose the ChatName object that the User object is retrieved from. This is necessary for GitLab Chatops as we need for example the user ID of the chat user.
Diffstat (limited to 'app/models/chat_name.rb')
-rw-r--r--app/models/chat_name.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/app/models/chat_name.rb b/app/models/chat_name.rb
index f321db75eeb..fbd0f123341 100644
--- a/app/models/chat_name.rb
+++ b/app/models/chat_name.rb
@@ -1,4 +1,6 @@
class ChatName < ActiveRecord::Base
+ LAST_USED_AT_INTERVAL = 1.hour
+
belongs_to :service
belongs_to :user
@@ -9,4 +11,23 @@ class ChatName < ActiveRecord::Base
validates :user_id, uniqueness: { scope: [:service_id] }
validates :chat_id, uniqueness: { scope: [:service_id, :team_id] }
+
+ # 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
end