From bfeb730df572ea78a4d2e7b881f64fdc650b4107 Mon Sep 17 00:00:00 2001 From: David Florness Date: Mon, 18 Jan 2021 22:02:39 -0500 Subject: [PATCH] Temporarily remove election saving I can't get this to work and it's not an essential feature --- cmd/tallyard/main.go | 28 ++++++++-------------------- election/election.go | 35 +++++++++++------------------------ election/map.go | 8 ++++++-- election/msg.go | 2 +- election/voter.go | 4 ---- matrix/data.go | 2 -- 6 files changed, 26 insertions(+), 53 deletions(-) diff --git a/cmd/tallyard/main.go b/cmd/tallyard/main.go index bc47a3f..b8fbbe1 100644 --- a/cmd/tallyard/main.go +++ b/cmd/tallyard/main.go @@ -53,12 +53,7 @@ func main() { panic(err) } - if data.Elections == nil { - data.Elections = election.NewElectionsMap() - } - data.Elections.Save = func() { - data.Save() - } + elections := election.NewElectionsMap() syncer := client.Syncer.(*mautrix.DefaultSyncer) syncer.OnEvent(client.Store.(*mautrix.InMemoryStore).UpdateState) @@ -68,8 +63,7 @@ func main() { log.Debug("redacted") return } - election.OnCreateElectionMessage(evt, data.Elections) - data.Save() + election.OnCreateElectionMessage(evt, elections) }) syncer.OnEventType(election.JoinElectionMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -77,8 +71,7 @@ func main() { log.Debug("redacted") return } - election.OnJoinElectionMessage(client, evt, data.Elections) - data.Save() + election.OnJoinElectionMessage(client, evt, elections) }) syncer.OnEventType(election.StartElectionMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -86,8 +79,7 @@ func main() { log.Debug("redacted") return } - election.OnStartElectionMessage(client, evt, data.Elections) - data.Save() + election.OnStartElectionMessage(client, evt, elections) }) syncer.OnEventType(election.EvalMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -95,8 +87,7 @@ func main() { log.Debug("redacted") return } - election.OnEvalMessage(client, evt, data.Elections) - data.Save() + election.OnEvalMessage(client, evt, elections) }) syncer.OnEventType(election.SumMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -104,8 +95,7 @@ func main() { log.Debug("redacted") return } - election.OnSumMessage(client, evt, data.Elections) - data.Save() + election.OnSumMessage(client, evt, elections) }) syncer.OnEventType(election.ResultMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -113,8 +103,7 @@ func main() { log.Debug("redacted") return } - election.OnResultMessage(client, evt, data.Elections) - data.Save() + election.OnResultMessage(client, evt, elections) }) go func() { @@ -129,11 +118,10 @@ func main() { } }() - el := ui.TUI(client, data.Elections) + el := ui.TUI(client, elections) el.Lock() el.LocalVoter.Poly = math.NewRandomPoly(uint(len(*el.FinalVoters)-1), 1024, *el.LocalVoter.Ballot) - el.Save() el.Unlock() // TODO we may not have all voters' info diff --git a/election/election.go b/election/election.go index 28191ff..00af9cc 100644 --- a/election/election.go +++ b/election/election.go @@ -1,7 +1,6 @@ package election import ( - "encoding/json" "sync" "maunium.net/go/mautrix/event" @@ -11,22 +10,21 @@ import ( type Election struct { sync.RWMutex - Candidates []Candidate `json:"candidates"` - CreateEventId id.EventID `json:"create_event_id"` - CreationTimestamp int64 `json:"creation_timestamp"` - Creator id.UserID `json:"creator"` - FinalVoters *[]id.EventID `json:"final_voters,omitempty"` - Joins map[id.EventID]*Voter `json:"joins"` - LocalVoter *LocalVoter `json:"local_voter,omitempty"` - RoomID id.RoomID `json:"room_id"` - Save func() `json:"-"` - StartEvt *event.Event `json:"start_evt,omitempty"` - Title string `json:"title"` + Candidates []Candidate `json:"candidates"` + CreateEventId id.EventID `json:"create_event_id"` + CreationTimestamp int64 `json:"creation_timestamp"` + Creator id.UserID `json:"creator"` + FinalVoters *[]id.EventID `json:"final_voters,omitempty"` + Joins map[id.EventID]*Voter `json:"joins"` + LocalVoter *LocalVoter `json:"local_voter,omitempty"` + RoomID id.RoomID `json:"room_id"` + StartEvt *event.Event `json:"start_evt,omitempty"` + Title string `json:"title"` } func NewElection(candidates []Candidate, createEventId id.EventID, creationTimestamp int64, creator id.UserID, roomID id.RoomID, - title string, save func()) *Election { + title string) *Election { return &Election{ Candidates: candidates, CreateEventId: createEventId, @@ -34,17 +32,6 @@ func NewElection(candidates []Candidate, createEventId id.EventID, Creator: creator, Joins: make(map[id.EventID]*Voter), RoomID: roomID, - Save: save, Title: title, } } - -func (el *Election) UnmarshalJSON(b []byte) error { - err := json.Unmarshal(b, &el) - if err != nil { - return err - } - // ensure these are the same pointer - el.Joins[el.LocalVoter.JoinEvt.ID] = el.LocalVoter.Voter - return nil -} diff --git a/election/map.go b/election/map.go index 2f63b52..9ca94c9 100644 --- a/election/map.go +++ b/election/map.go @@ -13,7 +13,6 @@ type ElectionsMap struct{ M map[id.EventID]*Election L []*Election // sorted list of elections by CreationTimestamp (newest to oldest) Joins map[id.EventID]*Voter - Save func() } func NewElectionsMap() *ElectionsMap { @@ -64,6 +63,12 @@ func (em *ElectionsMap) GetI(i int) (*Election) { return em.L[i] } +func (em *ElectionsMap) Set(createEventID id.EventID, el *Election) { + em.Lock() + defer em.Unlock() + em.set(createEventID, el) +} + func (em *ElectionsMap) SetIfNotExists(createEventID id.EventID, el *Election) { em.Lock() defer em.Unlock() @@ -72,7 +77,6 @@ func (em *ElectionsMap) SetIfNotExists(createEventID id.EventID, el *Election) { return } em.set(createEventID, el) - em.Save() } func (em *ElectionsMap) set(createEventID id.EventID, el *Election) { diff --git a/election/msg.go b/election/msg.go index 863f2fc..9d87d0b 100644 --- a/election/msg.go +++ b/election/msg.go @@ -95,7 +95,7 @@ func OnCreateElectionMessage(evt *event.Event, elections *ElectionsMap) { return } elections.SetIfNotExists(evt.ID, NewElection(content.Candidates, evt.ID, - evt.Timestamp, evt.Sender, evt.RoomID, content.Title, elections.Save)) + evt.Timestamp, evt.Sender, evt.RoomID, content.Title)) } func getElection(client *mautrix.Client, roomID id.RoomID, createEventId id.EventID, elections *ElectionsMap) *Election { diff --git a/election/voter.go b/election/voter.go index 964f3ff..93114f1 100644 --- a/election/voter.go +++ b/election/voter.go @@ -109,7 +109,6 @@ func (el *Election) JoinElection(client *mautrix.Client) error { PrivKey: *privKey, } el.Joins[resp.EventID] = el.LocalVoter.Voter - el.Save() return nil } @@ -152,7 +151,6 @@ func (el *Election) StartElection(client *mautrix.Client) error { // OnStartElectionMessage(client, startEvt, elections) el.StartEvt = startEvt el.FinalVoters = &voters - el.Save() return nil } @@ -205,7 +203,6 @@ func (el *Election) SendEvals(client *mautrix.Client) error { el.LocalVoter.Eval = nil return err } - el.Save() return nil } @@ -231,7 +228,6 @@ func (el *Election) SendSum(client *mautrix.Client) error { return err } el.LocalVoter.Sum = sum - el.Save() return nil } diff --git a/matrix/data.go b/matrix/data.go index 8ae4979..15d6585 100644 --- a/matrix/data.go +++ b/matrix/data.go @@ -10,13 +10,11 @@ import ( "github.com/kyoh86/xdg" "maunium.net/go/mautrix" "maunium.net/go/mautrix/id" - "tallyard.xyz/election" ) type Data struct { AccessToken string `json:"access_token"` DeviceID id.DeviceID `json:"device_id"` - Elections *election.ElectionsMap `json:"elections,omitempty"` Homeserver string `json:"homeserver"` UserID id.UserID `json:"user_id"` Username string `json:"username"` -- 2.38.4