mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-04-10 13:53:40 +00:00
Query AS /alias/ API at a lower level
This commit is contained in:
parent
bb408c53ad
commit
4624dbbae7
11 changed files with 90 additions and 116 deletions
|
@ -21,6 +21,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/appservice/api"
|
"github.com/matrix-org/dendrite/appservice/api"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
@ -48,6 +49,11 @@ func (a *AppServiceQueryAPI) RoomAliasExists(
|
||||||
span, ctx := opentracing.StartSpanFromContext(ctx, "ApplicationServiceRoomAlias")
|
span, ctx := opentracing.StartSpanFromContext(ctx, "ApplicationServiceRoomAlias")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
|
// Create an HTTP client if one does not already exist
|
||||||
|
if a.HTTPClient == nil {
|
||||||
|
a.HTTPClient = makeHTTPClient()
|
||||||
|
}
|
||||||
|
|
||||||
// Determine which application service should handle this request
|
// Determine which application service should handle this request
|
||||||
for _, appservice := range a.Cfg.Derived.ApplicationServices {
|
for _, appservice := range a.Cfg.Derived.ApplicationServices {
|
||||||
if appservice.URL != "" && appservice.IsInterestedInRoomAlias(request.Alias) {
|
if appservice.URL != "" && appservice.IsInterestedInRoomAlias(request.Alias) {
|
||||||
|
@ -96,6 +102,13 @@ func (a *AppServiceQueryAPI) RoomAliasExists(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// makeHTTPClient creates an HTTP client with certain options that will be used for all query requests to application services
|
||||||
|
func makeHTTPClient() *http.Client {
|
||||||
|
return &http.Client{
|
||||||
|
Timeout: time.Second * 30,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetupHTTP adds the AppServiceQueryPAI handlers to the http.ServeMux. This
|
// SetupHTTP adds the AppServiceQueryPAI handlers to the http.ServeMux. This
|
||||||
// handles and muxes incoming api requests the to internal AppServiceQueryAPI.
|
// handles and muxes incoming api requests the to internal AppServiceQueryAPI.
|
||||||
func (a *AppServiceQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
func (a *AppServiceQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
package clientapi
|
package clientapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
||||||
"github.com/matrix-org/dendrite/clientapi/consumers"
|
"github.com/matrix-org/dendrite/clientapi/consumers"
|
||||||
|
@ -39,7 +38,6 @@ func SetupClientAPIComponent(
|
||||||
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
inputAPI roomserverAPI.RoomserverInputAPI,
|
inputAPI roomserverAPI.RoomserverInputAPI,
|
||||||
queryAPI roomserverAPI.RoomserverQueryAPI,
|
queryAPI roomserverAPI.RoomserverQueryAPI,
|
||||||
asAPI appserviceAPI.AppServiceQueryAPI,
|
|
||||||
transactionsCache *transactions.Cache,
|
transactionsCache *transactions.Cache,
|
||||||
) {
|
) {
|
||||||
roomserverProducer := producers.NewRoomserverProducer(inputAPI)
|
roomserverProducer := producers.NewRoomserverProducer(inputAPI)
|
||||||
|
@ -62,7 +60,7 @@ func SetupClientAPIComponent(
|
||||||
}
|
}
|
||||||
|
|
||||||
routing.Setup(
|
routing.Setup(
|
||||||
base.APIMux, *base.Cfg, roomserverProducer, queryAPI, asAPI, aliasAPI,
|
base.APIMux, *base.Cfg, roomserverProducer, queryAPI, aliasAPI,
|
||||||
accountsDB, deviceDB, federation, *keyRing, userUpdateProducer,
|
accountsDB, deviceDB, federation, *keyRing, userUpdateProducer,
|
||||||
syncProducer, transactionsCache,
|
syncProducer, transactionsCache,
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,7 +18,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
"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"
|
||||||
|
@ -30,14 +29,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// DirectoryRoom looks up a room alias
|
// DirectoryRoom looks up a room alias
|
||||||
// nolint: gocyclo
|
|
||||||
func DirectoryRoom(
|
func DirectoryRoom(
|
||||||
req *http.Request,
|
req *http.Request,
|
||||||
roomAlias string,
|
roomAlias string,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
cfg *config.Dendrite,
|
cfg *config.Dendrite,
|
||||||
rsAPI roomserverAPI.RoomserverAliasAPI,
|
rsAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
asAPI appserviceAPI.AppServiceQueryAPI,
|
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -48,41 +45,18 @@ func DirectoryRoom(
|
||||||
}
|
}
|
||||||
|
|
||||||
if domain == cfg.Matrix.ServerName {
|
if domain == cfg.Matrix.ServerName {
|
||||||
queryResp, err := getRoomIDForAlias(req, rsAPI, roomAlias)
|
// Query the roomserver API to check if the alias exists locally
|
||||||
if err != nil {
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
||||||
|
if err = rsAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(req, err)
|
return httputil.LogThenError(req, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List any roomIDs found associated with this alias
|
// List any roomIDs found associated with this alias
|
||||||
if len(queryResp.RoomID) > 0 {
|
if len(queryRes.RoomID) > 0 {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusOK,
|
Code: http.StatusOK,
|
||||||
JSON: queryResp,
|
JSON: queryRes,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// No rooms found locally, try our application services by making a call to
|
|
||||||
// the appservice component
|
|
||||||
aliasReq := appserviceAPI.RoomAliasExistsRequest{Alias: roomAlias}
|
|
||||||
var aliasResp appserviceAPI.RoomAliasExistsResponse
|
|
||||||
err = asAPI.RoomAliasExists(req.Context(), &aliasReq, &aliasResp)
|
|
||||||
if err != nil {
|
|
||||||
return httputil.LogThenError(req, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if aliasResp.AliasExists {
|
|
||||||
// Query the roomserver API again. We should have the room now
|
|
||||||
queryResp, err = getRoomIDForAlias(req, rsAPI, roomAlias)
|
|
||||||
if err != nil {
|
|
||||||
return httputil.LogThenError(req, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// List any roomIDs found associated with this alias
|
|
||||||
if len(queryResp.RoomID) > 0 {
|
|
||||||
return util.JSONResponse{
|
|
||||||
Code: http.StatusOK,
|
|
||||||
JSON: queryResp,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -113,25 +87,6 @@ func DirectoryRoom(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRoomIDForAlias queries the roomserver API and returns a Directory Response
|
|
||||||
// on a successful query
|
|
||||||
func getRoomIDForAlias(
|
|
||||||
req *http.Request,
|
|
||||||
rsAPI roomserverAPI.RoomserverAliasAPI,
|
|
||||||
roomAlias string,
|
|
||||||
) (resp gomatrixserverlib.RespDirectory, err error) {
|
|
||||||
// Query the roomserver API to check if the alias exists locally
|
|
||||||
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
||||||
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
|
||||||
if err = rsAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return gomatrixserverlib.RespDirectory{
|
|
||||||
RoomID: queryRes.RoomID,
|
|
||||||
Servers: []gomatrixserverlib.ServerName{},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
||||||
// TODO: Check if the user has the power level to set an alias
|
// TODO: Check if the user has the power level to set an alias
|
||||||
func SetLocalAlias(
|
func SetLocalAlias(
|
||||||
|
|
|
@ -27,7 +27,7 @@ import (
|
||||||
"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/common"
|
||||||
"github.com/matrix-org/dendrite/common/config"
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
|
@ -42,8 +42,8 @@ func JoinRoomByIDOrAlias(
|
||||||
cfg config.Dendrite,
|
cfg config.Dendrite,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
producer *producers.RoomserverProducer,
|
producer *producers.RoomserverProducer,
|
||||||
queryAPI api.RoomserverQueryAPI,
|
queryAPI roomserverAPI.RoomserverQueryAPI,
|
||||||
aliasAPI api.RoomserverAliasAPI,
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
keyRing gomatrixserverlib.KeyRing,
|
keyRing gomatrixserverlib.KeyRing,
|
||||||
accountDB *accounts.Database,
|
accountDB *accounts.Database,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
|
@ -87,8 +87,8 @@ type joinRoomReq struct {
|
||||||
cfg config.Dendrite
|
cfg config.Dendrite
|
||||||
federation *gomatrixserverlib.FederationClient
|
federation *gomatrixserverlib.FederationClient
|
||||||
producer *producers.RoomserverProducer
|
producer *producers.RoomserverProducer
|
||||||
queryAPI api.RoomserverQueryAPI
|
queryAPI roomserverAPI.RoomserverQueryAPI
|
||||||
aliasAPI api.RoomserverAliasAPI
|
aliasAPI roomserverAPI.RoomserverAliasAPI
|
||||||
keyRing gomatrixserverlib.KeyRing
|
keyRing gomatrixserverlib.KeyRing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,11 +100,10 @@ func (r joinRoomReq) joinRoomByID(roomID string) util.JSONResponse {
|
||||||
// If the server is not in the room the we will need to look up the
|
// If the server is not in the room the we will need to look up the
|
||||||
// remote server the invite came from in order to request a join event
|
// remote server the invite came from in order to request a join event
|
||||||
// from that server.
|
// from that server.
|
||||||
queryReq := api.QueryInvitesForUserRequest{
|
queryReq := roomserverAPI.QueryInvitesForUserRequest{
|
||||||
RoomID: roomID, TargetUserID: r.userID,
|
RoomID: roomID, TargetUserID: r.userID,
|
||||||
}
|
}
|
||||||
fmt.Println(queryReq)
|
var queryRes roomserverAPI.QueryInvitesForUserResponse
|
||||||
var queryRes api.QueryInvitesForUserResponse
|
|
||||||
if err := r.queryAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
|
if err := r.queryAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(r.req, err)
|
return httputil.LogThenError(r.req, err)
|
||||||
}
|
}
|
||||||
|
@ -146,8 +145,8 @@ func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if domain == r.cfg.Matrix.ServerName {
|
if domain == r.cfg.Matrix.ServerName {
|
||||||
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
var queryRes api.GetRoomIDForAliasResponse
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
||||||
if err = r.aliasAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
|
if err = r.aliasAPI.GetRoomIDForAlias(r.req.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(r.req, err)
|
return httputil.LogThenError(r.req, err)
|
||||||
}
|
}
|
||||||
|
@ -215,7 +214,7 @@ func (r joinRoomReq) joinRoomUsingServers(
|
||||||
return httputil.LogThenError(r.req, err)
|
return httputil.LogThenError(r.req, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryRes api.QueryLatestEventsAndStateResponse
|
var queryRes roomserverAPI.QueryLatestEventsAndStateResponse
|
||||||
event, err := common.BuildEvent(r.req.Context(), &eb, r.cfg, r.queryAPI, &queryRes)
|
event, err := common.BuildEvent(r.req.Context(), &eb, r.cfg, r.queryAPI, &queryRes)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if _, err = r.producer.SendEvents(r.req.Context(), []gomatrixserverlib.Event{*event}, r.cfg.Matrix.ServerName, nil); err != nil {
|
if _, err = r.producer.SendEvents(r.req.Context(), []gomatrixserverlib.Event{*event}, r.cfg.Matrix.ServerName, nil); err != nil {
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth"
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||||
|
@ -43,8 +42,9 @@ const pathPrefixUnstable = "/_matrix/client/unstable"
|
||||||
// to clients which need to make outbound HTTP requests.
|
// to clients which need to make outbound HTTP requests.
|
||||||
func Setup(
|
func Setup(
|
||||||
apiMux *mux.Router, cfg config.Dendrite,
|
apiMux *mux.Router, cfg config.Dendrite,
|
||||||
producer *producers.RoomserverProducer, queryAPI roomserverAPI.RoomserverQueryAPI,
|
producer *producers.RoomserverProducer,
|
||||||
appserviceAPI appserviceAPI.AppServiceQueryAPI, aliasAPI roomserverAPI.RoomserverAliasAPI,
|
queryAPI roomserverAPI.RoomserverQueryAPI,
|
||||||
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
accountDB *accounts.Database,
|
accountDB *accounts.Database,
|
||||||
deviceDB *devices.Database,
|
deviceDB *devices.Database,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
|
@ -144,7 +144,7 @@ func Setup(
|
||||||
r0mux.Handle("/directory/room/{roomAlias}",
|
r0mux.Handle("/directory/room/{roomAlias}",
|
||||||
common.MakeExternalAPI("directory_room", func(req *http.Request) util.JSONResponse {
|
common.MakeExternalAPI("directory_room", func(req *http.Request) util.JSONResponse {
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
return DirectoryRoom(req, vars["roomAlias"], federation, &cfg, aliasAPI, appserviceAPI)
|
return DirectoryRoom(req, vars["roomAlias"], federation, &cfg, aliasAPI)
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
|
|
|
@ -34,12 +34,11 @@ func main() {
|
||||||
keyRing := keydb.CreateKeyRing(federation.Client, keyDB)
|
keyRing := keydb.CreateKeyRing(federation.Client, keyDB)
|
||||||
|
|
||||||
alias, input, query := base.CreateHTTPRoomserverAPIs()
|
alias, input, query := base.CreateHTTPRoomserverAPIs()
|
||||||
asQuery := base.CreateHTTPAppServiceAPIs()
|
|
||||||
cache := transactions.New()
|
cache := transactions.New()
|
||||||
|
|
||||||
clientapi.SetupClientAPIComponent(
|
clientapi.SetupClientAPIComponent(
|
||||||
base, deviceDB, accountDB, federation, &keyRing,
|
base, deviceDB, accountDB, federation, &keyRing,
|
||||||
alias, input, query, asQuery, cache,
|
alias, input, query, cache,
|
||||||
)
|
)
|
||||||
|
|
||||||
base.SetupAndServeHTTP(string(base.Cfg.Listen.ClientAPI))
|
base.SetupAndServeHTTP(string(base.Cfg.Listen.ClientAPI))
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/common/keydb"
|
"github.com/matrix-org/dendrite/common/keydb"
|
||||||
"github.com/matrix-org/dendrite/common/transactions"
|
"github.com/matrix-org/dendrite/common/transactions"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/appservice"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi"
|
"github.com/matrix-org/dendrite/clientapi"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/common/basecomponent"
|
"github.com/matrix-org/dendrite/common/basecomponent"
|
||||||
|
@ -55,13 +54,10 @@ func main() {
|
||||||
keyRing := keydb.CreateKeyRing(federation.Client, keyDB)
|
keyRing := keydb.CreateKeyRing(federation.Client, keyDB)
|
||||||
|
|
||||||
alias, input, query := roomserver.SetupRoomServerComponent(base)
|
alias, input, query := roomserver.SetupRoomServerComponent(base)
|
||||||
asQuery := appservice.SetupAppServiceAPIComponent(
|
|
||||||
base, accountDB, deviceDB, federation, alias, query, transactions.New(),
|
|
||||||
)
|
|
||||||
|
|
||||||
clientapi.SetupClientAPIComponent(
|
clientapi.SetupClientAPIComponent(
|
||||||
base, deviceDB, accountDB,
|
base, deviceDB, accountDB,
|
||||||
federation, &keyRing, alias, input, query, asQuery,
|
federation, &keyRing, alias, input, query,
|
||||||
transactions.New(),
|
transactions.New(),
|
||||||
)
|
)
|
||||||
federationapi.SetupFederationAPIComponent(base, accountDB, federation, &keyRing, alias, input, query)
|
federationapi.SetupFederationAPIComponent(base, accountDB, federation, &keyRing, alias, input, query)
|
||||||
|
|
|
@ -21,7 +21,7 @@ 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/common/config"
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
|
@ -32,7 +32,7 @@ func RoomAliasToID(
|
||||||
httpReq *http.Request,
|
httpReq *http.Request,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
cfg config.Dendrite,
|
cfg config.Dendrite,
|
||||||
aliasAPI api.RoomserverAliasAPI,
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
roomAlias := httpReq.FormValue("alias")
|
roomAlias := httpReq.FormValue("alias")
|
||||||
if roomAlias == "" {
|
if roomAlias == "" {
|
||||||
|
@ -52,8 +52,8 @@ func RoomAliasToID(
|
||||||
var resp gomatrixserverlib.RespDirectory
|
var resp gomatrixserverlib.RespDirectory
|
||||||
|
|
||||||
if domain == cfg.Matrix.ServerName {
|
if domain == cfg.Matrix.ServerName {
|
||||||
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
||||||
var queryRes api.GetRoomIDForAliasResponse
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
||||||
if err = aliasAPI.GetRoomIDForAlias(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
if err = aliasAPI.GetRoomIDForAlias(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
||||||
return httputil.LogThenError(httpReq, err)
|
return httputil.LogThenError(httpReq, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ import (
|
||||||
"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/common"
|
||||||
"github.com/matrix-org/dendrite/common/config"
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
roomserverAPI "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"
|
||||||
)
|
)
|
||||||
|
@ -37,8 +37,8 @@ const (
|
||||||
func Setup(
|
func Setup(
|
||||||
apiMux *mux.Router,
|
apiMux *mux.Router,
|
||||||
cfg config.Dendrite,
|
cfg config.Dendrite,
|
||||||
query api.RoomserverQueryAPI,
|
query roomserverAPI.RoomserverQueryAPI,
|
||||||
aliasAPI api.RoomserverAliasAPI,
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
||||||
producer *producers.RoomserverProducer,
|
producer *producers.RoomserverProducer,
|
||||||
keys gomatrixserverlib.KeyRing,
|
keys gomatrixserverlib.KeyRing,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
|
|
|
@ -21,9 +21,10 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/common/config"
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
roomserverAPI "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"
|
||||||
)
|
)
|
||||||
|
@ -46,17 +47,18 @@ type RoomserverAliasAPIDatabase interface {
|
||||||
|
|
||||||
// RoomserverAliasAPI is an implementation of alias.RoomserverAliasAPI
|
// RoomserverAliasAPI is an implementation of alias.RoomserverAliasAPI
|
||||||
type RoomserverAliasAPI struct {
|
type RoomserverAliasAPI struct {
|
||||||
DB RoomserverAliasAPIDatabase
|
DB RoomserverAliasAPIDatabase
|
||||||
Cfg *config.Dendrite
|
Cfg *config.Dendrite
|
||||||
InputAPI api.RoomserverInputAPI
|
InputAPI roomserverAPI.RoomserverInputAPI
|
||||||
QueryAPI api.RoomserverQueryAPI
|
QueryAPI roomserverAPI.RoomserverQueryAPI
|
||||||
|
AppserviceAPI appserviceAPI.AppServiceQueryAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRoomAlias implements alias.RoomserverAliasAPI
|
// SetRoomAlias implements alias.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) SetRoomAlias(
|
func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.SetRoomAliasRequest,
|
request *roomserverAPI.SetRoomAliasRequest,
|
||||||
response *api.SetRoomAliasResponse,
|
response *roomserverAPI.SetRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Check if the alias isn't already referring to a room
|
// Check if the alias isn't already referring to a room
|
||||||
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
|
@ -85,8 +87,8 @@ func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||||
// GetRoomIDForAlias implements alias.RoomserverAliasAPI
|
// GetRoomIDForAlias implements alias.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) GetRoomIDForAlias(
|
func (r *RoomserverAliasAPI) GetRoomIDForAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.GetRoomIDForAliasRequest,
|
request *roomserverAPI.GetRoomIDForAliasRequest,
|
||||||
response *api.GetRoomIDForAliasResponse,
|
response *roomserverAPI.GetRoomIDForAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Look up the room ID in the database
|
// Look up the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
|
@ -94,6 +96,14 @@ func (r *RoomserverAliasAPI) GetRoomIDForAlias(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No rooms found locally, try our application services by making a call to
|
||||||
|
// the appservice component
|
||||||
|
aliasReq := appserviceAPI.RoomAliasExistsRequest{Alias: request.Alias}
|
||||||
|
var aliasResp appserviceAPI.RoomAliasExistsResponse
|
||||||
|
if err = r.AppserviceAPI.RoomAliasExists(ctx, &aliasReq, &aliasResp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
response.RoomID = roomID
|
response.RoomID = roomID
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -101,8 +111,8 @@ func (r *RoomserverAliasAPI) GetRoomIDForAlias(
|
||||||
// GetAliasesForRoomID implements alias.RoomserverAliasAPI
|
// GetAliasesForRoomID implements alias.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) GetAliasesForRoomID(
|
func (r *RoomserverAliasAPI) GetAliasesForRoomID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.GetAliasesForRoomIDRequest,
|
request *roomserverAPI.GetAliasesForRoomIDRequest,
|
||||||
response *api.GetAliasesForRoomIDResponse,
|
response *roomserverAPI.GetAliasesForRoomIDResponse,
|
||||||
) error {
|
) error {
|
||||||
// Look up the aliases in the database for the given RoomID
|
// Look up the aliases in the database for the given RoomID
|
||||||
aliases, err := r.DB.GetAliasesForRoomID(ctx, request.RoomID)
|
aliases, err := r.DB.GetAliasesForRoomID(ctx, request.RoomID)
|
||||||
|
@ -117,8 +127,8 @@ func (r *RoomserverAliasAPI) GetAliasesForRoomID(
|
||||||
// RemoveRoomAlias implements alias.RoomserverAliasAPI
|
// RemoveRoomAlias implements alias.RoomserverAliasAPI
|
||||||
func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
request *api.RemoveRoomAliasRequest,
|
request *roomserverAPI.RemoveRoomAliasRequest,
|
||||||
response *api.RemoveRoomAliasResponse,
|
response *roomserverAPI.RemoveRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Look up the room ID in the database
|
// Look up the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
||||||
|
@ -177,11 +187,11 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req := api.QueryLatestEventsAndStateRequest{
|
req := roomserverAPI.QueryLatestEventsAndStateRequest{
|
||||||
RoomID: roomID,
|
RoomID: roomID,
|
||||||
StateToFetch: eventsNeeded.Tuples(),
|
StateToFetch: eventsNeeded.Tuples(),
|
||||||
}
|
}
|
||||||
var res api.QueryLatestEventsAndStateResponse
|
var res roomserverAPI.QueryLatestEventsAndStateResponse
|
||||||
if err = r.QueryAPI.QueryLatestEventsAndState(ctx, &req, &res); err != nil {
|
if err = r.QueryAPI.QueryLatestEventsAndState(ctx, &req, &res); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -213,16 +223,16 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the request
|
// Create the request
|
||||||
ire := api.InputRoomEvent{
|
ire := roomserverAPI.InputRoomEvent{
|
||||||
Kind: api.KindNew,
|
Kind: roomserverAPI.KindNew,
|
||||||
Event: event,
|
Event: event,
|
||||||
AuthEventIDs: event.AuthEventIDs(),
|
AuthEventIDs: event.AuthEventIDs(),
|
||||||
SendAsServer: serverName,
|
SendAsServer: serverName,
|
||||||
}
|
}
|
||||||
inputReq := api.InputRoomEventsRequest{
|
inputReq := roomserverAPI.InputRoomEventsRequest{
|
||||||
InputRoomEvents: []api.InputRoomEvent{ire},
|
InputRoomEvents: []roomserverAPI.InputRoomEvent{ire},
|
||||||
}
|
}
|
||||||
var inputRes api.InputRoomEventsResponse
|
var inputRes roomserverAPI.InputRoomEventsResponse
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
return r.InputAPI.InputRoomEvents(ctx, &inputReq, &inputRes)
|
return r.InputAPI.InputRoomEvents(ctx, &inputReq, &inputRes)
|
||||||
|
@ -231,10 +241,10 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(
|
||||||
// SetupHTTP adds the RoomserverAliasAPI handlers to the http.ServeMux.
|
// SetupHTTP adds the RoomserverAliasAPI handlers to the http.ServeMux.
|
||||||
func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
servMux.Handle(
|
servMux.Handle(
|
||||||
api.RoomserverSetRoomAliasPath,
|
roomserverAPI.RoomserverSetRoomAliasPath,
|
||||||
common.MakeInternalAPI("setRoomAlias", func(req *http.Request) util.JSONResponse {
|
common.MakeInternalAPI("setRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||||
var request api.SetRoomAliasRequest
|
var request roomserverAPI.SetRoomAliasRequest
|
||||||
var response api.SetRoomAliasResponse
|
var response roomserverAPI.SetRoomAliasResponse
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
@ -245,10 +255,10 @@ func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
servMux.Handle(
|
servMux.Handle(
|
||||||
api.RoomserverGetRoomIDForAliasPath,
|
roomserverAPI.RoomserverGetRoomIDForAliasPath,
|
||||||
common.MakeInternalAPI("GetRoomIDForAlias", func(req *http.Request) util.JSONResponse {
|
common.MakeInternalAPI("GetRoomIDForAlias", func(req *http.Request) util.JSONResponse {
|
||||||
var request api.GetRoomIDForAliasRequest
|
var request roomserverAPI.GetRoomIDForAliasRequest
|
||||||
var response api.GetRoomIDForAliasResponse
|
var response roomserverAPI.GetRoomIDForAliasResponse
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
@ -259,10 +269,10 @@ func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
servMux.Handle(
|
servMux.Handle(
|
||||||
api.RoomserverRemoveRoomAliasPath,
|
roomserverAPI.RoomserverRemoveRoomAliasPath,
|
||||||
common.MakeInternalAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse {
|
common.MakeInternalAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||||
var request api.RemoveRoomAliasRequest
|
var request roomserverAPI.RemoveRoomAliasRequest
|
||||||
var response api.RemoveRoomAliasResponse
|
var response roomserverAPI.RemoveRoomAliasResponse
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.ErrorResponse(err)
|
return util.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
|
||||||
|
asQuery "github.com/matrix-org/dendrite/appservice/query"
|
||||||
"github.com/matrix-org/dendrite/common/basecomponent"
|
"github.com/matrix-org/dendrite/common/basecomponent"
|
||||||
"github.com/matrix-org/dendrite/roomserver/alias"
|
"github.com/matrix-org/dendrite/roomserver/alias"
|
||||||
"github.com/matrix-org/dendrite/roomserver/input"
|
"github.com/matrix-org/dendrite/roomserver/input"
|
||||||
|
@ -51,11 +52,14 @@ func SetupRoomServerComponent(
|
||||||
|
|
||||||
queryAPI.SetupHTTP(http.DefaultServeMux)
|
queryAPI.SetupHTTP(http.DefaultServeMux)
|
||||||
|
|
||||||
|
asAPI := asQuery.AppServiceQueryAPI{Cfg: base.Cfg}
|
||||||
|
|
||||||
aliasAPI := alias.RoomserverAliasAPI{
|
aliasAPI := alias.RoomserverAliasAPI{
|
||||||
DB: roomserverDB,
|
DB: roomserverDB,
|
||||||
Cfg: base.Cfg,
|
Cfg: base.Cfg,
|
||||||
InputAPI: &inputAPI,
|
InputAPI: &inputAPI,
|
||||||
QueryAPI: &queryAPI,
|
QueryAPI: &queryAPI,
|
||||||
|
AppserviceAPI: &asAPI,
|
||||||
}
|
}
|
||||||
|
|
||||||
aliasAPI.SetupHTTP(http.DefaultServeMux)
|
aliasAPI.SetupHTTP(http.DefaultServeMux)
|
||||||
|
|
Loading…
Reference in a new issue