From f609cef79e2e040c4c1e9ec929c4b1be1b5a1a29 Mon Sep 17 00:00:00 2001 From: mngshm Date: Wed, 23 Apr 2025 17:16:40 +0530 Subject: [PATCH] modify code to pass timeDuration as parameter to get list of users previously all users without families were fetched and later 30 days check was being done. With this modification, in the future custom duration can be passed and depending on that one could get the list of users. The compiler decides if more time has passed dependeing on what is the underlying nanoseconds value of time.Time value. Older dates are less in size compared to Newer Dates. Hence, s.created_at >= is an appropriate calculation --- .../controller/email/email_notification.go | 19 ++++++++++--------- server/pkg/repo/user.go | 11 +++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/server/pkg/controller/email/email_notification.go b/server/pkg/controller/email/email_notification.go index bac77d0274..8a5e7ef8a8 100644 --- a/server/pkg/controller/email/email_notification.go +++ b/server/pkg/controller/email/email_notification.go @@ -46,7 +46,7 @@ const ( LoginSuccessSubject = "New login to your Ente account" LoginSuccessTemplate = "on_login.html" - FamilyNudgeEmailTemplate = "family_nudge.html" + FamilyNudgeEmailTemplate = "nudge_for_family.html" FamilyNudgeSubject = "Share your Ente Subscription with your Family!" FamilyNudgeTemplateID = "family_nudge" ) @@ -193,7 +193,7 @@ func (c *EmailNotificationController) SendStorageAlerts() { getListofSubscribers func() ([]ente.User, error) template string subject string - notifID string + notifID string }{ { getListofSubscribers: func() ([]ente.User, error) { @@ -201,7 +201,7 @@ func (c *EmailNotificationController) SendStorageAlerts() { }, template: StorageLimitExceedingTemplate, subject: StorageLimitExceedingSubject, - notifID: StorageLimitExceedingID, + notifID: StorageLimitExceedingID, }, { getListofSubscribers: func() ([]ente.User, error) { @@ -209,7 +209,7 @@ func (c *EmailNotificationController) SendStorageAlerts() { }, template: StorageLimitExceededTemplate, subject: StorageLimitExceededSubject, - notifID: StorageLimitExceededTemplateID, + notifID: StorageLimitExceededTemplateID, }, } for _, alertGroup := range storageAlertGroups { @@ -245,11 +245,14 @@ func (c *EmailNotificationController) setStorageLimitExceededMailerJobStatus(isS } func (c *EmailNotificationController) SendFamilyNudgeEmail() error { - subscribedUsers, subUsersErr := c.UserRepo.GetSubscribedUsersWithoutFamily() + thirtyDaysDuration := 30 * 24 * t.Hour + thirtyDaysAgo := t.Now().Add(-thirtyDaysDuration) + formattedTime := thirtyDaysAgo.Format("2006-01-02 15:04:05.999999") + + subscribedUsers, subUsersErr := c.UserRepo.GetSubscribedUsersWithoutFamily(formattedTime) if subUsersErr != nil { return stacktrace.Propagate(subUsersErr, "Failed to get subscribers") } - batchSize := 100 totalSubUsers := len(subscribedUsers) @@ -268,11 +271,9 @@ func (c *EmailNotificationController) SendFamilyNudgeEmail() error { break } - thirtyDays := 30 * 24 * t.Hour creationTime := t.Unix(0, user.CreationTime) timeSinceCreation := t.Since(creationTime) - - if timeSinceCreation >= thirtyDays { + if timeSinceCreation >= thirtyDaysDuration { lastNudgeSent, err := c.NotificationHistoryRepo.GetLastNotificationTime(user.ID, FilesCollectedTemplateID) if err != nil { log.Error("Failed to set Notification History") diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go index 639f2921dc..4b282db6de 100644 --- a/server/pkg/repo/user.go +++ b/server/pkg/repo/user.go @@ -130,10 +130,13 @@ func (repo *UserRepository) GetAll(sinceTime int64, tillTime int64) ([]ente.User } // GetSubscribedUsers will return the list of all subscribed. -func (repo *UserRepository) GetSubscribedUsersWithoutFamily() ([]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.family_admin_id IS NULL AND s.product_id != 'free'`) +func (repo *UserRepository) GetSubscribedUsersWithoutFamily(duration string) ([]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 s.created_at >= $1 + AND u.family_admin_id IS NULL + AND s.product_id != 'free'`, duration) if err != nil { return nil, stacktrace.Propagate(err, "") }