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 @@
<% @enabled_sailors_logs.each do |sl| %>
-
- <%= sl.slack_channel_id %>
+ <%= render "shared/slack_channel_mention", channel_id: sl.slack_channel_id %>
<% end %>
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