package repo import ( "context" "database/sql" "errors" "github.com/agentmail/gateway/internal/db" "github.com/agentmail/gateway/internal/models" ) // 用户外观(主题 + 壁纸)的读写。 // // 为什么放服务端:原先主题与壁纸只存在浏览器 localStorage 里,换设备/换浏览器就没了, // 而且**多账号共用一份**(键是全局常量)—— 同一台机器换账号背景不跟着走。 // 语义是**账号级**(跟账号走,不跟设备走)。 // GetAppearance 读某个用户的外观。没有记录时返回**默认值 + false**(不是错误): // 「从没设置过」是正常状态,调用方不该为此处理 404。 func GetAppearance(ctx context.Context, userID string) (models.Appearance, bool, error) { a := models.DefaultAppearance() var updated sql.NullString err := db.DB.QueryRowContext(ctx, ` SELECT theme, bg_kind, bg_preset_id, bg_dim, bg_blur, image_sha256, image_type, image_bytes, CAST(updated_at AS TEXT) FROM user_appearance WHERE user_id = $1`, userID). Scan(&a.Theme, &a.BgKind, &a.BgPresetID, &a.BgDim, &a.BgBlur, &a.ImageSHA256, &a.ImageType, &a.ImageBytes, &updated) if errors.Is(err, sql.ErrNoRows) { return models.DefaultAppearance(), false, nil } if err != nil { return models.Appearance{}, false, err } a.UpdatedAt = updated.String return a, true, nil } // UpsertAppearance 写入主题与背景档(不含图片本身,图片见 SetAppearanceImage)。 func UpsertAppearance(ctx context.Context, userID string, a models.Appearance) error { _, err := db.DB.ExecContext(ctx, ` INSERT INTO user_appearance (user_id, theme, bg_kind, bg_preset_id, bg_dim, bg_blur, updated_at) VALUES ($1, $2, $3, $4, $5, $6, NOW()) ON CONFLICT (user_id) DO UPDATE SET theme = $2, bg_kind = $3, bg_preset_id = $4, bg_dim = $5, bg_blur = $6, updated_at = NOW()`, userID, a.Theme, a.BgKind, a.BgPresetID, a.BgDim, a.BgBlur) return err } // SetAppearanceImage 记下这张壁纸(文件已落 blob 存储)。 func SetAppearanceImage(ctx context.Context, userID, sha256, contentType string, sizeBytes int64) error { _, err := db.DB.ExecContext(ctx, ` INSERT INTO user_appearance (user_id, image_sha256, image_type, image_bytes, updated_at) VALUES ($1, $2, $3, $4, NOW()) ON CONFLICT (user_id) DO UPDATE SET image_sha256 = $2, image_type = $3, image_bytes = $4, updated_at = NOW()`, userID, sha256, contentType, sizeBytes) return err } // ClearAppearanceImage 清掉壁纸记录。 // // **不删 blob 文件**:内容寻址意味着同一张图可能被别的记录引用,而且删除是不可逆的 // —— 交给 SweepUnreferencedBlobs 在确认无人引用后再收(它会读这张表)。 func ClearAppearanceImage(ctx context.Context, userID string) error { _, err := db.DB.ExecContext(ctx, ` UPDATE user_appearance SET image_sha256 = '', image_type = '', image_bytes = 0, updated_at = NOW() WHERE user_id = $1`, userID) return err }