Use gomatrixserverlib.StateKeyTuple and helper functions for auth (#44)

* Update gomatrixserverib dep

* Use helper functions when creating rooms

* Use gomatrixserverlib.StateKeyTuple
This commit is contained in:
Kegsay 2017-03-17 16:28:15 +00:00 committed by GitHub
parent 44c63254a7
commit 346f5d6334
9 changed files with 101 additions and 107 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/clientapi/producers"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)
@ -112,8 +111,6 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string, producer
"roomID": roomID,
}).Info("Creating new room")
// Remember events we've built and key off the state tuple so we can look them up easily when filling in auth_events
builtEventMap := make(map[common.StateKeyTuple]*gomatrixserverlib.Event)
var builtEvents []gomatrixserverlib.Event
// send events into the room in order of:
@ -165,7 +162,7 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string, producer
if i > 0 {
builder.PrevEvents = []gomatrixserverlib.EventReference{builtEvents[i-1].EventReference()}
}
ev, err := buildEvent(&builder, builtEventMap, cfg)
ev, err := buildEvent(&builder, &authEvents, cfg)
if err != nil {
return httputil.LogThenError(req, err)
}
@ -175,7 +172,6 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string, producer
}
// Add the event to the list of auth events
builtEventMap[common.StateKeyTuple{e.Type, e.StateKey}] = ev
builtEvents = append(builtEvents, *ev)
authEvents.AddEvent(ev)
}
@ -193,14 +189,18 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string, producer
// buildEvent fills out auth_events for the builder then builds the event
func buildEvent(builder *gomatrixserverlib.EventBuilder,
events map[common.StateKeyTuple]*gomatrixserverlib.Event,
provider gomatrixserverlib.AuthEventProvider,
cfg config.ClientAPI) (*gomatrixserverlib.Event, error) {
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
if err != nil {
return nil, err
}
builder.AuthEvents = authEventsFromStateNeeded(eventsNeeded, events)
refs, err := eventsNeeded.AuthEventReferences(provider)
if err != nil {
return nil, err
}
builder.AuthEvents = refs
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.ServerName)
now := time.Now()
event, err := builder.Build(eventID, now, cfg.ServerName, cfg.KeyID, cfg.PrivateKey)
@ -209,41 +209,3 @@ func buildEvent(builder *gomatrixserverlib.EventBuilder,
}
return &event, nil
}
func authEventsFromStateNeeded(eventsNeeded gomatrixserverlib.StateNeeded,
events map[common.StateKeyTuple]*gomatrixserverlib.Event) (authEvents []gomatrixserverlib.EventReference) {
// These events are only "needed" if they exist, so if they don't exist we can safely ignore them.
if eventsNeeded.Create {
ev := events[common.StateKeyTuple{"m.room.create", ""}]
if ev != nil {
authEvents = append(authEvents, ev.EventReference())
}
}
if eventsNeeded.JoinRules {
ev := events[common.StateKeyTuple{"m.room.join_rules", ""}]
if ev != nil {
authEvents = append(authEvents, ev.EventReference())
}
}
if eventsNeeded.PowerLevels {
ev := events[common.StateKeyTuple{"m.room.power_levels", ""}]
if ev != nil {
authEvents = append(authEvents, ev.EventReference())
}
}
for _, userID := range eventsNeeded.Member {
ev := events[common.StateKeyTuple{"m.room.member", userID}]
if ev != nil {
authEvents = append(authEvents, ev.EventReference())
}
}
for _, token := range eventsNeeded.ThirdPartyInvite {
ev := events[common.StateKeyTuple{"m.room.member", token}]
if ev != nil {
authEvents = append(authEvents, ev.EventReference())
}
}
return
}

View file

