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

Keep the old configuration rules behaviour

This commit is contained in:
Roch D'Amour 2020-10-25 22:19:56 -04:00
parent 395d039d41
commit 178c417757
2 changed files with 20 additions and 2 deletions

View File

@ -28,7 +28,24 @@ func (h *Heartbeat) Valid() bool {
return h.User != nil && h.UserID != "" && h.Time != CustomTime(time.Time{}) return h.User != nil && h.UserID != "" && h.Time != CustomTime(time.Time{})
} }
func (h *Heartbeat) Augment(customRules []*CustomRule) { func (h *Heartbeat) AugmentWithConfigRules(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 {
return
}
ending := groups[0][1]
if _, ok := customLangs[ending]; !ok {
return
}
h.Language, _ = customLangs[ending]
}
}
func (h *Heartbeat) AugmentWithUserRules(customRules []*CustomRule) {
for _, lang := range customRules { for _, lang := range customRules {
reg := fmt.Sprintf(".*%s$", lang.Extension) reg := fmt.Sprintf(".*%s$", lang.Extension)
match, err := regexp.MatchString(reg, h.Entity) match, err := regexp.MatchString(reg, h.Entity)

View File

@ -56,7 +56,8 @@ func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
hb.Machine = machineName hb.Machine = machineName
hb.User = user hb.User = user
hb.UserID = user.ID hb.UserID = user.ID
hb.Augment(rules) hb.AugmentWithConfigRules(h.config.App.CustomLanguages)
hb.AugmentWithUserRules(rules)
if !hb.Valid() { if !hb.Valid() {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)