~edwargix/tallyard

d6e95ccb9e03d8a03cb185e6e7a6846e726c2555 — David Florness 2 years ago d6054f9
Start and join events for keys msgs must belong to the same election
1 files changed, 20 insertions(+), 10 deletions(-)

M election/msg.go
M election/msg.go => election/msg.go +20 -10
@@ 415,26 415,36 @@ func (elections *ElectionsMap) onKeysMessage(evt *event.Event, client *mautrix.C
		return
	}

	// this makes JoinIDIndex availabe below
	startEvt := elections.EventStore.GetStartEvent(evt.RoomID, content.StartID)
	if startEvt == nil {
		debugf("we couldn't process the start event, %s", content.StartID)
		return
	}

	el := elections.GetElection(startEvt.CreateID)
	if el == nil {
		// should never happen because we retrieved the join event above
		errorf("election %s doesn't exist", startEvt.ID)
		return
	}

	joinEvt := elections.EventStore.GetJoinEvent(evt.RoomID, content.JoinID)
	if joinEvt == nil {
		debugf("we couldn't get the join event, %s", content.JoinID)
		return
	}

	// ensure keys sender also sent join event
	if evt.Sender != joinEvt.Sender {
		warnf("they did not send the join event; %s did", joinEvt.Sender)
		return
	}

	// process the start event so that JoinIDIndex is availabe below
	if elections.EventStore.GetStartEvent(evt.RoomID, content.StartID) == nil {
		debugf("we couldn't process the start event, %s", content.StartID)
		return
	}

	el := elections.GetElection(joinEvt.CreateID)
	if el == nil {
		// should never happen because we retrieved the join event above
		errorf("election %s doesn't exist", joinEvt.ID)
	// ensure join event and start event belong to the same election
	if _, voterExists := el.Joins[joinEvt.ID]; !voterExists {
		warnf("the join event %s and start event %s do not belong to the same election",
			joinEvt.ID, startEvt.ID)
		return
	}