Feature: Availability Statuses (#874)
Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
module AvailabilityStatusable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def online?
|
||||
::OnlineStatusTracker.subscription_count(pubsub_token) != 0
|
||||
def online_presence?
|
||||
::OnlineStatusTracker.get_presence(availability_account_id, self.class.name, id)
|
||||
end
|
||||
|
||||
def availability_status
|
||||
online? ? 'online' : 'offline'
|
||||
return 'offline' unless online_presence?
|
||||
return 'online' if is_a? Contact
|
||||
|
||||
::OnlineStatusTracker.get_status(availability_account_id, id) || 'online'
|
||||
end
|
||||
|
||||
def availability_account_id
|
||||
return account_id if is_a? Contact
|
||||
|
||||
Current.account.present? ? Current.account.id : accounts.first.id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
# Table name: users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# availability :integer default(0)
|
||||
# confirmation_sent_at :datetime
|
||||
# confirmation_token :string
|
||||
# confirmed_at :datetime
|
||||
@@ -53,6 +54,8 @@ class User < ApplicationRecord
|
||||
:validatable,
|
||||
:confirmable
|
||||
|
||||
enum availability: { online: 0, offline: 1, busy: 2 }
|
||||
|
||||
# The validation below has been commented out as it does not
|
||||
# work because :validatable in devise overrides this.
|
||||
# validates_uniqueness_of :email, scope: :account_id
|
||||
@@ -75,6 +78,7 @@ class User < ApplicationRecord
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
after_create :create_access_token
|
||||
after_save :update_presence_in_redis, if: :saved_change_to_availability?
|
||||
|
||||
def send_devise_notification(notification, *args)
|
||||
devise_mailer.send(notification, self, *args).deliver_later
|
||||
@@ -126,7 +130,8 @@ class User < ApplicationRecord
|
||||
id: id,
|
||||
name: name,
|
||||
avatar_url: avatar_url,
|
||||
type: 'user'
|
||||
type: 'user',
|
||||
availability_status: availability_status
|
||||
}
|
||||
end
|
||||
|
||||
@@ -138,4 +143,12 @@ class User < ApplicationRecord
|
||||
type: 'user'
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_presence_in_redis
|
||||
accounts.each do |account|
|
||||
OnlineStatusTracker.set_status(account.id, id, availability)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user