Add slack channel name to sailors log prefs

This commit is contained in:
Max Wofford 2025-03-11 22:05:10 -04:00
parent f5fad7ae49
commit 6e50eda1fe
6 changed files with 36 additions and 1 deletions

View file

@ -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

View file

@ -0,0 +1 @@
<%= link_to SlackChannel.find_by_id(channel_id), "https://slack.com/app_redirect?channel=#{channel_id}", target: "_blank" %>

View file

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

View file

@ -15,6 +15,10 @@ Rails.application.configure do
sailors_log_poll: { sailors_log_poll: {
cron: "* * * * *", cron: "* * * * *",
class: "SailorsLogPollForChangesJob" class: "SailorsLogPollForChangesJob"
},
update_slack_channel_cache: {
cron: "0 11 * * *",
class: "SlackCommand::UpdateSlackChannelCacheJob"
} }
} }
end end

12
lib/slack_channel.rb Normal file
View file

@ -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

View file

@ -20,6 +20,10 @@ oauth_config:
- chat:write.public - chat:write.public
- commands - commands
- users:read - users:read
- channels:read
- groups:read
- mpim:read
- im:read
settings: settings:
org_deploy_enabled: false org_deploy_enabled: false
socket_mode_enabled: false socket_mode_enabled: false