From 4ffc6bba8c02d2a8faec1842565d8687155fe9d5 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 11 Jan 2022 23:11:42 -0500 Subject: [PATCH] Add pause until, store as unix second, to disable a user for a time --- config.json | 9 +++++++++ config/config.go | 4 ++-- config/duration.go | 3 +++ config/time.go | 23 +++++++++++++++++++++++ config/time_test.go | 22 ++++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 config/time.go create mode 100644 config/time_test.go diff --git a/config.json b/config.json index 443cc21..1d984d5 100644 --- a/config.json +++ b/config.json @@ -13,6 +13,15 @@ "Matrix": "@belandbroc:matrix.org" } }, + "broc": { + "States": [ + "OH" + ], + "IDs": { + "Matrix": "@belandbroc:matrix.org" + }, + "PauseUntil": 5641960674 + }, "caleb": { "States": [ "OH" diff --git a/config/config.go b/config/config.go index bfe3591..3680e06 100644 --- a/config/config.go +++ b/config/config.go @@ -47,7 +47,7 @@ type Client struct { IDs struct { Matrix string } - PauseUntil time.Time + PauseUntil Time } var live Config @@ -64,7 +64,7 @@ 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) { + if clients[k].PauseUntil.Get().IsZero() || time.Now().After(clients[k].PauseUntil.Get()) { result[k] = clients[k] } } diff --git a/config/duration.go b/config/duration.go index d290c1a..5555b31 100644 --- a/config/duration.go +++ b/config/duration.go @@ -14,6 +14,9 @@ type Duration struct { } func (d Duration) Get() time.Duration { + if d.least == 0 { + d.least = time.Second + } jitter := d.most - d.least if jitter >= time.Second { jitter = time.Second * time.Duration(rand.Int()%int(jitter.Seconds())) diff --git a/config/time.go b/config/time.go new file mode 100644 index 0000000..aefffd3 --- /dev/null +++ b/config/time.go @@ -0,0 +1,23 @@ +package config + +import ( + "encoding/json" + "time" +) + +type Time time.Time + +func (t Time) Get() time.Time { + return time.Time(t) +} + +func (t Time) MarshalJSON() ([]byte, error) { + return json.Marshal(t.Get().Unix()) +} + +func (t *Time) UnmarshalJSON(b []byte) error { + var d int64 + err := json.Unmarshal(b, &d) + *t = Time(time.Unix(d, 0)) + return err +} diff --git a/config/time_test.go b/config/time_test.go new file mode 100644 index 0000000..10e7a84 --- /dev/null +++ b/config/time_test.go @@ -0,0 +1,22 @@ +package config + +import ( + "encoding/json" + "testing" + "time" +) + +func TestTimeMarshal(t *testing.T) { + now := time.Now() + nowt := Time(now) + var other Time + if b, err := json.Marshal(nowt); err != nil { + t.Fatal(err) + } else if len(b) < 5 { + t.Fatal(string(b)) + } else if err := json.Unmarshal(b, &other); err != nil { + t.Fatal(err) + } else if now.Unix() != other.Unix() { + t.Fatal(other.Unix(), now.Unix()) + } +}