Refactoring the code for pub sub (#155)
- We were using the attribute name 'channel' to store pubsub tokens, which was confusing. - switched to faker from ffaker - spec for contact.rb
This commit is contained in:
@@ -108,9 +108,9 @@ export default {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
getChannel() {
|
||||
getPubSubToken() {
|
||||
if (this.isLoggedIn()) {
|
||||
return Cookies.getJSON('user').channel;
|
||||
return Cookies.getJSON('user').pubsub_token;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
@@ -63,7 +63,7 @@ export default {
|
||||
const pusher = new VuePusher(CONSTANTS.PUSHER.token, options);
|
||||
// Add to global Obj
|
||||
if (AuthAPI.isLoggedIn()) {
|
||||
pusher.subscribe(AuthAPI.getChannel());
|
||||
pusher.subscribe(AuthAPI.getPubSubToken());
|
||||
return pusher.pusher;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -3,45 +3,45 @@ class PusherListener < BaseListener
|
||||
|
||||
def conversation_created(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
Pusher.trigger(members, CONVERSATION_CREATED , conversation.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
def conversation_read(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
Pusher.trigger(members, CONVERSATION_READ , conversation.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
def message_created(event)
|
||||
message, account, timestamp = extract_message_and_account(event)
|
||||
conversation = message.conversation
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
|
||||
Pusher.trigger(members, MESSAGE_CREATED , message.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
def conversation_reopened(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
Pusher.trigger(members, CONVERSATION_REOPENED, conversation.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
def conversation_lock_toggle(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
Pusher.trigger(members, CONVERSATION_LOCK_TOGGLE, conversation.lock_event_data) if members.present?
|
||||
end
|
||||
|
||||
def assignee_changed(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
members = conversation.inbox.members.pluck(:pubsub_token)
|
||||
Pusher.trigger(members, ASSIGNEE_CHANGED, conversation.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def push(channel, data)
|
||||
def push(pubsub_token, data)
|
||||
# Enqueue sidekiq job to push event to corresponding channel
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
class Contact < ApplicationRecord
|
||||
|
||||
|
||||
#Used by the pusher/PubSub Service we use for real time communications
|
||||
has_secure_token :pubsub_token
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
|
||||
@@ -8,21 +10,13 @@ class Contact < ApplicationRecord
|
||||
has_many :conversations, dependent: :destroy, foreign_key: :sender_id
|
||||
mount_uploader :avatar, AvatarUploader
|
||||
|
||||
before_create :set_channel
|
||||
|
||||
def push_event_data
|
||||
{
|
||||
id: id,
|
||||
name: name,
|
||||
thumbnail: avatar.thumb.url,
|
||||
channel: inbox.try(:channel).try(:name),
|
||||
chat_channel: chat_channel
|
||||
pubsub_token: pubsub_token
|
||||
}
|
||||
end
|
||||
|
||||
def set_channel
|
||||
begin
|
||||
self.chat_channel = SecureRandom.hex
|
||||
end while self.class.exists?(chat_channel: chat_channel)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,6 +11,9 @@ class User < ApplicationRecord
|
||||
:validatable,
|
||||
:confirmable
|
||||
|
||||
#Used by the pusher/PubSub Service we use for real time communications
|
||||
has_secure_token :pubsub_token
|
||||
|
||||
validates_uniqueness_of :email, scope: :account_id
|
||||
validates :email, presence: true
|
||||
validates :name, presence: true
|
||||
@@ -26,7 +29,6 @@ class User < ApplicationRecord
|
||||
has_many :assigned_inboxes, through: :inbox_members, source: :inbox
|
||||
has_many :messages
|
||||
|
||||
before_create :set_channel
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
accepts_nested_attributes_for :account
|
||||
@@ -42,12 +44,6 @@ class User < ApplicationRecord
|
||||
super(options).merge(confirmed: confirmed?, subscription: account.try(:subscription).try(:summary) )
|
||||
end
|
||||
|
||||
def set_channel
|
||||
begin
|
||||
self.channel = SecureRandom.hex
|
||||
end while self.class.exists?(channel: channel)
|
||||
end
|
||||
|
||||
def notify_creation
|
||||
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: self.account)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user