M cmd/tallyard/main.go => cmd/tallyard/main.go +20 -8
@@ 52,7 52,12 @@ func main() {
panic(err)
}
- elections := election.NewElectionsMap()
+ if data.Elections == nil {
+ data.Elections = election.NewElectionsMap()
+ }
+ data.Elections.Save = func() {
+ data.Save()
+ }
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEvent(client.Store.(*mautrix.InMemoryStore).UpdateState)
@@ 62,7 67,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnCreateElectionMessage(evt, elections)
+ election.OnCreateElectionMessage(evt, data.Elections)
+ data.Save()
})
syncer.OnEventType(election.JoinElectionMessage, func(source mautrix.EventSource, evt *event.Event) {
DebugCB(source, evt)
@@ 70,7 76,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnJoinElectionMessage(client, evt, elections)
+ election.OnJoinElectionMessage(client, evt, data.Elections)
+ data.Save()
})
syncer.OnEventType(election.StartElectionMessage, func(source mautrix.EventSource, evt *event.Event) {
DebugCB(source, evt)
@@ 78,7 85,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnStartElectionMessage(client, evt, elections)
+ election.OnStartElectionMessage(client, evt, data.Elections)
+ data.Save()
})
syncer.OnEventType(election.EvalMessage, func(source mautrix.EventSource, evt *event.Event) {
DebugCB(source, evt)
@@ 86,7 94,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnEvalMessage(client, evt, elections)
+ election.OnEvalMessage(client, evt, data.Elections)
+ data.Save()
})
syncer.OnEventType(election.SumMessage, func(source mautrix.EventSource, evt *event.Event) {
DebugCB(source, evt)
@@ 94,7 103,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnSumMessage(client, evt, elections)
+ election.OnSumMessage(client, evt, data.Elections)
+ data.Save()
})
syncer.OnEventType(election.ResultMessage, func(source mautrix.EventSource, evt *event.Event) {
DebugCB(source, evt)
@@ 102,7 112,8 @@ func main() {
log.Debug("redacted")
return
}
- election.OnResultMessage(client, evt, elections)
+ election.OnResultMessage(client, evt, data.Elections)
+ data.Save()
})
go func() {
@@ 117,7 128,7 @@ func main() {
}
}()
- el := ui.TUI(client, elections)
+ el := ui.TUI(client, data.Elections)
if el == nil {
// user likely hit C-c
return
@@ 125,6 136,7 @@ func main() {
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
M election/election.go => election/election.go +14 -1
@@ 1,6 1,7 @@
package election
import (
+ "encoding/json"
"sync"
"maunium.net/go/mautrix/event"
@@ 18,13 19,14 @@ type Election struct {
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"`
}
func NewElection(candidates []Candidate, createEventId id.EventID,
creationTimestamp int64, creator id.UserID, roomID id.RoomID,
- title string) *Election {
+ title string, save func()) *Election {
return &Election{
Candidates: candidates,
CreateEventId: createEventId,
@@ 32,6 34,17 @@ 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
+}
M election/map.go => election/map.go +2 -6
@@ 13,6 13,7 @@ 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 {
@@ 63,12 64,6 @@ 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()
@@ 77,6 72,7 @@ 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) {
M election/msg.go => election/msg.go +1 -1
@@ 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))
+ evt.Timestamp, evt.Sender, evt.RoomID, content.Title, elections.Save))
}
func getElection(client *mautrix.Client, roomID id.RoomID, createEventId id.EventID, elections *ElectionsMap) *Election {
M election/voter.go => election/voter.go +4 -0
@@ 109,6 109,7 @@ func (el *Election) JoinElection(client *mautrix.Client) error {
PrivKey: *privKey,
}
el.Joins[resp.EventID] = el.LocalVoter.Voter
+ el.Save()
return nil
}
@@ 151,6 152,7 @@ func (el *Election) StartElection(client *mautrix.Client) error {
// OnStartElectionMessage(client, startEvt, elections)
el.StartEvt = startEvt
el.FinalVoters = &voters
+ el.Save()
return nil
}
@@ 203,6 205,7 @@ func (el *Election) SendEvals(client *mautrix.Client) error {
el.LocalVoter.Eval = nil
return err
}
+ el.Save()
return nil
}
@@ 228,6 231,7 @@ func (el *Election) SendSum(client *mautrix.Client) error {
return err
}
el.LocalVoter.Sum = sum
+ el.Save()
return nil
}
M matrix/data.go => matrix/data.go +7 -5
@@ 10,14 10,16 @@ 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"`
- Homeserver string `json:"homeserver"`
- UserID id.UserID `json:"user_id"`
- Username string `json:"username"`
+ 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"`
}
var dataFname = xdg.DataHome() + "/tallyard/data.json"