π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Tighten inactive user deletion summary
Neeraj Guptace7615739dd5
diff --git a/server/pkg/controller/user/inactive_user_orchestrator.go b/server/pkg/controller/user/inactive_user_orchestrator.goindex dec05aed9c..2709922f44 100644--- a/server/pkg/controller/user/inactive_user_orchestrator.go+++ b/server/pkg/controller/user/inactive_user_orchestrator.go@@ -15,7 +15,6 @@ import ("github.com/ente/museum/pkg/repo"emergencyRepo "github.com/ente/museum/pkg/repo/emergency"emailUtil "github.com/ente/museum/pkg/utils/email"- "github.com/ente/museum/pkg/utils/rollout""github.com/ente/museum/pkg/utils/time"log "github.com/sirupsen/logrus")@@ -62,9 +61,6 @@ const (inactiveUserGap1mTo7d = 23 * inactiveUserOneDayInMicroSecondsinactiveUserGap7dTo1d = 6 * inactiveUserOneDayInMicroSecondsinactiveUserGap1dToFinal = inactiveUserOneDayInMicroSeconds-- inactiveUserRolloutPercentage = 100- inactiveUserRolloutNonce = "inactive-user-deletion-v1")var inactiveUserDeletionTemplateIDs = []string{@@ -102,22 +98,20 @@ type inactivityEmailStageConfig struct {}type inactiveUserRunStats struct {- ProcessedUsers int- SentEmails int- SuccessByStage map[inactivityEmailStage]int- FailureByStage map[inactivityEmailStage]int- PreStageFailures int- SkippedRolloutDomain int- SkippedRolloutPct int+ ProcessedUsers int+ SentEmails int+ SuccessByStage map[inactivityEmailStage]int+ FailureByStage map[inactivityEmailStage]int+ PreStageFailures int+ PausedLegacyContact int}type inactiveCandidateResult struct {- UserID int64- Stage inactivityEmailStage- Sent bool- SkippedRolloutDomain bool- SkippedRolloutPct bool- Err error+ UserID int64+ Stage inactivityEmailStage+ Sent bool+ PausedLegacyContact bool+ Err error}func newInactiveUserRunStats() inactiveUserRunStats {@@ -142,26 +136,49 @@ func hasAnyStageSuccess(successByStage map[inactivityEmailStage]int) bool {return false}-func formatStageCounts(counts map[inactivityEmailStage]int) string {+func hasInactiveUserRunSummaryActivity(stats inactiveUserRunStats) bool {+ return hasAnyStageSuccess(stats.SuccessByStage) || stats.PausedLegacyContact > 0+}++func formatNonZeroStageCounts(counts map[inactivityEmailStage]int) string {parts := make([]string, 0, len(inactivityEmailStageOrder))for _, stage := range inactivityEmailStageOrder {- parts = append(parts, fmt.Sprintf("%s=%d", stage, counts[stage]))+ if counts[stage] > 0 {+ parts = append(parts, fmt.Sprintf("%s=%d", stage, counts[stage]))+ }+ }+ if len(parts) == 0 {+ return ""}- return strings.Join(parts, ", ")+ return fmt.Sprintf("{%s}", strings.Join(parts, ", "))}func buildInactiveUserRunSummary(stats inactiveUserRunStats, runAt int64) string {- return fmt.Sprintf(- "Inactive user run summary (%s): processed=%d sent=%d | success={%s} | failures={%s} | pre_stage_failures=%d | skipped_rollout_domain=%d | skipped_rollout_percentage=%d",- stdtime.UnixMicro(runAt).UTC().Format(stdtime.RFC3339),- stats.ProcessedUsers,- stats.SentEmails,- formatStageCounts(stats.SuccessByStage),- formatStageCounts(stats.FailureByStage),- stats.PreStageFailures,- stats.SkippedRolloutDomain,- stats.SkippedRolloutPct,- )+ parts := make([]string, 0, 6)+ if stats.ProcessedUsers > 0 {+ parts = append(parts, fmt.Sprintf("processed=%d", stats.ProcessedUsers))+ }+ if stats.SentEmails > 0 {+ parts = append(parts, fmt.Sprintf("sent=%d", stats.SentEmails))+ }+ if success := formatNonZeroStageCounts(stats.SuccessByStage); success != "" {+ parts = append(parts, fmt.Sprintf("success=%s", success))+ }+ if failures := formatNonZeroStageCounts(stats.FailureByStage); failures != "" {+ parts = append(parts, fmt.Sprintf("failures=%s", failures))+ }+ if stats.PreStageFailures > 0 {+ parts = append(parts, fmt.Sprintf("pre_stage_failures=%d", stats.PreStageFailures))+ }+ if stats.PausedLegacyContact > 0 {+ parts = append(parts, fmt.Sprintf("paused_legacy_contact=%d", stats.PausedLegacyContact))+ }++ summary := fmt.Sprintf("Inactive user run summary (%s)", stdtime.UnixMicro(runAt).UTC().Format(stdtime.RFC3339))+ if len(parts) == 0 {+ return summary+ }+ return fmt.Sprintf("%s: %s", summary, strings.Join(parts, " | "))}// InactiveUserOrchestrator sends inactivity warning emails and final account@@ -230,11 +247,8 @@ func (c *InactiveUserOrchestrator) ProcessInactiveUsers() {log.WithError(result.Err).WithField("user_id", result.UserID).Error("Failed to process inactive user candidate")continue}- if result.SkippedRolloutDomain {- stats.SkippedRolloutDomain++- }- if result.SkippedRolloutPct {- stats.SkippedRolloutPct+++ if result.PausedLegacyContact {+ stats.PausedLegacyContact++}if result.Sent {stats.SentEmails++@@ -244,17 +258,17 @@ func (c *InactiveUserOrchestrator) ProcessInactiveUsers() {}log.WithFields(log.Fields{- "processed_users": stats.ProcessedUsers,- "sent_emails": stats.SentEmails,- "stage_success": stats.SuccessByStage,- "stage_failures": stats.FailureByStage,- "pre_stage_failures": stats.PreStageFailures,- "skipped_rollout_domain": stats.SkippedRolloutDomain,- "skipped_rollout_percentage": stats.SkippedRolloutPct,- "has_stage_movements": hasAnyStageSuccess(stats.SuccessByStage),+ "processed_users": stats.ProcessedUsers,+ "sent_emails": stats.SentEmails,+ "stage_success": stats.SuccessByStage,+ "stage_failures": stats.FailureByStage,+ "pre_stage_failures": stats.PreStageFailures,+ "paused_legacy_contact": stats.PausedLegacyContact,+ "has_stage_movements": hasAnyStageSuccess(stats.SuccessByStage),+ "has_summary_activity": hasInactiveUserRunSummaryActivity(stats),}).Info("Completed inactive user processing")- if c.DiscordController != nil && hasAnyStageSuccess(stats.SuccessByStage) {+ if c.DiscordController != nil && hasInactiveUserRunSummaryActivity(stats) {c.DiscordController.NotifyAdminAction(buildInactiveUserRunSummary(stats, now))}}@@ -290,14 +304,13 @@ func (c *InactiveUserOrchestrator) processCandidateBatch(candidates []repo.UserI}}()- stage, sent, skippedRolloutDomain, skippedRolloutPct, err := c.processCandidate(candidate, now, emailSemaphore)+ stage, sent, pausedLegacyContact, err := c.processCandidate(candidate, now, emailSemaphore)resultCh <- inactiveCandidateResult{- UserID: candidate.UserID,- Stage: stage,- Sent: sent,- SkippedRolloutDomain: skippedRolloutDomain,- SkippedRolloutPct: skippedRolloutPct,- Err: err,+ UserID: candidate.UserID,+ Stage: stage,+ Sent: sent,+ PausedLegacyContact: pausedLegacyContact,+ Err: err,}}(candidate)}@@ -319,109 +332,102 @@ func (c *InactiveUserOrchestrator) processCandidateBatch(candidates []repo.UserIreturn results}-func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivityCandidate, now int64, emailSemaphore chan struct{}) (inactivityEmailStage, bool, bool, bool, error) {+func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivityCandidate, now int64, emailSemaphore chan struct{}) (inactivityEmailStage, bool, bool, error) {stageHint, err := c.resolveNextStage(candidate.UserID, candidate.LastActivity, now)if err != nil {- return inactivityEmailStageNone, false, false, false, err+ return inactivityEmailStageNone, false, false, err}if stageHint == inactivityEmailStageNone {- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}user, err := c.UserRepo.Get(candidate.UserID)if err != nil {if errors.Is(err, ente.ErrUserDeleted) {- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}- return stageHint, false, false, false, err- }-- if !isInInactiveUserRollout(user.ID, user.Email) {- return inactivityEmailStageNone, false, false, true, nil+ return stageHint, false, false, err}hasActivePaidEntitlement, err := c.hasActivePaidEntitlement(user.ID)if err != nil {- return stageHint, false, false, false, err+ return stageHint, false, false, err}if hasActivePaidEntitlement {log.WithField("user_id", user.ID).Info("Skipping inactive user processing because user has active paid entitlement")- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}lastActivity, found, err := c.UserRepo.GetLatestActivity(user.ID)if err != nil {- return stageHint, false, false, false, err+ return stageHint, false, false, err}if !found {// User is no longer active.- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}stage := stageHintif lastActivity != candidate.LastActivity {stage, err = c.resolveNextStage(user.ID, lastActivity, now)if err != nil {- return stageHint, false, false, false, err+ return stageHint, false, false, err}}if stage == inactivityEmailStageNone {- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}config := inactivityStageConfig(stage)if config.TemplateID == "" {- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}accountRecoveryLink := ""if config.IsFinal {if c.UserController == nil {- return stage, false, false, false, fmt.Errorf("inactive user deletion requires user controller")+ return stage, false, false, fmt.Errorf("inactive user deletion requires user controller")}hasActivePaidEntitlement, err := c.hasActivePaidEntitlement(user.ID)if err != nil {- return stage, false, false, false, err+ return stage, false, false, err}if hasActivePaidEntitlement {log.WithField("user_id", user.ID).Info("Skipping inactive user deletion because user has active paid entitlement")- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}// Re-check right before deletion to avoid deleting users who became active// after earlier reads in long processing runs.latestActivity, latestFound, err := c.UserRepo.GetLatestActivity(user.ID)if err != nil {- return stage, false, false, false, err+ return stage, false, false, err}if !latestFound {- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}latestStage, err := c.resolveNextStage(user.ID, latestActivity, now)if err != nil {- return stage, false, false, false, err+ return stage, false, false, err}if latestStage != inactivityEmailStageFinal {log.WithField("user_id", user.ID).Info("Skipping inactive user deletion because user is no longer in final stage")- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, false, nil}if c.EmergencyContactRepo != nil {hasActiveLegacyContact, err := c.EmergencyContactRepo.HasActiveLegacyContact(context.Background(), user.ID)if err != nil {- return stage, false, false, false, err+ return stage, false, false, err}if hasActiveLegacyContact {- c.DiscordController.NotifyAdminAction(- fmt.Sprintf("Inactive user %d (%s) deletion paused at %s due to active legacy contact",- user.ID, user.Email, stdtime.UnixMicro(now).UTC().Format(stdtime.RFC3339)))log.WithField("user_id", user.ID).Info("Skipping inactive user deletion because user has active legacy contact configured")- return inactivityEmailStageNone, false, false, false, nil+ return inactivityEmailStageNone, false, true, nil}}accountRecoveryLink, err = c.UserController.getAccountRecoveryLink(user.ID, user.Email)if err != nil {- return stage, false, false, false, err+ return stage, false, false, err}deleteLogger := log.WithFields(log.Fields{@@ -429,7 +435,7 @@ func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivit"req_ctx": "inactive_account_deletion",})if _, err := c.UserController.HandleAutomatedAccountDeletion(context.Background(), user.ID, deleteLogger); err != nil {- return stage, false, false, false, err+ return stage, false, false, err}}@@ -457,11 +463,11 @@ func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivittemplateData,nil,); err != nil {- return stage, false, false, false, err+ return stage, false, false, err}if err := c.NotificationHistoryRepo.SetLastNotificationTimeToNow(user.ID, config.TemplateID); err != nil {- return stage, false, false, false, err+ return stage, false, false, err}log.WithFields(log.Fields{@@ -471,7 +477,7 @@ func (c *InactiveUserOrchestrator) processCandidate(candidate repo.UserInactivit"deletion_date": deletionDate,}).Info("Sent inactive user email")- return stage, true, false, false, nil+ return stage, true, false, nil}func (c *InactiveUserOrchestrator) hasActivePaidEntitlement(userID int64) (bool, error) {@@ -576,17 +582,6 @@ func inactivityStageConfig(stage inactivityEmailStage) inactivityEmailStageConfi}}-func isEnteDomainRolloutUser(email string) bool {- return strings.HasSuffix(emailUtil.NormalizeEmail(email), "@ente.io")-}--func isInInactiveUserRollout(userID int64, email string) bool {- if isEnteDomainRolloutUser(email) {- return true- }- return rollout.IsInPercentageRollout(userID, inactiveUserRolloutNonce, inactiveUserRolloutPercentage)-}-func formatDeletionDateForStage(stage inactivityEmailStage, now int64) string {var daysUntilDeletion int64switch stage {diff --git a/server/pkg/controller/user/inactive_user_orchestrator_test.go b/server/pkg/controller/user/inactive_user_orchestrator_test.goindex b4c1adfe3c..dc4e9d7913 100644--- a/server/pkg/controller/user/inactive_user_orchestrator_test.go+++ b/server/pkg/controller/user/inactive_user_orchestrator_test.go@@ -5,7 +5,6 @@ import ("testing""github.com/ente/museum/pkg/repo"- "github.com/ente/museum/pkg/utils/rollout")func TestNextInactivityEmailStage(t *testing.T) {@@ -128,6 +127,24 @@ func TestHasAnyStageSuccess(t *testing.T) {}}+func TestHasInactiveUserRunSummaryActivity(t *testing.T) {+ stats := newInactiveUserRunStats()+ if hasInactiveUserRunSummaryActivity(stats) {+ t.Fatal("expected no summary activity")+ }++ stats.PausedLegacyContact = 1+ if !hasInactiveUserRunSummaryActivity(stats) {+ t.Fatal("expected paused legacy contact to count as summary activity")+ }++ stats.PausedLegacyContact = 0+ stats.SuccessByStage[inactivityEmailStageWarn2m] = 1+ if !hasInactiveUserRunSummaryActivity(stats) {+ t.Fatal("expected stage success to count as summary activity")+ }+}+func TestBuildInactiveUserRunSummary(t *testing.T) {stats := newInactiveUserRunStats()stats.ProcessedUsers = 12@@ -136,8 +153,7 @@ func TestBuildInactiveUserRunSummary(t *testing.T) {stats.SuccessByStage[inactivityEmailStageWarn1m] = 1stats.FailureByStage[inactivityEmailStageWarn7d] = 4stats.PreStageFailures = 2- stats.SkippedRolloutDomain = 1- stats.SkippedRolloutPct = 5+ stats.PausedLegacyContact = 6summary := buildInactiveUserRunSummary(stats, 0)@@ -145,11 +161,10 @@ func TestBuildInactiveUserRunSummary(t *testing.T) {"Inactive user run summary (1970-01-01T00:00:00Z)","processed=12","sent=3",- "success={warn_2m=2, warn_1m=1, warn_7d=0, warn_1d=0, confirm_13m=0}",- "failures={warn_2m=0, warn_1m=0, warn_7d=4, warn_1d=0, confirm_13m=0}",+ "success={warn_2m=2, warn_1m=1}",+ "failures={warn_7d=4}","pre_stage_failures=2",- "skipped_rollout_domain=1",- "skipped_rollout_percentage=5",+ "paused_legacy_contact=6",}for _, fragment := range mustContain {@@ -157,27 +172,32 @@ func TestBuildInactiveUserRunSummary(t *testing.T) {t.Fatalf("summary missing fragment %q: %s", fragment, summary)}}-}-func TestIsInInactiveUserRollout(t *testing.T) {- if !isInInactiveUserRollout(12345, "[email protected]") {- t.Fatal("expected @ente.io users to always be in rollout")+ mustNotContain := []string{+ "warn_7d=0",+ "warn_1d=0",+ "confirm_13m=0",+ "warn_2m=0",+ "warn_1m=0",+ "sent=0",+ "pre_stage_failures=0",}- if !isInInactiveUserRollout(12345, " [email protected] ") {- t.Fatal("expected normalized @ente.io users to always be in rollout")+ for _, fragment := range mustNotContain {+ if strings.Contains(summary, fragment) {+ t.Fatalf("summary contains zero-value fragment %q: %s", fragment, summary)+ }}+}- userID := int64(987654321)- email := "[email protected]"- got1 := isInInactiveUserRollout(userID, email)- got2 := isInInactiveUserRollout(userID, email)- if got1 != got2 {- t.Fatal("expected deterministic rollout result for same user and nonce")- }+func TestBuildInactiveUserRunSummaryForPausedOnlyRun(t *testing.T) {+ stats := newInactiveUserRunStats()+ stats.ProcessedUsers = 12+ stats.PausedLegacyContact = 2- want := rollout.IsInPercentageRollout(userID, inactiveUserRolloutNonce, inactiveUserRolloutPercentage)- if got1 != want {- t.Fatalf("unexpected rollout decision: got %v want %v", got1, want)+ summary := buildInactiveUserRunSummary(stats, 0)+ want := "Inactive user run summary (1970-01-01T00:00:00Z): processed=12 | paused_legacy_contact=2"+ if summary != want {+ t.Fatalf("unexpected summary: got %q want %q", summary, want)}}