feat: merge main into the Partnership branch, keeping both the OAuth app import checks and the integration reconnect marking in the org transfer repository

This commit is contained in:
Matthew Meszaros
2026-11-09 18:11:57 +01:00
68 changed files with 2604 additions and 187 deletions
+48 -1
View File
@@ -304,6 +304,15 @@ jobs:
go test -race -v ./internal/repository -run '^TestLiveContactCampaignStateEmptySteps$' -count=1 | tee contact-campaign-steps.log
grep -q -- '--- PASS: TestLiveContactCampaignStateEmptySteps ' contact-campaign-steps.log
- name: Form submission paging across equal timestamps (PostgreSQL)
if: matrix.suite == 'live-1'
env:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: |
set -o pipefail
go test -race -v ./internal/repository -run '^TestLiveFormSubmissionsPageAcrossEqualAndCloseTimestamps$' -count=1 | tee form-submission-paging.log
grep -q -- '--- PASS: TestLiveFormSubmissionsPageAcrossEqualAndCloseTimestamps ' form-submission-paging.log
- name: Campaign send admission regressions (PostgreSQL)
if: matrix.suite == 'live-2'
env:
@@ -320,6 +329,19 @@ jobs:
run: |
go test -race -p 1 ./internal/repository ./internal/scheduler ./internal/tasks -run '^TestLive(MailboxSendClaim(AdmitsOnePerGap|WaitsOutAReservedSend)|BookedSendsLeaveOutCampaignWakeups|ParkedCampaignDoesNotThrottleItsPoolMate|BookedSendsStillSpaceTheCampaign|ClaimedMailboxIsPassedOver|CampaignsSharingAMailboxSendOncePerGap|LostMailboxClaimReschedulesWithoutSending)$' -count=1
# A real pg_dump and psql against the same major the self-host compose ships.
- name: Instance backup and restore round trip (PostgreSQL)
if: matrix.suite == 'live-3'
env:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: |
set -o pipefail
if [ ! -x /usr/lib/postgresql/16/bin/pg_dump ]; then
sudo apt-get update -q && sudo apt-get install -y -q postgresql-client-16
fi
go test -race -v ./cmd/warmblyctl -run '^TestLiveBackupRestoreRoundTrip$' -count=1 | tee backup-restore.log
grep -q -- '--- PASS: TestLiveBackupRestoreRoundTrip ' backup-restore.log
- name: Postgres queue claim regressions (PostgreSQL)
if: matrix.suite == 'live-3'
env:
@@ -333,6 +355,18 @@ jobs:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: go test -race ./internal/repository -run '^TestLiveInvitedSignup$' -count=1
- name: Tester initial password and sample data regressions (PostgreSQL)
if: matrix.suite == 'live-1'
env:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: |
set -o pipefail
go test -race -v -p 1 ./internal/repository -run '^TestLive(InitialPasswordLifecycle|ResetPasswordClearsAnIssuedExpiry|ReissueTesterPassword|TesterSampleData(IsSafeAndPreservesEdits|RollsBackAndSerializesRetries|RejectsNonTestAndInactiveWorkspaces|SurvivesInitialPasswordReplacement|ListsTheSeedableWorkspace))$' -count=1 | tee tester-lifecycle.log
for test in TestLiveInitialPasswordLifecycle TestLiveResetPasswordClearsAnIssuedExpiry TestLiveReissueTesterPassword TestLiveTesterSampleDataIsSafeAndPreservesEdits TestLiveTesterSampleDataRollsBackAndSerializesRetries TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces TestLiveTesterSampleDataSurvivesInitialPasswordReplacement TestLiveTesterSampleDataListsTheSeedableWorkspace; do
grep -q -- "--- PASS: $test " tester-lifecycle.log
done
if grep -q -- '--- SKIP:' tester-lifecycle.log; then exit 1; fi
- name: Mailbox batch and folder identity regressions (PostgreSQL)
if: matrix.suite == 'live-1'
env:
@@ -425,7 +459,7 @@ jobs:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: |
set -euo pipefail
go test -race -v -p 1 ./internal/api/handler ./internal/app/email ./internal/app/contact ./internal/app/contactimport ./internal/app/orgtransfer ./internal/app/form ./internal/app/websitetracking ./internal/repository ./internal/scheduler -run '^TestLive(SharedCampaignAccess|ContactTimelineAccess|TrackingDomain(WriteIsOrgScoped|VerifyRoundTrip|Sweep|BelongsToOneOrganization)|ImportLeavesAnotherWorkspaceAlone|ContactImportIsScopedToItsWorkspace|ImportRefuses(RowsOwnedByAnotherWorkspace|ReferencesIntoAnotherWorkspace)|TenantFamiliesRefuseAnotherOrganization|CampaignChildrenRefuseAnotherWorkspace|CRMSyncIntegrationsRefuseAnotherOrganization|FormLinkTokenIsBoundToItsForm|IdentifyTicketStaysInItsWorkspace|TenantMatrixRefusesAnotherOrganization|SenderResolutionStaysInsideTheCampaignOrg|SenderSchedulerNeverPicksAnotherOrgMailbox|SenderScopeWithoutAnOrganizationReachesNothing|ActiveCampaignLookupIsOrgScoped|OwnConversationDoesNotCrossWorkspaces|UniboxGetBySenderIsScopedToTheOrganization|UniboxMoveFolderIsOrgScoped|UniboxScopeMoveThreadsIsOrgScoped|ScheduledSendsAreScopedToTheMailboxOrganization|NoLabelIsReferencedAcrossWorkspaces|CampaignAudienceIsScoped)$' -count=1 | tee workspace-isolation.log
go test -race -v -p 1 ./internal/api/handler ./internal/app/email ./internal/app/contact ./internal/app/contactimport ./internal/app/orgtransfer ./internal/app/form ./internal/app/websitetracking ./internal/repository ./internal/scheduler -run '^TestLive(SharedCampaignAccess|ContactTimelineAccess|TrackingDomain(WriteIsOrgScoped|VerifyRoundTrip|Sweep|BelongsToOneOrganization)|ImportLeavesAnotherWorkspaceAlone|ContactImportIsScopedToItsWorkspace|ImportRefuses(RowsOwnedByAnotherWorkspace|ReferencesIntoAnotherWorkspace)|ArchiveCarriesIntegrationCredentialsAndDirectMailLinks|TenantFamiliesRefuseAnotherOrganization|CampaignChildrenRefuseAnotherWorkspace|CRMSyncIntegrationsRefuseAnotherOrganization|FormLinkTokenIsBoundToItsForm|IdentifyTicketStaysInItsWorkspace|TenantMatrixRefusesAnotherOrganization|SenderResolutionStaysInsideTheCampaignOrg|SenderSchedulerNeverPicksAnotherOrgMailbox|SenderScopeWithoutAnOrganizationReachesNothing|ActiveCampaignLookupIsOrgScoped|OwnConversationDoesNotCrossWorkspaces|UniboxGetBySenderIsScopedToTheOrganization|UniboxMoveFolderIsOrgScoped|UniboxScopeMoveThreadsIsOrgScoped|ScheduledSendsAreScopedToTheMailboxOrganization|NoLabelIsReferencedAcrossWorkspaces|CampaignAudienceIsScoped)$' -count=1 | tee workspace-isolation.log
if grep -q -- '--- SKIP:' workspace-isolation.log; then
echo "Workspace isolation regressions must execute without skips"
exit 1
@@ -438,6 +472,7 @@ jobs:
TestLiveIdentifyTicketStaysInItsWorkspace \
TestLiveTenantMatrixRefusesAnotherOrganization \
TestLiveImportRefusesReferencesIntoAnotherWorkspace \
TestLiveArchiveCarriesIntegrationCredentialsAndDirectMailLinks \
TestLiveSenderSchedulerNeverPicksAnotherOrgMailbox \
TestLiveTrackingDomainWriteIsOrgScoped; do
grep -q "^--- PASS: $test " workspace-isolation.log
@@ -514,6 +549,18 @@ jobs:
go test -race -v -p 1 ./internal/repository ./internal/app/consumer ./internal/tasks ./internal/app/orgtransfer -run '^TestLive(Warmup(ReservationPreservesOriginalChargeDay|RefundCASProtectsConcurrentAndRestampedTasks|RefundAndResultRollbackTogether|ReplyRefundUsesChargedShapeAfterParentDeletion|ChargeAndRefundUseUTCInNonUTCSession|ChargeProofRollsBackWhenCounterWriteFails|CancellationPathsRefundOnlyProvenCharge|GenericCompletionDoesNotProveChargedAccounting|SendFailureGivesTheDayBack|SendGiveBackIsIdempotent|SafetyHoldReschedulesInsteadOfBlockingTheDispatcher|Recovery(RetiresExistingUnstartedDispatchOnce|HandlesPartialReservations|PreservesExecutionAndReceiptEvidence|KeepsOtherUnknownAndRestrictiveHolds|AndBeginCannotBothWin))|AppliedWarmupFailureRepairsOnlyRefundedChargeDeadLetters|GiveBackDailySend(ReversesADispatchedWarmupSend|LeavesOtherDaysAlone)|AccountDailyUsage(CountsWarmupTasksNotTheStatisticsRow|ScopesWarmupToTypeAndDay)|ImportWarmupStatisticsCannotGrantChargeRefundAuthority|Legacy(FailedStatusCannotProveNonExecution|JSONAndAvroFailurePersistConservativeAdmission)|AuthoritativeDeadLetterResultsReconcileWithoutReplay|DeadLetterResultEffectsAndSettlementRollBackTogether|Campaign(DeadLetterWorkerResultReleasesReservationOnce|ForeignKeyCleanupDoesNotProveWakeupOnly)|UserEmailDeadLetterUsesUnderlyingSendTaskType|UnknownSendOperatorResolutionReconcilesAndAudits|RecordedWorkerOutcomeSurvivesLostEvent|UnreservedLegacyFailedTaskRetainsUnknownHold|UnknownResolutionRejectsMissingEvidenceAndLiveExecution|SendRecoveryResolutionRequiresEvidenceAndRetainsUnknown|UnstartedDispatchRetainsDeferredOutcomeConflict)$' -count=1
go test -race -v -p 1 ./internal/infrastructure/db -run '^TestLiveWarmupChargeUpgradeKeepsLegacyUnverifiedAndPreservesEvidence$' -count=1
- name: Manual send recovery regressions (PostgreSQL)
if: matrix.suite == 'live-1'
env:
WARMBLY_TEST_DB: postgres://warmbly:warmbly@127.0.0.1:5432/warmbly_test?sslmode=disable
run: |
set -o pipefail
go test -race -v -p 1 ./internal/tasks ./internal/repository -run '^TestLive(ManualSendWithUnknownHandOffIsReleasedUnsent|UnansweredManualSendIsReleasedUnsent|ManualSendInterruptedBeforeReservationRunsOnce|StaleOrUnrunnableInterruptedManualSendFails|UnstartedCampaignSendReleasesMailboxAndCannotStartLater|StartedManualSendLiftsHoldWithoutRunningAgain|HoldOnMissingTaskIsCleared)$' -count=1 | tee manual-send-recovery.log
for test in TestLiveManualSendWithUnknownHandOffIsReleasedUnsent TestLiveUnansweredManualSendIsReleasedUnsent TestLiveManualSendInterruptedBeforeReservationRunsOnce TestLiveStaleOrUnrunnableInterruptedManualSendFails TestLiveUnstartedCampaignSendReleasesMailboxAndCannotStartLater TestLiveStartedManualSendLiftsHoldWithoutRunningAgain TestLiveHoldOnMissingTaskIsCleared; do
grep -q -- "--- PASS: $test " manual-send-recovery.log
done
if grep -q -- '--- SKIP:' manual-send-recovery.log; then exit 1; fi
- name: Mailbox placement health recovery (PostgreSQL)
if: matrix.suite == 'live-2'
env:
+11 -3
View File
@@ -38,7 +38,7 @@ describe("tester sample-data controls", () => {
beforeEach(() => { access.canManage = true; });
it("offers sample data for an active owned Test workspace", () => {
const document = render({ test_workspace_id: "test-workspace" });
const document = render({ test_workspace_id: "test-workspace", sample_data_eligible: true });
const button = document.querySelector<HTMLButtonElement>('button[aria-label="Add sample data for reviewer@example.test"]');
expect(button?.disabled).toBe(false);
expect(button?.textContent).toBe("Add sample data");
@@ -51,9 +51,17 @@ describe("tester sample-data controls", () => {
expect(render({ test_workspace_id: "test-workspace" }).querySelector('button[aria-label^="Add sample data for"]')).toBeNull();
});
it("disables populated, expired and passwordless Test workspaces", () => {
it("keeps offering sample data after the tester replaces the initial password", () => {
const document = render({ test_workspace_id: "test-workspace", password_expires_at: null, sample_data_eligible: true });
const button = document.querySelector<HTMLButtonElement>('button[aria-label="Add sample data for reviewer@example.test"]');
expect(button?.disabled).toBe(false);
});
it("disables populated and ineligible Test workspaces", () => {
for (const tester of [
{ sample_data_seeded_at: "2026-01-01T00:00:00Z" },
{ sample_data_eligible: true, sample_data_seeded_at: "2026-01-01T00:00:00Z" },
{ sample_data_eligible: false },
// An older backend sends no eligibility; the outstanding password decides.
{ password_expires_at: "2020-01-01T00:00:00Z" },
{ password_expires_at: null },
]) {
+3 -1
View File
@@ -334,6 +334,8 @@ export default function TestersPage() {
{rows.map((t) => {
const expired =
!!t.password_expires_at && new Date(t.password_expires_at) <= new Date();
// A backend from before sample_data_eligible gated seeding on the outstanding password.
const canSeed = t.sample_data_eligible ?? (!!t.password_expires_at && !expired);
return (
<li
key={t.user_id}
@@ -371,7 +373,7 @@ export default function TestersPage() {
<Button
size="xs"
variant="outline"
disabled={expired || !t.password_expires_at || !!t.sample_data_seeded_at || seed.isPending || revoke.isPending}
disabled={!canSeed || !!t.sample_data_seeded_at || seed.isPending || revoke.isPending}
aria-label={`Add sample data for ${t.email}`}
onClick={() => setSampleTarget({ organizationID: t.test_workspace_id!, email: t.email })}
>
+2
View File
@@ -988,6 +988,8 @@ export interface LoginCodeExemption {
password_expires_at?: string | null;
test_workspace_id?: string | null;
sample_data_seeded_at?: string | null;
/** The backend's seeding check: an active tester whose Test workspace grant is unexpired. */
sample_data_eligible?: boolean;
/** Created through the tester flow, so it can be issued a new password. */
created_as_tester?: boolean;
}
+4
View File
@@ -2149,6 +2149,10 @@ func main() {
// started, so without this a stranded campaign stops sending forever.
go tasksService.StartCampaignReconciler(ctx, 5*time.Minute)
// Manual send recovery: a unibox send whose execution stopped after its
// claim but before its reservation runs again, or fails and says so.
go tasksService.StartManualSendRecovery(ctx, time.Minute)
// Send-plan snapshotter: walk every active campaign's send plan on an
// interval and store it, so GET /campaigns/:id/send-plan serves a stored
// snapshot instead of running the planner (lead supply, per-mailbox
+68 -41
View File
@@ -90,6 +90,9 @@ type manifest struct {
AppURL string `json:"app_url,omitempty"`
Database string `json:"database"`
DatabaseSHA string `json:"database_sha256"`
// PostgresVersion and DumpClient say which server was dumped and with what.
PostgresVersion string `json:"postgres_version,omitempty"`
DumpClient string `json:"pg_dump_version,omitempty"`
// Counts are for the confirmation line, not for logic.
Organizations int `json:"organizations"`
Users int `json:"users"`
@@ -132,16 +135,23 @@ func runBackup(ctx context.Context, args []string) error {
if err != nil {
return err
}
if _, err := exec.LookPath("pg_dump"); err != nil {
return errors.New("pg_dump is not on PATH, so the database cannot be dumped.\nRun this inside the backend container, where it is installed:\n " + composeExec + "backup --out /data/blobs/warmbly-backup.tar.gz")
server, err := readPGServer(ctx, dsn)
if err != nil {
return err
}
pgDump, err := pgTool("pg_dump", server)
if err != nil {
return err
}
m := manifest{
Version: bundleVersion,
CreatedAt: time.Now().UTC(),
AppVersion: version.String(),
AppURL: strings.TrimSpace(os.Getenv("APP_URL")),
Database: databaseName(dsn),
Version: bundleVersion,
CreatedAt: time.Now().UTC(),
AppVersion: version.String(),
AppURL: strings.TrimSpace(os.Getenv("APP_URL")),
Database: databaseName(dsn),
PostgresVersion: server.version,
DumpClient: pgClientVersion(pgDump),
}
// Counts first: they are the line that tells an operator this bundle is of
@@ -156,7 +166,7 @@ func runBackup(ctx context.Context, args []string) error {
fmt.Printf("Backing up %s\n", redact(dsn))
dumpFile, dumpSize, dumpSHA, err := dumpDatabase(ctx, dsn)
dumpFile, dumpSize, dumpSHA, err := dumpDatabase(ctx, pgDump, dsn)
if err != nil {
return err
}
@@ -164,7 +174,7 @@ func runBackup(ctx context.Context, args []string) error {
_ = os.Remove(dumpFile)
}()
m.DatabaseSHA = dumpSHA
fmt.Printf(" database %s\n", humanBytes(dumpSize))
fmt.Printf(" database %s, PostgreSQL %s dumped by %s\n", humanBytes(dumpSize), server.version, m.DumpClient)
var blobFiles []blobEntry
m.BlobProvider = strings.ToLower(strings.TrimSpace(os.Getenv("BLOB_PROVIDER")))
@@ -254,10 +264,6 @@ func runRestore(ctx context.Context, args []string) error {
if strings.TrimSpace(*file) == "" {
return errors.New("--file is required. Point it at a bundle written by `warmblyctl backup`.")
}
if _, err := exec.LookPath("psql"); err != nil {
return errors.New("psql is not on PATH, so the database cannot be restored.\nRun this inside the backend container, where it is installed.")
}
m, err := readManifest(*file)
if err != nil {
return err
@@ -270,17 +276,28 @@ func runRestore(ctx context.Context, args []string) error {
if err != nil {
return err
}
server, err := readPGServer(ctx, dsn)
if err != nil {
return err
}
psql, err := pgTool("psql", server)
if err != nil {
return err
}
fmt.Printf("Bundle %s\n", *file)
fmt.Printf(" written %s by Warmbly %s\n", m.CreatedAt.Local().Format(time.RFC1123), m.AppVersion)
if m.AppURL != "" {
fmt.Printf(" from %s\n", m.AppURL)
}
if m.PostgresVersion != "" {
fmt.Printf(" dumped PostgreSQL %s with %s\n", m.PostgresVersion, m.DumpClient)
}
fmt.Printf(" holds %d organizations, %d users, %d mailboxes\n", m.Organizations, m.Users, m.Mailboxes)
if m.BlobFiles > 0 {
fmt.Printf(" %d blob files, %s\n", m.BlobFiles, humanBytes(m.BlobBytes))
}
fmt.Printf("Target %s\n\n", redact(dsn))
fmt.Printf("Target %s (PostgreSQL %s)\n\n", redact(dsn), server.version)
// The key check is the point of this command. A restore onto a host whose
// keys differ produces an instance that looks fine and whose every mailbox
@@ -302,9 +319,8 @@ func runRestore(ctx context.Context, args []string) error {
fmt.Println()
}
// Checked before anything is dropped: the restore empties the schema first,
// so finding out afterwards that the bundle is truncated leaves the target
// with neither its own data nor the bundle's.
// Checked before the load starts, so a truncated copy is refused in seconds
// rather than after replaying most of a large dump.
if m.DatabaseSHA != "" {
if err := verifyDump(*file, m.DatabaseSHA); err != nil {
return err
@@ -313,11 +329,11 @@ func runRestore(ctx context.Context, args []string) error {
}
fmt.Println("Restoring the database...")
if err := restoreDatabase(ctx, dsn, *file); err != nil {
if err := restoreDatabase(ctx, psql, dsn, *file, server); err != nil {
return err
}
fmt.Println(" database restored")
if err := analyzeDatabase(ctx, dsn); err != nil {
if err := analyzeDatabase(ctx, psql, dsn); err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
@@ -417,7 +433,7 @@ func checkRestoreKeys(file string, m manifest, force bool) (bool, error) {
// dumpDatabase writes a plain-SQL dump to a temp file and returns its path,
// size and digest. Plain rather than custom format so the bundle can be
// inspected, and so a restore needs psql alone.
func dumpDatabase(ctx context.Context, dsn string) (string, int64, string, error) {
func dumpDatabase(ctx context.Context, pgDump, dsn string) (string, int64, string, error) {
tmp, err := os.CreateTemp("", "warmbly-dump-*.sql")
if err != nil {
return "", 0, "", err
@@ -430,7 +446,7 @@ func dumpDatabase(ctx context.Context, dsn string) (string, int64, string, error
// No owner or ACL statements: the destination's database user is whatever
// its own compose file created, and it is never guaranteed to carry the
// same name as this one's.
cmd := pgCommand(ctx, "pg_dump", dsn, "--no-owner", "--no-privileges", "--format=plain")
cmd := pgCommand(ctx, pgDump, dsn, "--no-owner", "--no-privileges", "--format=plain")
cmd.Stdout = w
var stderr strings.Builder
cmd.Stderr = &stderr
@@ -481,39 +497,50 @@ func short(sum string) string {
return sum
}
// restoreDatabase empties the schema and replays the dump into it. The schema
// is dropped rather than the dump carrying DROP statements, because the
// destination has already had migrations applied at boot and objects the
// bundle does not know about would otherwise survive into the restored
// instance.
func restoreDatabase(ctx context.Context, dsn, bundle string) error {
reset := pgCommand(ctx, "psql", dsn, "-v", "ON_ERROR_STOP=1", "-q",
"-c", "DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;")
var resetErr strings.Builder
reset.Stderr = &resetErr
if err := reset.Run(); err != nil {
return fmt.Errorf("could not empty the target schema: %s", strings.TrimSpace(resetErr.String()))
}
// restoreSchemaReset runs ahead of the dump. The schema is dropped rather than
// the dump carrying DROP statements, because the destination has already had
// migrations applied at boot and objects the bundle does not know about would
// otherwise survive into the restored instance.
const restoreSchemaReset = "DROP SCHEMA IF EXISTS public CASCADE;\nCREATE SCHEMA public;\n"
sql, closeFn, err := openBundleEntry(bundle, databasePath)
// restoreDatabase replaces the schema with the dump's in one transaction, so a
// load that fails anywhere leaves the database as it was.
func restoreDatabase(ctx context.Context, psql, dsn, bundle string, server pgServer) error {
dump, closeFn, err := openBundleEntry(bundle, databasePath)
if err != nil {
return err
}
defer closeFn()
load := pgCommand(ctx, "psql", dsn, "-v", "ON_ERROR_STOP=1", "-q")
load.Stdin = sql
sql, skipped, err := withoutUnknownSettings(dump, server.knows)
if err != nil {
return fmt.Errorf("reading the bundle's database dump: %w", err)
}
if len(skipped) > 0 {
fmt.Printf(" settings skipped %s, which PostgreSQL %d does not have\n", strings.Join(skipped, ", "), server.major)
}
load := pgCommand(ctx, psql, dsn, "-v", "ON_ERROR_STOP=1", "-q", "--single-transaction", "--file=-")
load.Stdin = io.MultiReader(strings.NewReader(restoreSchemaReset), sql)
var loadErr strings.Builder
load.Stderr = &loadErr
if err := load.Run(); err != nil {
return fmt.Errorf("the dump did not load cleanly, so the database is now empty and unusable.\nFix the cause and run the restore again:\n%s", strings.TrimSpace(loadErr.String()))
msg := strings.TrimSpace(loadErr.String())
if msg == "" {
msg = err.Error()
}
hint := "Fix the cause and run the restore again."
if strings.Contains(msg, "max_locks_per_transaction") {
hint = "The restore replaces every table in one transaction, which needs more locks than this server allows.\nRaise max_locks_per_transaction (for example to 1024), restart PostgreSQL, and run the restore again."
}
return fmt.Errorf("the dump did not load, so nothing was changed: the restore runs in one transaction and the database is as it was.\n%s\n%s", hint, msg)
}
return nil
}
// analyzeDatabase gathers planner statistics, which a dump does not carry.
func analyzeDatabase(ctx context.Context, dsn string) error {
cmd := pgCommand(ctx, "psql", dsn, "-v", "ON_ERROR_STOP=1", "-q", "-c", "ANALYZE;")
func analyzeDatabase(ctx context.Context, psql, dsn string) error {
cmd := pgCommand(ctx, psql, dsn, "-v", "ON_ERROR_STOP=1", "-q", "-c", "ANALYZE;")
var stderr strings.Builder
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
@@ -533,7 +560,7 @@ func analyzeDatabase(ctx context.Context, dsn string) error {
// world-readable on Linux, so the DSN that reaches argv carries no password.
func pgCommand(ctx context.Context, name, dsn string, args ...string) *exec.Cmd {
clean, password := splitDSNPassword(dsn)
cmd := exec.CommandContext(ctx, name, append([]string{"--dbname=" + clean}, args...)...) //nolint:gosec // name is a literal at every call site
cmd := exec.CommandContext(ctx, name, append([]string{"--dbname=" + clean}, args...)...) //nolint:gosec // name is a client binary resolved by pgTool
cmd.Env = os.Environ()
if password != "" {
cmd.Env = append(cmd.Env, "PGPASSWORD="+password)
+201
View File
@@ -0,0 +1,201 @@
package main
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/warmbly/warmbly/internal/infrastructure/db"
)
// TestLiveBackupRestoreRoundTrip dumps a migrated database with the client
// backup resolves, restores it with the client restore resolves, and replays a
// bundle as a PostgreSQL 17+ pg_dump writes it. It runs in a scratch database
// on the server WARMBLY_TEST_DB names, because a restore replaces the schema.
func TestLiveBackupRestoreRoundTrip(t *testing.T) {
base := os.Getenv("WARMBLY_TEST_DB")
if base == "" {
t.Skip("WARMBLY_TEST_DB is not set")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
dsn := scratchDatabase(ctx, t, base)
if err := db.RunMigrations(dsn); err != nil {
t.Fatalf("migrations: %v", err)
}
execSQL(ctx, t, dsn, `CREATE TABLE public.restore_probe (id int PRIMARY KEY, body text);
CREATE INDEX restore_probe_trgm ON public.restore_probe USING gin (body public.gin_trgm_ops);
INSERT INTO public.restore_probe VALUES (1, 'kept'), (2, 'SET transaction_timeout = 0;');`)
server, err := readPGServer(ctx, dsn)
if err != nil {
t.Fatal(err)
}
pgDump, err := pgTool("pg_dump", server)
if err != nil {
t.Fatal(err)
}
psql, err := pgTool("psql", server)
if err != nil {
t.Fatal(err)
}
t.Logf("server %s, %s, %s", server.version, pgClientVersion(pgDump), pgClientVersion(psql))
dumpFile, _, dumpSHA, err := dumpDatabase(ctx, pgDump, dsn)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Remove(dumpFile) })
dump, err := os.ReadFile(dumpFile)
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
bundle := writeTestBundle(t, dir, "current.tar.gz", dump)
if dumpSHA != sha256Hex(dump) {
t.Fatal("dumpDatabase reported a digest that is not the dump's")
}
// What the restore must replace: a row added and a table created after the backup.
execSQL(ctx, t, dsn, `INSERT INTO public.restore_probe VALUES (3, 'after the backup');
CREATE TABLE public.not_in_bundle (id int);`)
if err := restoreDatabase(ctx, psql, dsn, bundle, server); err != nil {
t.Fatalf("restoring its own backup: %v", err)
}
assertProbe(ctx, t, dsn, "1:kept,2:SET transaction_timeout = 0;")
if n := scalar(ctx, t, dsn, `SELECT count(*) FROM pg_tables WHERE schemaname = 'public' AND tablename = 'not_in_bundle'`); n != 0 {
t.Fatal("a table created after the backup survived the restore")
}
if n := scalar(ctx, t, dsn, `SELECT count(*) FROM public.schema_migrations`); n == 0 {
t.Fatal("the restored database has no schema_migrations row")
}
// A bundle written by a newer pg_dump carries transaction_timeout in its preamble.
newer := strings.Replace(string(dump), "SET lock_timeout = 0;\n", "SET lock_timeout = 0;\nSET transaction_timeout = 0;\n", 1)
if newer == string(dump) && server.major < 17 {
t.Fatal("the dump has no lock_timeout line to put transaction_timeout after")
}
if err := restoreDatabase(ctx, psql, dsn, writeTestBundle(t, dir, "newer.tar.gz", []byte(newer)), server); err != nil {
t.Fatalf("restoring a bundle written by a newer pg_dump: %v", err)
}
assertProbe(ctx, t, dsn, "1:kept,2:SET transaction_timeout = 0;")
// A load that fails at its very end changes nothing.
execSQL(ctx, t, dsn, `INSERT INTO public.restore_probe VALUES (4, 'before the failed restore')`)
broken := string(dump) + "\nSELECT 1 / 0;\n"
err = restoreDatabase(ctx, psql, dsn, writeTestBundle(t, dir, "broken.tar.gz", []byte(broken)), server)
if err == nil {
t.Fatal("a dump ending in an error restored without one")
}
if !strings.Contains(err.Error(), "nothing was changed") {
t.Fatalf("the failure does not say the database is untouched: %v", err)
}
assertProbe(ctx, t, dsn, "1:kept,2:SET transaction_timeout = 0;,4:before the failed restore")
}
func writeTestBundle(t *testing.T, dir, name string, dump []byte) string {
t.Helper()
dumpFile := filepath.Join(dir, name+".sql")
if err := os.WriteFile(dumpFile, dump, 0o600); err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, name)
m := manifest{Version: bundleVersion, CreatedAt: time.Now().UTC(), Database: "test", DatabaseSHA: sha256Hex(dump)}
if err := writeBundle(path, m, dumpFile, nil, nil); err != nil {
t.Fatal(err)
}
if err := verifyDump(path, m.DatabaseSHA); err != nil {
t.Fatal(err)
}
return path
}
func sha256Hex(b []byte) string {
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}
// scratchDatabase creates an empty database next to base and drops it when the test ends.
func scratchDatabase(ctx context.Context, t *testing.T, base string) string {
t.Helper()
u, err := url.Parse(base)
if err != nil {
t.Fatalf("WARMBLY_TEST_DB is not a URL: %v", err)
}
suffix := make([]byte, 4)
_, _ = rand.Read(suffix)
name := "warmbly_restore_" + hex.EncodeToString(suffix)
admin, err := pgx.Connect(ctx, base)
if err != nil {
t.Fatal(err)
}
defer func() { _ = admin.Close(ctx) }()
if _, err := admin.Exec(ctx, "CREATE DATABASE "+name); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
c, err := pgx.Connect(context.Background(), base)
if err != nil {
return
}
defer func() { _ = c.Close(context.Background()) }()
_, _ = c.Exec(context.Background(), "DROP DATABASE IF EXISTS "+name+" WITH (FORCE)")
})
u.Path = "/" + name
return u.String()
}
func execSQL(ctx context.Context, t *testing.T, dsn, sql string) {
t.Helper()
c, err := pgx.Connect(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer func() { _ = c.Close(ctx) }()
if _, err := c.Exec(ctx, sql); err != nil {
t.Fatal(err)
}
}
func scalar(ctx context.Context, t *testing.T, dsn, sql string) int {
t.Helper()
c, err := pgx.Connect(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer func() { _ = c.Close(ctx) }()
var n int
if err := c.QueryRow(ctx, sql).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
func assertProbe(ctx context.Context, t *testing.T, dsn, want string) {
t.Helper()
c, err := pgx.Connect(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer func() { _ = c.Close(ctx) }()
var got string
if err := c.QueryRow(ctx, `SELECT string_agg(id || ':' || body, ',' ORDER BY id) FROM public.restore_probe`).Scan(&got); err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("restore_probe holds %q, want %q", got, want)
}
var idx int
if err := c.QueryRow(ctx, `SELECT count(*) FROM pg_indexes WHERE indexname = 'restore_probe_trgm'`).Scan(&idx); err != nil || idx != 1 {
t.Fatalf("the trigram index did not come back (count %d, err %v)", idx, err)
}
}
+203
View File
@@ -0,0 +1,203 @@
package main
import (
"bufio"
"context"
"fmt"
"io"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/jackc/pgx/v5"
)
// pgServer is what backup and restore need to know about the database they talk to.
type pgServer struct {
major int
version string
// settings is every configuration parameter name the server recognises.
settings map[string]bool
}
func readPGServer(ctx context.Context, dsn string) (pgServer, error) {
conn, err := pgx.Connect(ctx, dsn)
if err != nil {
return pgServer{}, fmt.Errorf("could not reach postgres at %s: %w", redact(dsn), err)
}
defer func() { _ = conn.Close(context.Background()) }()
var s pgServer
var num int
if err := conn.QueryRow(ctx, `SELECT current_setting('server_version_num')::int, current_setting('server_version')`).Scan(&num, &s.version); err != nil {
return pgServer{}, fmt.Errorf("could not read the server's version: %w", err)
}
s.major = num / 10000
rows, err := conn.Query(ctx, `SELECT name FROM pg_settings`)
if err != nil {
return pgServer{}, fmt.Errorf("could not read the server's settings: %w", err)
}
names, err := pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
return pgServer{}, fmt.Errorf("could not read the server's settings: %w", err)
}
s.settings = make(map[string]bool, len(names))
for _, n := range names {
s.settings[strings.ToLower(n)] = true
}
return s, nil
}
// knows reports whether a SET of this parameter succeeds on the server. Dotted
// names are extension placeholders, which any server accepts.
func (s pgServer) knows(name string) bool {
name = strings.ToLower(name)
return s.settings[name] || strings.Contains(name, ".")
}
// pgClientLayouts are where packaged client majors install: Alpine, then Debian and Ubuntu.
var pgClientLayouts = []string{"/usr/libexec/postgresql%s", "/usr/lib/postgresql/%s/bin"}
var pgVersionRe = regexp.MustCompile(`\(PostgreSQL\) (\d+)`)
// installedPGClients maps each client major found on this host to the path of name.
func installedPGClients(name string) map[int]string {
found := map[int]string{}
for _, layout := range pgClientLayouts {
dirs, _ := filepath.Glob(fmt.Sprintf(layout, "[0-9]*"))
for _, dir := range dirs {
if major := pgVersionMajor(filepath.Join(dir, name)); major > 0 {
if _, ok := found[major]; !ok {
found[major] = filepath.Join(dir, name)
}
}
}
}
if path, err := exec.LookPath(name); err == nil {
if major := pgVersionMajor(path); major > 0 {
if _, ok := found[major]; !ok {
found[major] = path
}
}
}
return found
}
func pgVersionMajor(path string) int {
out, err := exec.Command(path, "--version").Output() //nolint:gosec // path is a client binary found by layout or PATH
if err != nil {
return 0
}
m := pgVersionRe.FindSubmatch(out)
if m == nil {
return 0
}
major, _ := strconv.Atoi(string(m[1]))
return major
}
// pickPGMajor chooses the client major for a server: its own, else the
// nearest newer one. An older pg_dump refuses a newer server, so none older.
func pickPGMajor(server int, installed []int) (int, bool) {
sort.Ints(installed)
for _, m := range installed {
if m >= server {
return m, true
}
}
return 0, false
}
// pgTool resolves name (pg_dump or psql) for the server it will talk to.
func pgTool(name string, server pgServer) (string, error) {
clients := installedPGClients(name)
majors := make([]int, 0, len(clients))
for m := range clients {
majors = append(majors, m)
}
major, ok := pickPGMajor(server.major, majors)
if !ok {
have := "none is installed"
if len(majors) > 0 {
sort.Ints(majors)
parts := make([]string, len(majors))
for i, m := range majors {
parts[i] = strconv.Itoa(m)
}
have = "this host has " + strings.Join(parts, ", ")
}
return "", fmt.Errorf("the database is PostgreSQL %d and %s needs a client of that version or newer, but %s.\nInstall the PostgreSQL %d client (postgresql%d-client on Alpine, postgresql-client-%d on Debian and Ubuntu), or run this inside the backend container, which ships 16, 17 and 18.",
server.major, name, have, server.major, server.major, server.major)
}
if major != server.major {
warn("no PostgreSQL %d %s is installed, so the %d one is used.", server.major, name, major)
}
return clients[major], nil
}
// withoutUnknownSettings comments out SET lines in a plain dump's preamble that
// name a parameter the target does not have. A newer pg_dump writes settings an
// older server rejects (17 added transaction_timeout); they only reset defaults.
// Only the preamble is read: it ends at the first statement that is not a SET,
// a set_config call, a psql meta-command or a comment, and the rest is passed
// through untouched.
func withoutUnknownSettings(dump io.Reader, knows func(string) bool) (io.Reader, []string, error) {
br := bufio.NewReaderSize(dump, 64<<10)
var head strings.Builder
var skipped []string
for {
line, err := br.ReadString('\n')
if line != "" {
name, isSet := preambleSetting(line)
switch {
case isSet && !knows(name):
skipped = append(skipped, name)
head.WriteString("-- skipped, not a setting on this server: " + strings.TrimRight(line, "\r\n") + "\n")
case isSet || isPreambleLine(line):
head.WriteString(line)
default:
head.WriteString(line)
return io.MultiReader(strings.NewReader(head.String()), br), skipped, nil
}
}
if err == io.EOF {
return strings.NewReader(head.String()), skipped, nil
}
if err != nil {
return nil, nil, err
}
}
}
// preambleSetting parses `SET name = value;` and `SET name TO value;`.
func preambleSetting(line string) (string, bool) {
f := strings.Fields(line)
if len(f) < 2 || !strings.EqualFold(f[0], "SET") {
return "", false
}
name := f[1]
if (strings.EqualFold(name, "SESSION") || strings.EqualFold(name, "LOCAL")) && len(f) > 2 {
name = f[2]
}
name, _, _ = strings.Cut(name, "=")
name = strings.Trim(name, `"`)
return name, name != ""
}
func isPreambleLine(line string) bool {
t := strings.TrimSpace(line)
return t == "" || strings.HasPrefix(t, "--") || strings.HasPrefix(t, `\`) ||
strings.HasPrefix(t, "SELECT pg_catalog.set_config(")
}
// pgClientVersion is the client's own version line, for the manifest.
func pgClientVersion(path string) string {
out, err := exec.Command(path, "--version").Output() //nolint:gosec // path is resolved by pgTool
if err != nil {
return filepath.Base(path)
}
return strings.Replace(strings.TrimSpace(string(out)), " (PostgreSQL)", "", 1)
}
+90
View File
@@ -0,0 +1,90 @@
package main
import (
"io"
"strings"
"testing"
)
func TestPickPGMajorPrefersTheServersOwnThenTheNearestNewer(t *testing.T) {
cases := []struct {
server int
installed []int
want int
ok bool
}{
{16, []int{18, 16, 17}, 16, true},
{17, []int{16, 17, 18}, 17, true},
{15, []int{18, 16, 17}, 16, true},
{19, []int{16, 17, 18}, 0, false},
{16, nil, 0, false},
}
for _, c := range cases {
got, ok := pickPGMajor(c.server, c.installed)
if got != c.want || ok != c.ok {
t.Errorf("server %d with %v: got %d %v, want %d %v", c.server, c.installed, got, ok, c.want, c.ok)
}
}
}
// pg18Dump is the head of what pg_dump 18 writes, then data that only looks like a preamble line.
const pg18Dump = `--
-- PostgreSQL database dump
--
\restrict abc123
-- Dumped from database version 16.15
-- Dumped by pg_dump version 18.6
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SELECT pg_catalog.set_config('search_path', '', false);
SET row_security = off;
CREATE TABLE public.notes (body text);
COPY public.notes (body) FROM stdin;
SET transaction_timeout = 0;
\.
\unrestrict abc123
`
func pg16Knows(name string) bool {
switch name {
case "statement_timeout", "lock_timeout", "idle_in_transaction_session_timeout", "client_encoding", "row_security":
return true
}
return false
}
func TestWithoutUnknownSettingsCommentsOutOnlyThePreamblesUnknownSettings(t *testing.T) {
r, skipped, err := withoutUnknownSettings(strings.NewReader(pg18Dump), pg16Knows)
if err != nil {
t.Fatal(err)
}
out, _ := io.ReadAll(r)
want := strings.Replace(pg18Dump, "SET transaction_timeout = 0;\nSET client_encoding",
"-- skipped, not a setting on this server: SET transaction_timeout = 0;\nSET client_encoding", 1)
if string(out) != want {
t.Fatalf("filtered dump:\n%s\nwant:\n%s", out, want)
}
if len(skipped) != 1 || skipped[0] != "transaction_timeout" {
t.Fatalf("skipped %v, want [transaction_timeout]", skipped)
}
}
func TestWithoutUnknownSettingsLeavesADumpTheServerAcceptsUnchanged(t *testing.T) {
r, skipped, err := withoutUnknownSettings(strings.NewReader(pg18Dump), func(string) bool { return true })
if err != nil {
t.Fatal(err)
}
out, _ := io.ReadAll(r)
if string(out) != pg18Dump || len(skipped) != 0 {
t.Fatalf("a server that knows every setting got a changed dump (skipped %v):\n%s", skipped, out)
}
}
+2
View File
@@ -67,6 +67,8 @@ x-http-healthcheck: &http-healthcheck
services:
postgres:
image: postgres:16-alpine
# warmblyctl restore replaces every table in one transaction, which needs a larger lock table than the default.
command: ["postgres", "-c", "max_locks_per_transaction=256"]
restart: unless-stopped
environment:
POSTGRES_USER: warmbly
+7 -4
View File
@@ -39,10 +39,13 @@ RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
FROM alpine:3.23
ARG GO_TAGS=""
# postgresql-client is here for `warmblyctl backup` and `warmblyctl restore`:
# the instance bundle is a pg_dump and the restore replays it with psql, and the
# backend container is where the CLI already has PRIMARY_DB and the blob root.
RUN apk add --no-cache ca-certificates tzdata postgresql-client && \
# The PostgreSQL clients are here for `warmblyctl backup` and `warmblyctl restore`,
# which run where the CLI already has PRIMARY_DB and the blob root. One per
# supported server major: warmblyctl picks the one matching the server, because
# an older pg_dump refuses a newer server and a newer one writes SQL an older
# server rejects. They land in /usr/libexec/postgresqlNN.
RUN apk add --no-cache ca-certificates tzdata \
postgresql16-client postgresql17-client postgresql18-client && \
if echo "$GO_TAGS" | grep -qw kafka; then apk add --no-cache librdkafka; fi && \
adduser -D -u 1000 warmbly
+2
View File
@@ -281,6 +281,8 @@ services:
postgres:
restart: unless-stopped
image: postgres:16-alpine
# warmblyctl restore replaces every table in one transaction, which needs a larger lock table than the default.
command: ["postgres", "-c", "max_locks_per_transaction=256"]
environment:
POSTGRES_USER: warmbly
POSTGRES_PASSWORD: warmbly
+4 -2
View File
@@ -292,6 +292,8 @@ Segments are saved contact audiences: a condition list plus per-contact manual o
Hosted lead-capture forms; see the [forms guide](/guides/forms/). A form's `redirect_url` must be on one of the workspace's own domains when it is set or changed (`redirect_target_not_own_domain` otherwise), and a form with one carries `redirect_url_active`, `false` when the saved address no longer qualifies and visitors see the success message instead. Contact scopes cover them because a form exists to create contacts. Minting a personalized link is a `GET` on top of an upsert, so retries always return the same token. The `/forms/domain` routes are the workspace-wide custom forms domain and additionally need the `manage_settings` organization permission for JWT callers; `PUT` resolves the record as part of saving, and only a verified domain is ever used to build a URL. A new domain verifies only with the workspace's ownership `TXT` record beside the `CNAME`; the status carries it as `txt_name` and `txt_value`, and a domain another workspace holds is refused with `forms_domain_taken`. On the custom domain the forms service serves only that workspace's forms. The public page (`/f/:public_id`), its submit endpoint and the embed loader (`/forms.js`) are served by the standalone forms service on its own host (`FORMS_DOMAIN`), not the API origin, and take no authentication; the unguessable public id is the capability, and the JSON endpoints additionally require the render token the page shell carries.
`GET /forms/:id/submissions` is keyset-paginated newest first, ordered by creation time and then id, so submissions that arrive in the same instant are neither skipped nor repeated at a page boundary. It answers `data` plus `pagination` (`next_cursor`, `has_more`); pass `?limit=` (1 to 100, default 50) and `?cursor=` the opaque `pagination.next_cursor` for the next page. A cursor this API did not issue returns `400`. The earlier `?before=` (an RFC 3339 timestamp, returning submissions strictly older than it) and the top-level `has_more` are still accepted and returned for existing clients; sending both `cursor` and `before` returns `400`.
### Lead sync
Saved Google Sheets sources that upsert contacts on demand. Gated under the contact write scope because a sync ultimately writes contacts. Nothing syncs on a timer: a source only runs when `/lead-sync/sources/:id/sync` is called.
@@ -784,8 +786,8 @@ Alongside them, `api_url` is this API's own public base (for a copyable example
- `GET /admin/organizations/:id/plan-offers` (`view_organizations`) lists the private plans offered to a workspace as `{"offers": [...]}`, lapsed ones included, each with `plan_id`, `reason`, `granted_by`, `granted_at`, `expires_at`, `plan` and `active`. `PUT /admin/organizations/:id/plan-offers/:planId` (`manage_organizations`) offers or renews one with `{"reason": "...", "expires_at": null}`: the reason is required (at most 500 characters), an end date must be in the future, and a public plan or the Warmup plan is refused with `400`. `DELETE` on the same path withdraws it and answers `204`. Every offer change writes the admin audit log. These routes need an MFA-verified admin session like the rest of `/admin/*`, and repeating one is safe. See [plans offered to a workspace](/guides/billing/#plans-offered-to-a-workspace).
- `GET /admin/organizations` and `GET /admin/organizations/:id` require `view_organizations`. Their acquisition fields include the nullable `referrer_host` saved at signup, alongside UTM source, medium, campaign and landing path. The list accepts an exact `referrer_host` filter as well as its existing acquisition filters. An absent source is unknown, not proof of a direct visit. See [signup source attribution](/guides/analytics/#signup-source-attribution-platform-admins).
- `GET /admin/warmup-content/overview` includes effective generation controls and requires `view_warmup_pool`; `PUT /admin/warmup-content/settings` writes effective controls with `manage_settings`. These are platform-admin routes, not organization API-key scopes. See [effective generation policy and stopping](/development/warmup-generation/).
- `POST /admin/organizations/:id/sample-data`: `manage_testers` and an MFA-verified admin session. Adds the [reviewer sample dataset](/development/accounts-and-access/#adding-sample-data) only to an active dedicated Test workspace. No request body. Returns `200` with `organization_id`, `created` (false when already seeded) and `seeded_at`; retries preserve edits and deletions. An ordinary workspace, revoked or expired tester, or missing/expired managed Test grant returns `400`. Inserts and the admin audit marker commit together. No mailbox connection, email delivery, external request or credit spend occurs.
- `GET /admin/testers`: `view_users` and an MFA-verified admin session. Each tester in `data` may include `test_workspace_id` for its owned dedicated Test workspace and `sample_data_seeded_at` when populated. These fields are absent for a tester merely attached to an ordinary workspace. Every tester carries `created_as_tester`, true for an account the tester flow created.
- `POST /admin/organizations/:id/sample-data`: `manage_testers` and an MFA-verified admin session. Adds the [reviewer sample dataset](/development/accounts-and-access/#adding-sample-data) only to an active dedicated Test workspace. No request body. Returns `200` with `organization_id`, `created` (false when already seeded) and `seeded_at`; retries preserve edits and deletions. An ordinary workspace, revoked tester, or missing/expired managed Test grant returns `400`; whether the owner's initial password is outstanding, replaced or expired does not matter. Inserts and the admin audit marker commit together. No mailbox connection, email delivery, external request or credit spend occurs.
- `GET /admin/testers`: `view_users` and an MFA-verified admin session. Each tester in `data` may include `test_workspace_id` for its owned dedicated Test workspace (with several, the one that can still take sample data, else the oldest) and `sample_data_seeded_at` when populated. `sample_data_eligible` is true when that workspace passes the same check as `POST /admin/organizations/:id/sample-data`. These fields are absent for a tester merely attached to an ordinary workspace. Every tester carries `created_as_tester`, true for an account the tester flow created.
- `POST /admin/testers/:id/password`: `manage_testers`, an MFA-verified admin session and a recent confirmation. No request body. Gives an account created as a tester a newly generated [initial password](/development/accounts-and-access/#initial-passwords), valid 48 hours and to be changed at first sign-in. It replaces whatever password the account held and signs out its sessions and remembered devices first. Returns `200` with `user_id`, `password` (shown only in this response), `password_expires_at` and `password_change_required: true`, and writes `reissue_tester_password` to the admin audit log. An account that does not exist or was not created as a tester returns `404` and nothing changes. Not idempotent by design: every call issues a different password, and only the last one works.
- `PUT /admin/users/:id/login-code-exempt`: `manage_testers`, an MFA-verified admin session and a recent confirmation. Body `{"exempt": true, "reason": "..."}` excuses that account from the [emailed login code](/development/accounts-and-access/#excusing-one-account); the reason is required (at most 500 characters) and the acting admin is recorded as the grantor. `{"exempt": false}` ends the exemption, clears the password of an account created as a tester and signs out every session the account holds. Both write the admin audit log, and setting the same state again is safe. An unknown account returns `404`. `GET /admin/users/:id` and `/preview` carry `login_code_exempt`, `login_code_exempt_reason` and `login_code_exempt_at`
- `GET /admin/sends/holds`: `view_campaigns` and an MFA-verified admin session. Lists mailboxes held from sending (up to `limit`, default 100, at most 500), switched-on first, with a `summary` of `total`, `active`, `inactive` and `by_reason`. Each row carries the held send's `held_task_id`, `recipients`, `message_id` and timestamps, and `resolvable` with a `blocker` when it is not. An unknown hold younger than ten minutes is a send in progress and is not listed.
+1
View File
@@ -205,6 +205,7 @@ Avatars, OAuth app logos, form logos, covers and backgrounds, and images placed
| `sso_link_expired` | 400 | The pending token from a `link_required` sign-in is unknown, expired (ten minutes), already used, or has had three wrong passwords. Start the provider sign-in again |
| `password_reused` | 400 | `POST /auth/login/initial-password` was given the issued initial password itself. Choose a password of your own |
| `password_change_expired` | 400 | The pending token from a `password_change_required` sign-in is unknown, expired (ten minutes) or already used, or the issued password it was for has run out. Sign in again; an issued password past its 48 hours is refused like a wrong password, so ask whoever issued it for a new one |
| `initial_password_web_only` | 403 | `POST /auth/login` was given a valid issued initial password by a Warmbly iOS app build that cannot choose a new password in the app. Nothing was opened. Finish the first sign-in on the web, where you choose your own password, then sign in to the app with it, or update the app. Clients that send `supports_password_change: true`, browsers and API clients get the `password_change_required` step instead |
| `password_change_required` | 403 | `POST /auth/sso/link` was given an issued initial password. Sign in with email and password first, which asks you to choose your own, then link the provider |
| `reset_link_invalid` | 401 | `POST /auth/reset-password/session` or `/confirm` was given a reset link or reset session that expired, was already used, or was issued before the password last changed. Request a new link |
| `api_credentials_revoke_unsupported` | 400 | `POST /auth/reset-password/confirm` or `POST /auth/me/password` asked to end connected apps and API keys (`revoke_api_credentials`) on a server that cannot. The password was not changed. Send it again without that option, then remove apps and keys from Settings |
@@ -29,6 +29,7 @@ Auth: public (not available to API keys).
| `password` | string | yes | Account password. |
| `turnstile` | string | yes, unless `app_attest` is sent | Cloudflare Turnstile token. |
| `app_attest` | object | no | The native app's [App Attest proof](#app-attest-native-apps), sent instead of `turnstile`. |
| `supports_password_change` | boolean | no | Sent by the iOS app to say it can take a `password_change_required` answer. |
```json
{
@@ -57,6 +58,8 @@ When the password is an [issued initial password](/development/accounts-and-acce
}
```
The iOS app takes this step when it sends `supports_password_change: true`. An earlier app build, which does not send it, is answered `initial_password_web_only` instead, so its holder finishes the first sign-in on the web. Browsers and API clients always get the step above.
### Choose a password after an issued one
`POST /auth/login/initial-password`
@@ -129,7 +129,7 @@ A password Warmbly generates for somebody else is an initial password: a way in
- **It lasts at most 48 hours from issuance.** The database caps the expiry on every write, so no path can store a longer one, and an expiry can never be moved later or cleared without writing a new password. Upgrading to the release that introduced this shortens any issued password that had longer left to 48 hours from the upgrade. Extending an issued password is not possible: issue a new one instead, which starts its own 48 hours.
- **Issuing a new one.** A tester's address cannot be used for a second tester, so when a password ran out before it was used, or the holder lost it, select **Issue new password** on that tester's row under **Testers**. It is offered only for accounts created there. It needs the platform `manage_testers` permission, an MFA-verified admin session and a recent confirmation of your own identity, and it asks you to confirm. The account gets a newly generated password valid for 48 hours, which has to be changed at first sign-in like the first one. Whatever password the account held before stops working, including one the holder chose, and every session and remembered device it had is signed out. The password is shown once, and the action is recorded in the admin audit log as `reissue_tester_password`. It does not extend the tester's Test workspace grant.
- **It never opens a session.** Signing in with it, while it is still valid, asks the holder to choose their own password. That step needs the captcha the sign-in already passed, refuses a password on the breached list or the issued password itself, signs out every other session, and then continues the sign-in as usual: the emailed code where the account needs one, and two-factor authentication where it is enrolled. From then on the account works with the password the holder chose, which does not expire.
- **It never opens a session.** Signing in with it, while it is still valid, asks the holder to choose their own password. That step needs the captcha the sign-in already passed, refuses a password on the breached list or the issued password itself, signs out every other session, and then continues the sign-in as usual: the emailed code where the account needs one, and two-factor authentication where it is enrolled. From then on the account works with the password the holder chose, which does not expire. The iOS app asks for the new password in the app. An app build from before that step existed is told to finish the first sign-in on the web (`initial_password_web_only`), and signs in normally with the chosen password afterwards.
- **Once it runs out, it is refused exactly like a wrong password.**
- A provider sign-in (Google, Apple, single sign-on) cannot be linked by presenting an initial password; sign in with the password first.
@@ -143,7 +143,7 @@ The managed plan expires when the issued password would have expired, whether or
### Adding sample data
In **Admins > Testers**, select **Add sample data** beside an active tester who owns a dedicated **Test** workspace, then confirm. This works for both newly created workspaces and existing dedicated Test workspaces after upgrading. Nothing is added automatically during deployment or account creation. The action requires the platform `manage_testers` permission and an MFA-verified admin session. The backend checks the Test category, the owner's current exemption, that the owner's issued password is still outstanding (not yet expired or replaced), and an unexpired managed Test grant. Testers attached to ordinary customer workspaces cannot use it.
In **Admins > Testers**, select **Add sample data** beside an active tester who owns a dedicated **Test** workspace, then confirm. This works for both newly created workspaces and existing dedicated Test workspaces after upgrading. Nothing is added automatically during deployment or account creation. The action requires the platform `manage_testers` permission and an MFA-verified admin session. The backend checks the Test category, the owner's current exemption and an unexpired managed Test grant. The owner's initial password does not matter: the action stays available after the tester replaces it at first sign-in, or after you issue a new one, until the grant ends. Revoking the tester, or the grant expiring, ends it. A tester who owns more than one Test workspace is listed with the one that can still take sample data. Testers attached to ordinary customer workspaces cannot use it.
The dataset contains:
@@ -604,6 +604,8 @@ sudo tar czf blobs.tgz -C /var/lib/warmbly blobs # 2. uploads and stored me
sudo cp /etc/warmbly/*.env somewhere-safe/ # 3. the env files, above all the two encryption keys
```
Use the `pg_dump` and `psql` of your server's major version: a newer `pg_dump` writes settings an older server rejects, and an older one refuses a newer server. `warmblyctl backup` and `restore` pick the matching client from `/usr/lib/postgresql/NN/bin` themselves.
Restore into an empty database before the backend starts, then start it and it applies only what is newer than the dump. See [restoring](/development/deployment-guide/#restoring); replace the `docker compose exec` commands with plain `psql`.
## Troubleshooting
@@ -241,7 +241,13 @@ docker compose -p warmbly exec backend warmblyctl restore --file /data/blobs/war
docker compose -p warmbly restart
```
The restore empties the schema and replays the dump, so it replaces everything currently on that instance and asks you to type `restore` first. It then runs `ANALYZE`, since a dump carries no planner statistics; a failure there only warns.
The restore replaces the schema with the dump's, so it replaces everything currently on that instance and asks you to type `restore` first. It runs as one transaction: a load that fails at any point rolls back and leaves the instance exactly as it was. It then runs `ANALYZE`, since a dump carries no planner statistics; a failure there only warns.
Both commands use the PostgreSQL client matching the server's major version. The backend image ships the 16, 17 and 18 clients, so the bundled PostgreSQL 16 and an external 17 or 18 all work. For any other version the nearest newer client is used, and when there is none the command stops and names the package to install. The manifest records which server was dumped and with which `pg_dump`.
A bundle written by a newer `pg_dump` still restores onto an older server. Settings the destination does not have, such as the `transaction_timeout` that PostgreSQL 17 added, are skipped from the dump's header, and the restore lists what it skipped.
Holding the whole replacement in one transaction takes a lock on every table and index it drops and creates. The bundled PostgreSQL in the installer and Compose files runs with `max_locks_per_transaction=256` for this. A server you run yourself with the default lock table usually has room, but one with a smaller lock table (`max_connections` well below the default 100) can refuse with `out of shared memory`. The restore then changes nothing and says to raise `max_locks_per_transaction` (for example to 1024).
Before it does anything it compares the bundle's `CREDENTIALS_ENCRYPTION_KEY` and `KMS_LOCAL_MASTER_KEY` against the destination's and refuses to continue when they differ, printing the two lines to put in `.env`. That check is the point of the command: without it a restore looks like it worked and every mailbox fails to authenticate days later, with no error that names the cause.
+5 -3
View File
@@ -70,7 +70,7 @@ A command that would set a password refuses on a non-TTY unless you passed `--pa
| `WARMBLY_API_KEY` | every API command | The command stops and explains where a key comes from |
| `WARMBLY_API_URL` | every API command | Falls back to the instance's own `API_PUBLIC_URL`, then the hosted service |
`backup` and `restore` also need `pg_dump` and `psql`, which the backend image ships for exactly this reason. Inside the backend container all of these are already set, except the API key, which is yours. Outside it, export what the command needs, and match `AUTH_SECRET` to the backend's exactly.
`backup` and `restore` also need `pg_dump` and `psql` of the server's major version, or a newer one. The backend image ships the 16, 17 and 18 clients for exactly this reason, and outside it the command looks in `/usr/libexec/postgresqlNN` (Alpine), `/usr/lib/postgresql/NN/bin` (Debian and Ubuntu) and then `PATH`. Inside the backend container all of these are already set, except the API key, which is yours. Outside it, export what the command needs, and match `AUTH_SECRET` to the backend's exactly.
Remote administrator commands require none of those instance secrets. They do not read `API_PUBLIC_URL`, `WARMBLY_API_URL` or `WARMBLY_API_KEY` to choose an instance or authenticate. Their only configuration-directory dependency is the operating system's user configuration location (`XDG_CONFIG_HOME` on Linux when set).
@@ -464,7 +464,7 @@ docker compose -p warmbly cp backend:/data/blobs/warmbly.tar.gz ./warmbly.tar.gz
`/data/blobs` is the one path the container and the host both see, so it is the hand-off. Move the bundle out and delete it: `backup` excludes its own output from the archive, but a bundle left behind is swept into the next one. The delete is chained with `&&` so a failed copy cannot take the only copy with it.
Writes one bundle holding the three things that only restore together: a `pg_dump` of the database, the blob root (message bodies, attachments, avatars), and the encryption keys.
Writes one bundle holding the three things that only restore together: a `pg_dump` of the database, the blob root (message bodies, attachments, avatars), and the encryption keys. The dump is taken with the client matching the server's major version, and the manifest records both versions.
That combination is the whole reason this is a command rather than a documented list of steps. A dump alone restores an instance whose every mailbox credential decrypts to nothing, because the ciphertext is in the database and the key that opens it is in `.env`. The blob root alone restores bodies nothing points at.
@@ -490,7 +490,9 @@ docker compose -p warmbly exec backend warmblyctl restore --file /data/blobs/war
docker compose -p warmbly restart
```
Empties the schema and replays the bundle into it, so it replaces every organization, user, campaign and mailbox currently on the instance. It prints what the bundle holds and asks you to type `restore` before it does; `--yes` skips that for scripts.
Replaces the schema with the bundle's, so it replaces every organization, user, campaign and mailbox currently on the instance. It prints what the bundle holds and asks you to type `restore` before it does; `--yes` skips that for scripts.
The drop and the load run as one transaction, so a dump that fails to load leaves the instance exactly as it was. A bundle written by a newer `pg_dump` restores onto an older server: settings the server does not have (PostgreSQL 17 added `transaction_timeout`) are skipped from the dump's header and listed. A server with a small lock table can refuse the transaction with `out of shared memory`; raise `max_locks_per_transaction` and run it again. [Data control](/development/data-control/#restore) has the details.
Before anything is written it compares the bundle's `CREDENTIALS_ENCRYPTION_KEY` and `KMS_LOCAL_MASTER_KEY` against this host's, and refuses to continue when they differ, printing the two lines to put in `.env` first. That check is the point of the command: without it a restore looks like it worked, and every mailbox fails to authenticate days later with no error that names the cause.
+1 -1
View File
@@ -182,7 +182,7 @@ Because each recipient's link is unique, those numbers are per contact rather th
## Submissions
The **Submissions** tab is a table of everyone who engaged, newest activity first and live: a new submission appears for everyone with the form open, without a refresh. Alongside completed responses it lists people who opened a personalized link but have not finished, marked **In progress** with the page they reached, so a stalled lead is visible instead of invisible. Search and the status filter narrow the list; click a response for every answer; the trash button deletes it (the contact it created is kept), and selecting rows offers a bulk delete.
The **Submissions** tab is a table of everyone who engaged, newest activity first and live: a new submission appears for everyone with the form open, without a refresh, and older pages you loaded stay in place while a deleted response drops out of them. Alongside completed responses it lists people who opened a personalized link but have not finished, marked **In progress** with the page they reached, so a stalled lead is visible instead of invisible. Search and the status filter narrow the list; click a response for every answer; the trash button deletes it (the contact it created is kept), and selecting rows offers a bulk delete.
Each submitted contact carries `form` as its first-touch source, gets a "Submitted a form" entry on its activity timeline, and can be filtered in segments by source. The form's page views, submission count and conversion rate show on the Forms list.
+3 -1
View File
@@ -414,7 +414,9 @@ A mailbox that is paused or reconnecting, held after a send problem, or cooling
A new email or a reply to an address on your [suppression list](/guides/unsubscribe/#the-suppression-list), directly or through its domain, is refused before it is queued, with a message naming the address and the entry that matched it. A forward is not: it passes on mail you already received to people you picked by hand, so you can forward a lead to a client or colleague whose domain you keep out of campaigns. Fair use still counts a forward's recipients.
When a send is refused for one of those reasons, it is not sent and the mailbox owner sees an error naming the cause and the limit it reached, so they can send it later or from another mailbox. If Warmbly cannot confirm the email reached the mailbox's sending worker, the error says so; check **Sent** before sending it again.
When a send is refused for one of those reasons, it is not sent and the mailbox owner sees an error naming the cause and the limit it reached, so they can send it later or from another mailbox. If Warmbly cannot confirm the email reached the mailbox's sending worker, the error says so; check **Sent** before sending it again. Warmbly never sends that email a second time on its own. If no worker started it within about 30 minutes, it never will: the owner is told it was not sent, and the mailbox sends again.
A send that was interrupted before it was handed to a worker, for example by a restart, is picked up again within a few minutes and goes out once. One that cannot go out within an hour of being sent fails instead, and the owner is told to send it again.
</Fold>
@@ -86,7 +86,7 @@ The data is split into groups. Every export includes **Workspace**; the rest are
| Assistant | Remie sessions and messages (including conversations held in [Slack](/guides/slack/)), skills, MCP servers, and AI settings |
| Warmup | Warmup participation, routing rules, statistics and inbox placement history, appeals, expiring reconnect-recognition fingerprints, and the standing of every penalised address, current or removed, so a move is not a way past a block |
| Inbox | Unified inbox threads, message bodies, conversation labels, mailbox sync state, and completed automatic-tagging verdicts with their raw probabilities |
| Send history | Queued and completed send tasks with their payloads |
| Send history | Queued and completed send tasks with their payloads, and the click-tracking links of campaign sends and direct mail |
| Delivery events | Bounces, complaints, opens, clicks, [placement tests](/guides/placement-tests/) and batches with where each copy landed, and website page views with the browser records that tie them to contacts |
| Verification evidence | What real mail showed about each contact's address (deliveries, opens, replies, bounces), so verdicts and confidence survive the move |
| Logs | Audit log, campaign logs, and notifications |
@@ -94,6 +94,8 @@ The data is split into groups. Every export includes **Workspace**; the rest are
Inbox, send history, delivery events, and logs are the ones that grow without limit. Turn them off and you get a small archive that still rebuilds a working workspace; leave them on for a faithful copy.
Click-tracking links travel with **Send history**, for campaign sends and [direct mail](/guides/mailboxes/#tracking-direct-mail) alike. Leave the group out and the links already in recipients' inboxes stop resolving on the destination; per-link clicks recorded under **Campaigns** still arrive, without the link they came through.
The pending warmup-filing queue stays on its source instance. The destination resyncs mail and files recognized warmup messages using its own workers and the mailbox's placement setting.
Campaign contacts waiting for an uncertain send retain that protection with **Campaigns**, even when you omit **Send history**. Imported uncertain commands cannot execute again. Without their send history, those contacts stay protected because the destination cannot confirm the original outcome. Manual resume does not remove this protection.
@@ -138,6 +140,8 @@ Warmbly does not keep it on either instance. If you lose it, the credentials ins
Leave the option off and the credential fields travel empty. Everything else imports normally, and each mailbox arrives marked for reconnection: open it on the destination and sign in again, exactly like connecting it the first time.
Integrations follow the same rule. Their API keys, OAuth tokens and settings are re-sealed for the destination when the archive carries credentials, and keep working. An integration whose credentials did not come across, because the export ran without them or the source could not decrypt one, arrives marked for reconnection, and the import report names each one by provider and label. A [Calendly](/guides/meetings/), Cal.com or [Clay](/guides/clay/) connection in that state also stops accepting deliveries at its old inbound URL; reconnect it and give the provider the new URL.
Either way, an SMTP/IMAP mailbox keeps its [server certificate](/guides/mailboxes/#servers-with-their-own-certificate) choice and the fingerprints it trusts, because they are not secret and describe the same server on any instance.
Webhook signing secrets follow the same rule, including the one an [OAuth app](/api/oauth/#receiving-events-as-an-app-webhooks) signs its deliveries with. Without the option, rotate the app's webhook secret on the destination before relying on its deliveries.
@@ -225,7 +229,7 @@ An import runs as one transaction. If anything fails, nothing lands and the work
## After a move
- **Reconnect any mailbox that needs it.** Mailboxes without credentials show as needing a reconnect in the mailbox list.
- **Point your tracking domain at the new instance.** Click links already delivered keep resolving as long as the domain follows. Tracking domains on mailboxes and campaigns arrive unverified: the destination re-checks mailbox ones hourly, and a campaign's override is verified again from the campaign's settings. Verifying on the destination also needs its ownership `TXT` record: the value belongs to the workspace on the instance that issued it, so publish the one the destination shows, at `_warmbly.<tracking domain>` or once at `_warmbly.<domain>`.
- **Point your tracking domain at the new instance.** Click links already delivered keep resolving as long as the archive included **Send history** and the domain follows. Tracking domains on mailboxes and campaigns arrive unverified: the destination re-checks mailbox ones hourly, and a campaign's override is verified again from the campaign's settings. Verifying on the destination also needs its ownership `TXT` record: the value belongs to the workspace on the instance that issued it, so publish the one the destination shows, at `_warmbly.<tracking domain>` or once at `_warmbly.<domain>`.
- **Keep the old instance's image host reachable for a while, or re-place the images.** The image library and its files travel, but mail already sent carries the old instance's address inside each `<img src>`, so those images keep loading from there. Bodies you edit after the move pick up the new address.
- **Register the destination's callback on your own OAuth apps.** On the [Partnership plan](/guides/partnership/), the workspace's own Google and Microsoft apps travel, sealed like a mailbox password, so mailboxes they signed in keep refreshing. Exported without credentials, an app arrives without its secret; save it again under **Settings > Mailbox sign-in**. New sign-ins on the destination need its callback added to each app, unless the app forwards from your own domain. Open sign-in links do not travel; create new ones on the destination. An import into a workspace that already has its own app for the same provider is refused, since a mailbox can only be refreshed by the app that signed it in; remove the destination's app first.
- **Check your inbox vendor connections.** Each vendor's API key is sealed with the workspace's own key, so it travels like a mailbox password: in an export with credentials it is re-sealed for the destination and keeps working. Exported without credentials, the connection arrives without its key; edit it and enter the key again. A vendor reconnect's progress stays on the instance that ran it: on the destination a mailbox shows no reconnect yet, and the [automatic reconnect](/guides/mailboxes/#automatic-reconnect-for-vendor-mailboxes) treats it as never asked. Which Google permissions a mailbox's sign-in carries travels with its token.
@@ -234,7 +238,7 @@ An import runs as one transaction. If anything fails, nothing lands and the work
- **Repoint your forms domain too.** A custom forms domain travels with the archive, but its verification does not: the record still points at the old instance. Update the `CNAME` and publish the ownership `TXT` value the destination shows, and the hourly re-check picks it up. Until then form links fall back to the shared host rather than breaking.
- **Check campaign schedules.** Per-contact progress travels, so a running campaign resumes at the step it reached rather than restarting. A campaign's choice to send in each lead's local time travels with it, and the destination works out every contact's location again from the fields that came across, usually within minutes of the import; until it has, [region segments](/guides/regions-and-timezones/) on the destination count those contacts as having no location.
- **Expect the daily send counters to be honoured.** Today's counts come across, so a mailbox cannot double its volume by being migrated mid-day.
- **Re-authorize integrations** if you exported without credentials. A Salesforce connection syncs nothing until it is reauthorized on the destination; see [Salesforce](/guides/salesforce/#what-moves-with-a-workspace-export).
- **Reconnect each integration the import report names.** Those arrived without their credentials and show as needing a reconnect under **Integrations**. A Salesforce connection syncs nothing until it is reauthorized on the destination; see [Salesforce](/guides/salesforce/#what-moves-with-a-workspace-export).
- **Reconnect Slack, then link again.** A Slack install belongs to the Slack app of the instance that made it, and Slack sends that app's messages to that instance only. Reconnect under **Integrations > Slack** on the destination, and each member links their Slack account again (the bot offers a link button the first time they message it). Assistant conversations held in Slack arrive in the dashboard history, where you can continue them.
- **Check HubSpot or Pipedrive mode.** The mode, its choices and the links between Warmbly and CRM records travel, so the destination keeps updating the same records instead of creating duplicates. Its token only keeps working when the archive carried credentials and the destination uses the same HubSpot or Pipedrive app; otherwise reconnect the CRM there. Writes still queued on the source do not travel, so let sync health ([HubSpot](/guides/hubspot/#sync-health-and-fixing-errors), [Pipedrive](/guides/pipedrive/#sync-health-and-fixing-errors)) show nothing pending before exporting. The destination starts its own pull, and in Pipedrive mode registers its own webhooks.
- **Rotate each webhook's signing secret.** The secret is encrypted at rest with the source instance's key, so it travels only in an export that carries credentials. Exported without them, the endpoint arrives with no secret and its deliveries will not verify at your receiver. Open each endpoint and use **Rotate secret**, then put the new value in your receiver.
+4
View File
@@ -47951,6 +47951,10 @@
},
"app_attest": {
"$ref": "#/components/schemas/AppAttestProof"
},
"supports_password_change": {
"type": "boolean",
"description": "Login only. Sent by the iOS app to say it can take a password_change_required answer. An iOS app build that omits it is answered initial_password_web_only for an issued initial password."
}
}
},
+36 -11
View File
@@ -16,6 +16,7 @@ import (
"github.com/warmbly/warmbly/internal/errx"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/pkg/imagetype"
"github.com/warmbly/warmbly/internal/utils/paging"
)
// formScope pulls the org and the :id path param once per handler.
@@ -381,21 +382,45 @@ func (h *Handler) ListFormSubmissions(c *gin.Context) {
}
limit = n
}
var before *time.Time
if raw := c.Query("before"); raw != "" {
t, err := time.Parse(time.RFC3339Nano, raw)
if err != nil {
errx.Handle(c, errx.New(errx.BadRequest, "before must be an RFC 3339 timestamp"))
return
}
before = &t
}
out, hasMore, xerr := h.FormService.ListSubmissions(c.Request.Context(), orgID, id, limit, before)
after, xerr := submissionsPosition(c.Query("cursor"), c.Query("before"))
if xerr != nil {
errx.Handle(c, xerr)
return
}
c.JSON(http.StatusOK, gin.H{"data": out, "has_more": hasMore})
out, hasMore, xerr := h.FormService.ListSubmissions(c.Request.Context(), orgID, id, limit, after)
if xerr != nil {
errx.Handle(c, xerr)
return
}
page := models.CPagination{HasMore: hasMore}
if hasMore && len(out) > 0 {
last := out[len(out)-1]
page.NextCursor = paging.EncodeTime(last.CreatedAt, last.ID)
}
// has_more stays at the top level for clients built before pagination.
c.JSON(http.StatusOK, gin.H{"data": out, "has_more": hasMore, "pagination": page})
}
// submissionsPosition reads the opaque cursor, or the RFC 3339 `before`
// timestamp earlier clients send.
func submissionsPosition(cursor, before string) (*models.FormSubmissionPosition, *errx.Error) {
switch {
case cursor != "" && before != "":
return nil, errx.New(errx.BadRequest, "send cursor or before, not both")
case cursor != "":
at, id, xerr := paging.DecodeTimeCursor(cursor)
if xerr != nil {
return nil, xerr
}
return &models.FormSubmissionPosition{At: at, ID: id}, nil
case before != "":
t, err := time.Parse(time.RFC3339Nano, before)
if err != nil {
return nil, errx.New(errx.BadRequest, "before must be an RFC 3339 timestamp")
}
return &models.FormSubmissionPosition{At: t}, nil
}
return nil, nil
}
func (h *Handler) DeleteFormSubmission(c *gin.Context) {
@@ -0,0 +1,40 @@
package handler
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/errx"
"github.com/warmbly/warmbly/internal/utils/paging"
)
func TestSubmissionsPositionReadsBothCursorForms(t *testing.T) {
at := time.Date(2026, 3, 1, 12, 0, 0, 123456000, time.UTC)
id := uuid.New()
pos, xerr := submissionsPosition(*paging.EncodeTime(at, id), "")
if xerr != nil || pos == nil || !pos.At.Equal(at) || pos.ID != id {
t.Fatalf("opaque cursor: %+v %v", pos, xerr)
}
pos, xerr = submissionsPosition("", at.Format(time.RFC3339Nano))
if xerr != nil || pos == nil || !pos.At.Equal(at) || pos.ID != uuid.Nil {
t.Fatalf("legacy before: %+v %v", pos, xerr)
}
if pos, xerr = submissionsPosition("", ""); xerr != nil || pos != nil {
t.Fatalf("first page: %+v %v", pos, xerr)
}
for name, args := range map[string][2]string{
"garbage cursor": {"not-a-cursor", ""},
"uuid cursor": {*paging.EncodeUUID(id), ""},
"garbage before": {"", "yesterday"},
"cursor and before": {*paging.EncodeTime(at, id), at.Format(time.RFC3339Nano)},
} {
if _, xerr := submissionsPosition(args[0], args[1]); xerr == nil || xerr.Code != errx.BadRequest {
t.Errorf("%s: want 400, got %v", name, xerr)
}
}
}
+1 -3
View File
@@ -3,7 +3,6 @@ package aitools
import (
"context"
"encoding/json"
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/models"
@@ -304,8 +303,7 @@ func (d Deps) listFormSubmissions(ctx context.Context, inv Invocation, args json
if limit <= 0 || limit > 100 {
limit = 50
}
var before *time.Time
out, hasMore, xerr := d.Forms.ListSubmissions(ctx, inv.OrgID, id, limit, before)
out, hasMore, xerr := d.Forms.ListSubmissions(ctx, inv.OrgID, id, limit, nil)
if xerr != nil {
return "", fromErrx(xerr)
}
+11
View File
@@ -7,6 +7,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"strings"
"time"
"github.com/google/uuid"
@@ -160,6 +161,16 @@ func (s *authService) CompleteInitialPassword(ctx context.Context, data *Initial
return s.continuePasswordLogin(ctx, pend.UserID, pend.Email, data.DeviceToken, verdict, ipaddr, userAgent)
}
// appCannotChangePassword is a native app build that predates the change step:
// it identifies as the app (an App Attest proof or its URLSession User-Agent)
// without declaring supports_password_change. Browsers and API clients are unaffected.
func appCannotChangePassword(data *AuthData, userAgent string) bool {
if data.SupportsPasswordChange {
return false
}
return data.AppAttest != nil || (strings.HasPrefix(userAgent, "Warmbly/") && strings.Contains(userAgent, " CFNetwork/"))
}
// passwordDigest names a stored hash without keeping it in the cache.
func passwordDigest(hash string) string {
sum := sha256.Sum256([]byte(hash))
@@ -9,6 +9,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/app/appattest"
"github.com/warmbly/warmbly/internal/app/token"
"github.com/warmbly/warmbly/internal/config"
"github.com/warmbly/warmbly/internal/errx"
@@ -349,3 +350,73 @@ func TestInitialPasswordReissueVoidsOpenChallenge(t *testing.T) {
t.Fatal("a challenge opened before the reissue replaced the reissued password")
}
}
// Only an app build that does not declare the change step is told to finish on the web.
func TestAppCannotChangePassword(t *testing.T) {
const appUA = "Warmbly/412 CFNetwork/3826.500.131 Darwin/25.0.0"
const safariUA = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"
proof := &appattest.Proof{KeyID: "k", Assertion: "a", Challenge: "c"}
cases := []struct {
name string
data AuthData
ua string
want bool
}{
{"older app by user agent", AuthData{}, appUA, true},
{"older app by app attest", AuthData{AppAttest: proof}, "", true},
{"current app", AuthData{SupportsPasswordChange: true}, appUA, false},
{"current app with app attest", AuthData{AppAttest: proof, SupportsPasswordChange: true}, appUA, false},
{"browser", AuthData{}, safariUA, false},
{"api client", AuthData{}, "curl/8.7.1", false},
{"another app on the same stack", AuthData{}, "OtherApp/3 CFNetwork/3826.500.131 Darwin/25.0.0", false},
}
for _, tc := range cases {
if got := appCannotChangePassword(&tc.data, tc.ua); got != tc.want {
t.Errorf("%s: got %v, want %v", tc.name, got, tc.want)
}
}
}
const olderAppUA = "Warmbly/412 CFNetwork/3826.500.131 Darwin/25.0.0"
// An older app build gets a stable refusal for an issued password, and nothing is opened.
func TestOlderAppIsSentToTheWebForAnInitialPassword(t *testing.T) {
s, repo, tokens := initialFixture(t, 48*time.Hour)
res, err := s.LoginStart(context.Background(), &AuthData{Email: repo.acct.email, Password: issuedPassword}, testIP, olderAppUA)
if err != errx.ErrInitialPasswordWebOnly || res != nil {
t.Fatalf("an older app answered %+v, %v; want ErrInitialPasswordWebOnly", res, err)
}
if tokens.sessions != 0 {
t.Fatal("an older app opened a session with an issued password")
}
// The issued password still works where the change step is understood.
web, err := signIn(s, repo.acct.email, issuedPassword)
if err != nil || !web.PasswordChangeRequired {
t.Fatalf("the web sign-in after an older app answered %+v, %v", web, err)
}
}
// A wrong password from an older app is refused as a wrong password, so the refusal reveals nothing.
func TestOlderAppWrongPasswordIsACredentialsError(t *testing.T) {
s, repo, _ := initialFixture(t, 48*time.Hour)
_, err := s.LoginStart(context.Background(), &AuthData{Email: repo.acct.email, Password: "not-the-password-at-all"}, testIP, olderAppUA)
if err != errx.ErrCredentials {
t.Fatalf("a wrong password from an older app answered %v; want ErrCredentials", err)
}
}
// A current app build gets the change step and continues the sign-in after it.
func TestCurrentAppTakesTheChangeStep(t *testing.T) {
s, repo, tokens := initialFixture(t, 48*time.Hour)
res, err := s.LoginStart(context.Background(), &AuthData{Email: repo.acct.email, Password: issuedPassword, SupportsPasswordChange: true}, testIP, olderAppUA)
if err != nil || !res.PasswordChangeRequired || res.PendingToken == "" || res.Token != nil {
t.Fatalf("a current app answered %+v, %v", res, err)
}
done, err := choose(s, res.PendingToken, chosenPassword)
if err != nil {
t.Fatal(err)
}
if done.PasswordChangeRequired || done.Token == nil || tokens.sessions != 1 {
t.Fatalf("the change step answered %+v with %d sessions", done, tokens.sessions)
}
}
+3
View File
@@ -61,6 +61,9 @@ func (s *authService) LoginStart(ctx context.Context, data *AuthData, ipaddr, us
return nil, xerr
}
if pending {
if appCannotChangePassword(data, userAgent) {
return nil, errx.ErrInitialPasswordWebOnly
}
return s.createInitialPasswordChallenge(ctx, uid, data.Email, data.Password, verdict)
}
+2
View File
@@ -23,6 +23,8 @@ type AuthData struct {
// DeviceToken is what LoginConfirm returned when the user asked to
// remember this browser. Login only: it skips the emailed code.
DeviceToken string `json:"device_token"`
// SupportsPasswordChange says the native app can take a password_change_required answer. Login only.
SupportsPasswordChange bool `json:"supports_password_change,omitempty"`
}
type ConfirmData struct {
@@ -557,6 +557,18 @@ func (s *JobsService) notifyUserSendFailed(ctx context.Context, task *repository
})
}
// notifyManualSendUnsent tells the owner of a manual send that recovery released before any worker started it.
func (s *JobsService) notifyManualSendUnsent(ctx context.Context, taskID uuid.UUID) {
if s.TaskRepo == nil {
return
}
task, err := s.TaskRepo.GetTask(ctx, taskID)
if err != nil || task == nil {
return
}
s.notifyUserSendFailed(ctx, task, "it never reached its sending worker, so nothing was sent. Send it again.")
}
// sendFailureReason picks the most useful human-readable reason and the
// machine code out of a worker result.
func sendFailureReason(result models.SendEmailResult) (reason, code string) {
+5 -2
View File
@@ -120,10 +120,13 @@ func (s *JobsService) reconcileRecordedSendResults(ctx context.Context) {
}
}
if holds, ok := s.TaskRepo.(interface {
RecoverUnknownSendHolds(context.Context, time.Time, time.Time, int) (int, error)
RecoverUnknownSendHolds(context.Context, time.Time, time.Time, int) (int, []uuid.UUID, error)
}); ok {
now := time.Now()
n, err := holds.RecoverUnknownSendHolds(ctx, now.Add(-10*time.Minute), now.Add(-time.Duration(config.CampaignSendReclaimAfterMinutes)*time.Minute), 200)
n, unsent, err := holds.RecoverUnknownSendHolds(ctx, now.Add(-10*time.Minute), now.Add(-time.Duration(config.CampaignSendReclaimAfterMinutes)*time.Minute), 200)
for _, id := range unsent {
s.notifyManualSendUnsent(ctx, id)
}
if err != nil {
log.Warn().Err(err).Msg("unknown send hold recovery deferred")
} else if n > 0 {
+3 -3
View File
@@ -29,7 +29,7 @@ type Service interface {
Create(ctx context.Context, orgID uuid.UUID, createdBy *uuid.UUID, name string) (*models.Form, *errx.Error)
Update(ctx context.Context, orgID, id uuid.UUID, in *models.FormWrite) (*models.Form, *errx.Error)
Delete(ctx context.Context, orgID, id uuid.UUID) *errx.Error
ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, before *time.Time) ([]models.FormSubmission, bool, *errx.Error)
ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, after *models.FormSubmissionPosition) ([]models.FormSubmission, bool, *errx.Error)
DeleteSubmission(ctx context.Context, orgID, formID, subID uuid.UUID) *errx.Error
// SetAsset stores an uploaded brand asset URL; an empty url clears it.
SetAsset(ctx context.Context, orgID, id uuid.UUID, kind, url string) (*models.Form, *errx.Error)
@@ -420,8 +420,8 @@ func (s *service) Delete(ctx context.Context, orgID, id uuid.UUID) *errx.Error {
return s.repo.Delete(ctx, orgID, id)
}
func (s *service) ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, before *time.Time) ([]models.FormSubmission, bool, *errx.Error) {
return s.repo.ListSubmissions(ctx, orgID, formID, limit, before)
func (s *service) ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, after *models.FormSubmissionPosition) ([]models.FormSubmission, bool, *errx.Error) {
return s.repo.ListSubmissions(ctx, orgID, formID, limit, after)
}
func (s *service) DeleteSubmission(ctx context.Context, orgID, formID, subID uuid.UUID) *errx.Error {
+41 -4
View File
@@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@@ -149,11 +150,12 @@ func (s *service) ExportTo(
// tables have neither secrets nor blobs, so they stream through
// untouched, which is what makes a million-row inbox export cheap.
passthrough := len(t.Secrets) == 0 && len(t.Blobs) == 0 && len(t.ExcludeOnExport) == 0 && len(t.ExcludeJSONOnExport) == 0
bytea := byteaColumns(cols)
err = s.repo.StreamScoped(ctx, t.Name, t.Scope, orgID, func(row []byte) error {
out := row
if !passthrough {
transformed, terr := s.exportRow(ctx, t, row, archiveKey, orgCipher, blobs)
transformed, terr := s.exportRow(ctx, t, row, bytea, archiveKey, orgCipher, blobs)
if terr != nil {
return terr
}
@@ -198,6 +200,7 @@ func (s *service) exportRow(
ctx context.Context,
t *Table,
row []byte,
bytea map[string]bool,
archiveKey []byte,
orgCipher *cipher.Cipher,
blobs *blobCollector,
@@ -244,10 +247,20 @@ func (s *service) exportRow(
continue
}
plain, err := s.openSecret(ctx, sc, stored, orgCipher)
if err != nil {
var openErr error
if bytea[sc.Column] {
if stored, openErr = decodeBytea(stored); openErr == nil && stored == "" {
obj[sc.Column] = json.RawMessage(`null`)
continue
}
}
var plain string
if openErr == nil {
plain, openErr = s.openSecret(ctx, sc, stored, orgCipher)
}
if openErr != nil {
// One unreadable credential must not sink the export. Blank it and
// let the mailbox arrive needing a reconnect.
// let the mailbox or integration arrive needing a reconnect.
blankSecret(obj, sc)
continue
}
@@ -502,6 +515,30 @@ func toAnyMap(in map[string]json.RawMessage) map[string]any {
return out
}
// byteaColumns names the bytea columns among cols.
func byteaColumns(cols []repository.ColumnInfo) map[string]bool {
out := map[string]bool{}
for _, c := range cols {
if c.IsBytea {
out[c.Name] = true
}
}
return out
}
// decodeBytea reads the \x hex form to_jsonb gives a bytea value.
func decodeBytea(v string) (string, error) {
h, ok := strings.CutPrefix(v, `\x`)
if !ok {
return "", errors.New("bytea value is not in hex form")
}
b, err := hex.DecodeString(h)
return string(b), err
}
// encodeBytea writes a value in the \x hex form a bytea column accepts.
func encodeBytea(v string) string { return `\x` + hex.EncodeToString([]byte(v)) }
func columnNames(cols []repository.ColumnInfo) []string {
out := make([]string, len(cols))
for i, c := range cols {
+2 -2
View File
@@ -10,7 +10,7 @@ import (
func TestMailTransportExportJSONExclusionPreservesEvidence(t *testing.T) {
row := []byte(`{"registered":{"transport_generation":"proof","task_id":"kept","success":false,"message_id":"accepted","error":{"code":"KEPT","message":"transport_generation is quoted text","details":[{"transport_generation":"nested","count":9007199254740993},null,true,"transport_generation"]},"future":[{"result":{"transport_generation":"proof","sent_at":"kept"}}]},"unregistered":{"transport_generation":"user-content"},"historical":"kept"}`)
out, err := (&service{}).exportRow(context.Background(), &Table{Name: "fixture", ExcludeJSONOnExport: []JSONColumnExclusion{{Column: "registered", Keys: []string{"transport_generation"}}}}, row, nil, nil, nil)
out, err := (&service{}).exportRow(context.Background(), &Table{Name: "fixture", ExcludeJSONOnExport: []JSONColumnExclusion{{Column: "registered", Keys: []string{"transport_generation"}}}}, row, nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
@@ -36,7 +36,7 @@ func TestMailTransportExportJSONExclusionPreservesEvidence(t *testing.T) {
func TestMailTransportExportExclusionIsRegistryDirected(t *testing.T) {
row := []byte(`{"id":"portable","local_only":"do-not-export","worker_id":"source-worker","historical":"preserved"}`)
out, err := (&service{}).exportRow(context.Background(), &Table{Name: "fixture", ExcludeOnExport: []string{"local_only"}, ResetOnImport: []string{"worker_id"}}, row, nil, nil, nil)
out, err := (&service{}).exportRow(context.Background(), &Table{Name: "fixture", ExcludeOnExport: []string{"local_only"}, ResetOnImport: []string{"worker_id"}}, row, nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
+29
View File
@@ -199,6 +199,16 @@ func (s *service) ImportFrom(
"%d mailbox(es) have no usable credentials and are marked for reconnection.", reconnect))
}
if selected[models.OrgDataGroupAutomations] {
stale, err := s.repo.MarkIntegrationsNeedingReconnect(ctx, tx, orgID, integrationReconnectDetail)
if err != nil {
return nil, err
}
for _, c := range stale {
result.Warnings = append(result.Warnings, integrationReconnectWarning(c))
}
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("commit import: %w", err)
}
@@ -219,6 +229,8 @@ type importContext struct {
selected map[models.OrgDataGroup]bool
// rules re-apply write rules to the tables in importRules.
rules *ruleEnv
// bytea names the current table's bytea columns on this instance.
bytea map[string]bool
}
// importTable streams one table's rows out of the archive and into the
@@ -326,6 +338,8 @@ func (s *service) importTable(
"%s has no primary key, so existing rows there were kept rather than overwritten.", t.Name))
}
ic.bytea = byteaColumns(destCols)
refs, err := s.referencePlan(ctx, t, destCols, ic.selected)
if err != nil {
return 0, nil, err
@@ -517,6 +531,9 @@ func (s *service) importRow(
if rerr != nil {
return nil, fmt.Errorf("%s.%s: %w", t.Name, sc.Column, rerr)
}
if ic.bytea[sc.Column] {
resealed = encodeBytea(resealed)
}
enc, merr := json.Marshal(resealed)
if merr != nil {
return nil, merr
@@ -527,6 +544,18 @@ func (s *service) importRow(
return json.Marshal(obj)
}
// integrationReconnectDetail is the health detail an integration without its credentials carries.
const integrationReconnectDetail = "Credentials did not come across with the workspace import: reconnect this integration."
// integrationReconnectWarning names one connection by its provider and label, never by a credential.
func integrationReconnectWarning(c repository.IntegrationReconnect) string {
name := c.Provider
if c.Label != "" {
name = fmt.Sprintf("%s (%s)", c.Provider, c.Label)
}
return fmt.Sprintf("The %s integration arrived without its credentials and is marked for reconnection.", name)
}
// sealSecret re-encrypts a plaintext under this instance's key for that domain.
func (s *service) sealSecret(ctx context.Context, sc SecretColumn, plain string, orgCipher *cipher.Cipher) (string, error) {
switch sc.Domain {
@@ -0,0 +1,351 @@
package orgtransfer
import (
"archive/zip"
"bufio"
"bytes"
"context"
"encoding/json"
"os"
"slices"
"strings"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/warmbly/warmbly/internal/app/cipher"
"github.com/warmbly/warmbly/internal/app/customhost"
"github.com/warmbly/warmbly/internal/infrastructure/db"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/repository"
)
// A workspace archive carries its integration credentials re-keyed for the
// destination, flags the ones it could not carry, and carries the click
// tickets of campaign and direct sends alike, never another workspace's.
//
// WARMBLY_TEST_DB=postgres://warmbly:warmbly@localhost:15432/<db>?sslmode=disable \
// go test ./internal/app/orgtransfer/ -run LiveArchiveCarries -v
// perOrgKeys gives every organization its own DEK, as two instances would.
type perOrgKeys map[uuid.UUID]cipher.CipherService
func (k perOrgKeys) Cipher(ctx context.Context, orgID uuid.UUID) (*cipher.Cipher, error) {
return k[orgID].Cipher(ctx, orgID)
}
func keyedImportService(t *testing.T, keys perOrgKeys) (Service, *pgxpool.Pool) {
t.Helper()
dsn := os.Getenv("WARMBLY_TEST_DB")
if dsn == "" {
t.Skip("WARMBLY_TEST_DB not set")
}
handle, err := db.New(context.Background(), dsn)
if err != nil {
t.Fatalf("connect: %v", err)
}
t.Cleanup(func() { handle.Pool.Close() })
return NewService(repository.NewOrgTransferRepository(handle), keys, nil, nil, InstanceInfo{}), handle.Pool
}
func staticKey(b byte) cipher.CipherService { return cipher.NewStatic(bytes.Repeat([]byte{b}, 32)) }
func sealFixture(t *testing.T, keys cipher.CipherService, plain string) string {
t.Helper()
c, err := keys.Cipher(context.Background(), uuid.Nil)
if err != nil {
t.Fatal(err)
}
out, err := c.Encrypt(context.Background(), plain)
if err != nil {
t.Fatal(err)
}
return out
}
// archiveRows reads one table's rows out of an archive.
func archiveRows(t *testing.T, archive []byte, table string) []map[string]any {
t.Helper()
zr, err := zip.NewReader(bytes.NewReader(archive), int64(len(archive)))
if err != nil {
t.Fatal(err)
}
var out []map[string]any
for _, f := range zr.File {
if f.Name != dataPath(table) {
continue
}
rc, err := f.Open()
if err != nil {
t.Fatal(err)
}
sc := bufio.NewScanner(rc)
sc.Buffer(make([]byte, 1<<20), 1<<24)
for sc.Scan() {
var row map[string]any
if err := json.Unmarshal(sc.Bytes(), &row); err != nil {
t.Fatal(err)
}
out = append(out, row)
}
_ = rc.Close()
}
return out
}
type linkFixture struct {
mailbox, directTask, campaignTask, directLink, campaignLink uuid.UUID
}
func seedLinks(t *testing.T, pool *pgxpool.Pool, f tenantFixture) linkFixture {
t.Helper()
ctx := context.Background()
l := linkFixture{mailbox: uuid.New(), directTask: uuid.New(), campaignTask: uuid.New(), directLink: uuid.New(), campaignLink: uuid.New()}
for _, q := range []struct {
sql string
args []any
}{
{`INSERT INTO email_accounts(id,user_id,organization_id,email,name,signature_plain,signature_html,provider) VALUES($1,$2,$3,$4,'Links','','','smtp_imap')`,
[]any{l.mailbox, f.owner, f.org, l.mailbox.String() + "@example.test"}},
{`INSERT INTO tasks(id,email_account_id,task_type,status,message_id) VALUES($1,$2,'email','completed','')`, []any{l.directTask, l.mailbox}},
{`INSERT INTO tasks(id,email_account_id,task_type,status,message_id) VALUES($1,$2,'campaign','completed','')`, []any{l.campaignTask, l.mailbox}},
{`INSERT INTO tracked_links(id,task_id,campaign_id,destination,label) VALUES($1,$2,$3,'https://example.test/direct','Direct')`, []any{l.directLink, l.directTask, uuid.Nil}},
{`INSERT INTO tracked_links(id,task_id,campaign_id,destination,label) VALUES($1,$2,$3,'https://example.test/campaign','Campaign')`, []any{l.campaignLink, l.campaignTask, f.campaign}},
} {
if _, err := pool.Exec(ctx, q.sql, q.args...); err != nil {
t.Fatalf("seed links: %v", err)
}
}
t.Cleanup(func() { dropLinks(pool, l) })
return l
}
func dropLinks(pool *pgxpool.Pool, l linkFixture) {
ctx := context.Background()
_, _ = pool.Exec(ctx, `DELETE FROM tracked_links WHERE id = ANY($1)`, []uuid.UUID{l.directLink, l.campaignLink})
_, _ = pool.Exec(ctx, `DELETE FROM email_accounts WHERE id = $1`, l.mailbox)
}
type connFixture struct{ apollo, hubspot, calendly uuid.UUID }
// Plaintexts the fixture seals. Assertions compare them and never print them.
const (
apolloConfig = `{"api_key":"fixture-apollo-key"}`
hubspotAccess = "fixture-hubspot-access"
hubspotRefresh = "fixture-hubspot-refresh"
)
func seedConnections(t *testing.T, pool *pgxpool.Pool, org uuid.UUID, keys cipher.CipherService) connFixture {
t.Helper()
ctx := context.Background()
c := connFixture{apollo: uuid.New(), hubspot: uuid.New(), calendly: uuid.New()}
for _, q := range []struct {
sql string
args []any
}{
// config_encrypted is bytea and holds the sealed text's bytes, as integration.sealConfig writes it.
{`INSERT INTO integration_connections(id,organization_id,provider,label,status,auth_method,config_encrypted,health) VALUES($1,$2,'apollo','Prospecting','connected','api_key',$3,'healthy')`,
[]any{c.apollo, org, []byte(sealFixture(t, keys, apolloConfig))}},
{`INSERT INTO integration_connections(id,organization_id,provider,label,status,auth_method,access_token_encrypted,refresh_token_encrypted,health) VALUES($1,$2,'hubspot','','connected','oauth',$3,$4,'healthy')`,
[]any{c.hubspot, org, sealFixture(t, keys, hubspotAccess), sealFixture(t, keys, hubspotRefresh)}},
// Sealed under a key this workspace does not hold, so it cannot be opened at export.
{`INSERT INTO integration_connections(id,organization_id,provider,label,status,auth_method,inbound_secret,config_encrypted,health) VALUES($1,$2,'calendly','Bookings','connected','webhook',$3,$4,'healthy')`,
[]any{c.calendly, org, "in_" + c.calendly.String(), []byte(sealFixture(t, staticKey(9), `{"inbound_signing_key":"fixture-signing-key"}`))}},
} {
if _, err := pool.Exec(ctx, q.sql, q.args...); err != nil {
t.Fatalf("seed connections: %v", err)
}
}
t.Cleanup(func() {
_, _ = pool.Exec(context.Background(), `DELETE FROM integration_connections WHERE id = ANY($1)`, []uuid.UUID{c.apollo, c.hubspot, c.calendly})
})
return c
}
func openFixture(t *testing.T, keys cipher.CipherService, sealed string) string {
t.Helper()
c, err := keys.Cipher(context.Background(), uuid.Nil)
if err != nil {
t.Fatal(err)
}
plain, err := c.Decrypt(context.Background(), sealed)
if err != nil {
t.Fatalf("destination key does not open the imported credential: %v", err)
}
return plain
}
func TestLiveArchiveCarriesIntegrationCredentialsAndDirectMailLinks(t *testing.T) {
for _, withPassphrase := range []bool{true, false} {
name := "with_passphrase"
if !withPassphrase {
name = "without_passphrase"
}
t.Run(name, func(t *testing.T) {
keys := perOrgKeys{}
svc, pool := keyedImportService(t, keys)
ctx := context.Background()
source := newTenantFixture(t, pool)
other := newTenantFixture(t, pool)
dest := newTenantFixture(t, pool)
keys[source.org], keys[other.org], keys[dest.org] = staticKey(1), staticKey(2), staticKey(3)
srcLinks := seedLinks(t, pool, source)
otherLinks := seedLinks(t, pool, other)
srcConns := seedConnections(t, pool, source.org, keys[source.org])
otherConns := seedConnections(t, pool, other.org, keys[other.org])
groups := []models.OrgDataGroup{models.OrgDataGroupCore, models.OrgDataGroupContacts, models.OrgDataGroupCampaigns, models.OrgDataGroupAutomations, models.OrgDataGroupSending}
opts := ExportOptions{Groups: groups}
if withPassphrase {
opts.Passphrase = "correct horse battery staple"
}
var buf bytes.Buffer
if _, err := svc.ExportTo(ctx, source.org, opts, &buf, nil); err != nil {
t.Fatal(err)
}
archive := buf.Bytes()
var linkIDs []string
for _, r := range archiveRows(t, archive, "tracked_links") {
linkIDs = append(linkIDs, r["id"].(string))
}
slices.Sort(linkIDs)
want := []string{srcLinks.directLink.String(), srcLinks.campaignLink.String()}
slices.Sort(want)
if !slices.Equal(linkIDs, want) {
t.Fatalf("archive tracked_links = %v, want the source's direct and campaign links %v", linkIDs, want)
}
for _, r := range archiveRows(t, archive, "integration_connections") {
if id := r["id"].(string); id == otherConns.apollo.String() || id == otherConns.hubspot.String() || id == otherConns.calendly.String() {
t.Fatal("archive carries another workspace's integration")
}
}
for _, plain := range []string{apolloConfig, hubspotAccess, hubspotRefresh, "fixture-signing-key"} {
if bytes.Contains(archive, []byte(plain)) {
t.Fatal("archive carries a credential in the clear")
}
}
// The destination is a different workspace under a different key; the
// source's rows leave first, as they would never exist on another instance.
if _, err := pool.Exec(ctx, `DELETE FROM integration_connections WHERE organization_id = $1`, source.org); err != nil {
t.Fatal(err)
}
dropLinks(pool, srcLinks)
for _, q := range []string{
`DELETE FROM campaigns WHERE organization_id = $1`,
`DELETE FROM contacts WHERE organization_id = $1`,
`DELETE FROM organization_members WHERE organization_id = $1`,
} {
if _, err := pool.Exec(ctx, q, source.org); err != nil {
t.Fatal(err)
}
}
t.Cleanup(func() {
_, _ = pool.Exec(context.Background(), `DELETE FROM integration_connections WHERE organization_id = $1`, dest.org)
})
result, err := svc.ImportFrom(ctx, dest.org, bytes.NewReader(archive), ImportOptions{
Groups: groups, Conflict: models.OrgImportConflictOverwrite, Passphrase: opts.Passphrase, ActorUserID: dest.owner,
}, nil)
if err != nil {
t.Fatal(err)
}
type conn struct {
org uuid.UUID
status, health string
config []byte
access, refresh, inbound *string
}
read := func(id uuid.UUID) conn {
t.Helper()
var c conn
if err := pool.QueryRow(ctx, `SELECT organization_id, status, health, config_encrypted, access_token_encrypted, refresh_token_encrypted, inbound_secret FROM integration_connections WHERE id = $1`, id).
Scan(&c.org, &c.status, &c.health, &c.config, &c.access, &c.refresh, &c.inbound); err != nil {
t.Fatal(err)
}
return c
}
var reconnect []string
for _, w := range result.Warnings {
if strings.Contains(w, "integration arrived without its credentials") {
reconnect = append(reconnect, w)
}
}
apollo, hubspot, calendly := read(srcConns.apollo), read(srcConns.hubspot), read(srcConns.calendly)
for _, c := range []conn{apollo, hubspot, calendly} {
if c.org != dest.org {
t.Fatal("imported connection does not belong to the destination workspace")
}
}
if calendly.status != "reauth_required" || calendly.health != "down" || calendly.config != nil || calendly.inbound != nil {
t.Fatalf("uncarried credential not flagged: status=%s health=%s config=%t inbound=%t", calendly.status, calendly.health, calendly.config != nil, calendly.inbound != nil)
}
if withPassphrase {
if apollo.status != "connected" || hubspot.status != "connected" {
t.Fatalf("carried connections changed state: apollo=%s hubspot=%s", apollo.status, hubspot.status)
}
if openFixture(t, keys[dest.org], string(apollo.config)) != apolloConfig ||
hubspot.access == nil || openFixture(t, keys[dest.org], *hubspot.access) != hubspotAccess ||
hubspot.refresh == nil || openFixture(t, keys[dest.org], *hubspot.refresh) != hubspotRefresh {
t.Fatal("imported credentials do not open to the source's values under the destination key")
}
if len(reconnect) != 1 || !strings.Contains(reconnect[0], "calendly (Bookings)") {
t.Fatalf("want one reconnect warning naming calendly, got %d", len(reconnect))
}
} else {
for name, c := range map[string]conn{"apollo": apollo, "hubspot": hubspot} {
if c.status != "reauth_required" || c.config != nil || c.access != nil || c.refresh != nil {
t.Fatalf("%s arrived without credentials but was not flagged: status=%s", name, c.status)
}
}
if len(reconnect) != 3 {
t.Fatalf("want a reconnect warning per connection, got %d", len(reconnect))
}
}
// The other workspace keeps its connections and credentials as they were.
for _, id := range []uuid.UUID{otherConns.apollo, otherConns.hubspot, otherConns.calendly} {
if c := read(id); c.org != other.org || c.status != "connected" {
t.Fatalf("another workspace's connection changed: status=%s", c.status)
}
}
if c := read(otherConns.apollo); openFixture(t, keys[other.org], string(c.config)) != apolloConfig {
t.Fatal("another workspace's credential changed")
}
// Both tickets resolve to the destination, on a tracking domain it holds.
links := repository.NewTrackedLinkRepository(pool)
host := "links-" + dest.org.String()[:8] + ".example.test"
if _, err := pool.Exec(ctx, `UPDATE email_accounts SET tracking_domain = $2, tracking_domain_verified = true WHERE id = $1 AND organization_id = $3`, srcLinks.mailbox, host, dest.org); err != nil {
t.Fatal(err)
}
owners, restricted, err := customhost.New(repository.NewCustomDomainRepository(pool), nil, nil).Guard(ctx, host)
if err != nil || !restricted {
t.Fatalf("destination tracking domain not served: restricted=%t err=%v", restricted, err)
}
for _, id := range []uuid.UUID{srcLinks.directLink, srcLinks.campaignLink} {
link, err := links.GetByID(ctx, id)
if err != nil || link == nil {
t.Fatalf("imported ticket does not resolve: %v", err)
}
if !customhost.Allowed(owners, link.OrganizationID) {
t.Fatal("imported ticket is refused on the destination's tracking domain")
}
}
foreign, err := links.GetByID(ctx, otherLinks.directLink)
if err != nil || foreign == nil || foreign.OrganizationID == nil || *foreign.OrganizationID != other.org {
t.Fatalf("another workspace's ticket moved: %v", err)
}
if customhost.Allowed(owners, foreign.OrganizationID) {
t.Fatal("another workspace's ticket resolves on the destination's tracking domain")
}
})
}
}
+4 -2
View File
@@ -953,9 +953,11 @@ var Tables = []Table{
Note: "Historical reports travel; operation and clock provenance reset to unknown on the destination.",
},
{
// A direct send mints its tickets with a nil campaign id, so the owning task scopes them too.
Name: "tracked_links", Group: models.OrgDataGroupSending,
Scope: `campaign_id IN ` + orgCampaigns,
Note: "Click tickets already in the wild keep resolving after the move, provided the tracking domain follows.",
Scope: `(campaign_id IN ` + orgCampaigns + ` OR task_id IN ` + orgTasks + `)`,
Owner: `(campaign_id IN ` + orgCampaigns + ` OR task_id IN (SELECT id FROM tasks WHERE email_account_id IN ` + orgMailboxes + `))`,
Note: "Click tickets from campaign and direct sends already in the wild keep resolving after the move, provided the tracking domain follows.",
},
{
// Campaign engagement like campaign_contact_progress, one row per
+9
View File
@@ -270,6 +270,15 @@ const (
// it must stay well clear of a slow provider handshake.
CampaignSendReclaimAfterMinutes = 30
// ManualSendInterruptedAfterMinutes is how long a manual send may sit claimed
// without a reservation before recovery treats its execution as lost. It is
// well past the handler's two-minute deadline and the execution key's lease.
ManualSendInterruptedAfterMinutes = 10
// ManualSendRetryWithinMinutes bounds how old an interrupted manual send may
// be and still go out: older ones fail and the owner is told instead.
ManualSendRetryWithinMinutes = 60
// UnknownSendHoldVisibleAfterMinutes keeps an unconfirmed send out of mailbox health until it outlasts automatic recovery.
UnknownSendHoldVisibleAfterMinutes = 2 * CampaignSendReclaimAfterMinutes
+4
View File
@@ -91,6 +91,10 @@ var (
ErrPasswordChangeRequired = NewWithIdentifier(Forbidden, "password_change_required",
"This account still has the password it was issued, which has to be replaced first. Sign in with your email and password to choose your own.")
// ErrInitialPasswordWebOnly answers an issued initial password from an app build that cannot take the change step.
ErrInitialPasswordWebOnly = NewWithIdentifier(Forbidden, "initial_password_web_only",
"This account still has the temporary password it was given. Finish your first sign-in on the web, where you choose your own password, then sign in here with that password.")
// ErrPasswordChangeExpired ends a choose-your-password step that is unknown, expired, already used, or whose issued password ran out.
ErrPasswordChangeExpired = NewWithIdentifier(BadRequest, "password_change_expired",
"That sign-in request expired or was already used. Sign in again.")
+7
View File
@@ -235,6 +235,13 @@ const (
// FormSubmission is one public submit, kept verbatim. Data is keyed by field
// id; checkbox groups store a string slice, everything else a string.
// FormSubmissionPosition is the keyset a submissions page continues after; a
// nil ID is a legacy timestamp-only cursor and continues strictly before At.
type FormSubmissionPosition struct {
At time.Time
ID uuid.UUID
}
type FormSubmission struct {
ID uuid.UUID `json:"id"`
FormID uuid.UUID `json:"form_id"`
+2
View File
@@ -77,6 +77,8 @@ type LoginCodeExemption struct {
PasswordExpiresAt *time.Time `json:"password_expires_at,omitempty"`
TestWorkspaceID *uuid.UUID `json:"test_workspace_id,omitempty"`
SampleDataSeededAt *time.Time `json:"sample_data_seeded_at,omitempty"`
// SampleDataEligible is the seeding check itself: an active tester with an unexpired Test grant.
SampleDataEligible bool `json:"sample_data_eligible"`
// CreatedAsTester marks an account the tester flow created, which alone can be issued a new password.
CreatedAsTester bool `json:"created_as_tester"`
}
+100
View File
@@ -3,9 +3,11 @@ package repository
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/utils/paging"
)
// Issue #267: forms CRUD, the category aggregation scan and the submission
@@ -179,3 +181,101 @@ func TestLiveFormCreateEmptyLists(t *testing.T) {
t.Fatalf("update round-trip: %#v %#v", updated.AllowedDomains, updated.Fields)
}
}
// Issue #1073: paging through submissions that share a timestamp, or sit a
// microsecond apart, at every page boundary returns each one exactly once.
func TestLiveFormSubmissionsPageAcrossEqualAndCloseTimestamps(t *testing.T) {
handle, pool := liveContactDB(t)
f := newSharedOrgFixture(t, pool)
repo := NewFormRepository(handle)
ctx := context.Background()
t.Cleanup(func() {
if _, err := pool.Exec(context.Background(), `DELETE FROM forms WHERE organization_id = $1`, f.org); err != nil {
t.Errorf("cleanup forms: %v", err)
}
})
form, xerr := repo.Create(ctx, f.org, &f.owner, &models.Form{
PublicID: "livetest-" + uuid.New().String()[:13],
Name: "Paging",
Status: models.FormStatusPublished,
SuccessMessage: "Thanks",
})
if xerr != nil {
t.Fatalf("create: %v", xerr)
}
// Five rows on one microsecond, two a microsecond either side, one a
// millisecond away: every page size below lands a boundary inside a tie.
base := time.Date(2026, 3, 1, 12, 0, 0, 123456000, time.UTC)
offsets := []time.Duration{time.Millisecond, time.Microsecond, 0, 0, 0, 0, 0, -time.Microsecond}
for _, off := range offsets {
if _, err := pool.Exec(ctx, `INSERT INTO form_submissions (form_id, organization_id, created_at) VALUES ($1, $2, $3)`,
form.ID, f.org, base.Add(off)); err != nil {
t.Fatalf("insert submission: %v", err)
}
}
var want []uuid.UUID
rows, err := pool.Query(ctx, `SELECT id FROM form_submissions WHERE form_id = $1 ORDER BY created_at DESC, id DESC`, form.ID)
if err != nil {
t.Fatalf("expected order: %v", err)
}
for rows.Next() {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
t.Fatalf("scan: %v", err)
}
want = append(want, id)
}
rows.Close()
for limit := 1; limit <= len(want); limit++ {
var got []uuid.UUID
var after *models.FormSubmissionPosition
for pages := 0; ; pages++ {
if pages > len(want) {
t.Fatalf("limit %d: paging did not terminate", limit)
}
page, hasMore, xerr := repo.ListSubmissions(ctx, f.org, form.ID, limit, after)
if xerr != nil {
t.Fatalf("limit %d: list: %v", limit, xerr)
}
for _, s := range page {
got = append(got, s.ID)
}
if !hasMore {
break
}
// Round-trip the opaque cursor the handler hands out.
last := page[len(page)-1]
at, id, xerr := paging.DecodeTimeCursor(*paging.EncodeTime(last.CreatedAt, last.ID))
if xerr != nil {
t.Fatalf("limit %d: cursor: %v", limit, xerr)
}
after = &models.FormSubmissionPosition{At: at, ID: id}
}
if len(got) != len(want) {
t.Fatalf("limit %d: got %d rows, want %d", limit, len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("limit %d: row %d is %s, want %s", limit, i, got[i], want[i])
}
}
}
// A timestamp-only cursor from the previous release keeps microseconds:
// strictly before the tie returns only the row a microsecond older.
legacy := base.Format(time.RFC3339Nano)
at, err := time.Parse(time.RFC3339Nano, legacy)
if err != nil {
t.Fatalf("parse legacy cursor: %v", err)
}
older, _, xerr := repo.ListSubmissions(ctx, f.org, form.ID, 50, &models.FormSubmissionPosition{At: at})
if xerr != nil {
t.Fatalf("legacy list: %v", xerr)
}
if len(older) != 1 || older[0].ID != want[len(want)-1] {
t.Fatalf("legacy before %s returned %d rows", legacy, len(older))
}
}
@@ -0,0 +1,80 @@
package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// InterruptedManualSend is a manual send whose claim ended before it reserved anything.
type InterruptedManualSend struct {
TaskID uuid.UUID
MailboxID uuid.UUID
// Retried is true when the send went back to pending, false when it failed.
Retried bool
}
// RecoverInterruptedManualSends settles manual sends claimed before claimedBefore
// that never reserved: nothing was handed to a worker, so one claimed at or after
// retrySince goes back to pending and anything older fails.
func (r *taskRepository) RecoverInterruptedManualSends(ctx context.Context, claimedBefore, retrySince time.Time, limit int) ([]InterruptedManualSend, error) {
rows, err := r.db.Query(ctx, `SELECT id FROM tasks WHERE status='active' AND task_type='email'
AND send_executor_nonce IS NULL AND updated_at<$1 ORDER BY updated_at,id LIMIT $2`, claimedBefore, min(max(limit, 1), 500))
if err != nil {
return nil, err
}
ids, err := pgx.CollectRows(rows, pgx.RowTo[uuid.UUID])
if err != nil {
return nil, err
}
var out []InterruptedManualSend
var errs []error
for _, id := range ids {
if ctx.Err() != nil {
break
}
send, ok, err := r.recoverInterruptedManualSend(ctx, id, claimedBefore, retrySince)
if err != nil {
errs = append(errs, err)
continue
}
if ok {
out = append(out, send)
}
}
return out, errors.Join(append(errs, ctx.Err())...)
}
func (r *taskRepository) recoverInterruptedManualSend(ctx context.Context, id uuid.UUID, claimedBefore, retrySince time.Time) (InterruptedManualSend, bool, error) {
send := InterruptedManualSend{TaskID: id}
tx, err := r.db.Begin(ctx)
if err != nil {
return send, false, err
}
defer tx.Rollback(ctx)
// ReserveOutbound holds this lock until it commits, so a reservation in flight is never overtaken.
if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,268))`, id.String()); err != nil {
return send, false, err
}
// A test send has no email_tasks row to run again, so it fails.
err = tx.QueryRow(ctx, `UPDATE tasks t SET status=(CASE WHEN t.updated_at>=$3 AND EXISTS(SELECT 1 FROM email_tasks et WHERE et.task_id=t.id)
THEN 'pending' ELSE 'failed' END)::task_status,updated_at=NOW(),dispatch_retry_at=NULL,dispatch_claimed_until=NULL
WHERE t.id=$1 AND t.status='active' AND t.task_type='email' AND t.send_executor_nonce IS NULL AND t.updated_at<$2
RETURNING t.email_account_id,t.status='pending'`, id, claimedBefore, retrySince).Scan(&send.MailboxID, &send.Retried)
if errors.Is(err, pgx.ErrNoRows) {
return send, false, nil
}
if err != nil {
return send, false, err
}
if !send.Retried {
if _, err = tx.Exec(ctx, `INSERT INTO task_failures(task_id,title,message) VALUES($1,'Send interrupted','The send stopped before it was handed to a worker, so nothing was sent')
ON CONFLICT(task_id) DO UPDATE SET title=EXCLUDED.title,message=EXCLUDED.message`, id); err != nil {
return send, false, err
}
}
return send, true, tx.Commit(ctx)
}
+14 -6
View File
@@ -30,7 +30,7 @@ type FormRepository interface {
RecordView(ctx context.Context, formID uuid.UUID) *errx.Error
// CreateSubmission stores the answers and bumps the form's counters.
CreateSubmission(ctx context.Context, sub *models.FormSubmission) (*models.FormSubmission, *errx.Error)
ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, before *time.Time) ([]models.FormSubmission, bool, *errx.Error)
ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, after *models.FormSubmissionPosition) ([]models.FormSubmission, bool, *errx.Error)
// LogSubmissionActivity adds the form_submitted event to the contact
// timeline; best-effort, called after the submission is stored.
LogSubmissionActivity(ctx context.Context, orgID, contactID, formID uuid.UUID, formName string, submissionID uuid.UUID) *errx.Error
@@ -326,20 +326,28 @@ func (r *formRepository) CreateSubmission(ctx context.Context, sub *models.FormS
return sub, nil
}
func (r *formRepository) ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, before *time.Time) ([]models.FormSubmission, bool, *errx.Error) {
func (r *formRepository) ListSubmissions(ctx context.Context, orgID, formID uuid.UUID, limit int, after *models.FormSubmissionPosition) ([]models.FormSubmission, bool, *errx.Error) {
if limit <= 0 || limit > 100 {
limit = 50
}
var at *time.Time
var afterID *uuid.UUID
if after != nil {
at = &after.At
if after.ID != uuid.Nil {
afterID = &after.ID
}
}
rows, err := r.DB.Query(ctx, `
SELECT s.id, s.form_id, s.organization_id, s.contact_id, s.campaign_id, s.data, s.source_url, s.triage, s.triage_confidence, s.created_at,
COALESCE(c.email, ''), COALESCE(TRIM(c.first_name || ' ' || c.last_name), ''), COALESCE(cp.name, '')
FROM form_submissions s
LEFT JOIN contacts c ON c.id = s.contact_id
LEFT JOIN campaigns cp ON cp.id = s.campaign_id
WHERE s.organization_id = $1 AND s.form_id = $2 AND ($3::timestamptz IS NULL OR s.created_at < $3)
ORDER BY s.created_at DESC
LIMIT $4
`, orgID, formID, before, limit+1)
WHERE s.organization_id = $1 AND s.form_id = $2 AND ($3::timestamptz IS NULL OR s.created_at < $3 OR (s.created_at = $3 AND s.id < $4::uuid))
ORDER BY s.created_at DESC, s.id DESC
LIMIT $5
`, orgID, formID, at, afterID, limit+1)
if err != nil {
db.CaptureError(err, "form submissions list", nil, "query")
return nil, false, errx.InternalError()
+47 -2
View File
@@ -93,6 +93,8 @@ type OrgTransferRepository interface {
MissingOAuthApps(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, ids []uuid.UUID) (int64, error)
// ChangedOAuthAppClients counts archived OAuth apps whose id the destination already holds under a different client.
ChangedOAuthAppClients(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, ids []uuid.UUID, clientIDs []string) (int64, error)
// MarkIntegrationsNeedingReconnect flags connections whose credentials did not travel.
MarkIntegrationsNeedingReconnect(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, detail string) ([]IntegrationReconnect, error)
// Begin starts the import transaction.
Begin(ctx context.Context) (pgx.Tx, error)
@@ -133,6 +135,8 @@ type ColumnInfo struct {
IsGenerated bool
IsIdentity bool
NotNull bool
// IsBytea marks a bytea column, which to_jsonb renders as a \x hex string.
IsBytea bool
}
// Writable reports whether the column accepts a supplied value.
@@ -238,7 +242,8 @@ func (r *orgTransferRepository) TableColumns(ctx context.Context, table string)
SELECT a.attname,
a.attgenerated <> '' AS is_generated,
a.attidentity <> '' AS is_identity,
a.attnotnull AS not_null
a.attnotnull AS not_null,
a.atttypid = 'bytea'::regtype AS is_bytea
FROM pg_attribute a
JOIN pg_class c ON c.oid = a.attrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
@@ -257,7 +262,7 @@ func (r *orgTransferRepository) TableColumns(ctx context.Context, table string)
var out []ColumnInfo
for rows.Next() {
var c ColumnInfo
if err := rows.Scan(&c.Name, &c.IsGenerated, &c.IsIdentity, &c.NotNull); err != nil {
if err := rows.Scan(&c.Name, &c.IsGenerated, &c.IsIdentity, &c.NotNull, &c.IsBytea); err != nil {
return nil, err
}
out = append(out, c)
@@ -896,6 +901,46 @@ func (r *orgTransferRepository) MarkMailboxesNeedingReconnect(ctx context.Contex
return tag.RowsAffected(), nil
}
// IntegrationReconnect names a connection an import left needing reconnection.
type IntegrationReconnect struct {
Provider string
Label string
}
// MarkIntegrationsNeedingReconnect moves every connection holding a credential
// the archive blanked (an empty string, where the app itself writes NULL) to
// reauth_required, and closes its inbound URL so it is re-minted on reconnect.
func (r *orgTransferRepository) MarkIntegrationsNeedingReconnect(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, detail string) ([]IntegrationReconnect, error) {
rows, err := tx.Query(ctx, `
UPDATE integration_connections
SET status = CASE WHEN status = 'disconnected' THEN status ELSE 'reauth_required' END,
health = 'down',
health_detail = $2,
health_checked_at = NOW(),
inbound_secret = CASE WHEN config_encrypted = '\x'::bytea THEN NULL ELSE inbound_secret END,
config_encrypted = NULLIF(config_encrypted, '\x'::bytea),
access_token_encrypted = NULLIF(access_token_encrypted, ''),
refresh_token_encrypted = NULLIF(refresh_token_encrypted, ''),
updated_at = NOW()
WHERE organization_id = $1
AND (config_encrypted = '\x'::bytea OR access_token_encrypted = '' OR refresh_token_encrypted = '')
RETURNING provider, label
`, orgID, detail)
if err != nil {
return nil, fmt.Errorf("flag integrations needing reconnect: %w", err)
}
defer rows.Close()
var out []IntegrationReconnect
for rows.Next() {
var c IntegrationReconnect
if err := rows.Scan(&c.Provider, &c.Label); err != nil {
return nil, err
}
out = append(out, c)
}
return out, rows.Err()
}
func (r *orgTransferRepository) ResolveUsersByEmail(ctx context.Context, emails []string) (map[string]uuid.UUID, error) {
out := make(map[string]uuid.UUID, len(emails))
if len(emails) == 0 {
+10 -6
View File
@@ -11,6 +11,15 @@ import (
var ErrTesterWorkspaceInactive = errors.New("sample data requires an active dedicated Test workspace")
// testerWorkspaceActiveSQL is the seeding authority over workspace o and owner u: an active tester
// and an unexpired managed Test grant, whatever state the owner's initial password is in.
func testerWorkspaceActiveSQL(planParam string) string {
return `o.category = 'test' AND u.login_code_exempt AND EXISTS (
SELECT 1 FROM subscriptions s WHERE s.organization_id = o.id
AND s.managed_plan_id = ` + planParam + ` AND s.managed_at IS NOT NULL AND s.managed_until > now()
)`
}
func (r *organizationRepository) SeedTesterWorkspace(ctx context.Context, orgID, adminID uuid.UUID, ip, userAgent string) (*models.TesterSampleData, error) {
tx, err := r.db.Begin(ctx)
if err != nil {
@@ -21,12 +30,7 @@ func (r *organizationRepository) SeedTesterWorkspace(ctx context.Context, orgID,
var ownerID uuid.UUID
err = tx.QueryRow(ctx, `
SELECT o.owner_user_id FROM organizations o JOIN users u ON u.id = o.owner_user_id
WHERE o.id = $1 AND o.category = 'test' AND u.login_code_exempt
AND u.password_expires_at > now()
AND EXISTS (
SELECT 1 FROM subscriptions s WHERE s.organization_id = o.id
AND s.managed_plan_id = $2 AND s.managed_at IS NOT NULL AND s.managed_until > now()
)
WHERE o.id = $1 AND `+testerWorkspaceActiveSQL("$2")+`
FOR UPDATE OF o, u`, orgID, models.TestPlanID).Scan(&ownerID)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrTesterWorkspaceInactive
+9 -5
View File
@@ -384,13 +384,17 @@ func (r *userRepository) SetLoginCodeExempt(ctx context.Context, id uuid.UUID, e
func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.LoginCodeExemption, error) {
rows, err := r.DB.Query(ctx, `
SELECT u.id, u.email, u.login_code_exempt_reason, u.login_code_exempt_at, u.password_expires_at,
workspace.id, seeded.created_at,
workspace.id, seeded.created_at, COALESCE(workspace.active, false),
EXISTS (SELECT 1 FROM admin_audit_logs t WHERE t.action = 'create_tester' AND t.target_id = u.id)
FROM users u
LEFT JOIN LATERAL (
SELECT o.id FROM organizations o
SELECT o.id, `+testerWorkspaceActiveSQL("$1")+` AS active FROM organizations o
WHERE o.owner_user_id = u.id AND o.category = 'test'
ORDER BY o.created_at, o.id LIMIT 1
-- Of several owned Test workspaces, list the one sample data can still be added to.
ORDER BY active DESC, EXISTS (
SELECT 1 FROM admin_audit_logs a
WHERE a.target_type = 'organization' AND a.target_id = o.id AND a.action = 'seed_tester_workspace'
), o.created_at, o.id LIMIT 1
) workspace ON true
LEFT JOIN LATERAL (
SELECT a.created_at FROM admin_audit_logs a
@@ -399,7 +403,7 @@ func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.Logi
ORDER BY a.created_at LIMIT 1
) seeded ON true
WHERE u.login_code_exempt
ORDER BY u.login_code_exempt_at NULLS FIRST`)
ORDER BY u.login_code_exempt_at NULLS FIRST`, models.TestPlanID)
if err != nil {
return nil, err
}
@@ -408,7 +412,7 @@ func (r *userRepository) ListLoginCodeExempt(ctx context.Context) ([]models.Logi
out := []models.LoginCodeExemption{}
for rows.Next() {
var e models.LoginCodeExemption
if err := rows.Scan(&e.UserID, &e.Email, &e.Reason, &e.GrantedAt, &e.PasswordExpiresAt, &e.TestWorkspaceID, &e.SampleDataSeededAt, &e.CreatedAsTester); err != nil {
if err := rows.Scan(&e.UserID, &e.Email, &e.Reason, &e.GrantedAt, &e.PasswordExpiresAt, &e.TestWorkspaceID, &e.SampleDataSeededAt, &e.SampleDataEligible, &e.CreatedAsTester); err != nil {
return nil, err
}
out = append(out, e)
+24 -12
View File
@@ -591,13 +591,19 @@ func (r *taskRepository) CancelOutbound(ctx context.Context, task, mailbox, work
}
func (r *taskRepository) cancelOutbound(ctx context.Context, task, mailbox, worker, nonce uuid.UUID, reason error) error {
_, err := r.releaseOutbound(ctx, task, mailbox, worker, nonce, reason)
return err
}
// releaseOutbound walks back a reservation that never started and returns its task type, or "" when there was none to release.
func (r *taskRepository) releaseOutbound(ctx context.Context, task, mailbox, worker, nonce uuid.UUID, reason error) (string, error) {
tx, err := r.db.Begin(ctx)
if err != nil {
return err
return "", err
}
defer tx.Rollback(ctx)
if err = lockSend(ctx, tx, task, mailbox); err != nil {
return err
return "", err
}
var lane string
err = tx.QueryRow(ctx, `UPDATE tasks SET send_released_at=NOW(),send_result_state='failed',send_result_applied_at=NOW(),status='cancelled'
@@ -605,45 +611,51 @@ func (r *taskRepository) cancelOutbound(ctx context.Context, task, mailbox, work
AND (task_type<>'warmup' OR EXISTS(SELECT 1 FROM warmup_tasks w WHERE w.task_id=tasks.id AND w.dispatch_nonce=$4
AND w.dispatch_worker_id=$3 AND w.dispatch_started_at IS NULL AND w.dispatch_result IS NULL)) RETURNING task_type`, task, mailbox, worker, nonce).Scan(&lane)
if errors.Is(err, pgx.ErrNoRows) {
return nil
return "", nil
}
if err != nil {
return err
return "", err
}
if lane == "campaign" {
var c, contact, sequence uuid.UUID
var newLead bool
err = tx.QueryRow(ctx, `SELECT ct.campaign_id,ct.contact_id,ct.sequence_id,NOT EXISTS(SELECT 1 FROM campaign_contact_progress p WHERE p.campaign_id=ct.campaign_id AND p.contact_id=ct.contact_id AND p.sent_at IS NOT NULL) FROM campaign_tasks ct WHERE ct.task_id=$1`, task).Scan(&c, &contact, &sequence, &newLead)
if err != nil {
return err
return "", err
}
inner := context.WithValue(ctx, sendResultKey{}, sendResultContext{tx: tx, taskID: task})
if err = NewCampaignProgressRepository(r.db).ReleaseSend(inner, c, contact, sequence, newLead); err != nil {
return err
return "", err
}
if reason != nil {
if _, err = tx.Exec(ctx, `INSERT INTO task_failures(task_id,title,message) VALUES($1,'Send admission denied',$2)
ON CONFLICT(task_id) DO UPDATE SET title=EXCLUDED.title,message=EXCLUDED.message`, task, reason.Error()); err != nil {
return err
return "", err
}
metadata := map[string]any{"level": "error", "code": SendAdmissionCode(reason), "contact_id": contact.String(), "sequence_id": sequence.String(), "account_id": mailbox.String(), "task_id": task.String(), "error": reason.Error(), "will_retry": true}
if _, err = tx.Exec(ctx, `INSERT INTO campaign_logs(campaign_id,event_type,message,metadata) VALUES($1,'email_failed',$2,$3)`, c, "Send admission denied before worker execution, will retry: "+reason.Error(), metadata); err != nil {
return err
return "", err
}
}
}
if lane == "warmup" {
if err = refundWarmupCharge(ctx, tx, task, mailbox); err != nil {
return err
return "", err
}
if _, err = tx.Exec(ctx, `DELETE FROM warmup_tokens WHERE task_id=$1`, task); err != nil {
return err
return "", err
}
}
if lane == "email" {
// The outcome is known now: nothing was sent.
if err = resolveSendDeadLetter(ctx, tx, task); err != nil {
return "", err
}
}
if err = refreshUnknownSendHold(ctx, tx, mailbox, task); err != nil {
return err
return "", err
}
return tx.Commit(ctx)
return lane, tx.Commit(ctx)
}
func checkWarmupRecipientReservation(ctx context.Context, tx pgx.Tx, task, sender uuid.UUID) error {
@@ -200,7 +200,6 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) {
name, query string
}{
{"standard workspace", `UPDATE organizations SET category = 'standard' WHERE id = $1`},
{"expired password", `UPDATE users SET password_expires_at = now() - interval '1 hour' WHERE id = (SELECT owner_user_id FROM organizations WHERE id = $1)`},
{"revoked exemption", `UPDATE users SET login_code_exempt = false WHERE id = (SELECT owner_user_id FROM organizations WHERE id = $1)`},
{"expired grant", `UPDATE subscriptions SET managed_until = now() - interval '1 hour' WHERE organization_id = $1`},
{"different managed plan", `UPDATE subscriptions SET managed_plan_id = '00000000-0000-0000-0000-000000000001' WHERE organization_id = $1`},
@@ -210,6 +209,9 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) {
if _, err := pool.Exec(ctx, tc.query, f.orgID); err != nil {
t.Fatal(err)
}
if testerListEntry(t, f).SampleDataEligible {
t.Fatal("tester list offers sample data for an ineligible workspace")
}
if _, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", ""); !errors.Is(err, ErrTesterWorkspaceInactive) {
t.Fatalf("ineligible workspace returned %v", err)
}
@@ -217,3 +219,110 @@ func TestLiveTesterSampleDataRejectsNonTestAndInactiveWorkspaces(t *testing.T) {
})
}
}
func testerListEntry(t *testing.T, f testerSampleFixture) models.LoginCodeExemption {
t.Helper()
list, err := f.users.ListLoginCodeExempt(context.Background())
if err != nil {
t.Fatal(err)
}
for _, tester := range list {
if tester.UserID == f.userID {
return tester
}
}
return models.LoginCodeExemption{UserID: f.userID}
}
func testerGrantUntil(t *testing.T, pool *pgxpool.Pool, orgID uuid.UUID) time.Time {
t.Helper()
var until time.Time
if err := pool.QueryRow(context.Background(), `SELECT managed_until FROM subscriptions WHERE organization_id = $1`, orgID).Scan(&until); err != nil {
t.Fatal(err)
}
return until
}
// The grant, not the initial password, decides seeding: replacing or reissuing it keeps the workspace eligible.
func TestLiveTesterSampleDataSurvivesInitialPasswordReplacement(t *testing.T) {
db, pool := liveContactDB(t)
requireSchemaVersion(t, pool, 336)
ctx := context.Background()
f := newTesterSampleFixture(t, pool, NewUserRepostory(db, nil))
auth := NewAuthRepostory(db)
if _, err := pool.Exec(ctx, `INSERT INTO admin_audit_logs (admin_user_id, action, target_type, target_id, details)
VALUES ($1, 'create_tester', 'user', $1, '{}'::jsonb)`, f.userID); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM admin_audit_logs WHERE target_id = $1`, f.userID) })
grant := testerGrantUntil(t, pool, f.orgID)
if ok, xerr := auth.ReplaceInitialPassword(ctx, f.userID, "hash", "chosen-hash"); xerr != nil || !ok {
t.Fatalf("replace = %v, %v", ok, xerr)
}
if entry := testerListEntry(t, f); entry.PasswordExpiresAt != nil || !entry.SampleDataEligible {
t.Fatalf("after replacement: password_expires_at %v, eligible %v", entry.PasswordExpiresAt, entry.SampleDataEligible)
}
if _, err := f.users.ReissueTesterPassword(ctx, f.userID, "reissued-hash"); err != nil {
t.Fatal(err)
}
if ok, xerr := auth.ReplaceInitialPassword(ctx, f.userID, "reissued-hash", "second-chosen-hash"); xerr != nil || !ok {
t.Fatalf("replace reissued = %v, %v", ok, xerr)
}
if got := testerGrantUntil(t, pool, f.orgID); !got.Equal(grant) {
t.Fatalf("grant expiry moved from %v to %v", grant, got)
}
if !testerListEntry(t, f).SampleDataEligible {
t.Fatal("tester list withholds sample data from an active Test workspace")
}
result, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", "")
if err != nil || result == nil || !result.Created {
t.Fatalf("seed after replacement = %+v, %v", result, err)
}
assertTesterSampleCounts(t, pool, f.orgID, 1)
if _, err := f.users.RevokeTester(ctx, f.userID); err != nil {
t.Fatal(err)
}
if _, err := f.orgs.SeedTesterWorkspace(ctx, f.orgID, f.userID, "", ""); !errors.Is(err, ErrTesterWorkspaceInactive) {
t.Fatalf("revoked tester returned %v", err)
}
}
// A tester owning several Test workspaces is listed with the one sample data can still be added to.
func TestLiveTesterSampleDataListsTheSeedableWorkspace(t *testing.T) {
db, pool := liveContactDB(t)
requireSchemaVersion(t, pool, 336)
ctx := context.Background()
f := newTesterSampleFixture(t, pool, NewUserRepostory(db, nil))
second := newTesterSampleFixture(t, pool, f.users)
for _, q := range []string{
`UPDATE organizations SET created_at = now() - interval '1 day' WHERE id = $1`,
`UPDATE subscriptions SET managed_until = now() - interval '1 hour' WHERE organization_id = $1`,
} {
if _, err := pool.Exec(ctx, q, f.orgID); err != nil {
t.Fatal(err)
}
}
if _, err := pool.Exec(ctx, `UPDATE organizations SET owner_user_id = $2 WHERE id = $1`, second.orgID, f.userID); err != nil {
t.Fatal(err)
}
listed := func(want uuid.UUID, seeded bool) {
t.Helper()
entry := testerListEntry(t, f)
if entry.TestWorkspaceID == nil || *entry.TestWorkspaceID != want || !entry.SampleDataEligible || (entry.SampleDataSeededAt != nil) != seeded {
t.Fatalf("listed %v, eligible %v, seeded %v; want %v", entry.TestWorkspaceID, entry.SampleDataEligible, entry.SampleDataSeededAt, want)
}
}
listed(second.orgID, false)
if _, err := f.orgs.SeedTesterWorkspace(ctx, second.orgID, f.userID, "", ""); err != nil {
t.Fatal(err)
}
listed(second.orgID, true)
if _, err := pool.Exec(ctx, `UPDATE subscriptions SET managed_until = now() + interval '1 day' WHERE organization_id = $1`, f.orgID); err != nil {
t.Fatal(err)
}
listed(f.orgID, false)
}
@@ -17,7 +17,9 @@ const liveUnknownSend = `t.send_result_state='unknown' AND t.warmup_send_retired
const unresolvedUnknownSend = `t.send_result_state='unknown' AND t.warmup_send_retired_at IS NULL AND t.campaign_send_isolated_at IS NULL`
// unstartedHeldSend never reached the provider: BeginOutbound refuses it once released under the same lock.
const unstartedHeldSend = `t.task_type IN ('campaign','email','placement') AND t.status='completed'
// A manual send whose hand-off was unknown is failed or dead-lettered, which BeginOutbound already refuses.
const unstartedHeldSend = `t.task_type IN ('campaign','email','placement')
AND (t.status='completed' OR t.task_type='email' AND t.status IN ('failed','dead_lettered'))
AND t.send_result_applied_at IS NULL AND t.send_released_at IS NULL
AND t.send_executor_nonce IS NOT NULL AND t.send_executor_worker IS NOT NULL
AND t.send_executor_started_at IS NULL AND t.send_executor_result IS NULL
@@ -38,14 +40,15 @@ const recoverableUnknownHold = `ea.send_recovery_hold AND ea.send_recovery_reaso
OR COALESCE((` + liveUnknownSend + `) AND (` + unstartedHeldSend + `),false)
OR COALESCE((` + unresolvedUnknownSend + `) AND (` + liftableHeldSend + `),false))`
// RecoverUnknownSendHolds frees mailboxes held by a send that can never run again or never started.
// RecoverUnknownSendHolds frees mailboxes held by a send that can never run again or never started,
// and names the manual sends it released unsent so their owners can be told.
// Campaign and warmup sends that did start keep their own isolation and retirement paths.
func (r *taskRepository) RecoverUnknownSendHolds(ctx context.Context, startedBefore, unstartedBefore time.Time, limit int) (int, error) {
func (r *taskRepository) RecoverUnknownSendHolds(ctx context.Context, startedBefore, unstartedBefore time.Time, limit int) (int, []uuid.UUID, error) {
rows, err := r.db.Query(ctx, `SELECT ea.id,ea.send_recovery_task_id FROM email_accounts ea
LEFT JOIN tasks t ON t.id=ea.send_recovery_task_id AND t.email_account_id=ea.id
WHERE `+recoverableUnknownHold+` ORDER BY ea.id LIMIT $3`, startedBefore, unstartedBefore, min(max(limit, 1), 500))
if err != nil {
return 0, err
return 0, nil, err
}
type held struct{ mailbox, task uuid.UUID }
var candidates []held
@@ -53,23 +56,24 @@ func (r *taskRepository) RecoverUnknownSendHolds(ctx context.Context, startedBef
var h held
if err := rows.Scan(&h.mailbox, &h.task); err != nil {
rows.Close()
return 0, err
return 0, nil, err
}
candidates = append(candidates, h)
}
err = rows.Err()
rows.Close()
if err != nil {
return 0, err
return 0, nil, err
}
recovered := 0
var unsent []uuid.UUID
var errs []error
for _, h := range candidates {
if ctx.Err() != nil {
break
}
// One mailbox that cannot be recovered must not stop the ones after it.
ok, err := r.recoverUnknownSendHold(ctx, h.mailbox, h.task, startedBefore, unstartedBefore)
ok, released, err := r.recoverUnknownSendHold(ctx, h.mailbox, h.task, startedBefore, unstartedBefore)
if err != nil {
errs = append(errs, err)
continue
@@ -77,18 +81,22 @@ func (r *taskRepository) RecoverUnknownSendHolds(ctx context.Context, startedBef
if ok {
recovered++
}
if released == "email" {
unsent = append(unsent, h.task)
}
}
return recovered, errors.Join(append(errs, ctx.Err())...)
return recovered, unsent, errors.Join(append(errs, ctx.Err())...)
}
func (r *taskRepository) recoverUnknownSendHold(ctx context.Context, mailbox, task uuid.UUID, startedBefore, unstartedBefore time.Time) (bool, error) {
// recoverUnknownSendHold reports whether the hold was recovered and, when its send was released unsent, that send's task type.
func (r *taskRepository) recoverUnknownSendHold(ctx context.Context, mailbox, task uuid.UUID, startedBefore, unstartedBefore time.Time) (bool, string, error) {
tx, err := r.db.Begin(ctx)
if err != nil {
return false, err
return false, "", err
}
defer tx.Rollback(ctx)
if err = lockSend(ctx, tx, task, mailbox); err != nil {
return false, err
return false, "", err
}
var missing, unstarted, liftable bool
var worker, nonce *uuid.UUID
@@ -100,32 +108,33 @@ func (r *taskRepository) recoverUnknownSendHold(ctx context.Context, mailbox, ta
WHERE ea.id=$3 AND ea.send_recovery_task_id=$4 AND `+recoverableUnknownHold+`
FOR UPDATE OF ea`, startedBefore, unstartedBefore, mailbox, task).Scan(&missing, &unstarted, &liftable, &worker, &nonce)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
return false, "", nil
}
if err != nil {
return false, err
return false, "", err
}
switch {
case missing:
// The held send is gone; recompute the hold from what is still unknown. A settled send keeps the hold its result left.
case unstarted && worker != nil && nonce != nil:
if err = tx.Rollback(ctx); err != nil {
return false, err
return false, "", err
}
// The ordinary release path walks the reservation back and refreshes the hold.
return true, r.cancelOutbound(ctx, task, mailbox, *worker, *nonce, nil)
lane, err := r.releaseOutbound(ctx, task, mailbox, *worker, *nonce, nil)
return true, lane, err
case liftable:
if _, err = tx.Exec(ctx, `UPDATE tasks SET send_hold_lifted_at=COALESCE(send_hold_lifted_at,NOW()),updated_at=NOW() WHERE id=$1`, task); err != nil {
return false, err
return false, "", err
}
if err = resolveSendDeadLetter(ctx, tx, task); err != nil {
return false, err
return false, "", err
}
default:
return false, nil
return false, "", nil
}
if err = refreshUnknownSendHold(ctx, tx, mailbox, task); err != nil {
return false, err
return false, "", err
}
return true, tx.Commit(ctx)
return true, "", tx.Commit(ctx)
}
@@ -12,7 +12,7 @@ import (
func recoverHolds(t *testing.T, f *campaignAdmissionFixture) int {
t.Helper()
now := time.Now()
n, err := f.r.RecoverUnknownSendHolds(t.Context(), now.Add(-10*time.Minute), now.Add(-30*time.Minute), 100)
n, _, err := f.r.RecoverUnknownSendHolds(t.Context(), now.Add(-10*time.Minute), now.Add(-30*time.Minute), 100)
if err != nil {
t.Fatal(err)
}
+2
View File
@@ -67,6 +67,8 @@ type TasksService interface {
// task chain died (swallowed enqueue / crash between ticks). Campaigns have
// no other bootstrap once started, so this is the stall backstop.
StartCampaignReconciler(ctx context.Context, interval time.Duration)
// StartManualSendRecovery runs again, or fails visibly, a manual send whose execution stopped before it reserved.
StartManualSendRecovery(ctx context.Context, interval time.Duration)
// SetAI wires the LLM provider + credit ledger the campaign "switch" sequence
// steps run over (mirrors integration.Service.SetAI). Nil provider leaves
+61
View File
@@ -0,0 +1,61 @@
package tasks
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog/log"
"github.com/warmbly/warmbly/internal/config"
"github.com/warmbly/warmbly/internal/jobrun"
"github.com/warmbly/warmbly/internal/repository"
"github.com/warmbly/warmbly/internal/tasks/proto"
)
type interruptedManualSends interface {
RecoverInterruptedManualSends(ctx context.Context, claimedBefore, retrySince time.Time, limit int) ([]repository.InterruptedManualSend, error)
}
// StartManualSendRecovery settles manual sends whose execution stopped between
// their claim and their reservation, so each one runs again or fails visibly.
func (s *tasksService) StartManualSendRecovery(ctx context.Context, interval time.Duration) {
jobrun.Loop(ctx, "manual_send_recovery", interval, true, func(ctx context.Context) error {
s.recoverInterruptedManualSends(ctx)
return nil
})
}
func (s *tasksService) recoverInterruptedManualSends(ctx context.Context) {
repo, ok := s.taskRepo.(interruptedManualSends)
if !ok {
return
}
now := time.Now()
sends, err := repo.RecoverInterruptedManualSends(ctx,
now.Add(-config.ManualSendInterruptedAfterMinutes*time.Minute),
now.Add(-config.ManualSendRetryWithinMinutes*time.Minute), 200)
if err != nil {
log.Warn().Err(err).Msg("interrupted manual send recovery deferred")
}
for _, send := range sends {
if send.Retried {
// The local scheduler fires the pending row on its own; Cloud Tasks needs a new callback.
if s.tasksClient != nil {
if _, err := s.tasksClient.CreateTask(ctx, &proto.ProcessTask{TaskId: send.TaskID.String()}, time.Now()); err != nil {
log.Warn().Err(err).Str("task_id", send.TaskID.String()).Msg("interrupted manual send could not be re-enqueued")
}
}
continue
}
account, xerr := s.emailRepo.GetByID(ctx, send.MailboxID)
if xerr != nil || account == nil {
continue
}
s.notifyUserEmailFailed(ctx, send.TaskID, account, "Email was not sent",
fmt.Sprintf("%s did not send your email: sending was interrupted before it reached its worker. Send it again.", account.Email))
}
if len(sends) > 0 {
log.Info().Int("settled", len(sends)).Msg("settled manual sends interrupted before reservation")
}
}
@@ -0,0 +1,249 @@
package tasks
import (
"context"
"errors"
"slices"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/events"
"github.com/warmbly/warmbly/internal/repository"
"github.com/warmbly/warmbly/internal/tasks/proto"
)
// countingPublisher hands a send to nobody; fail makes the publish call itself error.
type countingPublisher struct {
events.Publisher
mu sync.Mutex
fail error
nonces map[uuid.UUID]string
}
func (p *countingPublisher) PublishSendEmail(_ context.Context, _ uuid.UUID, params *events.SendEmailParams) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.nonces == nil {
p.nonces = map[uuid.UUID]string{}
}
if _, again := p.nonces[params.TaskID]; again {
p.nonces[params.TaskID] += "+again"
} else {
p.nonces[params.TaskID] = params.DispatchNonce
}
return p.fail
}
func (p *countingPublisher) published(id uuid.UUID) (string, bool) {
p.mu.Lock()
defer p.mu.Unlock()
nonce, ok := p.nonces[id]
return nonce, ok
}
type manualRecoveryFixture struct {
*campaignSendFixture
svc *tasksService
pub *countingPublisher
worker uuid.UUID
}
type manualRecoveryRepo interface {
interruptedManualSends
RecoverUnknownSendHolds(context.Context, time.Time, time.Time, int) (int, []uuid.UUID, error)
BeginOutbound(context.Context, uuid.UUID, uuid.UUID, uuid.UUID, uuid.UUID) (*repository.WarmupDispatchState, error)
}
// newManualRecoveryFixture runs unibox sends through the real sender and admission, on a live worker.
func newManualRecoveryFixture(t *testing.T) *manualRecoveryFixture {
t.Helper()
handle := liveCampaignDB(t)
f := &manualRecoveryFixture{campaignSendFixture: newCampaignSendFixture(t, handle.Pool), worker: uuid.New(), pub: &countingPublisher{}}
t.Cleanup(func() {
_, _ = f.pool.Exec(context.Background(), `DELETE FROM task_dead_letters WHERE task_id IN (SELECT id FROM tasks WHERE email_account_id=$1)`, f.mailbox)
_, _ = f.pool.Exec(context.Background(), `DELETE FROM fleet_nodes WHERE id=$1`, f.worker)
})
f.exec(t, `INSERT INTO fleet_nodes(id,role,active,last_seen_at,warmup_send_protocol) VALUES($1,'worker',true,NOW(),2)`, f.worker)
f.exec(t, `INSERT INTO workers(id) VALUES($1)`, f.worker)
f.exec(t, `UPDATE email_accounts SET worker_id=$1 WHERE id=$2`, f.worker, f.mailbox)
f.svc = liveCampaignService(t, handle, nil)
sender := NewEmailSender(f.svc.emailRepo, f.pub).(*emailSender)
sender.WireSendAdmission(f.svc.taskRepo.(repository.OutboundAdmissionRepository))
f.svc.emailSender = sender
return f
}
func (f *manualRecoveryFixture) exec(t *testing.T, sql string, args ...any) {
t.Helper()
if _, err := f.pool.Exec(t.Context(), sql, args...); err != nil {
t.Fatal(err)
}
}
func (f *manualRecoveryFixture) repo() manualRecoveryRepo { return f.svc.taskRepo.(manualRecoveryRepo) }
func (f *manualRecoveryFixture) handle(t *testing.T, id uuid.UUID) {
t.Helper()
if xerr := f.svc.HandleUserEmailTask(&proto.ProcessTask{TaskId: id.String()}); xerr != nil {
t.Fatalf("handle send: %v", xerr)
}
}
// recoverHolds runs the hold sweep as if every reservation had outlived its reclaim window.
func (f *manualRecoveryFixture) recoverHolds(t *testing.T) []uuid.UUID {
t.Helper()
later := time.Now().Add(time.Hour)
_, unsent, err := f.repo().RecoverUnknownSendHolds(t.Context(), later, later, 100)
if err != nil {
t.Fatal(err)
}
return unsent
}
type manualSendState struct {
status, result string
hold bool
}
func (f *manualRecoveryFixture) state(t *testing.T, id uuid.UUID) manualSendState {
t.Helper()
var s manualSendState
if err := f.pool.QueryRow(t.Context(), `SELECT t.status,COALESCE(t.send_result_state,''),ea.send_recovery_hold
FROM tasks t JOIN email_accounts ea ON ea.id=t.email_account_id WHERE t.id=$1`, id).Scan(&s.status, &s.result, &s.hold); err != nil {
t.Fatal(err)
}
return s
}
// A manual send whose hand-off is unknown keeps its mailbox held only until
// recovery proves no worker started it; then it is released as not sent, the
// mailbox sends again, and the original command can never start.
func TestLiveManualSendWithUnknownHandOffIsReleasedUnsent(t *testing.T) {
f := newManualRecoveryFixture(t)
f.pub.fail = errors.New("publish timed out")
id := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.handle(t, id)
if got := f.state(t, id); got != (manualSendState{"dead_lettered", "unknown", true}) {
t.Fatalf("after an unknown hand-off: %+v", got)
}
nonce, _ := f.pub.published(id)
if unsent := f.recoverHolds(t); !slices.Equal(unsent, []uuid.UUID{id}) {
t.Fatalf("recovery released %v, want the unknown send", unsent)
}
if got := f.state(t, id); got != (manualSendState{"cancelled", "failed", false}) {
t.Fatalf("after recovery: %+v", got)
}
var letter string
if err := f.pool.QueryRow(t.Context(), `SELECT status FROM task_dead_letters WHERE task_id=$1`, id).Scan(&letter); err != nil || letter != "resolved" {
t.Fatalf("dead letter %q %v, want resolved", letter, err)
}
if again := f.recoverHolds(t); len(again) != 0 {
t.Fatalf("a second sweep released %v again", again)
}
state, err := f.repo().BeginOutbound(t.Context(), id, f.mailbox, f.worker, uuid.MustParse(nonce))
if err == nil && state.State == "execute" {
t.Fatal("a released send started when its command arrived late")
}
f.pub.fail = nil
next := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.handle(t, next)
if _, ok := f.pub.published(next); !ok {
t.Fatalf("the mailbox's next send was not handed over: %+v", f.state(t, next))
}
}
// A send handed to a worker that never starts it is released as not sent.
func TestLiveUnansweredManualSendIsReleasedUnsent(t *testing.T) {
f := newManualRecoveryFixture(t)
id := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.handle(t, id)
if got := f.state(t, id); got != (manualSendState{"completed", "unknown", true}) {
t.Fatalf("after hand-off: %+v", got)
}
if unsent := f.recoverHolds(t); !slices.Equal(unsent, []uuid.UUID{id}) {
t.Fatalf("recovery released %v, want the unanswered send", unsent)
}
if got := f.state(t, id); got != (manualSendState{"cancelled", "failed", false}) {
t.Fatalf("after recovery: %+v", got)
}
}
// A send that stopped between its claim and its reservation runs again exactly
// once; a redelivery while it is stranded, or after it went out, sends nothing.
func TestLiveManualSendInterruptedBeforeReservationRunsOnce(t *testing.T) {
f := newManualRecoveryFixture(t)
id := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.exec(t, `UPDATE tasks SET status='active',updated_at=NOW()-INTERVAL '20 minutes' WHERE id=$1`, id)
f.exec(t, `INSERT INTO task_execution_keys(task_id,execution_key,status,metadata,first_seen_at,last_seen_at,attempts)
VALUES($1,$2,'in_progress','{}',NOW()-INTERVAL '20 minutes',NOW()-INTERVAL '20 minutes',1)`, id, "user-email:"+id.String())
f.handle(t, id)
if _, ok := f.pub.published(id); ok {
t.Fatal("a redelivery of a claimed send handed it over")
}
f.svc.recoverInterruptedManualSends(t.Context())
if got := f.state(t, id); got.status != "pending" {
t.Fatalf("interrupted send is %+v, want pending", got)
}
f.svc.recoverInterruptedManualSends(t.Context())
f.handle(t, id)
f.handle(t, id)
if nonce, ok := f.pub.published(id); !ok || nonce == "" || len(nonce) != len(uuid.Nil.String()) {
t.Fatalf("interrupted send handed over %q %v, want exactly once", nonce, ok)
}
if got := f.state(t, id); got != (manualSendState{"completed", "unknown", true}) {
t.Fatalf("after the retry: %+v", got)
}
f.svc.recoverInterruptedManualSends(t.Context())
if got := f.state(t, id); got.status != "completed" {
t.Fatalf("recovery touched a reserved send: %+v", got)
}
}
// An interrupted send too old to go out now, or one with nothing to run again
// (a test send), fails with a recorded reason instead of staying claimed.
func TestLiveStaleOrUnrunnableInterruptedManualSendFails(t *testing.T) {
f := newManualRecoveryFixture(t)
stale := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.exec(t, `UPDATE tasks SET status='active',updated_at=NOW()-INTERVAL '3 hours' WHERE id=$1`, stale)
testSend := uuid.New()
f.exec(t, `INSERT INTO tasks(id,task_type,email_account_id,status,message_id,updated_at) VALUES($1,'email',$2,'active','',NOW()-INTERVAL '20 minutes')`, testSend, f.mailbox)
fresh := queueUniboxReply(t, f.pool, f.mailbox, "", "")
f.exec(t, `UPDATE tasks SET status='active',updated_at=NOW() WHERE id=$1`, fresh)
now := time.Now()
got, err := f.repo().RecoverInterruptedManualSends(t.Context(), now.Add(-10*time.Minute), now.Add(-time.Hour), 100)
if err != nil {
t.Fatal(err)
}
settled := map[uuid.UUID]bool{}
for _, s := range got {
if s.MailboxID != f.mailbox {
t.Fatalf("settled send names mailbox %s", s.MailboxID)
}
settled[s.TaskID] = s.Retried
}
if retried, ok := settled[stale]; !ok || retried {
t.Fatalf("stale send settled=%v retried=%v, want failed", ok, retried)
}
if retried, ok := settled[testSend]; !ok || retried {
t.Fatalf("test send settled=%v retried=%v, want failed", ok, retried)
}
if _, ok := settled[fresh]; ok {
t.Fatal("a send still inside its handler's window was settled")
}
for _, id := range []uuid.UUID{stale, testSend} {
var status, title string
if err := f.pool.QueryRow(t.Context(), `SELECT t.status,f.title FROM tasks t JOIN task_failures f ON f.task_id=t.id WHERE t.id=$1`, id).Scan(&status, &title); err != nil || status != "failed" || title != "Send interrupted" {
t.Fatalf("%s: status=%q failure=%q %v", id, status, title, err)
}
}
if got := f.state(t, fresh); got.status != "active" {
t.Fatalf("fresh claim is %+v", got)
}
}
+1 -1
View File
@@ -136,7 +136,7 @@ Debug builds read launch environment variables so a script can open the app sign
- `WARMBLY_DEBUG_CAMPAIGN`: with `WARMBLY_DEBUG_TAB=campaigns`, opens the campaign whose name contains the value (the first campaign when nothing matches).
- `WARMBLY_DEBUG_CONTACT`: with `WARMBLY_DEBUG_TAB=contacts`, opens the contact whose name or address contains the value. `WARMBLY_DEBUG_SORT` sets the contacts sort by its API name (`first_name`, `created_at`, ...).
- `WARMBLY_DEBUG_SETTINGS`: with `WARMBLY_DEBUG_TAB=more`, opens a settings page (`profile`, `notifications`, `security`, `billing`, `team`, `templates` or `activity`) or a full-screen browser (`analytics`, `mailboxes`, `deals`, `tasks` or `meetings`). `WARMBLY_DEBUG_NOTIFICATIONS=preferences` opens the notifications page on its preferences. `WARMBLY_DEBUG_ANALYTICS` picks the analytics page (`overview`, `deliverability`, `warmup` or `accounts`), and `WARMBLY_DEBUG_ANALYTICS_ACCOUNT=1` then opens a mailbox's health. `WARMBLY_DEBUG_TEMPLATE=1` opens the first template and `WARMBLY_DEBUG_AUDIT_ENTRY=1` the newest activity entry. With `mailboxes`, `WARMBLY_DEBUG_CONNECT` opens the add-a-mailbox flow on a step (`provider`, `google`, `twostep`, `apppassword`, `appconnect`, `account`, `imap`, `smtp`, `grants`, `users`, `vendors`, `vendorkey`, `vendormailboxes`, `file`, `filereview` or `running`); every step past `provider` uses sample data, including an admin grant, a vendor catalog and a file preview, and `home` opens the first step with that data.
- `WARMBLY_DEBUG_PHASE`: opens a screen from before the app, leaving the stored session alone: `launching` holds the splash, `signedout` shows sign-in, `onboarding` the first-run questions and `workspace` the create-a-workspace step. With `signedout`, `WARMBLY_DEBUG_AUTH` opens a later sign-in step (`signup`, `password`, `verify`, `twofa` or `reset`), or `server` to open the Server sheet; with `onboarding`, `WARMBLY_DEBUG_ONBOARDING` opens `referral`, `role` or `team`.
- `WARMBLY_DEBUG_PHASE`: opens a screen from before the app, leaving the stored session alone: `launching` holds the splash, `signedout` shows sign-in, `onboarding` the first-run questions and `workspace` the create-a-workspace step. With `signedout`, `WARMBLY_DEBUG_AUTH` opens a later sign-in step (`signup`, `password`, `verify`, `twofa`, `newpassword` or `reset`), or `server` to open the Server sheet; with `onboarding`, `WARMBLY_DEBUG_ONBOARDING` opens `referral`, `role` or `team`.
- `WARMBLY_DEBUG_ASSISTANT`: opens Remie on launch. `1` opens it empty, any other text is sent as the first message, and `demo` or `demo-approval` plays a scripted run (tool steps, a streamed answer and, for the second, an approval card) without calling the AI provider, which the local stack does not configure.
- `WARMBLY_DEBUG_FRAME`: renders the app in a window of `<width>x<height>` points, scaled to fit, so an iPad simulator can show another device's layout. `951x669` is an unfolded iPhone Duo, `1376x1032` a 13-inch iPad in landscape. It sets the size only: safe-area insets and full-screen covers still come from the host, so check those on the target simulator.
+3
View File
@@ -70,6 +70,8 @@ struct AuthSession: Codable, Sendable {
var twoFARequired: Bool?
var pendingToken: String?
var expiresIn: Int?
/// The password was an issued initial one: `pendingToken` goes to `/auth/login/initial-password`.
var passwordChangeRequired: Bool?
enum CodingKeys: String, CodingKey {
case session
@@ -78,6 +80,7 @@ struct AuthSession: Codable, Sendable {
case twoFARequired = "two_fa_required"
case pendingToken = "pending_token"
case expiresIn = "expires_in"
case passwordChangeRequired = "password_change_required"
}
/// Older backends omit the field entirely; they always sent a code.
+22 -2
View File
@@ -18,6 +18,8 @@ enum LoginOutcome {
/// deployment sends one: self-hosted installs default to signing in without
/// it, so a login can resolve on the first call.
case needsCode(session: String)
/// The password was an issued initial one; `chooseInitialPassword` replaces it and continues.
case needsNewPassword(pendingToken: String)
}
/// A federated sign-in on an address that already has a password: nothing is
@@ -216,11 +218,18 @@ final class SessionStore {
func startLogin(email: String, password: String) async throws -> LoginOutcome {
let result: AuthSession = try await verified(.login) { check in
try await self.api.post("auth/login", body: PasswordAuthBody(email: email, password: password, check: check), authorized: false)
try await self.api.post("auth/login", body: PasswordAuthBody(email: email, password: password, check: check, supportsPasswordChange: true), authorized: false)
}
return try await resolve(result)
}
/// Replaces an issued initial password with the holder's own, then continues the sign-in.
func chooseInitialPassword(pendingToken: String, password: String) async throws -> LoginOutcome {
let body = ["pending_token": pendingToken, "password": password]
let result: AuthSession = try await api.post("auth/login/initial-password", body: body, authorized: false)
return try await resolve(result)
}
// MARK: - Human check
/// What stands in for a captcha on the email paths: nothing, the dev stack's bypass token, or an App Attest proof.
@@ -252,17 +261,21 @@ final class SessionStore {
var password: String
var turnstile: String?
var appAttest: AppAttestClient.Proof?
/// Login only: this build can take a `password_change_required` answer.
var supportsPasswordChange: Bool?
init(email: String, password: String, check: HumanCheck) {
init(email: String, password: String, check: HumanCheck, supportsPasswordChange: Bool? = nil) {
self.email = email
self.password = password
turnstile = check.turnstile
appAttest = check.appAttest
self.supportsPasswordChange = supportsPasswordChange
}
enum CodingKeys: String, CodingKey {
case email, password, turnstile
case appAttest = "app_attest"
case supportsPasswordChange = "supports_password_change"
}
}
@@ -300,6 +313,13 @@ final class SessionStore {
/// Maps a start-of-login response onto an outcome, completing the sign-in
/// when the backend already resolved it.
private func resolve(_ result: AuthSession) async throws -> LoginOutcome {
// Carries code_required false and no token, so it is read first.
if result.passwordChangeRequired == true {
guard let pending = result.pendingToken else {
throw APIError.decoding(URLError(.cannotParseResponse))
}
return .needsNewPassword(pendingToken: pending)
}
if result.needsCode {
guard let session = result.session else {
throw APIError.decoding(URLError(.cannotParseResponse))
+193 -4
View File
@@ -13,6 +13,8 @@ struct AuthFlowView: View {
case password
case verify(session: String)
case twoFA(pendingToken: String)
/// The password was an issued initial one; the holder chooses their own.
case newPassword(pendingToken: String)
/// Apple or Google signed in to an address that already has a password.
case link(SSOLinkChallenge)
case resetSent
@@ -29,6 +31,8 @@ struct AuthFlowView: View {
@State private var mode = Mode.signIn
@State private var email = ""
@State private var password = ""
@State private var newPassword = ""
@State private var confirmPassword = ""
@State private var code = ""
@State private var recoveryCode = ""
@State private var useRecovery = false
@@ -40,13 +44,17 @@ struct AuthFlowView: View {
@State private var resendRemaining = 0
@State private var providers: AuthProvidersInfo?
@State private var showServerConfig = false
/// The server a sign-in in progress is talking to, read when the settings sheet opens.
@State private var originBeforeSettings = AppConfig.serverOrigin
/// A chosen password whose save went unanswered, so it may be the account's password now.
@State private var unconfirmedPassword: String?
@State private var providersGeneration = 0
@State private var heroAppeared = false
@State private var modeSwitchEdge = Edge.trailing
@FocusState private var focusedField: Field?
private enum Field { case email, password, recovery }
private enum Field { case email, password, newPassword, confirmPassword, recovery }
/// The email step keeps the big cloud art; deeper steps clear the sky
/// for the step badge.
@@ -60,7 +68,7 @@ struct AuthFlowView: View {
.sensoryFeedback(.impact(weight: .light), trigger: step)
.sensoryFeedback(.error, trigger: errorPulse)
// The sheet can change the server, so the sign-in options are read again when it closes.
.sheet(isPresented: $showServerConfig, onDismiss: { providersGeneration += 1 }) { ServerConfigSheet() }
.sheet(isPresented: $showServerConfig, onDismiss: settingsClosed) { ServerConfigSheet() }
.task(id: providersGeneration) {
providers = nil
let loaded = await env.session.fetchSignInProviders()
@@ -76,9 +84,10 @@ struct AuthFlowView: View {
case "password": step = .password
case "verify": step = .verify(session: "debug")
case "twofa": step = .twoFA(pendingToken: "debug")
case "newpassword": step = .newPassword(pendingToken: "debug")
case "link": step = .link(SSOLinkChallenge(pendingToken: "debug", email: email, provider: "google"))
case "reset": step = .resetSent
case "server": showServerConfig = true
case "server": openSettings()
default: break
}
}
@@ -113,7 +122,7 @@ struct AuthFlowView: View {
onBack: step == .email ? nil : { goBackOneStep() },
progress: stepProgress.map { ($0.index, 3) }
) {
FlowSkyButton(symbol: "gearshape", label: "Settings") { showServerConfig = true }
FlowSkyButton(symbol: "gearshape", label: "Settings") { openSettings() }
}
heroArea
@@ -178,6 +187,8 @@ struct AuthFlowView: View {
return nil
case .password:
return (1, "lock.fill", mode == .signIn ? "Your password" : "Choose a password")
case .newPassword:
return (2, "key.fill", "Choose your password")
case .verify:
return (2, "envelope.open.fill", "Check your email")
case .twoFA:
@@ -202,6 +213,8 @@ struct AuthFlowView: View {
verifyStep
case .twoFA:
twoFAStep
case .newPassword:
newPasswordStep
case let .link(challenge):
linkStep(challenge)
case .resetSent:
@@ -258,6 +271,14 @@ struct AuthFlowView: View {
code = ""
infoMessage = nil
go(to: .password, direction: -1)
case .newPassword:
// A save in flight finishes the sign-in it started.
guard !busy else { return }
// The issued password opens a fresh challenge when entered again.
password = ""
newPassword = ""
confirmPassword = ""
go(to: .password, direction: -1)
case .twoFA, .link:
restart()
default:
@@ -524,6 +545,75 @@ struct AuthFlowView: View {
.accessibilityHidden(true)
}
// MARK: - Step: choose a password after an issued one
/// Why the save button is off, once there is enough typed to say.
private var newPasswordProblem: String? {
if newPassword.count < 8 { return nil }
if newPassword == password { return "Choose a password different from the one you were given." }
if !confirmPassword.isEmpty, confirmPassword != newPassword { return "The passwords don't match." }
return nil
}
private var newPasswordReady: Bool {
newPassword.count >= 8 && newPassword != password && confirmPassword == newPassword
}
private var newPasswordStep: some View {
VStack(alignment: .leading, spacing: 0) {
sheetTitle(
"Choose your password",
"You signed in with a temporary password. Choose your own to finish signing in. At least 8 characters."
)
VStack(spacing: 12) {
HStack {
emailPill
Spacer()
}
.background(autofillUsername)
authField(
SecureField("New password", text: $newPassword)
.textContentType(.newPassword)
.focused($focusedField, equals: .newPassword)
.submitLabel(.next)
.onSubmit { focusedField = .confirmPassword },
focused: focusedField == .newPassword
)
if !newPassword.isEmpty {
PasswordStrengthBar(password: newPassword)
.transition(.opacity.combined(with: .move(edge: .top)))
}
authField(
SecureField("Confirm password", text: $confirmPassword)
.textContentType(.newPassword)
.focused($focusedField, equals: .confirmPassword)
.submitLabel(.go)
.onSubmit { Task { await submitNewPassword() } },
focused: focusedField == .confirmPassword
)
if let newPasswordProblem {
Text(newPasswordProblem)
.font(.system(size: 13.5))
.foregroundStyle(WTheme.inkSecondary)
.frame(maxWidth: .infinity, alignment: .leading)
.transition(.opacity)
}
primaryButton("Save and sign in", disabled: !newPasswordReady) {
await submitNewPassword()
}
}
.animation(.spring(response: 0.35, dampingFraction: 0.85), value: newPassword.isEmpty)
.animation(.easeOut(duration: 0.18), value: newPasswordProblem)
}
.onAppear { focusedField = .newPassword }
}
// MARK: - Step: link a provider to a password account
private func providerName(_ provider: String) -> String {
@@ -684,6 +774,28 @@ struct AuthFlowView: View {
// MARK: - Actions
private func openSettings() {
// A request in flight is answered by the server it was sent to.
guard !busy else { return }
originBeforeSettings = AppConfig.serverOrigin
showServerConfig = true
}
private func settingsClosed() {
providersGeneration += 1
guard AppConfig.serverOrigin != originBeforeSettings else { return }
// Codes, challenges and passwords belong to the server that issued them.
switch step {
case .email, .password, .resetSent:
break
default:
password = ""
newPassword = ""
confirmPassword = ""
restart()
}
}
private func restart() {
code = ""
recoveryCode = ""
@@ -733,6 +845,8 @@ struct AuthFlowView: View {
infoMessage = nil
resendRemaining = 60
go(to: .verify(session: session))
case let .needsNewPassword(pendingToken):
showNewPassword(pendingToken)
}
case .signUp:
registerFlow = true
@@ -751,6 +865,8 @@ struct AuthFlowView: View {
code = ""
resendRemaining = 60
go(to: .verify(session: loginSession))
case let .needsNewPassword(pendingToken):
showNewPassword(pendingToken)
}
return
}
@@ -782,6 +898,8 @@ struct AuthFlowView: View {
infoMessage = "Account created. We emailed you a sign-in code."
}
go(to: .verify(session: loginSession))
case let .needsNewPassword(pendingToken):
showNewPassword(pendingToken)
}
} else {
let outcome = try await env.session.confirmLogin(session: session, code: submitted)
@@ -826,6 +944,77 @@ struct AuthFlowView: View {
if errorMessage != nil { code = "" }
}
private func showNewPassword(_ pendingToken: String) {
unconfirmedPassword = nil
newPassword = ""
confirmPassword = ""
infoMessage = nil
go(to: .newPassword(pendingToken: pendingToken))
}
private func submitNewPassword() async {
guard case let .newPassword(pendingToken) = step, !busy, newPasswordReady else { return }
let chosen = newPassword
var expired = false
await run {
do {
let outcome = try await env.session.chooseInitialPassword(pendingToken: pendingToken, password: chosen)
guard step == .newPassword(pendingToken: pendingToken) else { return }
// A resent code signs in again, now with the chosen password.
password = chosen
unconfirmedPassword = nil
newPassword = ""
confirmPassword = ""
switch outcome {
case .loggedIn:
return
case let .needsTwoFA(pending):
code = ""
go(to: .twoFA(pendingToken: pending))
case let .needsCode(session):
code = ""
resendRemaining = 60
withAnimation(.easeOut(duration: 0.2)) {
infoMessage = "Password saved. We emailed you a sign-in code."
}
go(to: .verify(session: session))
case let .needsNewPassword(pending):
showNewPassword(pending)
}
} catch {
guard step == .newPassword(pendingToken: pendingToken) else { return }
expired = (error as? APIError)?.code == "password_change_expired"
if !expired, !Self.refusedBeforeSaving(error) { unconfirmedPassword = chosen }
throw error
}
}
// The challenge is spent, expired or voided by a reissue: only a new sign-in continues.
if expired, step == .newPassword(pendingToken: pendingToken) {
let message = errorMessage
newPassword = ""
confirmPassword = ""
if let saved = unconfirmedPassword {
// An earlier attempt may have saved it before its answer was lost.
unconfirmedPassword = nil
password = saved
go(to: .password, direction: -1)
withAnimation(.easeOut(duration: 0.2)) {
infoMessage = "Your new password may already be saved, so it is filled in. Continue with it, or enter the password you were given if it is refused."
}
} else {
password = ""
go(to: .password, direction: -1)
if let message { showError(message) }
}
}
}
/// A refusal the server gives before it writes the password, so the issued one still stands.
private static func refusedBeforeSaving(_ error: Error) -> Bool {
guard let code = (error as? APIError)?.code else { return false }
return ["password_reused", "password_breached", "sessions_not_ended"].contains(code)
}
private func handleSocial(_ outcome: SocialOutcome) {
switch outcome {
case .loggedIn:
+2
View File
@@ -1325,6 +1325,8 @@ HDR
postgres:
restart: unless-stopped
image: postgres:16-alpine
# warmblyctl restore replaces every table in one transaction, which needs a larger lock table than the default.
command: ["postgres", "-c", "max_locks_per_transaction=256"]
environment:
POSTGRES_USER: warmbly
POSTGRES_PASSWORD: \${POSTGRES_PASSWORD}
+1 -1
View File
@@ -1 +1 @@
b0e9c7a1efe85abc91ddde77aad5aa2833d890b58bb903dffc2bbc93d50707bc install.sh
ba74db9ec1ce82bd13ca9b0acb4378b52641d521d00c77d2cc8ffa7c64261062 install.sh
@@ -9,7 +9,10 @@ import SubmissionsTab from "./SubmissionsTab";
vi.mock("@/hooks/context/confirm", () => ({ useConfirm: () => ({ show: vi.fn() }) }));
vi.mock("@/hooks/usePermission", () => ({ useWriteGuard: () => ({ guard: (fn: () => void) => fn }) }));
vi.mock("@/lib/api/hooks/app/forms", () => ({
useFormSubmissions: () => ({ data: { data: [{ id: "response", contact_name: "Alice", data: {}, created_at: new Date(), triage: "good" }] } }),
useFormSubmissions: () => ({
data: { rows: [{ id: "response", contact_name: "Alice", data: {}, created_at: new Date().toISOString(), triage: "good" }], next: null, hasMore: false },
}),
useLoadMoreFormSubmissions: () => ({ isPending: false }),
useFormStats: () => ({ data: { identified: [] } }),
useDeleteFormSubmission: () => ({}),
}));
+13 -25
View File
@@ -1,7 +1,7 @@
// SubmissionsTab — the responses table: every submission plus every identified
// visitor who opened their link but has not finished, one activity-sorted list.
// The newest submissions page stays live through the realtime spine; older
// pages are appended on demand.
// Every loaded page stays live through the realtime spine; older pages are
// appended on demand.
import React from "react";
import useBrowseState from "@/hooks/useBrowseState";
@@ -14,8 +14,12 @@ import { EmptyBlock } from "@/components/layout/Page";
import { SearchInput } from "@/components/ui/field";
import { useConfirm } from "@/hooks/context/confirm";
import { useWriteGuard } from "@/hooks/usePermission";
import { listFormSubmissions } from "@/lib/api/client/app/forms";
import { useDeleteFormSubmission, useFormStats, useFormSubmissions } from "@/lib/api/hooks/app/forms";
import {
useDeleteFormSubmission,
useFormStats,
useFormSubmissions,
useLoadMoreFormSubmissions,
} from "@/lib/api/hooks/app/forms";
import type Form from "@/lib/api/models/app/forms/Form";
import type { FormSubmission, FormTriage } from "@/lib/api/models/app/forms/Form";
import type { FormIdentifiedVisitor } from "@/lib/api/models/app/forms/FormStats";
@@ -72,6 +76,7 @@ export default function SubmissionsTab({ form }: { form: Form }) {
const confirm = useConfirm();
const write = useWriteGuard("MANAGE_CONTACTS");
const first = useFormSubmissions(form.id);
const more = useLoadMoreFormSubmissions(form.id);
// Identified-visitor rows come from the stats payload; widest range so an
// in-progress opener does not vanish from the list after a week.
const stats = useFormStats(form.id, "90d");
@@ -79,10 +84,6 @@ export default function SubmissionsTab({ form }: { form: Form }) {
const totalPages = React.useMemo(() => splitPages(form.fields).length, [form.fields]);
// Older pages, loaded past the live first page.
const [older, setOlder] = React.useState<FormSubmission[]>([]);
const [olderHasMore, setOlderHasMore] = React.useState(false);
const [loadingMore, setLoadingMore] = React.useState(false);
const [selected, setSelected] = React.useState<FormSubmission | null>(null);
const [query, setQuery] = useBrowseState(`forms.${form.id}.submissions.query`, "", browseSearchSchema);
@@ -90,13 +91,9 @@ export default function SubmissionsTab({ form }: { form: Form }) {
const [checked, setChecked] = React.useState<string[]>([]);
const [bulkBusy, setBulkBusy] = React.useState(false);
const firstPage = React.useMemo(() => first.data?.data ?? [], [first.data]);
const firstIds = React.useMemo(() => new Set(firstPage.map((s) => s.id)), [firstPage]);
const submissions = React.useMemo(
() => [...firstPage, ...older.filter((s) => !firstIds.has(s.id))],
[firstPage, older, firstIds],
);
const hasMore = older.length > 0 ? olderHasMore : (first.data?.has_more ?? false);
const submissions = React.useMemo(() => first.data?.rows ?? [], [first.data]);
const hasMore = first.data?.hasMore ?? false;
const loadingMore = more.isPending;
const rows = React.useMemo<Row[]>(() => {
const out: Row[] = submissions.map((s) => ({
@@ -162,17 +159,10 @@ export default function SubmissionsTab({ form }: { form: Form }) {
}
async function loadMore() {
const last = submissions[submissions.length - 1];
if (!last) return;
setLoadingMore(true);
try {
const res = await listFormSubmissions(form.id, 50, new Date(last.created_at).toISOString());
setOlder((prev) => [...prev, ...res.data]);
setOlderHasMore(res.has_more);
await more.mutateAsync();
} catch (err) {
toast.error(buildError(err as AppError));
} finally {
setLoadingMore(false);
}
}
@@ -181,7 +171,6 @@ export default function SubmissionsTab({ form }: { form: Form }) {
confirm.show("Delete this submission? The contact it created is kept.", async () => {
try {
await remove.mutateAsync({ formId: form.id, submissionId: s.id });
setOlder((prev) => prev.filter((x) => x.id !== s.id));
setChecked((prev) => prev.filter((x) => x !== s.id));
setSelected((cur) => (cur?.id === s.id ? null : cur));
toast.success("Submission deleted");
@@ -202,7 +191,6 @@ export default function SubmissionsTab({ form }: { form: Form }) {
);
setBulkBusy(false);
const okIds = new Set(ids.filter((_, i) => results[i].status === "fulfilled"));
setOlder((prev) => prev.filter((x) => !okIds.has(x.id)));
setSelected((cur) => (cur && okIds.has(cur.id) ? null : cur));
setChecked([]);
if (okIds.size === ids.length) toast.success(`${okIds.size} submissions deleted`);
+27 -5
View File
@@ -33,19 +33,41 @@ export async function deleteForm(id: string): Promise<void> {
await Request<void>({ method: "DELETE", url: `/forms/${id}`, authorization: true });
}
/** Where the next submissions page starts: the opaque cursor, or the raw
* `created_at` an older backend that issues no cursor pages by. */
export type FormSubmissionsPageParam = { cursor: string } | { before: string };
export interface FormSubmissionsPage {
data: FormSubmission[];
has_more: boolean;
next: FormSubmissionsPageParam | null;
}
export async function listFormSubmissions(
id: string,
limit = 50,
before?: string,
): Promise<{ data: FormSubmission[]; has_more: boolean }> {
page?: FormSubmissionsPageParam,
): Promise<FormSubmissionsPage> {
const params = new URLSearchParams({ limit: String(limit) });
if (before) params.set("before", before);
const res = await Request<{ data: FormSubmission[]; has_more: boolean }>({
if (page && "cursor" in page) params.set("cursor", page.cursor);
if (page && "before" in page) params.set("before", page.before);
const res = await Request<{
data: FormSubmission[];
has_more: boolean;
pagination?: { next_cursor: string | null; has_more: boolean };
}>({
method: "GET",
url: `/forms/${id}/submissions?${params.toString()}`,
authorization: true,
});
return { data: res.data ?? [], has_more: res.has_more ?? false };
const data = res.data ?? [];
const hasMore = res.pagination?.has_more ?? res.has_more ?? false;
const last = data[data.length - 1];
let next: FormSubmissionsPageParam | null = null;
if (res.pagination?.next_cursor) next = { cursor: res.pagination.next_cursor };
// Verbatim, never through Date: that would drop the microseconds.
else if (hasMore && last) next = { before: last.created_at };
return { data, has_more: hasMore, next };
}
export async function deleteFormSubmission(formId: string, submissionId: string): Promise<void> {
+51 -4
View File
@@ -19,6 +19,15 @@ import {
import type { FormAssetKind } from "@/lib/api/client/app/forms";
import type { FormWrite } from "@/lib/api/models/app/forms/Form";
import type { FormStatsRange } from "@/lib/api/models/app/forms/FormStats";
import {
appendPage,
mergeRefreshed,
readWindow,
removeSubmissions,
restoreSubmission,
SUBMISSIONS_PAGE_SIZE,
type SubmissionsWindow,
} from "./submissionsWindow";
// Every form read lives under ["forms"]: the realtime spine invalidates that
// prefix on any form mutation, and FORM_SUBMISSION_CREATED events refresh
@@ -40,14 +49,38 @@ export function useFormsConfig() {
return useQuery({ queryKey: ["forms", "instance-config"], queryFn: getFormsConfig, staleTime: 5 * 60 * 1000 });
}
export function useFormSubmissions(id: string | undefined, before?: string) {
export const formSubmissionsKey = (id: string | undefined) => ["forms", id, "submissions"] as const;
// Realtime invalidations of ["forms"] land here: the refresh re-reads every
// loaded row, so later pages keep their rows and lose deleted ones.
export function useFormSubmissions(id: string | undefined) {
const queryClient = useQueryClient();
return useQuery({
queryKey: ["forms", id, "submissions", before ?? ""],
queryFn: () => listFormSubmissions(id as string, 50, before),
queryKey: formSubmissionsKey(id),
queryFn: async (): Promise<SubmissionsWindow> => {
const key = formSubmissionsKey(id);
const before = queryClient.getQueryData<SubmissionsWindow>(key);
const fresh = await readWindow((page, limit) => listFormSubmissions(id as string, limit, page), before);
return mergeRefreshed(fresh, queryClient.getQueryData<SubmissionsWindow>(key));
},
enabled: !!id,
});
}
export function useLoadMoreFormSubmissions(id: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async () => {
const next = queryClient.getQueryData<SubmissionsWindow>(formSubmissionsKey(id))?.next;
return next ? await listFormSubmissions(id, SUBMISSIONS_PAGE_SIZE, next) : null;
},
onSuccess: (page) => {
if (!page) return;
queryClient.setQueryData<SubmissionsWindow>(formSubmissionsKey(id), (w) => (w ? appendPage(w, page) : w));
},
});
}
function invalidateForms(queryClient: ReturnType<typeof useQueryClient>) {
return queryClient.invalidateQueries({ queryKey: ["forms"] });
}
@@ -108,12 +141,26 @@ export function useDeleteFormImage() {
});
}
// Optimistic: the row leaves every loaded page at once and comes back if the
// server refuses.
export function useDeleteFormSubmission() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ formId, submissionId }: { formId: string; submissionId: string }) =>
deleteFormSubmission(formId, submissionId),
onSuccess: () => invalidateForms(queryClient),
onMutate: async ({ formId, submissionId }) => {
const key = formSubmissionsKey(formId);
await queryClient.cancelQueries({ queryKey: key });
const removed = queryClient.getQueryData<SubmissionsWindow>(key)?.rows.find((r) => r.id === submissionId);
queryClient.setQueryData<SubmissionsWindow>(key, (w) => (w ? removeSubmissions(w, new Set([submissionId])) : w));
return { removed };
},
onError: (_err, { formId }, ctx) => {
const removed = ctx?.removed;
if (!removed) return;
queryClient.setQueryData<SubmissionsWindow>(formSubmissionsKey(formId), (w) => (w ? restoreSubmission(w, removed) : w));
},
onSettled: () => invalidateForms(queryClient),
});
}
@@ -0,0 +1,146 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { FormSubmissionsPage, FormSubmissionsPageParam } from "@/lib/api/client/app/forms";
import type { FormSubmission } from "@/lib/api/models/app/forms/Form";
import { compareSubmissions } from "./submissionsWindow";
// A keyset server: newest first by (created_at, id), cursor = the last row's
// position, so a deleted cursor row still pages correctly.
const server = vi.hoisted(() => ({ rows: [] as { id: string; created_at: string }[], fail: false }));
vi.mock("@/lib/api/client/app/forms", async (orig) => ({
...(await orig<Record<string, unknown>>()),
listFormSubmissions: async (_id: string, limit: number, page?: FormSubmissionsPageParam): Promise<FormSubmissionsPage> => {
const key = (r: { id: string; created_at: string }) => `${r.created_at}|${r.id}`;
const sorted = [...server.rows].sort((a, b) => (key(a) < key(b) ? 1 : -1));
const after = page && "cursor" in page ? page.cursor : null;
const rest = after ? sorted.filter((r) => key(r) < after) : sorted;
const data = rest.slice(0, limit).map((r) => sub(r.id, r.created_at));
const hasMore = rest.length > limit;
return { data, has_more: hasMore, next: hasMore ? { cursor: key(data[data.length - 1]) } : null };
},
deleteFormSubmission: async (_form: string, id: string) => {
if (server.fail) throw new Error("refused");
server.rows = server.rows.filter((r) => r.id !== id);
},
}));
import { useDeleteFormSubmission, useFormSubmissions, useLoadMoreFormSubmissions } from "./index";
function sub(id: string, created_at: string): FormSubmission {
return { id, created_at, form_id: "form", organization_id: "org", data: {}, source_url: "" };
}
// Fixed-width microsecond stamps, so the fake server can order them as strings.
const at = (us: number) => `2026-03-01T12:00:00.${String(us).padStart(6, "0")}Z`;
function seed(n: number) {
// Pairs share a microsecond so page boundaries fall inside ties.
server.rows = Array.from({ length: n }, (_, i) => ({ id: `s${String(i).padStart(3, "0")}`, created_at: at(100 + Math.floor(i / 2)) }));
}
function setup() {
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const wrapper = ({ children }: { children: ReactNode }) => <QueryClientProvider client={client}>{children}</QueryClientProvider>;
const hook = renderHook(
() => ({ list: useFormSubmissions("form"), more: useLoadMoreFormSubmissions("form"), remove: useDeleteFormSubmission() }),
{ wrapper },
);
const ids = () => hook.result.current.list.data?.rows.map((r) => r.id) ?? [];
const refresh = async () => {
await act(() => client.invalidateQueries({ queryKey: ["forms"] }));
await waitFor(() => expect(hook.result.current.list.isFetching).toBe(false));
};
return { hook, ids, refresh };
}
const serverOrder = () => [...server.rows].map((r) => sub(r.id, r.created_at)).sort(compareSubmissions).map((r) => r.id);
describe("loaded form submissions under realtime refreshes", () => {
beforeEach(() => {
seed(120);
server.fail = false;
});
it("keeps every loaded page when new submissions push the first page down", async () => {
const { hook, ids, refresh } = setup();
await waitFor(() => expect(ids()).toHaveLength(50));
await act(() => hook.result.current.more.mutateAsync());
await waitFor(() => expect(ids()).toHaveLength(100));
expect(ids()).toEqual(serverOrder().slice(0, 100));
const loaded = ids();
server.rows.push(...["n1", "n2", "n3"].map((id, i) => ({ id, created_at: at(900 + i) })));
await refresh();
expect(ids().slice(0, 3)).toEqual(["n3", "n2", "n1"]);
expect(ids()).toHaveLength(103);
for (const id of loaded) expect(ids()).toContain(id);
expect(new Set(ids()).size).toBe(ids().length);
expect(ids()).toEqual(serverOrder().slice(0, ids().length));
// Loading on continues from the oldest loaded row with nothing skipped or repeated.
while (hook.result.current.list.data?.hasMore) await act(() => hook.result.current.more.mutateAsync());
expect(ids()).toEqual(serverOrder());
});
it("drops a submission deleted elsewhere from a later page, including the oldest loaded row", async () => {
const { hook, ids, refresh } = setup();
await waitFor(() => expect(ids()).toHaveLength(50));
await act(() => hook.result.current.more.mutateAsync());
await waitFor(() => expect(ids()).toHaveLength(100));
const loaded = ids();
const gone = new Set([loaded[70], loaded[loaded.length - 1]]);
server.rows = server.rows.filter((r) => !gone.has(r.id));
await refresh();
for (const id of gone) expect(ids()).not.toContain(id);
// The refresh reads as many rows as were loaded, so it reaches two past the old floor.
expect(ids()).toEqual(serverOrder().slice(0, 100));
for (const id of loaded.filter((id) => !gone.has(id))) expect(ids()).toContain(id);
while (hook.result.current.list.data?.hasMore) await act(() => hook.result.current.more.mutateAsync());
expect(ids()).toEqual(serverOrder());
});
it("removes a deleted row from every loaded page at once and restores it when the server refuses", async () => {
const { hook, ids } = setup();
await waitFor(() => expect(ids()).toHaveLength(50));
await act(() => hook.result.current.more.mutateAsync());
await waitFor(() => expect(ids()).toHaveLength(100));
const target = ids()[80];
server.fail = true;
await act(async () => {
await hook.result.current.remove.mutateAsync({ formId: "form", submissionId: target }).catch(() => {});
});
await waitFor(() => expect(hook.result.current.list.isFetching).toBe(false));
expect(ids()).toContain(target);
server.fail = false;
let pending: Promise<void> | undefined;
act(() => {
pending = hook.result.current.remove.mutateAsync({ formId: "form", submissionId: target });
});
await waitFor(() => expect(ids()).not.toContain(target));
await act(() => pending);
await waitFor(() => expect(hook.result.current.list.isFetching).toBe(false));
expect(ids()).not.toContain(target);
expect(ids()).toHaveLength(99);
});
});
describe("compareSubmissions", () => {
it("orders by microseconds Date cannot see, then by id", () => {
const rows = [
sub("a", "2026-03-01T12:00:00.123456Z"),
sub("b", "2026-03-01T12:00:00.123457Z"),
sub("c", "2026-03-01T12:00:00.123456Z"),
sub("d", "2026-03-01T13:00:00.1234565+01:00"),
sub("e", "2026-03-01T12:00:00Z"),
];
expect([...rows].sort(compareSubmissions).map((r) => r.id)).toEqual(["b", "d", "c", "a", "e"]);
});
});
@@ -0,0 +1,90 @@
// The loaded submissions as one window, newest down to the oldest row loaded;
// a refresh re-reads that whole range so loaded rows never shift out of view.
import type { FormSubmissionsPage, FormSubmissionsPageParam } from "@/lib/api/client/app/forms";
import type { FormSubmission } from "@/lib/api/models/app/forms/Form";
export interface SubmissionsWindow {
rows: FormSubmission[];
next: FormSubmissionsPageParam | null;
hasMore: boolean;
}
export type FetchSubmissionsPage = (page: FormSubmissionsPageParam | undefined, limit: number) => Promise<FormSubmissionsPage>;
export const SUBMISSIONS_PAGE_SIZE = 50;
const MAX_PAGE_SIZE = 100;
// Pages a refresh may read before it settles for a shorter window.
const MAX_REFRESH_PAGES = 20;
const STAMP = /^(.+T\d{2}:\d{2}:\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})$/i;
// Seconds since the epoch and the nanoseconds past them; Date alone stops at
// milliseconds, and the server orders by microseconds.
function stamp(iso: string): [number, number] {
const m = STAMP.exec(iso);
if (!m) return [Math.floor(Date.parse(iso) / 1000), 0];
return [Date.parse(m[1] + m[3]) / 1000, Number((m[2] ?? "").padEnd(9, "0").slice(0, 9))];
}
/** The server's order, newest first: created_at DESC, then id DESC. Negative when a comes first. */
export function compareSubmissions(a: FormSubmission, b: FormSubmission): number {
const [as, an] = stamp(a.created_at);
const [bs, bn] = stamp(b.created_at);
if (as !== bs) return bs - as;
if (an !== bn) return bn - an;
return a.id === b.id ? 0 : a.id < b.id ? 1 : -1;
}
function sortedUnique(rows: FormSubmission[]): FormSubmission[] {
const byId = new Map<string, FormSubmission>();
for (const r of rows) if (!byId.has(r.id)) byId.set(r.id, r);
return [...byId.values()].sort(compareSubmissions);
}
function last<T>(xs: T[]): T | undefined {
return xs[xs.length - 1];
}
/** Re-reads from the newest row down to the oldest row of `previous`, or one page when nothing was loaded. */
export async function readWindow(fetchPage: FetchSubmissionsPage, previous?: SubmissionsWindow): Promise<SubmissionsWindow> {
const floor = previous && last(previous.rows);
let page = await fetchPage(undefined, SUBMISSIONS_PAGE_SIZE);
let rows = page.data;
for (let n = 1; previous && floor && page.has_more && page.next && n < MAX_REFRESH_PAGES; n++) {
const tail = last(page.data);
if (!tail || compareSubmissions(tail, floor) >= 0) break;
// Ask for exactly the loaded rows still unseen, so the window ends on the floor instead of past it.
const unseen = previous.rows.filter((r) => compareSubmissions(r, tail) > 0).length;
page = await fetchPage(page.next, Math.min(Math.max(unseen, 1), MAX_PAGE_SIZE));
rows = rows.concat(page.data);
}
return { rows: sortedUnique(rows), next: page.has_more ? page.next : null, hasMore: page.has_more && !!page.next };
}
/** Lays a refreshed window over the cache as it is now, keeping older rows a load-more appended meanwhile. */
export function mergeRefreshed(fresh: SubmissionsWindow, current: SubmissionsWindow | undefined): SubmissionsWindow {
const tail = last(fresh.rows);
if (!current || !fresh.hasMore || !tail) return fresh;
const older = current.rows.filter((r) => compareSubmissions(r, tail) > 0);
if (older.length === 0) return fresh;
return { rows: sortedUnique([...fresh.rows, ...older]), next: current.next, hasMore: current.hasMore };
}
/** Appends the page that continues the window. */
export function appendPage(window: SubmissionsWindow, page: FormSubmissionsPage): SubmissionsWindow {
const pageTail = last(page.data);
if (!pageTail) return { ...window, next: null, hasMore: false };
const rows = sortedUnique([...window.rows, ...page.data]);
if (last(rows)?.id !== pageTail.id) return { ...window, rows };
return { rows, next: page.has_more ? page.next : null, hasMore: page.has_more && !!page.next };
}
export function removeSubmissions(window: SubmissionsWindow, ids: ReadonlySet<string>): SubmissionsWindow {
return { ...window, rows: window.rows.filter((r) => !ids.has(r.id)) };
}
/** Puts a row back where the server order has it, as when a delete is refused. */
export function restoreSubmission(window: SubmissionsWindow, row: FormSubmission): SubmissionsWindow {
return { ...window, rows: sortedUnique([...window.rows, row]) };
}
+2 -1
View File
@@ -153,7 +153,8 @@ export interface FormSubmission {
source_url: string;
triage?: FormTriage;
triage_confidence?: number;
created_at: Date;
/** RFC 3339 with microseconds; the paging order, so never round it through Date. */
created_at: string;
contact_email?: string;
contact_name?: string;
campaign_name?: string;