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>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
const (
|
|
emailCodeKeyPrefix = "auth:email-code:"
|
|
emailCodeCDPrefix = "auth:email-cd:"
|
|
)
|
|
|
|
type RedisCodes struct {
|
|
rdb *redis.Client
|
|
}
|
|
|
|
func NewRedisCodes(rdb *redis.Client) *RedisCodes {
|
|
return &RedisCodes{rdb: rdb}
|
|
}
|
|
|
|
func (r *RedisCodes) Put(ctx context.Context, email, code string, ttlSeconds int) error {
|
|
if r == nil || r.rdb == nil {
|
|
return errs.ErrAvailable
|
|
}
|
|
if err := r.rdb.Set(ctx, emailCodeKeyPrefix+email, code, time.Duration(ttlSeconds)*time.Second).Err(); err != nil {
|
|
return errs.ErrAvailable.WithError("email-code", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *RedisCodes) Get(ctx context.Context, email string) (string, error) {
|
|
if r == nil || r.rdb == nil {
|
|
return "", errs.ErrAvailable
|
|
}
|
|
v, err := r.rdb.Get(ctx, emailCodeKeyPrefix+email).Result()
|
|
if err == redis.Nil {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", errs.ErrAvailable.WithError("email-code", err)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func (r *RedisCodes) Del(ctx context.Context, email string) error {
|
|
if r == nil || r.rdb == nil {
|
|
return errs.ErrAvailable
|
|
}
|
|
if err := r.rdb.Del(ctx, emailCodeKeyPrefix+email).Err(); err != nil {
|
|
return errs.ErrAvailable.WithError("email-code", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *RedisCodes) SetCooldown(ctx context.Context, email string, ttlSeconds int) (bool, error) {
|
|
if r == nil || r.rdb == nil {
|
|
return false, errs.ErrAvailable
|
|
}
|
|
ok, err := r.rdb.SetNX(ctx, emailCodeCDPrefix+email, "1", time.Duration(ttlSeconds)*time.Second).Result()
|
|
if err != nil {
|
|
return false, errs.ErrAvailable.WithError("email-code", err)
|
|
}
|
|
return ok, nil
|
|
}
|