From 0b5ae4692ef851c672f4292dd45690baf29b9d35 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Mon, 20 Aug 2018 02:45:17 -0700 Subject: [PATCH] Make use of /users/{userID} in relevant APIs (#522) * Query whether a room alias exists on app services Signed-off-by: Andrew Morgan * Query AS /alias/ API at a lower level * Add support for querying /users/ on appservices * Have endpoints query AS users if not found locally --- .../dendrite/appservice/appservice.go | 2 +- .../dendrite/clientapi/clientapi.go | 4 +- .../dendrite/clientapi/routing/createroom.go | 22 ++++----- .../dendrite/clientapi/routing/membership.go | 26 +++++++---- .../dendrite/clientapi/routing/profile.go | 46 ++++--------------- .../dendrite/clientapi/routing/routing.go | 12 +++-- .../cmd/dendrite-client-api-server/main.go | 3 +- .../dendrite-federation-api-server/main.go | 3 +- .../cmd/dendrite-monolith-server/main.go | 10 ++-- .../dendrite/federationapi/federationapi.go | 13 ++++-- .../dendrite/federationapi/routing/profile.go | 14 ++---- .../dendrite/federationapi/routing/routing.go | 6 ++- .../federationapi/routing/threepid.go | 23 ++++++---- 13 files changed, 89 insertions(+), 95 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/appservice/appservice.go b/src/github.com/matrix-org/dendrite/appservice/appservice.go index e33793d2f..8703959f8 100644 --- a/src/github.com/matrix-org/dendrite/appservice/appservice.go +++ b/src/github.com/matrix-org/dendrite/appservice/appservice.go @@ -90,7 +90,7 @@ func SetupAppServiceAPIComponent( roomserverQueryAPI, roomserverAliasAPI, workerStates, ) if err := consumer.Start(); err != nil { - logrus.WithError(err).Panicf("failed to start app service roomserver consumer") + logrus.WithError(err).Panicf("failed to start appservice roomserver consumer") } // Create application service transaction workers diff --git a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go index 362e251c1..5b6e21c80 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go +++ b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go @@ -15,6 +15,7 @@ package clientapi 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/devices" "github.com/matrix-org/dendrite/clientapi/consumers" @@ -40,6 +41,7 @@ func SetupClientAPIComponent( inputAPI roomserverAPI.RoomserverInputAPI, queryAPI roomserverAPI.RoomserverQueryAPI, typingInputAPI typingServerAPI.TypingServerInputAPI, + asAPI appserviceAPI.AppServiceQueryAPI, transactionsCache *transactions.Cache, ) { roomserverProducer := producers.NewRoomserverProducer(inputAPI) @@ -63,7 +65,7 @@ func SetupClientAPIComponent( } routing.Setup( - base.APIMux, *base.Cfg, roomserverProducer, queryAPI, aliasAPI, + base.APIMux, *base.Cfg, roomserverProducer, queryAPI, aliasAPI, asAPI, accountsDB, deviceDB, federation, *keyRing, userUpdateProducer, syncProducer, typingProducer, transactionsCache, ) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index 9924f9725..b531a7bd0 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -20,7 +20,8 @@ import ( "strings" "time" - "github.com/matrix-org/dendrite/roomserver/api" + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -116,12 +117,13 @@ type fledglingEvent struct { func CreateRoom( req *http.Request, device *authtypes.Device, cfg config.Dendrite, producer *producers.RoomserverProducer, - accountDB *accounts.Database, aliasAPI api.RoomserverAliasAPI, + accountDB *accounts.Database, aliasAPI roomserverAPI.RoomserverAliasAPI, + 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) + return createRoom(req, device, cfg, roomID, producer, accountDB, aliasAPI, asAPI) } // createRoom implements /createRoom @@ -129,7 +131,8 @@ func CreateRoom( func createRoom( req *http.Request, device *authtypes.Device, cfg config.Dendrite, roomID string, producer *producers.RoomserverProducer, - accountDB *accounts.Database, aliasAPI api.RoomserverAliasAPI, + accountDB *accounts.Database, aliasAPI roomserverAPI.RoomserverAliasAPI, + asAPI appserviceAPI.AppServiceQueryAPI, ) util.JSONResponse { logger := util.GetLogger(req.Context()) userID := device.UserID @@ -154,12 +157,7 @@ func createRoom( "roomID": roomID, }).Info("Creating new room") - localpart, _, err := gomatrixserverlib.SplitID('@', userID) - if err != nil { - return httputil.LogThenError(req, err) - } - - profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) + profile, err := appserviceAPI.RetreiveUserProfile(req.Context(), userID, asAPI, accountDB) if err != nil { return httputil.LogThenError(req, err) } @@ -280,13 +278,13 @@ func createRoom( if r.RoomAliasName != "" { roomAlias = fmt.Sprintf("#%s:%s", r.RoomAliasName, cfg.Matrix.ServerName) - aliasReq := api.SetRoomAliasRequest{ + aliasReq := roomserverAPI.SetRoomAliasRequest{ Alias: roomAlias, RoomID: roomID, UserID: userID, } - var aliasResp api.SetRoomAliasResponse + var aliasResp roomserverAPI.SetRoomAliasResponse err = aliasAPI.SetRoomAlias(req.Context(), &aliasReq, &aliasResp) if err != nil { return httputil.LogThenError(req, err) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go index d134b9fb1..d00369aa9 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/membership.go @@ -20,6 +20,7 @@ import ( "net/http" "time" + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -28,7 +29,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/threepid" "github.com/matrix-org/dendrite/common" "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/util" @@ -41,7 +42,8 @@ var errMissingUserID = errors.New("'user_id' must be supplied") func SendMembership( req *http.Request, accountDB *accounts.Database, device *authtypes.Device, roomID string, membership string, cfg config.Dendrite, - queryAPI api.RoomserverQueryAPI, producer *producers.RoomserverProducer, + queryAPI roomserverAPI.RoomserverQueryAPI, asAPI appserviceAPI.AppServiceQueryAPI, + producer *producers.RoomserverProducer, ) util.JSONResponse { var body threepid.MembershipRequest if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil { @@ -83,7 +85,7 @@ func SendMembership( } event, err := buildMembershipEvent( - req.Context(), body, accountDB, device, membership, roomID, cfg, evTime, queryAPI, + req.Context(), body, accountDB, device, membership, roomID, cfg, evTime, queryAPI, asAPI, ) if err == errMissingUserID { return util.JSONResponse{ @@ -114,15 +116,17 @@ func SendMembership( func buildMembershipEvent( ctx context.Context, body threepid.MembershipRequest, accountDB *accounts.Database, - device *authtypes.Device, membership string, roomID string, cfg config.Dendrite, - evTime time.Time, queryAPI api.RoomserverQueryAPI, + device *authtypes.Device, + membership, roomID string, + cfg config.Dendrite, evTime time.Time, + queryAPI roomserverAPI.RoomserverQueryAPI, asAPI appserviceAPI.AppServiceQueryAPI, ) (*gomatrixserverlib.Event, error) { stateKey, reason, err := getMembershipStateKey(body, device, membership) if err != nil { return nil, err } - profile, err := loadProfile(ctx, stateKey, cfg, accountDB) + profile, err := loadProfile(ctx, stateKey, cfg, accountDB, asAPI) if err != nil { return nil, err } @@ -158,16 +162,20 @@ func buildMembershipEvent( // Returns an error if the retrieval failed or if the first parameter isn't a // valid Matrix ID. func loadProfile( - ctx context.Context, userID string, cfg config.Dendrite, accountDB *accounts.Database, + ctx context.Context, + userID string, + cfg config.Dendrite, + accountDB *accounts.Database, + asAPI appserviceAPI.AppServiceQueryAPI, ) (*authtypes.Profile, error) { - localpart, serverName, err := gomatrixserverlib.SplitID('@', userID) + _, serverName, err := gomatrixserverlib.SplitID('@', userID) if err != nil { return nil, err } var profile *authtypes.Profile if serverName == cfg.Matrix.ServerName { - profile, err = accountDB.GetProfileByLocalpart(ctx, localpart) + profile, err = appserviceAPI.RetreiveUserProfile(ctx, userID, asAPI, accountDB) } else { profile = &authtypes.Profile{} } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go index 2cb60896c..1469da6f8 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go @@ -16,10 +16,10 @@ package routing import ( "context" - "database/sql" "net/http" "time" + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -35,7 +35,7 @@ import ( // GetProfile implements GET /profile/{userID} func GetProfile( - req *http.Request, accountDB *accounts.Database, userID string, + req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI, ) util.JSONResponse { if req.Method != http.MethodGet { return util.JSONResponse{ @@ -43,10 +43,9 @@ func GetProfile( JSON: jsonerror.NotFound("Bad method"), } } - - profile, err := getProfileByUserID(req, accountDB, userID) + profile, err := appserviceAPI.RetreiveUserProfile(req.Context(), userID, asAPI, accountDB) if err != nil { - return *err + return httputil.LogThenError(req, err) } res := common.ProfileResponse{ @@ -59,37 +58,13 @@ func GetProfile( } } -// getProfileByUserID returns the profile for userID, otherwise returns an error response -func getProfileByUserID( - req *http.Request, accountDB *accounts.Database, userID string, -) (*authtypes.Profile, *util.JSONResponse) { - localpart, _, err := gomatrixserverlib.SplitID('@', userID) - if err != nil { - resErr := httputil.LogThenError(req, err) - return nil, &resErr - } - - profile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) - if err == sql.ErrNoRows { - return nil, &util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"), - } - } else if err != nil { - resErr := httputil.LogThenError(req, err) - return nil, &resErr - } - - return profile, nil -} - // GetAvatarURL implements GET /profile/{userID}/avatar_url func GetAvatarURL( - req *http.Request, accountDB *accounts.Database, userID string, + req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI, ) util.JSONResponse { - profile, err := getProfileByUserID(req, accountDB, userID) + profile, err := appserviceAPI.RetreiveUserProfile(req.Context(), userID, asAPI, accountDB) if err != nil { - return *err + return httputil.LogThenError(req, err) } res := common.AvatarURL{ @@ -175,13 +150,12 @@ func SetAvatarURL( // GetDisplayName implements GET /profile/{userID}/displayname func GetDisplayName( - req *http.Request, accountDB *accounts.Database, userID string, + req *http.Request, accountDB *accounts.Database, userID string, asAPI appserviceAPI.AppServiceQueryAPI, ) util.JSONResponse { - profile, err := getProfileByUserID(req, accountDB, userID) + profile, err := appserviceAPI.RetreiveUserProfile(req.Context(), userID, asAPI, accountDB) if err != nil { - return *err + return httputil.LogThenError(req, err) } - res := common.DisplayName{ DisplayName: profile.DisplayName, } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 6081fc4a8..1d95ffe72 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -20,6 +20,7 @@ import ( "strings" "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/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -45,6 +46,7 @@ func Setup( producer *producers.RoomserverProducer, queryAPI roomserverAPI.RoomserverQueryAPI, aliasAPI roomserverAPI.RoomserverAliasAPI, + asAPI appserviceAPI.AppServiceQueryAPI, accountDB *accounts.Database, deviceDB *devices.Database, federation *gomatrixserverlib.FederationClient, @@ -79,7 +81,7 @@ func Setup( r0mux.Handle("/createRoom", common.MakeAuthAPI("createRoom", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI) + return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI, asAPI) }), ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/join/{roomIDOrAlias}", @@ -93,7 +95,7 @@ func Setup( r0mux.Handle("/rooms/{roomID}/{membership:(?:join|kick|ban|unban|leave|invite)}", common.MakeAuthAPI("membership", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { vars := mux.Vars(req) - return SendMembership(req, accountDB, device, vars["roomID"], vars["membership"], cfg, queryAPI, producer) + return SendMembership(req, accountDB, device, vars["roomID"], vars["membership"], cfg, queryAPI, asAPI, producer) }), ).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/rooms/{roomID}/send/{eventType}", @@ -234,14 +236,14 @@ func Setup( r0mux.Handle("/profile/{userID}", common.MakeExternalAPI("profile", func(req *http.Request) util.JSONResponse { vars := mux.Vars(req) - return GetProfile(req, accountDB, vars["userID"]) + return GetProfile(req, accountDB, vars["userID"], asAPI) }), ).Methods(http.MethodGet, http.MethodOptions) r0mux.Handle("/profile/{userID}/avatar_url", common.MakeExternalAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse { vars := mux.Vars(req) - return GetAvatarURL(req, accountDB, vars["userID"]) + return GetAvatarURL(req, accountDB, vars["userID"], asAPI) }), ).Methods(http.MethodGet, http.MethodOptions) @@ -257,7 +259,7 @@ func Setup( r0mux.Handle("/profile/{userID}/displayname", common.MakeExternalAPI("profile_displayname", func(req *http.Request) util.JSONResponse { vars := mux.Vars(req) - return GetDisplayName(req, accountDB, vars["userID"]) + return GetDisplayName(req, accountDB, vars["userID"], asAPI) }), ).Methods(http.MethodGet, http.MethodOptions) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go index 5c844de49..dd0656441 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-client-api-server/main.go @@ -35,12 +35,13 @@ func main() { federation := base.CreateFederationClient() keyRing := keydb.CreateKeyRing(federation.Client, keyDB) + asQuery := base.CreateHTTPAppServiceAPIs() alias, input, query := base.CreateHTTPRoomserverAPIs() typingInputAPI := typingserver.SetupTypingServerComponent(base, cache.NewTypingCache()) clientapi.SetupClientAPIComponent( base, deviceDB, accountDB, federation, &keyRing, - alias, input, query, typingInputAPI, transactions.New(), + alias, input, query, typingInputAPI, asQuery, transactions.New(), ) base.SetupAndServeHTTP(string(base.Cfg.Listen.ClientAPI)) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go index d843c143c..014ed3343 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go @@ -32,10 +32,11 @@ func main() { keyRing := keydb.CreateKeyRing(federation.Client, keyDB) alias, input, query := base.CreateHTTPRoomserverAPIs() + asQuery := base.CreateHTTPAppServiceAPIs() federationapi.SetupFederationAPIComponent( base, accountDB, deviceDB, federation, &keyRing, - alias, input, query, + alias, input, query, asQuery, ) base.SetupAndServeHTTP(string(base.Cfg.Listen.FederationAPI)) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go index 268224015..87a625b8b 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go @@ -18,6 +18,7 @@ import ( "flag" "net/http" + "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/clientapi" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/basecomponent" @@ -56,13 +57,16 @@ func main() { alias, input, query := roomserver.SetupRoomServerComponent(base) typingInputAPI := typingserver.SetupTypingServerComponent(base, cache.NewTypingCache()) + asQuery := appservice.SetupAppServiceAPIComponent( + base, accountDB, deviceDB, federation, alias, query, transactions.New(), + ) clientapi.SetupClientAPIComponent( base, deviceDB, accountDB, - federation, &keyRing, alias, input, query, typingInputAPI, - transactions.New(), + federation, &keyRing, alias, input, query, + typingInputAPI, asQuery, transactions.New(), ) - federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, &keyRing, alias, input, query) + federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, &keyRing, alias, input, query, asQuery) federationsender.SetupFederationSenderComponent(base, federation, query) mediaapi.SetupMediaAPIComponent(base, deviceDB) publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB) diff --git a/src/github.com/matrix-org/dendrite/federationapi/federationapi.go b/src/github.com/matrix-org/dendrite/federationapi/federationapi.go index 4c8d5de3f..87402d976 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/federationapi.go +++ b/src/github.com/matrix-org/dendrite/federationapi/federationapi.go @@ -15,10 +15,12 @@ package federationapi 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/devices" "github.com/matrix-org/dendrite/common/basecomponent" - "github.com/matrix-org/dendrite/roomserver/api" + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" + // TODO: Are we really wanting to pull in the producer from clientapi "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/federationapi/routing" @@ -33,14 +35,15 @@ func SetupFederationAPIComponent( deviceDB *devices.Database, federation *gomatrixserverlib.FederationClient, keyRing *gomatrixserverlib.KeyRing, - aliasAPI api.RoomserverAliasAPI, - inputAPI api.RoomserverInputAPI, - queryAPI api.RoomserverQueryAPI, + aliasAPI roomserverAPI.RoomserverAliasAPI, + inputAPI roomserverAPI.RoomserverInputAPI, + queryAPI roomserverAPI.RoomserverQueryAPI, + asAPI appserviceAPI.AppServiceQueryAPI, ) { roomserverProducer := producers.NewRoomserverProducer(inputAPI) routing.Setup( - base.APIMux, *base.Cfg, queryAPI, aliasAPI, + base.APIMux, *base.Cfg, queryAPI, aliasAPI, asAPI, roomserverProducer, *keyRing, federation, accountsDB, deviceDB, ) } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go index c52051380..aa4fcdc42 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go @@ -15,9 +15,9 @@ package routing import ( - "database/sql" "net/http" + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -32,6 +32,7 @@ func GetProfile( httpReq *http.Request, accountDB *accounts.Database, cfg config.Dendrite, + asAPI appserviceAPI.AppServiceQueryAPI, ) util.JSONResponse { userID, field := httpReq.FormValue("user_id"), httpReq.FormValue("field") @@ -43,7 +44,7 @@ func GetProfile( } } - localpart, domain, err := gomatrixserverlib.SplitID('@', userID) + _, domain, err := gomatrixserverlib.SplitID('@', userID) if err != nil { return httputil.LogThenError(httpReq, err) } @@ -52,13 +53,8 @@ func GetProfile( return httputil.LogThenError(httpReq, err) } - profile, err := accountDB.GetProfileByLocalpart(httpReq.Context(), localpart) - if err == sql.ErrNoRows { - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("no profile information for this user or this user does not exist"), - } - } else if err != nil { + profile, err := appserviceAPI.RetreiveUserProfile(httpReq.Context(), userID, asAPI, accountDB) + if err != nil { return httputil.LogThenError(httpReq, err) } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index 463208368..c7c0f7e21 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -18,6 +18,7 @@ import ( "net/http" "github.com/gorilla/mux" + 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/devices" "github.com/matrix-org/dendrite/clientapi/producers" @@ -39,6 +40,7 @@ func Setup( cfg config.Dendrite, query roomserverAPI.RoomserverQueryAPI, aliasAPI roomserverAPI.RoomserverAliasAPI, + asAPI appserviceAPI.AppServiceQueryAPI, producer *producers.RoomserverProducer, keys gomatrixserverlib.KeyRing, federation *gomatrixserverlib.FederationClient, @@ -83,7 +85,7 @@ func Setup( v1fedmux.Handle("/3pid/onbind", common.MakeExternalAPI("3pid_onbind", func(req *http.Request) util.JSONResponse { - return CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation, accountDB) + return CreateInvitesFrom3PIDInvites(req, query, asAPI, cfg, producer, federation, accountDB) }, )).Methods(http.MethodPost, http.MethodOptions) @@ -140,7 +142,7 @@ func Setup( "federation_query_profile", cfg.Matrix.ServerName, keys, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse { return GetProfile( - httpReq, accountDB, cfg, + httpReq, accountDB, cfg, asAPI, ) }, )).Methods(http.MethodGet) diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/threepid.go b/src/github.com/matrix-org/dendrite/federationapi/routing/threepid.go index 06bebc62f..1c952c80a 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/threepid.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/threepid.go @@ -22,13 +22,14 @@ import ( "net/http" "time" + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "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/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/util" @@ -58,7 +59,8 @@ var ( // CreateInvitesFrom3PIDInvites implements POST /_matrix/federation/v1/3pid/onbind func CreateInvitesFrom3PIDInvites( - req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite, + req *http.Request, queryAPI roomserverAPI.RoomserverQueryAPI, + asAPI appserviceAPI.AppServiceQueryAPI, cfg config.Dendrite, producer *producers.RoomserverProducer, federation *gomatrixserverlib.FederationClient, accountDB *accounts.Database, ) util.JSONResponse { @@ -70,7 +72,7 @@ func CreateInvitesFrom3PIDInvites( evs := []gomatrixserverlib.Event{} for _, inv := range body.Invites { event, err := createInviteFrom3PIDInvite( - req.Context(), queryAPI, cfg, inv, federation, accountDB, + req.Context(), queryAPI, asAPI, cfg, inv, federation, accountDB, ) if err != nil { return httputil.LogThenError(req, err) @@ -96,7 +98,7 @@ func ExchangeThirdPartyInvite( httpReq *http.Request, request *gomatrixserverlib.FederationRequest, roomID string, - queryAPI api.RoomserverQueryAPI, + queryAPI roomserverAPI.RoomserverQueryAPI, cfg config.Dendrite, federation *gomatrixserverlib.FederationClient, producer *producers.RoomserverProducer, @@ -170,11 +172,12 @@ func ExchangeThirdPartyInvite( // Returns an error if there was a problem building the event or fetching the // necessary data to do so. func createInviteFrom3PIDInvite( - ctx context.Context, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite, + ctx context.Context, queryAPI roomserverAPI.RoomserverQueryAPI, + asAPI appserviceAPI.AppServiceQueryAPI, cfg config.Dendrite, inv invite, federation *gomatrixserverlib.FederationClient, accountDB *accounts.Database, ) (*gomatrixserverlib.Event, error) { - localpart, server, err := gomatrixserverlib.SplitID('@', inv.MXID) + _, server, err := gomatrixserverlib.SplitID('@', inv.MXID) if err != nil { return nil, err } @@ -191,7 +194,7 @@ func createInviteFrom3PIDInvite( StateKey: &inv.MXID, } - profile, err := accountDB.GetProfileByLocalpart(ctx, localpart) + profile, err := appserviceAPI.RetreiveUserProfile(ctx, inv.MXID, asAPI, accountDB) if err != nil { return nil, err } @@ -227,7 +230,7 @@ func createInviteFrom3PIDInvite( // Returns an error if something failed during the process. func buildMembershipEvent( ctx context.Context, - builder *gomatrixserverlib.EventBuilder, queryAPI api.RoomserverQueryAPI, + builder *gomatrixserverlib.EventBuilder, queryAPI roomserverAPI.RoomserverQueryAPI, cfg config.Dendrite, ) (*gomatrixserverlib.Event, error) { eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder) @@ -236,11 +239,11 @@ func buildMembershipEvent( } // Ask the roomserver for information about this room - queryReq := api.QueryLatestEventsAndStateRequest{ + queryReq := roomserverAPI.QueryLatestEventsAndStateRequest{ RoomID: builder.RoomID, StateToFetch: eventsNeeded.Tuples(), } - var queryRes api.QueryLatestEventsAndStateResponse + var queryRes roomserverAPI.QueryLatestEventsAndStateResponse if err = queryAPI.QueryLatestEventsAndState(ctx, &queryReq, &queryRes); err != nil { return nil, err }