feat: sync public links and phone change updates
This commit is contained in:
parent
41c7f1d018
commit
da04c0fa6a
53 changed files with 3029 additions and 111 deletions
|
|
@ -262,17 +262,28 @@ func (s *AuthorizationStore) DeleteByUserExcept(_ context.Context, userID int64,
|
|||
|
||||
// CodeStore 是 store.CodeStore 的内存实现(带 TTL)。
|
||||
type CodeStore struct {
|
||||
mu sync.Mutex
|
||||
m map[string]codeEntry
|
||||
mu sync.Mutex
|
||||
m map[string]codeEntry
|
||||
scopes map[store.PhoneCodeScope]string
|
||||
}
|
||||
|
||||
// NewCodeStore 创建内存 CodeStore。
|
||||
func NewCodeStore() *CodeStore {
|
||||
return &CodeStore{m: make(map[string]codeEntry)}
|
||||
return &CodeStore{
|
||||
m: make(map[string]codeEntry),
|
||||
scopes: make(map[store.PhoneCodeScope]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CodeStore) Set(_ context.Context, hash string, code store.PhoneCode, ttl time.Duration) error {
|
||||
s.mu.Lock()
|
||||
scope := code.Scope()
|
||||
if scope.Valid() {
|
||||
if oldHash, ok := s.scopes[scope]; ok && oldHash != hash {
|
||||
delete(s.m, oldHash)
|
||||
}
|
||||
s.scopes[scope] = hash
|
||||
}
|
||||
s.m[hash] = codeEntry{code: code, expires: time.Now().Add(ttl)}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
|
|
@ -283,6 +294,9 @@ func (s *CodeStore) Get(_ context.Context, hash string) (store.PhoneCode, bool,
|
|||
defer s.mu.Unlock()
|
||||
e, ok := s.m[hash]
|
||||
if !ok || time.Now().After(e.expires) {
|
||||
if ok {
|
||||
s.deleteCodeLocked(hash, e.code)
|
||||
}
|
||||
return store.PhoneCode{}, false, nil
|
||||
}
|
||||
return e.code, true, nil
|
||||
|
|
@ -293,6 +307,9 @@ func (s *CodeStore) Update(_ context.Context, hash string, code store.PhoneCode)
|
|||
defer s.mu.Unlock()
|
||||
e, ok := s.m[hash]
|
||||
if !ok || time.Now().After(e.expires) {
|
||||
if ok {
|
||||
s.deleteCodeLocked(hash, e.code)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
e.code = code
|
||||
|
|
@ -302,7 +319,41 @@ func (s *CodeStore) Update(_ context.Context, hash string, code store.PhoneCode)
|
|||
|
||||
func (s *CodeStore) Del(_ context.Context, hash string) error {
|
||||
s.mu.Lock()
|
||||
delete(s.m, hash)
|
||||
if e, ok := s.m[hash]; ok {
|
||||
s.deleteCodeLocked(hash, e.code)
|
||||
} else {
|
||||
delete(s.m, hash)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *CodeStore) ConsumeScoped(_ context.Context, hash string, scope store.PhoneCodeScope) (store.PhoneCode, bool, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !scope.Valid() || s.scopes[scope] != hash {
|
||||
return store.PhoneCode{}, false, nil
|
||||
}
|
||||
e, ok := s.m[hash]
|
||||
if !ok || time.Now().After(e.expires) {
|
||||
if ok {
|
||||
s.deleteCodeLocked(hash, e.code)
|
||||
} else {
|
||||
delete(s.scopes, scope)
|
||||
}
|
||||
return store.PhoneCode{}, false, nil
|
||||
}
|
||||
if e.code.Scope() != scope {
|
||||
return store.PhoneCode{}, false, nil
|
||||
}
|
||||
s.deleteCodeLocked(hash, e.code)
|
||||
return e.code, true, nil
|
||||
}
|
||||
|
||||
func (s *CodeStore) deleteCodeLocked(hash string, code store.PhoneCode) {
|
||||
delete(s.m, hash)
|
||||
scope := code.Scope()
|
||||
if scope.Valid() && s.scopes[scope] == hash {
|
||||
delete(s.scopes, scope)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue