Skip to content

Commit

Permalink
Consolidation of roomserver APIs (#994)
Browse files Browse the repository at this point in the history
* Consolidation of roomserver APIs

* Comment out alias tests for now, they are broken

* Wire AS API into roomserver again

* Roomserver didn't take asAPI param before so return to that

* Prevent roomserver asking AS API for alias info

* Rename some files

* Remove alias_test, incoherent tests and unwanted appservice integration

* Remove FS API inject on syncapi component
  • Loading branch information
neilalexander authored May 1, 2020
1 parent ebbfc12 commit e15f667
Show file tree
Hide file tree
Showing 72 changed files with 894 additions and 1,170 deletions.
7 changes: 3 additions & 4 deletions appservice/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func SetupAppServiceAPIComponent(
accountsDB accounts.Database,
deviceDB devices.Database,
federation *gomatrixserverlib.FederationClient,
roomserverAliasAPI roomserverAPI.RoomserverAliasAPI,
roomserverQueryAPI roomserverAPI.RoomserverQueryAPI,
rsAPI roomserverAPI.RoomserverInternalAPI,
transactionsCache *transactions.Cache,
) appserviceAPI.AppServiceQueryAPI {
// Create a connection to the appservice postgres DB
Expand Down Expand Up @@ -87,7 +86,7 @@ func SetupAppServiceAPIComponent(

consumer := consumers.NewOutputRoomEventConsumer(
base.Cfg, base.KafkaConsumer, accountsDB, appserviceDB,
roomserverQueryAPI, roomserverAliasAPI, workerStates,
rsAPI, workerStates,
)
if err := consumer.Start(); err != nil {
logrus.WithError(err).Panicf("failed to start appservice roomserver consumer")
Expand All @@ -100,7 +99,7 @@ func SetupAppServiceAPIComponent(

// Set up HTTP Endpoints
routing.Setup(
base.APIMux, base.Cfg, roomserverQueryAPI, roomserverAliasAPI,
base.APIMux, base.Cfg, rsAPI,
accountsDB, federation, transactionsCache,
)

Expand Down
13 changes: 5 additions & 8 deletions appservice/consumers/roomserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ type OutputRoomEventConsumer struct {
roomServerConsumer *common.ContinualConsumer
db accounts.Database
asDB storage.Database
query api.RoomserverQueryAPI
alias api.RoomserverAliasAPI
rsAPI api.RoomserverInternalAPI
serverName string
workerStates []types.ApplicationServiceWorkerState
}
Expand All @@ -48,8 +47,7 @@ func NewOutputRoomEventConsumer(
kafkaConsumer sarama.Consumer,
store accounts.Database,
appserviceDB storage.Database,
queryAPI api.RoomserverQueryAPI,
aliasAPI api.RoomserverAliasAPI,
rsAPI api.RoomserverInternalAPI,
workerStates []types.ApplicationServiceWorkerState,
) *OutputRoomEventConsumer {
consumer := common.ContinualConsumer{
Expand All @@ -61,8 +59,7 @@ func NewOutputRoomEventConsumer(
roomServerConsumer: &consumer,
db: store,
asDB: appserviceDB,
query: queryAPI,
alias: aliasAPI,
rsAPI: rsAPI,
serverName: string(cfg.Matrix.ServerName),
workerStates: workerStates,
}
Expand Down Expand Up @@ -139,7 +136,7 @@ func (s *OutputRoomEventConsumer) lookupMissingStateEvents(
// Request the missing events from the roomserver
eventReq := api.QueryEventsByIDRequest{EventIDs: missing}
var eventResp api.QueryEventsByIDResponse
if err := s.query.QueryEventsByID(context.TODO(), &eventReq, &eventResp); err != nil {
if err := s.rsAPI.QueryEventsByID(context.TODO(), &eventReq, &eventResp); err != nil {
return nil, err
}

Expand Down Expand Up @@ -200,7 +197,7 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont
// Check all known room aliases of the room the event came from
queryReq := api.GetAliasesForRoomIDRequest{RoomID: event.RoomID()}
var queryRes api.GetAliasesForRoomIDResponse
if err := s.alias.GetAliasesForRoomID(ctx, &queryReq, &queryRes); err == nil {
if err := s.rsAPI.GetAliasesForRoomID(ctx, &queryReq, &queryRes); err == nil {
for _, alias := range queryRes.Aliases {
if appservice.IsInterestedInRoomAlias(alias) {
return true
Expand Down
2 changes: 1 addition & 1 deletion appservice/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const pathPrefixApp = "/_matrix/app/v1"
// nolint: gocyclo
func Setup(
apiMux *mux.Router, cfg *config.Dendrite, // nolint: unparam
queryAPI api.RoomserverQueryAPI, aliasAPI api.RoomserverAliasAPI, // nolint: unparam
rsAPI api.RoomserverInternalAPI, // nolint: unparam
accountDB accounts.Database, // nolint: unparam
federation *gomatrixserverlib.FederationClient, // nolint: unparam
transactionsCache *transactions.Cache, // nolint: unparam
Expand Down
10 changes: 4 additions & 6 deletions clientapi/clientapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ func SetupClientAPIComponent(
accountsDB accounts.Database,
federation *gomatrixserverlib.FederationClient,
keyRing *gomatrixserverlib.KeyRing,
aliasAPI roomserverAPI.RoomserverAliasAPI,
inputAPI roomserverAPI.RoomserverInputAPI,
queryAPI roomserverAPI.RoomserverQueryAPI,
rsAPI roomserverAPI.RoomserverInternalAPI,
eduInputAPI eduServerAPI.EDUServerInputAPI,
asAPI appserviceAPI.AppServiceQueryAPI,
transactionsCache *transactions.Cache,
fsAPI federationSenderAPI.FederationSenderInternalAPI,
) {
roomserverProducer := producers.NewRoomserverProducer(inputAPI, queryAPI)
roomserverProducer := producers.NewRoomserverProducer(rsAPI)
eduProducer := producers.NewEDUServerProducer(eduInputAPI)

userUpdateProducer := &producers.UserUpdateProducer{
Expand All @@ -60,14 +58,14 @@ func SetupClientAPIComponent(
}

consumer := consumers.NewOutputRoomEventConsumer(
base.Cfg, base.KafkaConsumer, accountsDB, queryAPI,
base.Cfg, base.KafkaConsumer, accountsDB, rsAPI,
)
if err := consumer.Start(); err != nil {
logrus.WithError(err).Panicf("failed to start room server consumer")
}

routing.Setup(
base.APIMux, base.Cfg, roomserverProducer, queryAPI, aliasAPI, asAPI,
base.APIMux, base.Cfg, roomserverProducer, rsAPI, asAPI,
accountsDB, deviceDB, federation, *keyRing, userUpdateProducer,
syncProducer, eduProducer, transactionsCache, fsAPI,
)
Expand Down
22 changes: 11 additions & 11 deletions clientapi/consumers/roomserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ import (

// OutputRoomEventConsumer consumes events that originated in the room server.
type OutputRoomEventConsumer struct {
roomServerConsumer *common.ContinualConsumer
db accounts.Database
query api.RoomserverQueryAPI
serverName string
rsAPI api.RoomserverInternalAPI
rsConsumer *common.ContinualConsumer
db accounts.Database
serverName string
}

// NewOutputRoomEventConsumer creates a new OutputRoomEventConsumer. Call Start() to begin consuming from room servers.
func NewOutputRoomEventConsumer(
cfg *config.Dendrite,
kafkaConsumer sarama.Consumer,
store accounts.Database,
queryAPI api.RoomserverQueryAPI,
rsAPI api.RoomserverInternalAPI,
) *OutputRoomEventConsumer {

consumer := common.ContinualConsumer{
Expand All @@ -50,10 +50,10 @@ func NewOutputRoomEventConsumer(
PartitionStore: store,
}
s := &OutputRoomEventConsumer{
roomServerConsumer: &consumer,
db: store,
query: queryAPI,
serverName: string(cfg.Matrix.ServerName),
rsConsumer: &consumer,
db: store,
rsAPI: rsAPI,
serverName: string(cfg.Matrix.ServerName),
}
consumer.ProcessMessage = s.onMessage

Expand All @@ -62,7 +62,7 @@ func NewOutputRoomEventConsumer(

// Start consuming from room servers
func (s *OutputRoomEventConsumer) Start() error {
return s.roomServerConsumer.Start()
return s.rsConsumer.Start()
}

// onMessage is called when the sync server receives a new event from the room server output log.
Expand Down Expand Up @@ -134,7 +134,7 @@ func (s *OutputRoomEventConsumer) lookupStateEvents(
// Request the missing events from the roomserver
eventReq := api.QueryEventsByIDRequest{EventIDs: missing}
var eventResp api.QueryEventsByIDResponse
if err := s.query.QueryEventsByID(context.TODO(), &eventReq, &eventResp); err != nil {
if err := s.rsAPI.QueryEventsByID(context.TODO(), &eventReq, &eventResp); err != nil {
return nil, err
}

Expand Down
12 changes: 5 additions & 7 deletions clientapi/producers/roomserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import (

// RoomserverProducer produces events for the roomserver to consume.
type RoomserverProducer struct {
InputAPI api.RoomserverInputAPI
QueryAPI api.RoomserverQueryAPI
RsAPI api.RoomserverInternalAPI
}

// NewRoomserverProducer creates a new RoomserverProducer
func NewRoomserverProducer(inputAPI api.RoomserverInputAPI, queryAPI api.RoomserverQueryAPI) *RoomserverProducer {
func NewRoomserverProducer(rsAPI api.RoomserverInternalAPI) *RoomserverProducer {
return &RoomserverProducer{
InputAPI: inputAPI,
QueryAPI: queryAPI,
RsAPI: rsAPI,
}
}

Expand Down Expand Up @@ -95,7 +93,7 @@ func (c *RoomserverProducer) SendInputRoomEvents(
) (eventID string, err error) {
request := api.InputRoomEventsRequest{InputRoomEvents: ires}
var response api.InputRoomEventsResponse
err = c.InputAPI.InputRoomEvents(ctx, &request, &response)
err = c.RsAPI.InputRoomEvents(ctx, &request, &response)
eventID = response.EventID
return
}
Expand All @@ -118,5 +116,5 @@ func (c *RoomserverProducer) SendInvite(
}},
}
var response api.InputRoomEventsResponse
return c.InputAPI.InputRoomEvents(ctx, &request, &response)
return c.RsAPI.InputRoomEvents(ctx, &request, &response)
}
4 changes: 2 additions & 2 deletions clientapi/routing/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
// SendMembership implements PUT /rooms/{roomID}/(join|kick|ban|unban|leave|invite)
// by building a m.room.member event then sending it to the room server
func GetCapabilities(
req *http.Request, queryAPI roomserverAPI.RoomserverQueryAPI,
req *http.Request, rsAPI roomserverAPI.RoomserverInternalAPI,
) util.JSONResponse {
roomVersionsQueryReq := roomserverAPI.QueryRoomVersionCapabilitiesRequest{}
roomVersionsQueryRes := roomserverAPI.QueryRoomVersionCapabilitiesResponse{}
if err := queryAPI.QueryRoomVersionCapabilities(
if err := rsAPI.QueryRoomVersionCapabilities(
req.Context(),
&roomVersionsQueryReq,
&roomVersionsQueryRes,
Expand Down
8 changes: 4 additions & 4 deletions clientapi/routing/createroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ type fledglingEvent struct {
func CreateRoom(
req *http.Request, device *authtypes.Device,
cfg *config.Dendrite, producer *producers.RoomserverProducer,
accountDB accounts.Database, aliasAPI roomserverAPI.RoomserverAliasAPI,
accountDB accounts.Database, rsAPI roomserverAPI.RoomserverInternalAPI,
asAPI appserviceAPI.AppServiceQueryAPI,
) util.JSONResponse {
// TODO (#267): Check room ID doesn't clash with an existing one, and we
// probably shouldn't be using pseudo-random strings, maybe GUIDs?
roomID := fmt.Sprintf("!%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
return createRoom(req, device, cfg, roomID, producer, accountDB, aliasAPI, asAPI)
return createRoom(req, device, cfg, roomID, producer, accountDB, rsAPI, asAPI)
}

// createRoom implements /createRoom
// nolint: gocyclo
func createRoom(
req *http.Request, device *authtypes.Device,
cfg *config.Dendrite, roomID string, producer *producers.RoomserverProducer,
accountDB accounts.Database, aliasAPI roomserverAPI.RoomserverAliasAPI,
accountDB accounts.Database, rsAPI roomserverAPI.RoomserverInternalAPI,
asAPI appserviceAPI.AppServiceQueryAPI,
) util.JSONResponse {
logger := util.GetLogger(req.Context())
Expand Down Expand Up @@ -340,7 +340,7 @@ func createRoom(
}

var aliasResp roomserverAPI.SetRoomAliasResponse
err = aliasAPI.SetRoomAlias(req.Context(), &aliasReq, &aliasResp)
err = rsAPI.SetRoomAlias(req.Context(), &aliasReq, &aliasResp)
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("aliasAPI.SetRoomAlias failed")
return jsonerror.InternalServerError()
Expand Down
6 changes: 3 additions & 3 deletions clientapi/routing/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func DirectoryRoom(
roomAlias string,
federation *gomatrixserverlib.FederationClient,
cfg *config.Dendrite,
rsAPI roomserverAPI.RoomserverAliasAPI,
rsAPI roomserverAPI.RoomserverInternalAPI,
fedSenderAPI federationSenderAPI.FederationSenderInternalAPI,
) util.JSONResponse {
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
Expand Down Expand Up @@ -115,7 +115,7 @@ func SetLocalAlias(
device *authtypes.Device,
alias string,
cfg *config.Dendrite,
aliasAPI roomserverAPI.RoomserverAliasAPI,
aliasAPI roomserverAPI.RoomserverInternalAPI,
) util.JSONResponse {
_, domain, err := gomatrixserverlib.SplitID('#', alias)
if err != nil {
Expand Down Expand Up @@ -190,7 +190,7 @@ func RemoveLocalAlias(
req *http.Request,
device *authtypes.Device,
alias string,
aliasAPI roomserverAPI.RoomserverAliasAPI,
aliasAPI roomserverAPI.RoomserverInternalAPI,
) util.JSONResponse {

creatorQueryReq := roomserverAPI.GetCreatorIDForAliasRequest{
Expand Down
6 changes: 3 additions & 3 deletions clientapi/routing/getevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func GetEvent(
roomID string,
eventID string,
cfg *config.Dendrite,
queryAPI api.RoomserverQueryAPI,
rsAPI api.RoomserverInternalAPI,
federation *gomatrixserverlib.FederationClient,
keyRing gomatrixserverlib.KeyRing,
) util.JSONResponse {
eventsReq := api.QueryEventsByIDRequest{
EventIDs: []string{eventID},
}
var eventsResp api.QueryEventsByIDResponse
err := queryAPI.QueryEventsByID(req.Context(), &eventsReq, &eventsResp)
err := rsAPI.QueryEventsByID(req.Context(), &eventsReq, &eventsResp)
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("queryAPI.QueryEventsByID failed")
return jsonerror.InternalServerError()
Expand Down Expand Up @@ -88,7 +88,7 @@ func GetEvent(
}},
}
var stateResp api.QueryStateAfterEventsResponse
if err := queryAPI.QueryStateAfterEvents(req.Context(), &stateReq, &stateResp); err != nil {
if err := rsAPI.QueryStateAfterEvents(req.Context(), &stateReq, &stateResp); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("queryAPI.QueryStateAfterEvents failed")
return jsonerror.InternalServerError()
}
Expand Down
14 changes: 6 additions & 8 deletions clientapi/routing/joinroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func JoinRoomByIDOrAlias(
cfg *config.Dendrite,
federation *gomatrixserverlib.FederationClient,
producer *producers.RoomserverProducer,
queryAPI roomserverAPI.RoomserverQueryAPI,
aliasAPI roomserverAPI.RoomserverAliasAPI,
rsAPI roomserverAPI.RoomserverInternalAPI,
fsAPI federationSenderAPI.FederationSenderInternalAPI,
keyRing gomatrixserverlib.KeyRing,
accountDB accounts.Database,
Expand Down Expand Up @@ -80,7 +79,7 @@ func JoinRoomByIDOrAlias(

r := joinRoomReq{
req, evTime, content, device.UserID, cfg, federation, producer,
queryAPI, aliasAPI, fsAPI, keyRing,
rsAPI, fsAPI, keyRing,
}

if strings.HasPrefix(roomIDOrAlias, "!") {
Expand All @@ -106,8 +105,7 @@ type joinRoomReq struct {
cfg *config.Dendrite
federation *gomatrixserverlib.FederationClient
producer *producers.RoomserverProducer
queryAPI roomserverAPI.RoomserverQueryAPI
aliasAPI roomserverAPI.RoomserverAliasAPI
rsAPI roomserverAPI.RoomserverInternalAPI
fsAPI federationSenderAPI.FederationSenderInternalAPI
keyRing gomatrixserverlib.KeyRing
}
Expand All @@ -124,7 +122,7 @@ func (r joinRoomReq) joinRoomByID(roomID string) util.JSONResponse {
RoomID: roomID, TargetUserID: r.userID,
}
var queryRes roomserverAPI.QueryInvitesForUserResponse
if err := r.queryAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
if err := r.rsAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
util.GetLogger(r.req.Context()).WithError(err).Error("r.queryAPI.QueryInvitesForUser failed")
return jsonerror.InternalServerError()
}
Expand Down Expand Up @@ -172,7 +170,7 @@ func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse {
if domain == r.cfg.Matrix.ServerName {
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
var queryRes roomserverAPI.GetRoomIDForAliasResponse
if err = r.aliasAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
if err = r.rsAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
util.GetLogger(r.req.Context()).WithError(err).Error("r.aliasAPI.GetRoomIDForAlias failed")
return jsonerror.InternalServerError()
}
Expand Down Expand Up @@ -243,7 +241,7 @@ func (r joinRoomReq) joinRoomUsingServers(
}

queryRes := roomserverAPI.QueryLatestEventsAndStateResponse{}
event, err := common.BuildEvent(r.req.Context(), &eb, r.cfg, r.evTime, r.queryAPI, &queryRes)
event, err := common.BuildEvent(r.req.Context(), &eb, r.cfg, r.evTime, r.rsAPI, &queryRes)
if err == nil {
// If we have successfully built an event at this point then we can
// assert that the room is a local room, as BuildEvent was able to
Expand Down
Loading

0 comments on commit e15f667

Please sign in to comment.