fix(backend): don't use redis GETDEL

This commit is contained in:
Sam 2023-04-20 01:30:33 +02:00
parent 661f0254fd
commit 86a1841f4f
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
2 changed files with 6 additions and 25 deletions

View File

@ -137,30 +137,6 @@ func (db *DB) GetJSON(ctx context.Context, key string, v any) error {
return nil return nil
} }
// GetDelJSON gets the given key as a JSON object and deletes it.
func (db *DB) GetDelJSON(ctx context.Context, key string, v any) error {
var b []byte
err := db.Redis.Do(ctx, radix.Cmd(&b, "GETDEL", key))
if err != nil {
return errors.Wrap(err, "reading from Redis")
}
if b == nil {
return nil
}
if v == nil {
return fmt.Errorf("nil pointer passed into GetDelJSON")
}
err = json.Unmarshal(b, v)
if err != nil {
return errors.Wrap(err, "unmarshaling json")
}
return nil
}
// NotNull is a little helper that returns an *empty slice* when the slice's length is 0. // NotNull is a little helper that returns an *empty slice* when the slice's length is 0.
// This is to prevent nil slices from being marshaled as JSON null // This is to prevent nil slices from being marshaled as JSON null
func NotNull[T any](slice []T) []T { func NotNull[T any](slice []T) []T {

View File

@ -67,7 +67,7 @@ func (s *Server) saveUndeleteToken(ctx context.Context, userID xid.ID, token str
func (s *Server) getUndeleteToken(ctx context.Context, token string) (userID xid.ID, err error) { func (s *Server) getUndeleteToken(ctx context.Context, token string) (userID xid.ID, err error) {
var idString string var idString string
err = s.DB.Redis.Do(ctx, radix.Cmd(&idString, "GETDEL", "undelete:"+token)) err = s.DB.Redis.Do(ctx, radix.Cmd(&idString, "GET", "undelete:"+token))
if err != nil { if err != nil {
return userID, errors.Wrap(err, "getting undelete key") return userID, errors.Wrap(err, "getting undelete key")
} }
@ -76,6 +76,11 @@ func (s *Server) getUndeleteToken(ctx context.Context, token string) (userID xid
if err != nil { if err != nil {
return userID, errors.Wrap(err, "parsing ID") return userID, errors.Wrap(err, "parsing ID")
} }
err = s.DB.Redis.Do(ctx, radix.Cmd(nil, "DEL", "undelete:"+token))
if err != nil {
return userID, errors.Wrap(err, "deleting undelete key")
}
return userID, nil return userID, nil
} }