Skip to content

Commit

Permalink
Channel created timestamp is in list results, so dropped extra API query
Browse files Browse the repository at this point in the history
  • Loading branch information
jshiell committed Oct 4, 2018
1 parent fc7e1ab commit 91b8441
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,50 @@ def initialize(api_token)
@client.auth_test
end

def archive_channels_unused_for_days(number_of_days)
def archive_channels_inactive_for(number_of_days)
unarchived_public_channels = @client.channels_list(exclude_archived: true).channels

archived_count = 0

unarchived_public_channels.each do |current_channel|
channel_info = @client.channels_info(channel: current_channel.id)
channel_created = Time.at(channel_info.channel.created)
if days_ago(channel_created) > number_of_days
last_messages = @client.channels_history(channel: current_channel.id, count: 1)
if last_messages.messages.empty?
puts "! Channel #{current_channel.name} is older than #{number_of_days} and has no messages"
# @client.chat_postMessage(channel: current_channel.id, text: "This channel has had no new messages in #{number_of_days} and will hence be archived - any queries, please see #slack-admin")
# @client.channels_archive(channel: current_channel.id)
archived_count += 1

elsif days_ago(last_messages.messages.first.ts.to_d) > number_of_days
puts "! Channel #{current_channel.name} is older than #{number_of_days} days and has had no messages in at least #{number_of_days} days"
# @client.chat_postMessage(channel: current_channel.id, text: "This channel is older than #{number_of_days} days and has no messages, and will hence be archived - any queries, please see #slack-admin")
# @client.channels_archive(channel: current_channel.id)
archived_count += 1
else
puts "o Channel #{current_channel.name} is in regular use"
end
else
puts "o Channel #{current_channel.name} is newer than #{number_of_days} days"
end
archived_count += archive_channel_if_inactive_for(number_of_days, current_channel)

# channels_history & channels_info are tier 3 methods, so we needs to limit acess to ~50 a second - https://api.slack.com/docs/rate-limits#tier_t3
# channels_history is a tier 3 method, so we needs to limit acess to ~50 a minute - https://api.slack.com/docs/rate-limits#tier_t3
sleep(1)
end

archived_count
end

private

def archive_channel_if_inactive_for(number_of_days, channel)
archived_count = 0

if days_ago(Time.at(channel.created)) > number_of_days
last_messages = @client.channels_history(channel: channel.id, count: 1)
if last_messages.messages.empty?
puts "! Channel #{channel.name} is older than #{number_of_days} and has no messages"
# @client.chat_postMessage(channel: channel.id, text: "This channel has had no new messages in #{number_of_days} and will hence be archived - any queries, please see #slack-admin")
# @client.channels_archive(channel: channel.id)
archived_count = 1

elsif days_ago(last_messages.messages.first.ts.to_d) > number_of_days
puts "! Channel #{channel.name} is older than #{number_of_days} days and has had no messages in at least #{number_of_days} days"
# @client.chat_postMessage(channel: channel.id, text: "This channel is older than #{number_of_days} days and has no messages, and will hence be archived - any queries, please see #slack-admin")
# @client.channels_archive(channel: channel.id)
archived_count = 1

else
puts "o Channel #{channel.name} is in regular use"
end
else
puts "o Channel #{channel.name} is newer than #{number_of_days} days"
end

archived_count
end

def days_ago(time)
(Time.now - time).to_i / 86400
end
Expand All @@ -67,7 +76,7 @@ def initialize(args)
puts "Please note only one channel will be checked per second due to Slack API rate limits"

slack_channel_archiver = SlackChannelArchiver.new(api_token)
archived_count = slack_channel_archiver.archive_channels_unused_for_days(number_of_days)
archived_count = slack_channel_archiver.archive_channels_inactive_for(number_of_days)

puts "#{archived_count} channels were archived"
end
Expand Down

0 comments on commit 91b8441

Please sign in to comment.