From 6e50eda1fe628337924cf7a372cef6c0cb71406b Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Tue, 11 Mar 2025 22:05:10 -0400 Subject: [PATCH] Add slack channel name to sailors log prefs --- .../update_slack_channel_cache_job.rb | 14 ++++++++++++++ app/views/shared/_slack_channel_mention.erb | 1 + app/views/users/edit.html.erb | 2 +- config/initializers/good_job.rb | 4 ++++ lib/slack_channel.rb | 12 ++++++++++++ slack_manifest_sailors_log.yml | 4 ++++ 6 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/jobs/slack_command/update_slack_channel_cache_job.rb create mode 100644 app/views/shared/_slack_channel_mention.erb create mode 100644 lib/slack_channel.rb diff --git a/app/jobs/slack_command/update_slack_channel_cache_job.rb b/app/jobs/slack_command/update_slack_channel_cache_job.rb new file mode 100644 index 0000000..a7a54ca --- /dev/null +++ b/app/jobs/slack_command/update_slack_channel_cache_job.rb @@ -0,0 +1,14 @@ +class SlackCommand::UpdateSlackChannelCacheJob < ApplicationJob + queue_as :default + + def perform + channels = SailorsLogNotificationPreference.where(enabled: true).distinct.pluck(:slack_channel_id) + + Rails.logger.info("Updating slack channel cache for #{channels.count} channels") + channels.each do |channel_id| + sleep 2 # slack rate limit is 50 per minute + Rails.logger.info("Updating slack channel cache for #{channel_id}") + SlackChannel.find_by_id(channel_id, force_refresh: true) + end + end +end diff --git a/app/views/shared/_slack_channel_mention.erb b/app/views/shared/_slack_channel_mention.erb new file mode 100644 index 0000000..4cc56a0 --- /dev/null +++ b/app/views/shared/_slack_channel_mention.erb @@ -0,0 +1 @@ +<%= link_to SlackChannel.find_by_id(channel_id), "https://slack.com/app_redirect?channel=#{channel_id}", target: "_blank" %> \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 74d9296..4acdc89 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -31,7 +31,7 @@ diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb index 1cfbaea..02d8b6a 100644 --- a/config/initializers/good_job.rb +++ b/config/initializers/good_job.rb @@ -15,6 +15,10 @@ Rails.application.configure do sailors_log_poll: { cron: "* * * * *", class: "SailorsLogPollForChangesJob" + }, + update_slack_channel_cache: { + cron: "0 11 * * *", + class: "SlackCommand::UpdateSlackChannelCacheJob" } } end diff --git a/lib/slack_channel.rb b/lib/slack_channel.rb new file mode 100644 index 0000000..90f96fb --- /dev/null +++ b/lib/slack_channel.rb @@ -0,0 +1,12 @@ +class SlackChannel + def self.find_by_id(id, force_refresh: false) + cached_name = Rails.cache.fetch("slack_channel_#{id}", expires_in: 1.week, force: force_refresh) do + response = HTTP.headers(Authorization: "Bearer #{ENV.fetch("SAILORS_LOG_SLACK_BOT_OAUTH_TOKEN")}").get("https://slack.com/api/conversations.info?channel=#{id}") + data = JSON.parse(response.body) + + data.dig("channel", "name") + end + + cached_name + end +end diff --git a/slack_manifest_sailors_log.yml b/slack_manifest_sailors_log.yml index 9a60b8a..c94c1e9 100644 --- a/slack_manifest_sailors_log.yml +++ b/slack_manifest_sailors_log.yml @@ -20,6 +20,10 @@ oauth_config: - chat:write.public - commands - users:read + - channels:read + - groups:read + - mpim:read + - im:read settings: org_deploy_enabled: false socket_mode_enabled: false