feat: approve button

This commit is contained in:
Vaala Cat
2023-08-28 15:57:58 +08:00
parent 46cf57764c
commit 5528766c13
15 changed files with 469 additions and 176 deletions

52
services/mc/auth.go Normal file
View File

@@ -0,0 +1,52 @@
package mc
import (
"sync"
"tg-mc/models"
"time"
)
type Auth interface {
IsAuthed(u models.User, expireMode bool) bool
Auth(u models.User)
Reject(u models.User)
}
type Authcator struct {
UserMap *sync.Map
}
var authcator *Authcator
func GetAuthcator() Auth {
if authcator == nil {
authcator = &Authcator{
UserMap: &sync.Map{},
}
}
return authcator
}
func (a *Authcator) IsAuthed(u models.User, expireMode bool) bool {
if u.MCName != "VaalaCat" {
return true
}
if approveTime, ok := a.UserMap.Load(u.MCName); ok {
if !expireMode {
return true
} else if time.Since(approveTime.(time.Time)) < 30*time.Second {
return true
} else {
return false
}
}
return false
}
func (a *Authcator) Auth(u models.User) {
a.UserMap.Store(u.MCName, time.Now())
}
func (a *Authcator) Reject(u models.User) {
a.UserMap.Delete(u.MCName)
}