-
Notifications
You must be signed in to change notification settings - Fork 161
Add goal completion notifications #1613
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
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class GoalCompletionCheckJob < ApplicationJob | ||
| queue_as :latency_10s | ||
|
|
||
| include GoodJob::ActiveJobExtensions::Concurrency | ||
|
|
||
| def self.schedule_for(user_id) | ||
| goals = Goal.where(user_id: user_id) | ||
| return unless goals.where(notify_by_email: true).or(goals.where(notify_by_slack: true)).exists? | ||
|
|
||
| perform_later(user_id) | ||
| end | ||
|
|
||
| good_job_control_concurrency_with( | ||
| total_limit: 1, key: -> { "goal_completion_check_job_#{arguments.first}" } | ||
| ) | ||
|
|
||
| def perform(user_id) | ||
| user = User.find_by(id: user_id) | ||
| return if user.nil? || user.pending_deletion? | ||
|
|
||
| goals = user.goals.where(notify_by_email: true).or(user.goals.where(notify_by_slack: true)) | ||
| return if goals.empty? | ||
|
|
||
| ProgrammingGoalsProgressService.new(user: user, goals: goals).call.each do |progress| | ||
| next unless progress[:complete] | ||
|
|
||
| notification = GoalCompletionNotification.create_or_find_by!( | ||
| goal_id: progress[:id], | ||
| period: progress[:period], | ||
| period_started_at: Time.zone.parse(progress[:period_start]) | ||
| ) do |record| | ||
| record.target_seconds = progress[:target_seconds] | ||
| record.tracked_seconds = progress[:tracked_seconds] | ||
| record.languages = progress[:languages] | ||
| record.projects = progress[:projects] | ||
| end | ||
|
|
||
| goal = notification.goal | ||
| GoalCompletionEmailJob.perform_later(notification.id) if goal.notify_by_email? && notification.email_delivered_at.nil? | ||
| GoalCompletionSlackJob.perform_later(notification.id) if goal.notify_by_slack? && notification.slack_delivered_at.nil? | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class GoalCompletionEmailJob < ApplicationJob | ||
| queue_as :latency_10s | ||
|
|
||
| include GoodJob::ActiveJobExtensions::Concurrency | ||
|
|
||
| good_job_control_concurrency_with( | ||
| total_limit: 1, key: -> { "goal_completion_email_job_#{arguments.first}" } | ||
| ) | ||
|
|
||
| def perform(notification_id) | ||
| notification = GoalCompletionNotification.find(notification_id) | ||
| return if notification.email_delivered_at.present? | ||
|
|
||
| goal = notification.goal | ||
| return unless goal.notify_by_email? | ||
|
|
||
| user = goal.user | ||
| return if user.pending_deletion? | ||
|
|
||
| recipient_email = user.email_addresses.order(:id).pick(:email) | ||
| return if recipient_email.blank? | ||
|
|
||
| GoalCompletionMailer.reached(notification, recipient_email: recipient_email).deliver_now | ||
| notification.update!(email_delivered_at: Time.current) | ||
|
Comment on lines
+23
to
+24
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 external delivery succeeds but the subsequent Context Used: AGENTS.md (source) Knowledge Base Used: Background Jobs Infrastructure (GoodJob) Prompt To Fix With AIThis is a comment left during a code review.
Path: app/jobs/goal_completion_email_job.rb
Line: 23-24
Comment:
**Delivery state permits duplicates**
When external delivery succeeds but the subsequent `delivered_at` update fails or the worker stops before it commits, a retry or later completion check sees a nil marker and sends the notification again, causing duplicate email; the Slack job has the same ordering problem.
**Context Used:** AGENTS.md ([source](https://github.com/hackclub/hackatime/blob/main/AGENTS.md))
**Knowledge Base Used:** [Background Jobs Infrastructure (GoodJob)](https://app.greptile.com/mahadk/-/custom-context/knowledge-base/hackclub/hackatime/-/docs/background-jobs-infra.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class GoalCompletionSlackJob < ApplicationJob | ||
| queue_as :latency_10s | ||
|
|
||
| include GoodJob::ActiveJobExtensions::Concurrency | ||
|
|
||
| good_job_control_concurrency_with( | ||
| total_limit: 1, key: -> { "goal_completion_slack_job_#{arguments.first}" } | ||
| ) | ||
|
|
||
| def perform(notification_id) | ||
| notification = GoalCompletionNotification.find(notification_id) | ||
| return if notification.slack_delivered_at.present? | ||
|
|
||
| goal = notification.goal | ||
| return unless goal.notify_by_slack? | ||
|
|
||
| user = goal.user | ||
| return if user.pending_deletion? || user.slack_uid.blank? | ||
|
|
||
| duration = ApplicationController.helpers.short_time_simple(notification.target_seconds) | ||
| scope = notification_scope(notification) | ||
| message = ":tada: You reached your #{notification.period} coding goal of *#{duration}*#{scope}!" | ||
| response = HTTP.auth("Bearer #{ENV['SAILORS_LOG_SLACK_BOT_OAUTH_TOKEN']}") | ||
| .post("https://slack.com/api/chat.postMessage", json: { channel: user.slack_uid, text: message }) | ||
| data = JSON.parse(response.body) | ||
| raise "Failed to send goal completion Slack notification: #{data["error"]}" unless data["ok"] | ||
|
|
||
| notification.update!(slack_delivered_at: Time.current) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def notification_scope(notification) | ||
| filters = [] | ||
| filters << "#{notification.languages.to_sentence} coding" if notification.languages.any? | ||
| filters << notification.projects.to_sentence if notification.projects.any? | ||
| filters.any? ? " for #{filters.join(" in ")}" : "" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class GoalCompletionMailer < ApplicationMailer | ||
| helper :application | ||
|
|
||
| def reached(notification, recipient_email:) | ||
| @notification = notification | ||
| @user = notification.goal.user | ||
| @target_duration = ApplicationController.helpers.short_time_simple(notification.target_seconds) | ||
| @tracked_duration = ApplicationController.helpers.short_time_simple(notification.tracked_seconds) | ||
| @goals_url = my_settings_goals_url | ||
|
|
||
| mail( | ||
| to: recipient_email, | ||
| subject: "You reached your #{notification.period} Hackatime goal!" | ||
| ) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class GoalCompletionNotification < ApplicationRecord | ||
| belongs_to :goal | ||
|
|
||
| validates :period, inclusion: { in: Goal::PERIODS } | ||
| validates :period_started_at, presence: true | ||
| validates :target_seconds, :tracked_seconds, | ||
| numericality: { only_integer: true, greater_than_or_equal_to: 0 } | ||
| end |
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 heartbeat processing overlaps for one user, the per-user
total_limitdiscards later checks while the accepted check can run before the threshold-crossing heartbeat is visible, causing the completed goal to receive no notification until another heartbeat schedules a check.Context Used: AGENTS.md (source)
Knowledge Base Used:
Prompt To Fix With AI