@ -11,7 +11,6 @@ import (
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/clientapi/producers"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
@ -48,7 +47,7 @@ func SendEvent(req *http.Request, roomID, eventType, txnID string, stateKey *str
builder.SetContent(r)
// work out what will be required in order to send this event
requiredStateEvents, err := stateNeeded(&builder)
needed, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
if err != nil {
return httputil.LogThenError(req, err)
}
@ -56,7 +55,7 @@ func SendEvent(req *http.Request, roomID, eventType, txnID string, stateKey *str
// Ask the roomserver for information about this room
queryReq := api.QueryLatestEventsAndStateRequest{
RoomID: roomID,
StateToFetch: requiredStateEvents,
StateToFetch: needed.Tuples(),
}
var queryRes api.QueryLatestEventsAndStateResponse
if queryErr := queryAPI.QueryLatestEventsAndState(&queryReq, &queryRes); queryErr != nil {
@ -105,26 +104,3 @@ func SendEvent(req *http.Request, roomID, eventType, txnID string, stateKey *str
JSON: sendEventResponse{e.EventID()},
}
}
func stateNeeded(builder *gomatrixserverlib.EventBuilder) (requiredStateEvents []common.StateKeyTuple, err error) {
authEvents, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
if err != nil {
return
}
if authEvents.Create {
requiredStateEvents = append(requiredStateEvents, common.StateKeyTuple{"m.room.create", ""})
}
if authEvents.JoinRules {
requiredStateEvents = append(requiredStateEvents, common.StateKeyTuple{"m.room.join_rules", ""})
}
if authEvents.PowerLevels {
requiredStateEvents = append(requiredStateEvents, common.StateKeyTuple{"m.room.power_levels", ""})
}
for _, userID := range authEvents.Member {
requiredStateEvents = append(requiredStateEvents, common.StateKeyTuple{"m.room.member", userID})
}
for _, token := range authEvents.ThirdPartyInvite {
requiredStateEvents = append(requiredStateEvents, common.StateKeyTuple{"m.room.third_party_invite", token})
}
return
}

View file

@ -1,13 +1 @@
package common
// StateKeyTuple is a pair of an event type and state_key.
// This is typically used as a key in a map.
type StateKeyTuple struct {
// The "type" key of a matrix event.
EventType string
// The "state_key" of a matrix event.
// The empty string is a legitimate value for the "state_key" in matrix
// so take care to initialise this field lest you accidentally request a
// "state_key" with the go default of the empty string.
EventStateKey string
}

View file

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib"
"net/http"
)
@ -15,7 +14,7 @@ type QueryLatestEventsAndStateRequest struct {
RoomID string
// The state key tuples to fetch from the room current state.
// If this list is empty or nil then no state events are returned.
StateToFetch []common.StateKeyTuple
StateToFetch []gomatrixserverlib.StateKeyTuple
}
// QueryLatestEventsAndStateResponse is a response to QueryLatestEventsAndState

View file

@ -2,7 +2,6 @@ package main
import (
"fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib"
"os"
@ -368,7 +367,7 @@ func main() {
if err := q.QueryLatestEventsAndState(
&api.QueryLatestEventsAndStateRequest{
RoomID: "!HCXfdvrfksxuYnIFiJ:matrix.org",
StateToFetch: []common.StateKeyTuple{
StateToFetch: []gomatrixserverlib.StateKeyTuple{
{"m.room.member", "@richvdh:matrix.org"},
},
},

View file

@ -4,8 +4,8 @@ package state
import (
"fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"sort"
)
@ -200,12 +200,12 @@ func DifferenceBetweeenStateSnapshots(db RoomStateDatabase, oldStateNID, newStat
// stringTuplesToNumericTuples converts the string state key tuples into numeric IDs
// If there isn't a numeric ID for either the event type or the event state key then the tuple is discarded.
// Returns an error if there was a problem talking to the database.
func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []common.StateKeyTuple) ([]types.StateKeyTuple, error) {
func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []gomatrixserverlib.StateKeyTuple) ([]types.StateKeyTuple, error) {
eventTypes := make([]string, len(stringTuples))
stateKeys := make([]string, len(stringTuples))
for i := range stringTuples {
eventTypes[i] = stringTuples[i].EventType
stateKeys[i] = stringTuples[i].EventStateKey
stateKeys[i] = stringTuples[i].StateKey
}
eventTypes = util.UniqueStrings(eventTypes)
eventTypeMap, err := db.EventTypeNIDs(eventTypes)
@ -223,7 +223,7 @@ func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []common.Sta
var numericTuple types.StateKeyTuple
var ok1, ok2 bool
numericTuple.EventTypeNID, ok1 = eventTypeMap[stringTuple.EventType]
numericTuple.EventStateKeyNID, ok2 = stateKeyMap[stringTuple.EventStateKey]
numericTuple.EventStateKeyNID, ok2 = stateKeyMap[stringTuple.StateKey]
// Discard the tuple if there wasn't a numeric ID for either the event type or the state key.
if ok1 && ok2 {
result = append(result, numericTuple)
@ -239,7 +239,7 @@ func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []common.Sta
// This is typically the state before an event or the current state of a room.
// Returns a sorted list of state entries or an error if there was a problem talking to the database.
func LoadStateAtSnapshotForStringTuples(
db RoomStateDatabase, stateNID types.StateSnapshotNID, stateKeyTuples []common.StateKeyTuple,
db RoomStateDatabase, stateNID types.StateSnapshotNID, stateKeyTuples []gomatrixserverlib.StateKeyTuple,
) ([]types.StateEntry, error) {
numericTuples, err := stringTuplesToNumericTuples(db, stateKeyTuples)
if err != nil {