Add pause until, store as unix second, to disable a user for a time

master
Bel LaPointe 2022-01-11 23:11:42 -05:00
parent 9840be93f6
commit 4ffc6bba8c
5 changed files with 59 additions and 2 deletions

View File

@ -13,6 +13,15 @@
"Matrix": "@belandbroc:matrix.org"
}
},
"broc": {
"States": [
"OH"
],
"IDs": {
"Matrix": "@belandbroc:matrix.org"
},
"PauseUntil": 5641960674
},
"caleb": {
"States": [
"OH"

View File

@ -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]
}
}

View File

@ -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()))

23
config/time.go Normal file
View File

@ -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
}

22
config/time_test.go Normal file
View File

@ -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())
}
}