2019-10-24 20:07:01 +00:00
|
|
|
class RoomChannel < ApplicationCable::Channel
|
|
|
|
def subscribed
|
2022-03-15 09:00:33 +00:00
|
|
|
# TODO: should we only do ensure stream if current account is present?
|
|
|
|
# for now going ahead with guard clauses in update_subscription and broadcast_presence
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
ensure_stream
|
|
|
|
current_user
|
|
|
|
current_account
|
|
|
|
update_subscription
|
2020-07-04 14:33:16 +00:00
|
|
|
broadcast_presence
|
2020-02-02 17:04:16 +00:00
|
|
|
end
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
def update_presence
|
|
|
|
update_subscription
|
2020-07-04 14:33:16 +00:00
|
|
|
broadcast_presence
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def broadcast_presence
|
2022-03-15 09:00:33 +00:00
|
|
|
return if @current_account.blank?
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
data = { account_id: @current_account.id, users: ::OnlineStatusTracker.get_available_users(@current_account.id) }
|
|
|
|
data[:contacts] = ::OnlineStatusTracker.get_available_contacts(@current_account.id) if @current_user.is_a? User
|
|
|
|
ActionCable.server.broadcast(@pubsub_token, { event: 'presence.update', data: data })
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_stream
|
|
|
|
@pubsub_token = params[:pubsub_token]
|
|
|
|
stream_from @pubsub_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_subscription
|
2022-03-15 09:00:33 +00:00
|
|
|
return if @current_account.blank?
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
::OnlineStatusTracker.update_presence(@current_account.id, @current_user.class.name, @current_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
@current_user ||= if params[:user_id].blank?
|
2021-11-22 18:02:17 +00:00
|
|
|
ContactInbox.find_by!(pubsub_token: @pubsub_token).contact
|
2020-07-04 06:12:47 +00:00
|
|
|
else
|
|
|
|
User.find_by!(pubsub_token: @pubsub_token, id: params[:user_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_account
|
2022-05-06 09:20:55 +00:00
|
|
|
return if current_user.blank?
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
@current_account ||= if @current_user.is_a? Contact
|
|
|
|
@current_user.account
|
|
|
|
else
|
|
|
|
@current_user.accounts.find(params[:account_id])
|
|
|
|
end
|
2019-10-24 20:07:01 +00:00
|
|
|
end
|
|
|
|
end
|