feat: add public account overview visibility

This commit is contained in:
2026-08-14 14:38:22 +08:00
parent bf37999833
commit 915d26d51d
14 changed files with 660 additions and 62 deletions
+44 -6
View File
@@ -95,13 +95,14 @@ func (s *Store) migrateAccounts() error {
email TEXT,
plan_type TEXT,
expected_kind TEXT NOT NULL DEFAULT 'any',
public_visible INTEGER NOT NULL DEFAULT 0,
connected INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)`); err != nil {
return err
}
var hasExpectedKind bool
var hasExpectedKind, hasPublicVisible bool
rows, qerr := tx.Query("PRAGMA table_info(accounts)")
if qerr != nil {
return qerr
@@ -112,6 +113,7 @@ func (s *Store) migrateAccounts() error {
var def any
_ = rows.Scan(&cid, &name, &typ, &notnull, &def, &pk)
hasExpectedKind = hasExpectedKind || name == "expected_kind"
hasPublicVisible = hasPublicVisible || name == "public_visible"
}
rows.Close()
if !hasExpectedKind {
@@ -119,6 +121,11 @@ func (s *Store) migrateAccounts() error {
return err
}
}
if !hasPublicVisible {
if _, err = tx.Exec("ALTER TABLE accounts ADD COLUMN public_visible INTEGER NOT NULL DEFAULT 0"); err != nil {
return err
}
}
var count int
if err = tx.QueryRow("SELECT COUNT(*) FROM accounts").Scan(&count); err != nil {
return err
@@ -181,6 +188,7 @@ type Account struct {
Email *string `json:"email"`
PlanType *string `json:"planType"`
ExpectedKind string `json:"expectedKind"`
PublicVisible bool `json:"publicVisible"`
ActualKind string `json:"actualKind"`
ValidationStatus string `json:"validationStatus"`
PossibleDuplicate bool `json:"possibleDuplicate"`
@@ -190,7 +198,7 @@ type Account struct {
}
func (s *Store) Accounts() ([]Account, error) {
rows, e := s.DB.Query("SELECT id,display_name,email,plan_type,expected_kind,connected,created_at,updated_at FROM accounts ORDER BY id")
rows, e := s.DB.Query("SELECT id,display_name,email,plan_type,expected_kind,public_visible,connected,created_at,updated_at FROM accounts ORDER BY id")
if e != nil {
return nil, e
}
@@ -198,7 +206,7 @@ func (s *Store) Accounts() ([]Account, error) {
out := []Account{}
for rows.Next() {
var a Account
if e = rows.Scan(&a.ID, &a.DisplayName, &a.Email, &a.PlanType, &a.ExpectedKind, &a.Connected, &a.CreatedAt, &a.UpdatedAt); e != nil {
if e = rows.Scan(&a.ID, &a.DisplayName, &a.Email, &a.PlanType, &a.ExpectedKind, &a.PublicVisible, &a.Connected, &a.CreatedAt, &a.UpdatedAt); e != nil {
return nil, e
}
a.ActualKind, a.ValidationStatus = AccountKind(a.PlanType), validationStatus(a.ExpectedKind, a.Connected, a.PlanType)
@@ -217,21 +225,51 @@ func (s *Store) Accounts() ([]Account, error) {
}
return out, rows.Err()
}
func (s *Store) Account(id int64) (Account, error) {
var a Account
err := s.DB.QueryRow("SELECT id,display_name,email,plan_type,expected_kind,public_visible,connected,created_at,updated_at FROM accounts WHERE id=?", id).
Scan(&a.ID, &a.DisplayName, &a.Email, &a.PlanType, &a.ExpectedKind, &a.PublicVisible, &a.Connected, &a.CreatedAt, &a.UpdatedAt)
if err != nil {
return Account{}, err
}
a.ActualKind, a.ValidationStatus = AccountKind(a.PlanType), validationStatus(a.ExpectedKind, a.Connected, a.PlanType)
return a, nil
}
func (s *Store) CreateAccount(name string, kinds ...string) (Account, error) {
return s.createAccount(name, false, kinds...)
}
func (s *Store) CreateAccountWithVisibility(name, expectedKind string, publicVisible bool) (Account, error) {
return s.createAccount(name, publicVisible, expectedKind)
}
func (s *Store) createAccount(name string, publicVisible bool, kinds ...string) (Account, error) {
expectedKind := "any"
if len(kinds) > 0 {
expectedKind = kinds[0]
}
now := time.Now().Unix()
r, e := s.DB.Exec("INSERT INTO accounts(display_name,expected_kind,created_at,updated_at) VALUES(?,?,?,?)", name, expectedKind, now, now)
r, e := s.DB.Exec("INSERT INTO accounts(display_name,expected_kind,public_visible,created_at,updated_at) VALUES(?,?,?,?,?)", name, expectedKind, publicVisible, now, now)
if e != nil {
return Account{}, e
}
id, _ := r.LastInsertId()
return Account{ID: id, DisplayName: name, ExpectedKind: expectedKind, ActualKind: "unknown", ValidationStatus: "pending", CreatedAt: now, UpdatedAt: now}, nil
return Account{ID: id, DisplayName: name, ExpectedKind: expectedKind, PublicVisible: publicVisible, ActualKind: "unknown", ValidationStatus: "pending", CreatedAt: now, UpdatedAt: now}, nil
}
func (s *Store) UpdateAccountSettings(id int64, name, expectedKind string) error {
r, e := s.DB.Exec("UPDATE accounts SET display_name=?,expected_kind=?,updated_at=? WHERE id=?", name, expectedKind, time.Now().Unix(), id)
return s.UpdateAccountSettingsWithVisibility(id, name, expectedKind, nil)
}
func (s *Store) UpdateAccountSettingsWithVisibility(id int64, name, expectedKind string, publicVisible *bool) error {
var r sql.Result
var e error
if publicVisible == nil {
r, e = s.DB.Exec("UPDATE accounts SET display_name=?,expected_kind=?,updated_at=? WHERE id=?", name, expectedKind, time.Now().Unix(), id)
} else {
r, e = s.DB.Exec("UPDATE accounts SET display_name=?,expected_kind=?,public_visible=?,updated_at=? WHERE id=?", name, expectedKind, *publicVisible, time.Now().Unix(), id)
}
if e != nil {
return e
}