Add/edit notifications

This commit is contained in:
Magnus Åhall 2024-06-28 15:28:52 +02:00
parent 1ede36b8aa
commit c7ad2aa1b6
19 changed files with 531 additions and 11 deletions

View file

@ -11,6 +11,9 @@ import (
"io"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
)
type NTFY struct {
@ -18,6 +21,13 @@ type NTFY struct {
Prio int
AcknowledgeURL string
logger *slog.Logger
exists bool
updated Service
}
func init() {
allServices = append(allServices, &NTFY{})
}
func NewNTFY(config []byte, prio int, ackURL string) (instance *NTFY, err error) {
@ -44,6 +54,22 @@ func (ntfy *NTFY) GetPrio() int {
return ntfy.Prio
}
func (ntfy *NTFY) SetPrio(prio int) {
ntfy.Prio = prio
}
func (ntfy *NTFY) SetExists(exists bool) {
ntfy.exists = exists
}
func (ntfy NTFY) Exists() bool {
return ntfy.exists
}
func (ntfy *NTFY) String() string {
return ntfy.URL
}
func (ntfy NTFY) Send(problemID int, msg []byte) (err error) {
var req *http.Request
var res *http.Response
@ -81,3 +107,40 @@ func (ntfy NTFY) Send(problemID int, msg []byte) (err error) {
return
}
func (ntfy *NTFY) Update(values url.Values) (err error) {
updated := NTFY{}
ntfy.updated = &updated
updated.Prio, err = strconv.Atoi(values.Get("prio"))
if err != nil {
return werr.Wrap(err)
}
givenURL := values.Get("url")
if strings.TrimSpace(givenURL) == "" {
return werr.New("URL cannot be empty")
}
updated.URL = strings.TrimSpace(givenURL)
return
}
func (ntfy *NTFY) Updated() Service {
return ntfy.updated
}
func (ntfy *NTFY) Commit() {
updatedNTFY := ntfy.updated.(*NTFY)
ntfy.Prio = updatedNTFY.Prio
ntfy.URL = updatedNTFY.URL
}
func (ntfy NTFY) JSON() []byte {
data := struct {
URL string `json:"url"`
}{
ntfy.URL,
}
j, _ := json.Marshal(data)
return j
}