mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
MVP for custom rules support
This commit is contained in:
18
models/customrule.go
Normal file
18
models/customrule.go
Normal file
@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
|
||||
type CustomRule struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
User *User `json:"-" gorm:"not null"`
|
||||
UserID string `json:"-" gorm:"not null; index:idx_customrule_user"`
|
||||
Extension string `json:"extension"`
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
func validateLanguage(language string) bool {
|
||||
return len(language) >= 1
|
||||
}
|
||||
|
||||
func validateExtension(extension string) bool {
|
||||
return len(extension) >= 2
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
@ -27,19 +28,13 @@ func (h *Heartbeat) Valid() bool {
|
||||
return h.User != nil && h.UserID != "" && h.Time != CustomTime(time.Time{})
|
||||
}
|
||||
|
||||
func (h *Heartbeat) Augment(customLangs map[string]string) {
|
||||
if h.Language == "" {
|
||||
if h.languageRegex == nil {
|
||||
h.languageRegex = regexp.MustCompile(`^.+\.(.+)$`)
|
||||
}
|
||||
groups := h.languageRegex.FindAllStringSubmatch(h.Entity, -1)
|
||||
if len(groups) == 0 || len(groups[0]) != 2 {
|
||||
func (h *Heartbeat) Augment(customRules []*CustomRule) {
|
||||
for _, lang := range customRules {
|
||||
reg := fmt.Sprintf(".*%s$", lang.Extension)
|
||||
match, err := regexp.MatchString(reg, h.Entity)
|
||||
if match && err == nil {
|
||||
h.Language = lang.Language
|
||||
return
|
||||
}
|
||||
ending := groups[0][1]
|
||||
if _, ok := customLangs[ending]; !ok {
|
||||
return
|
||||
}
|
||||
h.Language, _ = customLangs[ending]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user