mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-19 14:27:10 +00:00
Add slack channel name to sailors log prefs
This commit is contained in:
parent
f5fad7ae49
commit
6e50eda1fe
6 changed files with 36 additions and 1 deletions
14
app/jobs/slack_command/update_slack_channel_cache_job.rb
Normal file
14
app/jobs/slack_command/update_slack_channel_cache_job.rb
Normal 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
|
||||
1
app/views/shared/_slack_channel_mention.erb
Normal file
1
app/views/shared/_slack_channel_mention.erb
Normal file
|
|
@ -0,0 +1 @@
|
|||
<%= link_to SlackChannel.find_by_id(channel_id), "https://slack.com/app_redirect?channel=#{channel_id}", target: "_blank" %>
|
||||
|
|
@ -31,7 +31,7 @@
|
|||
<ul>
|
||||
<% @enabled_sailors_logs.each do |sl| %>
|
||||
<li>
|
||||
<%= sl.slack_channel_id %>
|
||||
<%= render "shared/slack_channel_mention", channel_id: sl.slack_channel_id %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
12
lib/slack_channel.rb
Normal file
12
lib/slack_channel.rb
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue