Use gomatrixserverlib.StateKeyTuple

This commit is contained in:
Kegan Dougal 2017-03-17 16:22:12 +00:00
parent ec8c123ade
commit 452cae7f82
5 changed files with 9 additions and 47 deletions

View file

@ -11,7 +11,6 @@ import (
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/clientapi/producers"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
@ -48,7 +47,7 @@ func SendEvent(req *http.Request, roomID, eventType, txnID string, stateKey *str
builder.SetContent(r) builder.SetContent(r)
// work out what will be required in order to send this event // work out what will be required in order to send this event
requiredStateEvents, err := stateNeeded(&builder) needed, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
if err != nil { if err != nil {
return httputil.LogThenError(req, err) 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 // Ask the roomserver for information about this room
queryReq := api.QueryLatestEventsAndStateRequest{ queryReq := api.QueryLatestEventsAndStateRequest{
RoomID: roomID, RoomID: roomID,
StateToFetch: requiredStateEvents, StateToFetch: needed.Tuples(),
} }
var queryRes api.QueryLatestEventsAndStateResponse var queryRes api.QueryLatestEventsAndStateResponse
if queryErr := queryAPI.QueryLatestEventsAndState(&queryReq, &queryRes); queryErr != nil { 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()}, 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 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" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"net/http" "net/http"
) )
@ -15,7 +14,7 @@ type QueryLatestEventsAndStateRequest struct {
RoomID string RoomID string
// The state key tuples to fetch from the room current state. // The state key tuples to fetch from the room current state.
// If this list is empty or nil then no state events are returned. // 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 // QueryLatestEventsAndStateResponse is a response to QueryLatestEventsAndState

View file

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

View file

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