diff options
Diffstat (limited to 'internal/vote/vote.go')
| -rw-r--r-- | internal/vote/vote.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/internal/vote/vote.go b/internal/vote/vote.go index 7407848..f65b21d 100644 --- a/internal/vote/vote.go +++ b/internal/vote/vote.go @@ -1,6 +1,7 @@ package vote import ( + "encoding/json" "log" "strings" "time" @@ -13,7 +14,7 @@ const Expiry = 20 * time.Second type Vote struct { FromID string IDs []string - ExpiresAt time.Time + ExpiresAt time.Time `json:"-"` } func New(conf config.Config, message string) Vote { @@ -34,10 +35,45 @@ func New(conf config.Config, message string) Vote { ids = append(ids, id) } - return Vote{fromID, ids, time.Now().Add(Expiry)} + return Vote{ + FromID: fromID, + IDs: ids, + } +} + +func NewFromJSON(bytes []byte) (v Vote, err error) { + if err = json.Unmarshal(bytes, &v); err != nil { + return + } + v.ExpiresAt = time.Now().Add(Expiry) + return +} + +func (v Vote) ToJSON() ([]byte, error) { + return json.Marshal(v) } func (v Vote) Expired() bool { now := time.Now() return now.After(v.ExpiresAt) || now.Equal(v.ExpiresAt) } + +func (v Vote) equals(v2 Vote) bool { + if v.FromID != v2.FromID { + return false + } + + if len(v.IDs) != len(v2.IDs) { + return false + } + + for i, id := range v.IDs { + if id != v2.IDs[i] { + return false + } + } + + // Not comparing ExpiresAt + + return true +} |
