Chore: Convert Message Sender to polymorphic (#740)

Fixes #680
This commit is contained in:
Sojan Jose
2020-06-27 21:34:53 +05:30
committed by GitHub
parent 4f83d5451e
commit cc02611007
27 changed files with 71 additions and 49 deletions

View File

@@ -29,7 +29,7 @@ class ContactMergeAction
end
def merge_messages
Message.where(contact_id: @mergee_contact.id).update(contact_id: @base_contact.id)
Message.where(sender: @mergee_contact).update(sender: @base_contact)
end
def merge_contact_inboxes

View File

@@ -117,7 +117,8 @@ class Messages::MessageBuilder
inbox_id: conversation.inbox_id,
message_type: @message_type,
content: response.content,
source_id: response.identifier
source_id: response.identifier,
sender: contact
}
end

View File

@@ -37,7 +37,7 @@ class Messages::Outgoing::NormalBuilder
message_type: :outgoing,
content: @content,
private: @private,
user_id: @user&.id,
sender: @user,
source_id: @fb_id,
content_type: @content_type,
items: @items

View File

@@ -4,7 +4,8 @@ class Api::V1::Accounts::Conversations::MessagesController < Api::V1::Accounts::
end
def create
mb = Messages::Outgoing::NormalBuilder.new(current_user, @conversation, params)
user = current_user || @resource
mb = Messages::Outgoing::NormalBuilder.new(user, @conversation, params)
@message = mb.perform
end

View File

@@ -45,7 +45,7 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
def message_params
{
account_id: conversation.account_id,
contact_id: @contact.id,
sender: @contact,
content: permitted_params[:message][:content],
inbox_id: conversation.inbox_id,
message_type: :incoming

View File

@@ -20,7 +20,7 @@ class ConversationMailbox < ApplicationMailbox
def create_message
@message = @conversation.messages.create(
account_id: @conversation.account_id,
contact_id: @conversation.contact_id,
sender: @conversation.contact,
content: processed_mail.text_content[:reply],
inbox_id: @conversation.inbox_id,
message_type: 'incoming',

View File

@@ -17,12 +17,13 @@ class AgentBot < ApplicationRecord
has_many :agent_bot_inboxes, dependent: :destroy
has_many :inboxes, through: :agent_bot_inboxes
has_many :messages, as: :sender, dependent: :restrict_with_exception
def push_event_data
def push_event_data(inbox = nil)
{
id: id,
name: name,
avatar_url: avatar_url,
avatar_url: avatar_url || inbox&.avatar_url,
type: 'agent_bot'
}
end

View File

@@ -35,7 +35,7 @@ class Contact < ApplicationRecord
has_many :conversations, dependent: :destroy
has_many :contact_inboxes, dependent: :destroy
has_many :inboxes, through: :contact_inboxes
has_many :messages, dependent: :destroy
has_many :messages, as: :sender, dependent: :destroy
before_validation :downcase_email
after_create_commit :dispatch_create_event

View File

@@ -8,28 +8,23 @@
# content_type :integer default("text")
# message_type :integer not null
# private :boolean default(FALSE)
# sender_type :string
# status :integer default("sent")
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# contact_id :bigint
# conversation_id :integer not null
# inbox_id :integer not null
# sender_id :bigint
# source_id :string
# user_id :integer
#
# Indexes
#
# index_messages_on_account_id (account_id)
# index_messages_on_contact_id (contact_id)
# index_messages_on_conversation_id (conversation_id)
# index_messages_on_inbox_id (inbox_id)
# index_messages_on_source_id (source_id)
# index_messages_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (contact_id => contacts.id)
# index_messages_on_account_id (account_id)
# index_messages_on_conversation_id (conversation_id)
# index_messages_on_inbox_id (inbox_id)
# index_messages_on_sender_type_and_sender_id (sender_type,sender_id)
# index_messages_on_source_id (source_id)
#
class Message < ApplicationRecord
@@ -65,8 +60,11 @@ class Message < ApplicationRecord
belongs_to :account
belongs_to :inbox
belongs_to :conversation, touch: true
# FIXME: phase out user and contact after 1.4 since the info is there in sender
belongs_to :user, required: false
belongs_to :contact, required: false
belongs_to :sender, polymorphic: true, required: false
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
@@ -88,7 +86,8 @@ class Message < ApplicationRecord
conversation_id: conversation.display_id
)
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
data.merge!(sender: user.push_event_data) if user
data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot)
data.merge!(sender: sender.push_event_data(inbox)) if sender&.is_a?(AgentBot)
data
end
@@ -105,8 +104,7 @@ class Message < ApplicationRecord
content_type: content_type,
content_attributes: content_attributes,
source_id: source_id,
sender: user.try(:webhook_data),
contact: contact.try(:webhook_data),
sender: sender.try(:webhook_data),
inbox: inbox.webhook_data,
conversation: conversation.webhook_data,
account: account.webhook_data

View File

@@ -65,7 +65,7 @@ class User < ApplicationRecord
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify
has_many :inbox_members, dependent: :destroy
has_many :inboxes, through: :inbox_members, source: :inbox
has_many :messages
has_many :messages, as: :sender
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
has_many :notifications, dependent: :destroy

View File

@@ -11,7 +11,7 @@ class Twilio::IncomingMessageService
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
contact_id: @contact.id,
sender: @contact,
source_id: params[:SmsSid]
)
attach_files

View File

@@ -12,7 +12,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: outgoing_message? ? :outgoing : :incoming,
contact_id: @contact.id,
sender: @contact,
source_id: direct_message_data['id']
)
end

View File

@@ -73,7 +73,7 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
set_conversation
@conversation.messages.create(
account_id: @inbox.account_id,
contact_id: @contact.id,
sender: @contact,
content: tweet_text,
inbox_id: @inbox.id,
message_type: message_type,

View File

@@ -7,4 +7,5 @@ json.content_type @message.content_type
json.content_attributes @message.content_attributes
json.created_at @message.created_at.to_i
json.private @message.private
json.sender @message.sender.push_event_data
json.attachments @message.attachments.map(&:push_event_data) if @message.attachments.present?

View File

@@ -17,6 +17,6 @@ json.payload do
json.private message.private
json.source_id message.source_id
json.attachments message.attachments.map(&:push_event_data) if message.attachments.present?
json.sender message.user.push_event_data if message.user
json.sender message.sender.push_event_data if message.sender
end
end

View File

@@ -7,4 +7,4 @@ json.created_at @message.created_at.to_i
json.private @message.private
json.source_id @message.source_id
json.attachments @message.attachments.map(&:push_event_data) if @message.attachments.present?
json.sender @message.user.push_event_data if @message.user
json.sender @message.sender.push_event_data if @message.sender

View File

@@ -7,5 +7,5 @@ json.array! @messages do |message|
json.created_at message.created_at.to_i
json.conversation_id message.conversation.display_id
json.attachments message.attachments.map(&:push_event_data) if message.attachments.present?
json.sender message.user.push_event_data if message.user
json.sender message.sender.push_event_data if message.sender
end