From 98f4c12bbc1c5301f0922c1cfb5cd62f4b6fb1eb Mon Sep 17 00:00:00 2001 From: Tomilla <5007859+Tomilla@users.noreply.github.com> Date: Sat, 27 Jun 2026 11:25:21 +0800 Subject: [PATCH] refactor(api-token): share timestamp threshold --- internal/database/db.go | 3 +-- internal/database/model/model.go | 4 ++++ internal/web/service/panel/api_token.go | 7 +------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/database/db.go b/internal/database/db.go index 13eab8182..ef21409e2 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -1092,9 +1092,8 @@ func InitDB(dbPath string) error { // autoCreateTime:milli. The threshold separates modern Unix milliseconds from // Unix seconds and makes this safe to run on every startup. func normalizeApiTokenCreatedAtSeconds() error { - const unixMillisecondsThreshold int64 = 100_000_000_000 return db.Model(&model.ApiToken{}). - Where("created_at >= ?", unixMillisecondsThreshold). + Where("created_at >= ?", model.ApiTokenUnixMillisecondsThreshold). UpdateColumn("created_at", gorm.Expr("created_at / ?", 1000)).Error } diff --git a/internal/database/model/model.go b/internal/database/model/model.go index a6b61b18e..a39d3b58a 100644 --- a/internal/database/model/model.go +++ b/internal/database/model/model.go @@ -149,6 +149,10 @@ type HistoryOfSeeders struct { SeederName string `json:"seederName"` } +// ApiTokenUnixMillisecondsThreshold separates legacy millisecond timestamps +// from the seconds-based API token timestamp contract. +const ApiTokenUnixMillisecondsThreshold int64 = 100_000_000_000 + type ApiToken struct { Id int `json:"id" gorm:"primaryKey;autoIncrement"` Name string `json:"name" gorm:"uniqueIndex;not null"` diff --git a/internal/web/service/panel/api_token.go b/internal/web/service/panel/api_token.go index f74c7251b..6360be522 100644 --- a/internal/web/service/panel/api_token.go +++ b/internal/web/service/panel/api_token.go @@ -16,11 +16,6 @@ type ApiTokenService struct{} const apiTokenLength = 48 -// API token timestamps predate the model's temporary switch to milliseconds. -// Keep the API contract in Unix seconds while accepting rows written in either -// unit during that period. -const unixMillisecondsThreshold int64 = 100_000_000_000 - type ApiTokenView struct { Id int `json:"id" example:"2"` Name string `json:"name" example:"central-panel-a"` @@ -30,7 +25,7 @@ type ApiTokenView struct { } func apiTokenCreatedAtSeconds(createdAt int64) int64 { - if createdAt >= unixMillisecondsThreshold { + if createdAt >= model.ApiTokenUnixMillisecondsThreshold { return createdAt / 1000 } return createdAt