1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

Merge branch 'notarock/62'

This commit is contained in:
Ferdinand Mütsch
2020-10-26 22:34:50 +01:00
13 changed files with 296 additions and 37 deletions

17
models/customrule.go Normal file
View File

@@ -0,0 +1,17 @@
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) >= 1
}

View File

@@ -1,6 +1,7 @@
package models
import (
"fmt"
"regexp"
"time"
)
@@ -27,7 +28,7 @@ func (h *Heartbeat) Valid() bool {
return h.User != nil && h.UserID != "" && h.Time != CustomTime(time.Time{})
}
func (h *Heartbeat) Augment(customLangs map[string]string) {
func (h *Heartbeat) AugmentWithConfigRules(customLangs map[string]string) {
if h.Language == "" {
if h.languageRegex == nil {
h.languageRegex = regexp.MustCompile(`^.+\.(.+)$`)
@@ -43,3 +44,14 @@ func (h *Heartbeat) Augment(customLangs map[string]string) {
h.Language, _ = customLangs[ending]
}
}
func (h *Heartbeat) AugmentWithUserRules(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
}
}
}