spark-server/internal/module/user/entity/spark_id.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

49 lines
821 B
Go

package entity
import (
"regexp"
"strings"
)
const (
SparkIDMin = 6
SparkIDMax = 32
)
var sparkIDRe = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]{5,31}$`)
func ValidSparkID(id string) bool {
n := len(id)
return n >= SparkIDMin && n <= SparkIDMax && sparkIDRe.MatchString(id)
}
// DefaultSparkID is a unique, letter-prefixed handle derived from the Snowflake id.
func DefaultSparkID(userID int64) string {
s := "s" + strings.ToLower(strconv36(userID))
for len(s) < SparkIDMin {
s += "0"
}
if len(s) > SparkIDMax {
s = s[:SparkIDMax]
}
return s
}
func strconv36(n int64) string {
if n < 0 {
n = -n
}
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
if n == 0 {
return "0"
}
var buf [16]byte
i := len(buf)
for n > 0 {
i--
buf[i] = digits[n%36]
n /= 36
}
return string(buf[i:])
}