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:
@ -13,14 +13,16 @@ import (
|
||||
)
|
||||
|
||||
type HeartbeatHandler struct {
|
||||
config *config2.Config
|
||||
heartbeatSrvc *services.HeartbeatService
|
||||
config *config2.Config
|
||||
heartbeatSrvc *services.HeartbeatService
|
||||
customRuleSrvc *services.CustomRuleService
|
||||
}
|
||||
|
||||
func NewHeartbeatHandler(heartbeatService *services.HeartbeatService) *HeartbeatHandler {
|
||||
func NewHeartbeatHandler(heartbeatService *services.HeartbeatService, customRuleService *services.CustomRuleService) *HeartbeatHandler {
|
||||
return &HeartbeatHandler{
|
||||
config: config2.Get(),
|
||||
heartbeatSrvc: heartbeatService,
|
||||
config: config2.Get(),
|
||||
heartbeatSrvc: heartbeatService,
|
||||
customRuleSrvc: customRuleService,
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,13 +43,21 @@ func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
rules, err := h.customRuleSrvc.GetCustomRuleForUser(user.ID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
for _, hb := range heartbeats {
|
||||
hb.OperatingSystem = opSys
|
||||
hb.Editor = editor
|
||||
hb.Machine = machineName
|
||||
hb.User = user
|
||||
hb.UserID = user.ID
|
||||
hb.Augment(h.config.App.CustomLanguages)
|
||||
hb.AugmentWithConfigRules(h.config.App.CustomLanguages)
|
||||
hb.AugmentWithUserRules(rules)
|
||||
|
||||
if !hb.Valid() {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
@ -9,19 +9,22 @@ import (
|
||||
"github.com/muety/wakapi/utils"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type SettingsHandler struct {
|
||||
config *conf.Config
|
||||
userSrvc *services.UserService
|
||||
config *conf.Config
|
||||
userSrvc *services.UserService
|
||||
customRuleSrvc *services.CustomRuleService
|
||||
}
|
||||
|
||||
var credentialsDecoder = schema.NewDecoder()
|
||||
|
||||
func NewSettingsHandler(userService *services.UserService) *SettingsHandler {
|
||||
func NewSettingsHandler(userService *services.UserService, customRuleService *services.CustomRuleService) *SettingsHandler {
|
||||
return &SettingsHandler{
|
||||
config: conf.Get(),
|
||||
userSrvc: userService,
|
||||
config: conf.Get(),
|
||||
customRuleSrvc: customRuleService,
|
||||
userSrvc: userService,
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,14 +34,14 @@ func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
user := r.Context().Value(models.UserKey).(*models.User)
|
||||
rules, _ := h.customRuleSrvc.GetCustomRuleForUser(user.ID)
|
||||
data := map[string]interface{}{
|
||||
"User": user,
|
||||
"User": user,
|
||||
"Rules": rules,
|
||||
"Success": r.FormValue("success"),
|
||||
"Error": r.FormValue("error"),
|
||||
}
|
||||
|
||||
// TODO: when alerts are present, other data will not be passed to the template
|
||||
if handleAlerts(w, r, conf.SettingsTemplate) {
|
||||
return
|
||||
}
|
||||
templates[conf.SettingsTemplate].Execute(w, data)
|
||||
}
|
||||
|
||||
@ -105,6 +108,56 @@ func (h *SettingsHandler) PostCredentials(w http.ResponseWriter, r *http.Request
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) DeleteCustomRule(w http.ResponseWriter, r *http.Request) {
|
||||
if h.config.IsDev() {
|
||||
loadTemplates()
|
||||
}
|
||||
|
||||
user := r.Context().Value(models.UserKey).(*models.User)
|
||||
ruleId, err := strconv.Atoi(r.PostFormValue("ruleid"))
|
||||
if err != nil {
|
||||
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rule := &models.CustomRule{
|
||||
ID: uint(ruleId),
|
||||
UserID: user.ID,
|
||||
}
|
||||
h.customRuleSrvc.Delete(rule)
|
||||
msg := url.QueryEscape("Custom rule deleted successfully.")
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) PostCreateCustomRule(w http.ResponseWriter, r *http.Request) {
|
||||
if h.config.IsDev() {
|
||||
loadTemplates()
|
||||
}
|
||||
user := r.Context().Value(models.UserKey).(*models.User)
|
||||
extension := r.PostFormValue("extension")
|
||||
language := r.PostFormValue("language")
|
||||
|
||||
if extension[0] == '.' {
|
||||
extension = extension[1:]
|
||||
}
|
||||
|
||||
rule := &models.CustomRule{
|
||||
UserID: user.ID,
|
||||
Extension: extension,
|
||||
Language: language,
|
||||
}
|
||||
|
||||
if _, err := h.customRuleSrvc.Create(rule); err != nil {
|
||||
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
msg := url.QueryEscape("Custom rule saved successfully.")
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) PostResetApiKey(w http.ResponseWriter, r *http.Request) {
|
||||
if h.config.IsDev() {
|
||||
loadTemplates()
|
||||
|
Reference in New Issue
Block a user