From 9840be93f6626b474d6d0e355b29f6dcd8e8ff7a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 11 Jan 2022 23:03:50 -0500 Subject: [PATCH] add pauseuntil field to pause clients individually --- broker/job.go | 2 +- config/config.go | 30 ++++++++++++++++++++++-------- message/matrix.go | 6 +++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/broker/job.go b/broker/job.go index fe929e2..7e050cc 100644 --- a/broker/job.go +++ b/broker/job.go @@ -53,7 +53,7 @@ func (j Job) FormatMultilineText() string { ) } out := "" - clients := config.Get().Clients + clients := config.Clients() for k := range clients { log.Printf("job multiline: %+v contains %s then use %v", clients[k].States, j.Pickup.State, k) if strings.Contains(fmt.Sprint(clients[k].States), j.Pickup.State) { diff --git a/config/config.go b/config/config.go index 4571203..bfe3591 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "local/storage" "os" "sync" + "time" ) type Config struct { @@ -14,12 +15,7 @@ type Config struct { OK Duration Error Duration } - Clients map[string]struct { - States []State - IDs struct { - Matrix string - } - } + Clients map[string]Client Storage []string Message struct { Matrix struct { @@ -46,6 +42,14 @@ type Config struct { db storage.DB } +type Client struct { + States []State + IDs struct { + Matrix string + } + PauseUntil time.Time +} + var live Config func configPath() string { @@ -56,10 +60,20 @@ func configPath() string { return p } +func Clients() map[string]Client { + clients := Get().Clients + result := map[string]Client{} + for k := range clients { + if clients[k].PauseUntil.IsZero() || time.Now().After(clients[k].PauseUntil) { + result[k] = clients[k] + } + } + return result +} + func AllStates() []State { - c := Get() statem := map[State]struct{}{} - for _, v := range c.Clients { + for _, v := range Clients() { for _, state := range v.States { statem[state] = struct{}{} } diff --git a/message/matrix.go b/message/matrix.go index b618b00..70f7ab2 100644 --- a/message/matrix.go +++ b/message/matrix.go @@ -34,12 +34,12 @@ func (m Matrix) Receive() ([]Message, error) { if m.mock { log.Printf("matrix.Receive()") messages := make([]Message, 0) - for k := range config.Get().Clients { + for k := range config.Clients() { messages = append(messages, Message{Sender: k, Content: "OH"}) } return messages, nil } - clients := config.Get().Clients + clients := config.Clients() matrixIDs := map[string]struct{}{} for k := range clients { matrixIDs[clients[k].IDs.Matrix] = struct{}{} @@ -69,7 +69,7 @@ func (m Matrix) Receive() ([]Message, error) { return nil, err } for i := range messages { - for k, v := range config.Get().Clients { + for k, v := range config.Clients() { if v.IDs.Matrix == messages[i].Sender { messages[i].Sender = k }