1
0
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:
Roch D'Amour
2020-10-25 02:22:10 -04:00
parent 06b3fdd17c
commit fdf2289f8e
11 changed files with 240 additions and 39 deletions

18
models/customrule.go Normal file
View 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
}

View File

@ -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]
}
}