-
Notifications
You must be signed in to change notification settings - Fork 146
Auto-resync Slack details upon changes #1525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
01234b5
9eade21
7213097
d5b52ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,12 @@ def initialize(retry_after) | |
| end | ||
| end | ||
|
|
||
| class EmailConflictError < StandardError | ||
| def initialize(email) | ||
| super("Slack email is already linked to another Hackatime account: #{email}") | ||
| end | ||
| end | ||
|
|
||
| STATUS_EMOJI_BUCKETS = [ | ||
| [ 30.minutes, %w[thinking cat-on-the-laptop loading-tumbleweed rac-yap] ], | ||
| [ 1.hour, %w[working-parrot meow_code] ], | ||
|
|
@@ -49,6 +55,7 @@ def update_from_slack | |
| return unless user_data.present? | ||
|
|
||
| apply_slack_profile_attributes(user_data) | ||
| sync_slack_email(user_data.dig("profile", "email")) | ||
| self.slack_synced_at = Time.current | ||
| end | ||
|
|
||
|
|
@@ -63,6 +70,20 @@ def apply_slack_profile_attributes(slack_user) | |
| slack_user["name"].presence | ||
| end | ||
|
|
||
| def sync_slack_email(raw_email) | ||
| email = raw_email.to_s.strip.downcase.presence | ||
| return unless email | ||
|
|
||
| email_address = EmailAddress.find_by(email: email) | ||
| raise EmailConflictError, email if email_address && email_address.user_id != id | ||
|
|
||
| transaction do | ||
| email_address ||= email_addresses.create!(email: email, source: :slack) | ||
| email_addresses.source_slack.where.not(id: email_address.id).destroy_all | ||
| email_address.update!(source: :slack) unless email_address.source_slack? | ||
| end | ||
|
Comment on lines
+80
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Knowledge Base Used: Authentication: sessions, API keys, and OAuth Prompt To Fix With AIThis is a comment left during a code review.
Path: app/models/concerns/slack_integration.rb
Line: 80-84
Comment:
**Email reconciliation commits before profile**
When `user.save!` fails after a Slack email change, this inner transaction has already committed the replacement email and deleted the former email, leaving authentication records updated while the profile fields and `slack_synced_at` remain unsaved.
**Knowledge Base Used:** [Authentication: sessions, API keys, and OAuth](https://app.greptile.com/mahadk/-/custom-context/knowledge-base/hackclub/hackatime/-/docs/api-authentication.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| end | ||
|
|
||
| def update_slack_status | ||
| return unless uses_slack_status? | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the Slack email is already attached to a different Hackatime user,
email_address.userselects that user and thisdestroy_allpermanently removes their other Slack-sourced email records before replacing their Slack credentials.Knowledge Base Used: Authentication: sessions, API keys, and OAuth
Prompt To Fix With AI