mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
Share StateKeyTuple between roomserver and clientapi
Remove 'common' from clientapi and break into distinct packages
This commit is contained in:
parent
270d8ae25d
commit
d563981955
|
|
@ -1,4 +1,4 @@
|
||||||
package common
|
package events
|
||||||
|
|
||||||
// CreateContent is the event content for http://matrix.org/docs/spec/client_server/r0.2.0.html#m-room-create
|
// CreateContent is the event content for http://matrix.org/docs/spec/client_server/r0.2.0.html#m-room-create
|
||||||
type CreateContent struct {
|
type CreateContent struct {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package common
|
package httputil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -8,15 +8,6 @@ import (
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StateTuple is the tuple of an event type and an event state_key, typically used as a key
|
|
||||||
// in maps.
|
|
||||||
type StateTuple struct {
|
|
||||||
// Type is the event type e.g "m.room.name"
|
|
||||||
Type string
|
|
||||||
// Key is the state key e.g. ""
|
|
||||||
Key string
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
|
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
|
||||||
// there was a problem unmarshalling. Calling this function consumes the request body.
|
// there was a problem unmarshalling. Calling this function consumes the request body.
|
||||||
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
|
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
|
||||||
|
|
@ -9,9 +9,11 @@ import (
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth"
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
||||||
"github.com/matrix-org/dendrite/clientapi/common"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/config"
|
"github.com/matrix-org/dendrite/clientapi/config"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/events"
|
||||||
|
"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/common"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
@ -90,7 +92,7 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string) util.JSO
|
||||||
return *resErr
|
return *resErr
|
||||||
}
|
}
|
||||||
var r createRoomRequest
|
var r createRoomRequest
|
||||||
resErr = common.UnmarshalJSONRequest(req, &r)
|
resErr = httputil.UnmarshalJSONRequest(req, &r)
|
||||||
if resErr != nil {
|
if resErr != nil {
|
||||||
return *resErr
|
return *resErr
|
||||||
}
|
}
|
||||||
|
|
@ -110,7 +112,7 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string) util.JSO
|
||||||
}).Info("Creating new room")
|
}).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
|
// 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.StateTuple]*gomatrixserverlib.Event)
|
builtEventMap := make(map[common.StateKeyTuple]*gomatrixserverlib.Event)
|
||||||
var builtEvents []*gomatrixserverlib.Event
|
var builtEvents []*gomatrixserverlib.Event
|
||||||
|
|
||||||
// send events into the room in order of:
|
// send events into the room in order of:
|
||||||
|
|
@ -133,12 +135,12 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string) util.JSO
|
||||||
// TODO: Synapse has txn/token ID on each event. Do we need to do this here?
|
// TODO: Synapse has txn/token ID on each event. Do we need to do this here?
|
||||||
emptyString := ""
|
emptyString := ""
|
||||||
eventsToMake := []fledglingEvent{
|
eventsToMake := []fledglingEvent{
|
||||||
{"m.room.create", &emptyString, common.CreateContent{Creator: userID}},
|
{"m.room.create", &emptyString, events.CreateContent{Creator: userID}},
|
||||||
{"m.room.member", &userID, common.MemberContent{Membership: "join"}}, // TODO: Set avatar_url / displayname
|
{"m.room.member", &userID, events.MemberContent{Membership: "join"}}, // TODO: Set avatar_url / displayname
|
||||||
{"m.room.power_levels", &emptyString, common.InitialPowerLevelsContent(userID)},
|
{"m.room.power_levels", &emptyString, events.InitialPowerLevelsContent(userID)},
|
||||||
// TODO: m.room.canonical_alias
|
// TODO: m.room.canonical_alias
|
||||||
{"m.room.join_rules", &emptyString, common.JoinRulesContent{"public"}}, // FIXME: Allow this to be changed
|
{"m.room.join_rules", &emptyString, events.JoinRulesContent{"public"}}, // FIXME: Allow this to be changed
|
||||||
{"m.room.history_visibility", &emptyString, common.HistoryVisibilityContent{"joined"}}, // FIXME: Allow this to be changed
|
{"m.room.history_visibility", &emptyString, events.HistoryVisibilityContent{"joined"}}, // FIXME: Allow this to be changed
|
||||||
// TODO: m.room.guest_access
|
// TODO: m.room.guest_access
|
||||||
// TODO: Other initial state items
|
// TODO: Other initial state items
|
||||||
// TODO: m.room.name
|
// TODO: m.room.name
|
||||||
|
|
@ -172,7 +174,7 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string) util.JSO
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
builtEventMap[common.StateTuple{e.Type, *e.StateKey}] = ev
|
builtEventMap[common.StateKeyTuple{e.Type, *e.StateKey}] = ev
|
||||||
builtEvents = append(builtEvents, ev)
|
builtEvents = append(builtEvents, ev)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +187,7 @@ func createRoom(req *http.Request, cfg config.ClientAPI, roomID string) util.JSO
|
||||||
|
|
||||||
// buildEvent fills out auth_events for the builder then builds the event
|
// buildEvent fills out auth_events for the builder then builds the event
|
||||||
func buildEvent(builder *gomatrixserverlib.EventBuilder,
|
func buildEvent(builder *gomatrixserverlib.EventBuilder,
|
||||||
events map[common.StateTuple]*gomatrixserverlib.Event,
|
events map[common.StateKeyTuple]*gomatrixserverlib.Event,
|
||||||
cfg config.ClientAPI) (*gomatrixserverlib.Event, error) {
|
cfg config.ClientAPI) (*gomatrixserverlib.Event, error) {
|
||||||
|
|
||||||
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
||||||
|
|
@ -203,36 +205,36 @@ func buildEvent(builder *gomatrixserverlib.EventBuilder,
|
||||||
}
|
}
|
||||||
|
|
||||||
func authEventsFromStateNeeded(eventsNeeded gomatrixserverlib.StateNeeded,
|
func authEventsFromStateNeeded(eventsNeeded gomatrixserverlib.StateNeeded,
|
||||||
events map[common.StateTuple]*gomatrixserverlib.Event) (authEvents []gomatrixserverlib.EventReference) {
|
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.
|
// These events are only "needed" if they exist, so if they don't exist we can safely ignore them.
|
||||||
if eventsNeeded.Create {
|
if eventsNeeded.Create {
|
||||||
ev := events[common.StateTuple{"m.room.create", ""}]
|
ev := events[common.StateKeyTuple{"m.room.create", ""}]
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
authEvents = append(authEvents, ev.EventReference())
|
authEvents = append(authEvents, ev.EventReference())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if eventsNeeded.JoinRules {
|
if eventsNeeded.JoinRules {
|
||||||
ev := events[common.StateTuple{"m.room.join_rules", ""}]
|
ev := events[common.StateKeyTuple{"m.room.join_rules", ""}]
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
authEvents = append(authEvents, ev.EventReference())
|
authEvents = append(authEvents, ev.EventReference())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if eventsNeeded.PowerLevels {
|
if eventsNeeded.PowerLevels {
|
||||||
ev := events[common.StateTuple{"m.room.power_levels", ""}]
|
ev := events[common.StateKeyTuple{"m.room.power_levels", ""}]
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
authEvents = append(authEvents, ev.EventReference())
|
authEvents = append(authEvents, ev.EventReference())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, userID := range eventsNeeded.Member {
|
for _, userID := range eventsNeeded.Member {
|
||||||
ev := events[common.StateTuple{"m.room.member", userID}]
|
ev := events[common.StateKeyTuple{"m.room.member", userID}]
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
authEvents = append(authEvents, ev.EventReference())
|
authEvents = append(authEvents, ev.EventReference())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, token := range eventsNeeded.ThirdPartyInvite {
|
for _, token := range eventsNeeded.ThirdPartyInvite {
|
||||||
ev := events[common.StateTuple{"m.room.member", token}]
|
ev := events[common.StateKeyTuple{"m.room.member", token}]
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
authEvents = append(authEvents, ev.EventReference())
|
authEvents = append(authEvents, ev.EventReference())
|
||||||
}
|
}
|
||||||
|
|
@ -241,30 +243,30 @@ func authEventsFromStateNeeded(eventsNeeded gomatrixserverlib.StateNeeded,
|
||||||
}
|
}
|
||||||
|
|
||||||
type authEventProvider struct {
|
type authEventProvider struct {
|
||||||
events map[common.StateTuple]*gomatrixserverlib.Event
|
events map[common.StateKeyTuple]*gomatrixserverlib.Event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) Create() (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) Create() (ev *gomatrixserverlib.Event, err error) {
|
||||||
return a.fetch(common.StateTuple{"m.room.create", ""})
|
return a.fetch(common.StateKeyTuple{"m.room.create", ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) JoinRules() (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) JoinRules() (ev *gomatrixserverlib.Event, err error) {
|
||||||
return a.fetch(common.StateTuple{"m.room.join_rules", ""})
|
return a.fetch(common.StateKeyTuple{"m.room.join_rules", ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) PowerLevels() (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) PowerLevels() (ev *gomatrixserverlib.Event, err error) {
|
||||||
return a.fetch(common.StateTuple{"m.room.power_levels", ""})
|
return a.fetch(common.StateKeyTuple{"m.room.power_levels", ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) Member(stateKey string) (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) Member(stateKey string) (ev *gomatrixserverlib.Event, err error) {
|
||||||
return a.fetch(common.StateTuple{"m.room.member", stateKey})
|
return a.fetch(common.StateKeyTuple{"m.room.member", stateKey})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) ThirdPartyInvite(stateKey string) (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) ThirdPartyInvite(stateKey string) (ev *gomatrixserverlib.Event, err error) {
|
||||||
return a.fetch(common.StateTuple{"m.room.third_party_invite", stateKey})
|
return a.fetch(common.StateKeyTuple{"m.room.third_party_invite", stateKey})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authEventProvider) fetch(tuple common.StateTuple) (ev *gomatrixserverlib.Event, err error) {
|
func (a *authEventProvider) fetch(tuple common.StateKeyTuple) (ev *gomatrixserverlib.Event, err error) {
|
||||||
ev, _ = a.events[tuple]
|
ev, _ = a.events[tuple]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
src/github.com/matrix-org/dendrite/common/types.go
Normal file
13
src/github.com/matrix-org/dendrite/common/types.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -4,29 +4,18 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StateKeyTuple is a pair of an event type and state_key.
|
|
||||||
// This is used when requesting parts of the state of a room.
|
|
||||||
type StateKeyTuple struct {
|
|
||||||
// The "type" key
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState
|
// QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState
|
||||||
type QueryLatestEventsAndStateRequest struct {
|
type QueryLatestEventsAndStateRequest struct {
|
||||||
// The roomID to query the latest events for.
|
// The roomID to query the latest events for.
|
||||||
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 []StateKeyTuple
|
StateToFetch []common.StateKeyTuple
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryLatestEventsAndStateResponse is a response to QueryLatestEventsAndState
|
// QueryLatestEventsAndStateResponse is a response to QueryLatestEventsAndState
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ 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"
|
||||||
|
|
@ -367,7 +368,7 @@ func main() {
|
||||||
if err := q.QueryLatestEventsAndState(
|
if err := q.QueryLatestEventsAndState(
|
||||||
&api.QueryLatestEventsAndStateRequest{
|
&api.QueryLatestEventsAndStateRequest{
|
||||||
RoomID: "!HCXfdvrfksxuYnIFiJ:matrix.org",
|
RoomID: "!HCXfdvrfksxuYnIFiJ:matrix.org",
|
||||||
StateToFetch: []api.StateKeyTuple{
|
StateToFetch: []common.StateKeyTuple{
|
||||||
{"m.room.member", "@richvdh:matrix.org"},
|
{"m.room.member", "@richvdh:matrix.org"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
@ -200,7 +200,7 @@ 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 []api.StateKeyTuple) ([]types.StateKeyTuple, error) {
|
func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []common.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 {
|
||||||
|
|
@ -239,7 +239,7 @@ func stringTuplesToNumericTuples(db RoomStateDatabase, stringTuples []api.StateK
|
||||||
// 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 []api.StateKeyTuple,
|
db RoomStateDatabase, stateNID types.StateSnapshotNID, stateKeyTuples []common.StateKeyTuple,
|
||||||
) ([]types.StateEntry, error) {
|
) ([]types.StateEntry, error) {
|
||||||
numericTuples, err := stringTuplesToNumericTuples(db, stateKeyTuples)
|
numericTuples, err := stringTuplesToNumericTuples(db, stateKeyTuples)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue