more code cleanups

This commit is contained in:
Muhsin Keloth
2023-09-14 16:21:04 +05:30
parent 5ee21bb9e9
commit 93bbe8d928
3 changed files with 78 additions and 68 deletions
@@ -66,4 +66,27 @@ module SlackMessageCreation
:file
end
end
def conversation
@conversation ||= Conversation.where(identifier: params[:event][:thread_ts]).first
end
def sender
user_email = slack_client.users_info(user: params[:event][:user])[:user][:profile][:email]
conversation.account.users.find_by(email: user_email)
end
def private_note?
params[:event][:text].strip.downcase.starts_with?('note:', 'private:')
end
def extract_conversation_id(url)
conversation_id_regex = %r{/conversations/(\d+)}
match_data = url.match(conversation_id_regex)
match_data[1] if match_data
end
def find_conversation_by_id(conversation_id)
Conversation.find_by(display_id: conversation_id)
end
end
@@ -1,5 +1,7 @@
class Integrations::Slack::IncomingMessageBuilder
include SlackMessageCreation
include Integrations::Slack::LinkUnfurlHelper
attr_reader :params
SUPPORTED_EVENT_TYPES = %w[event_callback url_verification].freeze
@@ -96,61 +98,6 @@ class Integrations::Slack::IncomingMessageBuilder
)
end
def extract_conversation_id(url)
conversation_id_regex = %r{/conversations/(\d+)}
match_data = url.match(conversation_id_regex)
match_data[1] if match_data
end
def find_conversation_by_id(conversation_id)
Conversation.find_by(display_id: conversation_id)
end
def generate_unfurls(url, user_name, email, phone_number, company_name)
{
url => {
'blocks' => [
{
:type => 'section',
'fields' => [
{
'type' => 'mrkdwn',
'text' => "*Name:*\n#{user_name}"
},
{
'type' => 'mrkdwn',
'text' => "*Email:*\n#{email}"
},
{
'type' => 'mrkdwn',
'text' => "*Phone:*\n#{phone_number}"
},
{
'type' => 'mrkdwn',
'text' => "*Company:*\n#{company_name}"
}
]
},
{
'type' => 'actions',
'elements' => [
{
'type' => 'button',
'text' => {
'type' => 'plain_text',
'text' => 'Open conversation',
'emoji' => true
},
'url' => url,
'action_id' => 'button-action'
}
]
}
]
}
}
end
def message
params[:event][:blocks]&.first
end
@@ -165,19 +112,6 @@ class Integrations::Slack::IncomingMessageBuilder
@integration_hook ||= Integrations::Hook.find_by(reference_id: params[:event][:channel])
end
def conversation
@conversation ||= Conversation.where(identifier: params[:event][:thread_ts]).first
end
def sender
user_email = slack_client.users_info(user: params[:event][:user])[:user][:profile][:email]
conversation.account.users.find_by(email: user_email)
end
def private_note?
params[:event][:text].strip.downcase.starts_with?('note:', 'private:')
end
def slack_client
@slack_client ||= Slack::Web::Client.new(token: @integration_hook.access_token)
end
@@ -0,0 +1,53 @@
module Integrations::Slack::LinkUnfurlHelper
def generate_unfurls(url, user_name, email, phone_number, company_name)
{
url => {
'blocks' => user_info_blocks(user_name, email, phone_number, company_name) +
open_conversation_button(url)
}
}
end
private
def user_info_blocks(user_name, email, phone_number, company_name)
[
{
'type' => 'section',
'fields' => [
user_info_field('Name', user_name),
user_info_field('Email', email),
user_info_field('Phone', phone_number),
user_info_field('Company', company_name)
]
}
]
end
def user_info_field(label, value)
{
'type' => 'mrkdwn',
'text' => "*#{label}:*\n#{value}"
}
end
def open_conversation_button(url)
[
{
'type' => 'actions',
'elements' => [
{
'type' => 'button',
'text' => {
'type' => 'plain_text',
'text' => 'Open conversation',
'emoji' => true
},
'url' => url,
'action_id' => 'button-action'
}
]
}
]
end
end