From 79adba43f0450365a8b27d3363b240be6423104a Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 13 Sep 2017 13:31:46 +0100 Subject: [PATCH] Append target profile to invite events created from a 3PID invite (#227) * Append target profile to invite events created from a 3PID invite * Don't redeclare err * Add check on invited server --- .../dendrite-federation-api-server/main.go | 8 ++++- .../cmd/dendrite-monolith-server/main.go | 1 + .../dendrite/federationapi/routing/routing.go | 4 ++- .../federationapi/writers/threepid.go | 31 ++++++++++++++++--- 4 files changed, 37 insertions(+), 7 deletions(-) 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 70fc144e..151a7a9f 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 @@ -20,6 +20,7 @@ import ( "os" "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" @@ -58,6 +59,11 @@ func main() { log.Panicf("Failed to setup key database(%q): %s", cfg.Database.ServerKey, err.Error()) } + accountDB, err := accounts.NewDatabase(string(cfg.Database.Account), cfg.Matrix.ServerName) + if err != nil { + log.Panicf("Failed to setup account database(%q): %s", cfg.Database.Account, err.Error()) + } + keyRing := gomatrixserverlib.KeyRing{ KeyFetchers: []gomatrixserverlib.KeyFetcher{ // TODO: Use perspective key fetchers for production. @@ -78,7 +84,7 @@ func main() { log.Info("Starting federation API server on ", cfg.Listen.FederationAPI) api := mux.NewRouter() - routing.Setup(api, *cfg, queryAPI, roomserverProducer, keyRing, federation) + routing.Setup(api, *cfg, queryAPI, roomserverProducer, keyRing, federation, accountDB) common.SetupHTTPAPI(http.DefaultServeMux, api) log.Fatal(http.ListenAndServe(string(cfg.Listen.FederationAPI), nil)) 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 dcbec023..1a4c3a46 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 @@ -333,6 +333,7 @@ func (m *monolith) setupAPIs() { federationapi_routing.Setup( m.api, *m.cfg, m.queryAPI, m.roomServerProducer, m.keyRing, m.federation, + m.accountDB, ) publicroomsapi_routing.Setup(m.api, m.deviceDB, m.publicRoomsAPIDB) 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 b3f6bda6..085cb771 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -19,6 +19,7 @@ import ( "time" "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" @@ -42,6 +43,7 @@ func Setup( producer *producers.RoomserverProducer, keys gomatrixserverlib.KeyRing, federation *gomatrixserverlib.FederationClient, + accountDB *accounts.Database, ) { v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter() v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter() @@ -81,7 +83,7 @@ func Setup( v1fedmux.Handle("/3pid/onbind", common.MakeAPI("3pid_onbind", func(req *http.Request) util.JSONResponse { - return writers.CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation) + return writers.CreateInvitesFrom3PIDInvites(req, query, cfg, producer, federation, accountDB) }, )).Methods("POST", "OPTIONS") diff --git a/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go b/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go index c119c32b..8215e1df 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go +++ b/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go @@ -22,6 +22,7 @@ import ( "net/http" "time" + "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" @@ -50,12 +51,16 @@ type invites struct { Invites []invite `json:"invites"` } -var errNotInRoom = errors.New("the server isn't currently in the room") +var ( + errNotLocalUser = errors.New("the user is not from this server") + errNotInRoom = errors.New("the server isn't currently in the room") +) // CreateInvitesFrom3PIDInvites implements POST /_matrix/federation/v1/3pid/onbind func CreateInvitesFrom3PIDInvites( req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite, producer *producers.RoomserverProducer, federation *gomatrixserverlib.FederationClient, + accountDB *accounts.Database, ) util.JSONResponse { var body invites if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil { @@ -65,7 +70,7 @@ func CreateInvitesFrom3PIDInvites( evs := []gomatrixserverlib.Event{} for _, inv := range body.Invites { event, err := createInviteFrom3PIDInvite( - req.Context(), queryAPI, cfg, inv, federation, + req.Context(), queryAPI, cfg, inv, federation, accountDB, ) if err != nil { return httputil.LogThenError(req, err) @@ -165,7 +170,17 @@ func ExchangeThirdPartyInvite( func createInviteFrom3PIDInvite( ctx context.Context, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite, inv invite, federation *gomatrixserverlib.FederationClient, + accountDB *accounts.Database, ) (*gomatrixserverlib.Event, error) { + localpart, server, err := gomatrixserverlib.SplitID('@', inv.MXID) + if err != nil { + return nil, err + } + + if server != cfg.Matrix.ServerName { + return nil, errNotLocalUser + } + // Build the event builder := &gomatrixserverlib.EventBuilder{ Type: "m.room.member", @@ -174,15 +189,21 @@ func createInviteFrom3PIDInvite( StateKey: &inv.MXID, } + profile, err := accountDB.GetProfileByLocalpart(localpart) + if err != nil { + return nil, err + } + content := common.MemberContent{ - // TODO: Load the profile - Membership: "invite", + AvatarURL: profile.AvatarURL, + DisplayName: profile.DisplayName, + Membership: "invite", ThirdPartyInvite: &common.TPInvite{ Signed: inv.Signed, }, } - if err := builder.SetContent(content); err != nil { + if err = builder.SetContent(content); err != nil { return nil, err }