From 0e435b6bcfefa68b2862056973aa5aed1c508eb9 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Fri, 25 Apr 2025 17:13:23 +0530 Subject: [PATCH] simplify family nudge email --- .../controller/email/email_notification.go | 35 ++++++++----------- server/pkg/repo/user.go | 5 +-- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/server/pkg/controller/email/email_notification.go b/server/pkg/controller/email/email_notification.go index c0ac17f645..ee70b5dcf0 100644 --- a/server/pkg/controller/email/email_notification.go +++ b/server/pkg/controller/email/email_notification.go @@ -2,9 +2,6 @@ package email import ( "fmt" - "strconv" - t "time" - "github.com/avct/uasurfer" "github.com/ente-io/museum/ente" "github.com/ente-io/museum/pkg/controller/lock" @@ -13,6 +10,7 @@ import ( "github.com/ente-io/museum/pkg/utils/time" "github.com/ente-io/stacktrace" log "github.com/sirupsen/logrus" + "strconv" ) const ( @@ -245,29 +243,24 @@ func (c *EmailNotificationController) setStorageLimitExceededMailerJobStatus(isS } func (c *EmailNotificationController) SendFamilyNudgeEmail() error { - thirtyDaysDuration := 30 * 24 * t.Hour - subscribedUsers, subUsersErr := c.UserRepo.GetSubscribedUsersWithoutFamily((30)) + subscribedUsers, subUsersErr := c.UserRepo.GetSubscribedUsersWithoutFamily(30) if subUsersErr != nil { return stacktrace.Propagate(subUsersErr, "Failed to get subscribers") } for _, user := range subscribedUsers { - creationTime := t.Unix(0, user.CreationTime) - timeSinceCreation := t.Since(creationTime) - if timeSinceCreation >= thirtyDaysDuration { - lastNudgeSent, err := c.NotificationHistoryRepo.GetLastNotificationTime(user.ID, FilesCollectedTemplateID) - if err != nil { - log.Error("Failed to set Notification History") - } + lastNudgeSent, err := c.NotificationHistoryRepo.GetLastNotificationTime(user.ID, FilesCollectedTemplateID) + if err != nil { + log.Error("Failed to set Notification History") + } - if lastNudgeSent == 0 { - go func(userEmails []string) { - err := email.SendTemplatedEmail(userEmails, "team@ente.io", "team@ente.io", FamilyNudgeSubject, FamilyNudgeEmailTemplate, nil, nil) - if err != nil { - log.Error("Failed to send family nudge email: ", err) - } - }([]string{user.Email}) - c.NotificationHistoryRepo.SetLastNotificationTimeToNow(user.ID, FamilyNudgeTemplateID) - } + if lastNudgeSent == 0 { + go func(userEmails []string) { + err := email.SendTemplatedEmail(userEmails, "team@ente.io", "team@ente.io", FamilyNudgeSubject, FamilyNudgeEmailTemplate, nil, nil) + if err != nil { + log.Error("Failed to send family nudge email: ", err) + } + }([]string{user.Email}) + c.NotificationHistoryRepo.SetLastNotificationTimeToNow(user.ID, FamilyNudgeTemplateID) } } return nil diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go index 96914123ea..a317d6f5ec 100644 --- a/server/pkg/repo/user.go +++ b/server/pkg/repo/user.go @@ -129,14 +129,15 @@ func (repo *UserRepository) GetAll(sinceTime int64, tillTime int64) ([]ente.User return users, nil } -// GetSubscribedUsers will return the list of all subscribed. +// GetSubscribedUsersWithoutFamily returns notification candidates who are on paid plan +// but not part of any family plan. func (repo *UserRepository) GetSubscribedUsersWithoutFamily(accountAgeInDays int64) ([]ente.User, error) { rows, err := repo.DB.Query(`SELECT u.user_id, u.encrypted_email, u.email_decryption_nonce, u.email_hash, s.created_at FROM subscriptions s INNER JOIN users u ON s.user_id = u.user_id WHERE u.creation_time <= $1 AND u.family_admin_id IS NULL AND s.product_id != 'free' - AND u.email_hash NOT LIKE 'deleted+'`, time.MicrosecondBeforeDays(int(accountAgeInDays))) + AND u.encrypted_email IS NOT NULL`, time.MicrosecondBeforeDays(int(accountAgeInDays))) if err != nil { return nil, stacktrace.Propagate(err, "") }