From f2f1a5606007fa55690b2adb4072fdf32e7e3cd6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 May 2020 15:07:30 +0200 Subject: [PATCH] Make default ack action if notification has none --- api/enriched-response.go | 2 +- notifications/notification.go | 29 ++++++++++++----------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/api/enriched-response.go b/api/enriched-response.go index 6aa5685..f1e34c2 100644 --- a/api/enriched-response.go +++ b/api/enriched-response.go @@ -54,7 +54,7 @@ func (lrw *LoggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) return nil, nil, errors.New("response does not implement http.Hijacker") } -// RequestLogger is a logging middleware +// RequestLogger is a logging middleware. func RequestLogger(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Tracef("api request: %s ___ %s", r.RemoteAddr, r.RequestURI) diff --git a/notifications/notification.go b/notifications/notification.go index 9bb56ae..08778a4 100644 --- a/notifications/notification.go +++ b/notifications/notification.go @@ -123,6 +123,18 @@ func (n *Notification) Save() *Notification { if n.GUID == "" { n.GUID = uuid.NewV4().String() } + + // make ack notification if there are no defined actions + if len(n.AvailableActions) == 0 { + n.AvailableActions = []*Action{ + { + ID: "ack", + Text: "OK", + }, + } + n.actionFunction = noOpAction + } + // check key if n.DatabaseKey() == "" { n.SetKey(fmt.Sprintf("notifications:all/%s", n.ID)) @@ -175,23 +187,6 @@ func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification { return n } -// MakeAck sets a default "OK" action and a no-op action function. -func (n *Notification) MakeAck() *Notification { - n.lock.Lock() - defer n.lock.Unlock() - - n.AvailableActions = []*Action{ - { - ID: "ack", - Text: "OK", - }, - } - n.Type = Info - n.actionFunction = noOpAction - - return n -} - // Response waits for the user to respond to the notification and returns the selected action. func (n *Notification) Response() <-chan string { n.lock.Lock()