Skip to content

Commit

Permalink
Load user's Google calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
hulloitskai committed Aug 11, 2023
1 parent 43053b8 commit 43251b9
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def self.from_google_auth!(auth)
end
user.attributes = info.slice("first_name", "last_name", "email").to_h
user.avatar_url = info["image"]
if (token = auth.dig("credentials", "refresh_token"))
user.google_refresh_token = token
if (refresh_token = auth.dig("credentials", "refresh_token"))
user.google_refresh_token = refresh_token
end
user.save!
user
Expand All @@ -149,12 +149,41 @@ def update_without_password(params, *options)
super(params)
end

# == Calendar
sig { params(id: String, refresh_token: String).returns(Google::Calendar) }
def self.google_calendar(id, refresh_token:)
@calendars = T.let(
@calendars,
T.nilable(T::Hash[[String, String], Google::Calendar]),
)
@calendars ||= Hash.new do |hash, key; calendar, refresh_token|
calendar, refresh_token = key
hash[key] = Google::Calendar.new(
client_id: Google.client_id!,
client_secret: Google.client_secret!,
redirect_url: "urn:ietf:wg:oauth:2.0:oob",
calendar:,
refresh_token:,
)
end
@calendars[[id, refresh_token]]
end

sig { returns(Google::Calendar) }
def google_calendar
self.class.google_calendar(email, refresh_token: google_refresh_token)
end

# == Methods
sig { returns(T::Boolean) }
def admin?
Admin.emails.include?(email) || Admin.email_domains.include?(email_domain)
end

# def calendar
# @calendar_hash = T.let(@calendar_hash, T::Hash[])
# end

# # == Devise: Callback handlers
# sig { void }
# def after_confirmation
Expand Down

0 comments on commit 43251b9

Please sign in to comment.