Contact model changes (#184)
* move source id from contacts * Fix contactInbox model name * rubocop fix * Fix rspec
This commit is contained in:
@@ -30,11 +30,14 @@ module Messages
|
||||
private
|
||||
|
||||
def contact
|
||||
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
|
||||
@contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact
|
||||
end
|
||||
|
||||
def build_contact
|
||||
@contact = @inbox.contacts.create!(contact_params) if contact.nil?
|
||||
return if contact.present?
|
||||
|
||||
@contact = Contact.create!(contact_params)
|
||||
ContactInbox.create(contact: contact, inbox: @inbox, source_id: @sender_id)
|
||||
end
|
||||
|
||||
def build_message
|
||||
@@ -120,7 +123,6 @@ module Messages
|
||||
{
|
||||
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
||||
account_id: @inbox.account_id,
|
||||
source_id: @sender_id,
|
||||
remote_avatar_url: result['profile_pic'] || nil
|
||||
}
|
||||
end
|
||||
|
||||
@@ -86,7 +86,7 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
message: '',
|
||||
private: false,
|
||||
isPrivate: false,
|
||||
showEmojiPicker: false,
|
||||
showCannedModal: false,
|
||||
};
|
||||
@@ -100,7 +100,7 @@ export default {
|
||||
},
|
||||
watch: {
|
||||
message(val) {
|
||||
if (this.private) {
|
||||
if (this.isPrivate) {
|
||||
return;
|
||||
}
|
||||
const isSlashCommand = val[0] === '/';
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
class Contact < ApplicationRecord
|
||||
include Pubsubable
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :inbox
|
||||
has_many :conversations, dependent: :destroy
|
||||
has_many :contact_inboxes, dependent: :destroy
|
||||
has_many :inboxes, through: :contact_inboxes
|
||||
mount_uploader :avatar, AvatarUploader
|
||||
|
||||
def get_source_id(inbox_id)
|
||||
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
||||
end
|
||||
|
||||
def push_event_data
|
||||
{
|
||||
id: id,
|
||||
name: name,
|
||||
thumbnail: avatar.thumb.url,
|
||||
channel: inbox.try(:channel).try(:name),
|
||||
pubsub_token: pubsub_token
|
||||
}
|
||||
end
|
||||
|
||||
8
app/models/contact_inbox.rb
Normal file
8
app/models/contact_inbox.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class ContactInbox < ApplicationRecord
|
||||
validates :inbox_id, presence: true
|
||||
validates :contact_id, presence: true
|
||||
validates :source_id, presence: true
|
||||
|
||||
belongs_to :contact
|
||||
belongs_to :inbox
|
||||
end
|
||||
@@ -6,11 +6,13 @@ class Inbox < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :channel, polymorphic: true, dependent: :destroy
|
||||
|
||||
has_many :contact_inboxes, dependent: :destroy
|
||||
has_many :contacts, through: :contact_inboxes
|
||||
|
||||
has_many :inbox_members, dependent: :destroy
|
||||
has_many :members, through: :inbox_members, source: :user
|
||||
has_many :conversations, dependent: :destroy
|
||||
has_many :messages, through: :conversations
|
||||
has_many :contacts, dependent: :destroy
|
||||
after_create :subscribe_webhook, if: :facebook?
|
||||
after_destroy :delete_round_robin_agents
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ module Facebook
|
||||
|
||||
def fb_message_params
|
||||
{
|
||||
recipient: { id: contact.source_id },
|
||||
recipient: { id: contact.get_source_id(inbox.id) },
|
||||
message: { text: message.content }
|
||||
}
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ json.data do
|
||||
json.array! @conversations do |conversation|
|
||||
json.meta do
|
||||
json.sender do
|
||||
json.id conversation.contact.source_id
|
||||
json.id conversation.contact.id
|
||||
json.name conversation.contact.name
|
||||
json.thumbnail conversation.contact.avatar.thumb.url
|
||||
json.channel conversation.inbox.try(:channel).try(:name)
|
||||
|
||||
Reference in New Issue
Block a user