purgeChatsInTx MUST BE CALLED WITH A TRANSACTION
(ctx context.Context, tx database.Store, start time.Time, chatRetentionDays, chatAutoArchiveDays int32)
| 483 | |
| 484 | // purgeChatsInTx MUST BE CALLED WITH A TRANSACTION |
| 485 | func (i *instance) purgeChatsInTx(ctx context.Context, tx database.Store, start time.Time, chatRetentionDays, chatAutoArchiveDays int32) (purgedChats, purgedChatFiles int64, archivedChats []database.AutoArchiveInactiveChatsRow, err error) { |
| 486 | // Delete old archived chats first, then orphaned files |
| 487 | // (cascade clears chat_file_links but not chat_files). |
| 488 | if chatRetentionDays > 0 { |
| 489 | deleteChatsBefore := start.Add(-time.Duration(chatRetentionDays) * 24 * time.Hour) |
| 490 | purgedChats, err = tx.DeleteOldChats(ctx, database.DeleteOldChatsParams{ |
| 491 | BeforeTime: deleteChatsBefore, |
| 492 | LimitCount: chatsBatchSize, |
| 493 | }) |
| 494 | if err != nil { |
| 495 | return 0, 0, nil, xerrors.Errorf("failed to delete old chats: %w", err) |
| 496 | } |
| 497 | |
| 498 | purgedChatFiles, err = tx.DeleteOldChatFiles(ctx, database.DeleteOldChatFilesParams{ |
| 499 | BeforeTime: deleteChatsBefore, |
| 500 | LimitCount: chatFilesBatchSize, |
| 501 | }) |
| 502 | if err != nil { |
| 503 | return 0, 0, nil, xerrors.Errorf("failed to delete old chat files: %w", err) |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // Auto-archive runs after the delete pass so newly |
| 508 | // archived chats aren't eligible for deletion this tick. |
| 509 | // Eligibility uses UTC day boundaries: a chat is archived on the |
| 510 | // start of the UTC day after its inactivity period has elapsed. |
| 511 | if chatAutoArchiveDays > 0 { |
| 512 | today := dbtime.StartOfDay(start) |
| 513 | archiveCutoff := today.Add(-time.Duration(chatAutoArchiveDays) * 24 * time.Hour) |
| 514 | archivedChats, err = tx.AutoArchiveInactiveChats(ctx, database.AutoArchiveInactiveChatsParams{ |
| 515 | ArchiveCutoff: archiveCutoff, |
| 516 | LimitCount: i.chatAutoArchiveBatchSize, |
| 517 | }) |
| 518 | if err != nil { |
| 519 | return 0, 0, nil, xerrors.Errorf("failed to auto-archive inactive chats: %w", err) |
| 520 | } |
| 521 | } |
| 522 | return purgedChats, purgedChatFiles, archivedChats, nil |
| 523 | } |
| 524 | |
| 525 | // dispatchChatAutoArchive audits every archived root chat and enqueues one |
| 526 | // notification per owner covering the roots archived in this tick. Children |
no test coverage detected