Commit Graph
2 Commits
  • fix: refresh stale client_traffics row when an inbound-deleted client's email is reused (#6003)
    * fix: refresh stale client_traffics row when an inbound-deleted client's email is reused
    
    AddClientStat's OnConflict was DoNothing on email, so once an inbound is
    deleted (DelInbound only removes the client_inbounds link, matching
    ClientService.Detach's intentional Detach-then-later-Attach behavior) the
    orphaned client_traffics row for that email survives untouched. Re-creating
    a client under the same email on a new inbound silently kept the old
    enable/expiry_time/reset/total/inbound_id instead of adopting the new
    client's config.
    
    Switch the conflict path to DoUpdates on inbound_id/total/expiry_time/
    enable/reset. up/down stay excluded on purpose: every call for an
    already-attached identity carries the same config values (one call per
    inbound), so the refresh is a no-op for that legitimate multi-inbound
    share, while zeroing usage counters on each additional attach would erase
    real traffic.
    
    Fixes #5958
    
    * fix: don't let AddClientStat clobber import's forced-enabled ClientStats rows
    
    github-actions[bot] review on #6003 found that AddInbound writes client_traffics
    twice for the same import payload: first inserting each ClientStats row
    (DoNothing, with Enable forced true by controller.importInbound), then calling
    AddClientStat once per Settings-derived client. With AddClientStat's OnConflict
    now DoUpdates, that second call was unconditionally overwriting enable (and
    total/expiry_time/reset/inbound_id) with the Settings.clients[].enable value —
    which still holds whatever the client had at export time, silently undoing the
    controller's "always import as enabled" behavior for any client disabled at
    export.
    
    Fix: track which emails were already seeded by the ClientStats loop and skip
    the AddClientStat call for those emails, leaving the import path's forced
    values as authoritative. Plain (non-import) creates are unaffected since
    ClientStats is empty there, so every client still goes through AddClientStat's
    refresh as before.
    
    Also updated a stale comment in addClientTraffic that still described
    AddClientStat as DoNothing.
    
    Added TestAddInbound_ImportForcedEnableSurvivesDisabledSettingsClient, which
    reproduces the exact regression (verified it fails without this fix) and
    passes with it.
  • fix(inbound): avoid UNIQUE email constraint when importing inbounds that share clients
    Importing a second inbound whose clients overlap an already-imported inbound
    failed with "UNIQUE constraint failed: client_traffics.email". The import path
    carries exported ClientStats, and tx.Save(inbound) cascaded that has-many
    association as INSERTs whose ON CONFLICT targets only the primary key, so a
    shared email (already owning a row from the first import) tripped the global
    unique constraint.
    
    Omit the ClientStats association on save and insert the carried stats ourselves
    with the same OnConflict{email, DoNothing} guard AddClientStat already uses:
    new clients keep their imported counters, shared emails reuse the existing row.
    Then run an idempotent AddClientStat pass over all clients so any client present
    in settings but missing from the stats payload still gets a traffic row (else it
    would escape quota/expiry accounting), and propagate insert errors so the tx
    rolls back instead of committing a partial state.