mirror of
https://github.com/SrIzan10/hc-harbor.git
synced 2026-05-01 10:45:21 +00:00
Bring timezone support to user model
This commit is contained in:
@@ -4,26 +4,8 @@ class OneTime::SetUserTimezoneFromSlackJob < ApplicationJob
|
||||
def perform
|
||||
User.where.not(slack_uid: nil).find_each(batch_size: 100) do |user|
|
||||
begin
|
||||
user_response = HTTP.auth("Bearer #{user.slack_access_token}")
|
||||
.get("https://slack.com/api/users.info?user=#{user.slack_uid}")
|
||||
|
||||
user_data = JSON.parse(user_response.body.to_s)
|
||||
|
||||
next unless user_data["ok"]
|
||||
|
||||
timezone = user_data.dig("user", "tz")
|
||||
next unless timezone.present?
|
||||
|
||||
# Convert IANA timezone to ActiveSupport timezone
|
||||
begin
|
||||
tz = ActiveSupport::TimeZone.find_tzinfo(timezone)
|
||||
user.update!(
|
||||
timezone: tz.name,
|
||||
)
|
||||
Rails.logger.info "Updated timezone for user #{user.id} to #{tz.name}"
|
||||
rescue TZInfo::InvalidTimezoneIdentifier => e
|
||||
Rails.logger.error "Invalid timezone #{timezone} for user #{user.id}: #{e.message}"
|
||||
end
|
||||
user.set_timezone_from_slack
|
||||
user.save!
|
||||
rescue => e
|
||||
Rails.logger.error "Failed to update timezone for user #{user.id}: #{e.message}"
|
||||
end
|
||||
|
||||
@@ -47,6 +47,32 @@ class User < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def set_timezone_from_slack
|
||||
return unless slack_uid.present?
|
||||
|
||||
user_response = HTTP.auth("Bearer #{slack_access_token}")
|
||||
.get("https://slack.com/api/users.info?user=#{slack_uid}")
|
||||
|
||||
user_data = JSON.parse(user_response.body.to_s)
|
||||
|
||||
return unless user_data["ok"]
|
||||
|
||||
timezone_string = user_data.dig("user", "tz")
|
||||
|
||||
return unless timezone_string.present?
|
||||
|
||||
parse_and_set_timezone(timezone_string)
|
||||
end
|
||||
|
||||
def parse_and_set_timezone(timezone)
|
||||
begin
|
||||
tz = ActiveSupport::TimeZone.find_tzinfo(timezone)
|
||||
self.timezone = tz.name
|
||||
rescue TZInfo::InvalidTimezoneIdentifier => e
|
||||
Rails.logger.error "Invalid timezone #{timezone} for user #{id}: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
def admin?
|
||||
is_admin
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user