2020-06-12 17:42:47 +00:00
|
|
|
class Integrations::Slack::ChannelBuilder
|
|
|
|
attr_reader :params, :channel
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2020-06-22 07:49:26 +00:00
|
|
|
find_or_create_channel
|
2020-06-12 17:42:47 +00:00
|
|
|
update_reference_id
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hook
|
|
|
|
@hook ||= params[:hook]
|
|
|
|
end
|
|
|
|
|
|
|
|
def slack_client
|
2020-06-25 18:05:16 +00:00
|
|
|
@slack_client ||= Slack::Web::Client.new(token: hook.access_token)
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
def find_or_create_channel
|
2021-02-10 14:52:06 +00:00
|
|
|
current_list = slack_client.conversations_list
|
|
|
|
channels = current_list.channels
|
|
|
|
while current_list.response_metadata.next_cursor.present?
|
|
|
|
current_list = slack_client.conversations_list(cursor: current_list.response_metadata.next_cursor)
|
|
|
|
channels.concat(current_list.channels)
|
|
|
|
end
|
|
|
|
existing_channel = channels.find { |channel| channel['name'] == params[:channel] }
|
2020-12-10 17:23:49 +00:00
|
|
|
@channel = existing_channel || slack_client.conversations_create(name: params[:channel])['channel']
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_reference_id
|
2020-06-25 18:05:16 +00:00
|
|
|
slack_client.conversations_join(channel: channel[:id])
|
|
|
|
@hook.update(reference_id: channel[:id])
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
end
|