Make simple actions parsing work
This commit is contained in:
parent
55869f551e
commit
5a9b2122c2
7 changed files with 160 additions and 21 deletions
|
@ -1,6 +1,10 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"heckel.io/ntfy/util"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
@ -40,3 +44,73 @@ func readQueryParam(r *http.Request, names ...string) string {
|
|||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func parseActions(s string) (actions []*action, err error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if strings.HasPrefix(s, "[") {
|
||||
actions, err = parseActionsFromJSON(s)
|
||||
} else {
|
||||
actions, err = parseActionsFromSimple(s)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range actions {
|
||||
actions[i].ID = util.RandomString(actionIDLength)
|
||||
if !util.InStringList([]string{"view", "broadcast", "http"}, actions[i].Action) {
|
||||
return nil, fmt.Errorf("cannot parse actions: action '%s' unknown", actions[i].Action)
|
||||
} else if actions[i].Label == "" {
|
||||
return nil, fmt.Errorf("cannot parse actions: label must be set")
|
||||
}
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func parseActionsFromJSON(s string) ([]*action, error) {
|
||||
actions := make([]*action, 0)
|
||||
if err := json.Unmarshal([]byte(s), &actions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func parseActionsFromSimple(s string) ([]*action, error) {
|
||||
actions := make([]*action, 0)
|
||||
rawActions := util.SplitNoEmpty(s, ";")
|
||||
for _, rawAction := range rawActions {
|
||||
newAction := &action{}
|
||||
parts := util.SplitNoEmpty(rawAction, ",")
|
||||
if len(parts) < 3 {
|
||||
return nil, fmt.Errorf("cannot parse action: action requires at least keys 'action', 'label' and one parameter: %s", rawAction)
|
||||
}
|
||||
for i, part := range parts {
|
||||
key, value := util.SplitKV(part, "=")
|
||||
if key == "" && i == 0 {
|
||||
newAction.Action = value
|
||||
} else if key == "" && i == 1 {
|
||||
newAction.Label = value
|
||||
} else if key == "" && i == 2 {
|
||||
newAction.URL = value // This works, because both "http" and "view" need a URL
|
||||
} else if key != "" {
|
||||
switch strings.ToLower(key) {
|
||||
case "action":
|
||||
newAction.Action = value
|
||||
case "label":
|
||||
newAction.Label = value
|
||||
case "url":
|
||||
newAction.URL = value
|
||||
case "method":
|
||||
newAction.Method = value
|
||||
case "body":
|
||||
newAction.Body = value
|
||||
default:
|
||||
return nil, errors.Errorf("cannot parse action: key '%s' not supported, please use JSON format instead", part)
|
||||
}
|
||||
} else {
|
||||
return nil, errors.Errorf("cannot parse action: unknown phrase '%s'", part)
|
||||
}
|
||||
}
|
||||
actions = append(actions, newAction)
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue