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
This commit is contained in:
Brendan Abolivier 2017-09-13 13:31:46 +01:00 committed by Mark Haines
parent 532cc082a9
commit 79adba43f0
4 changed files with 37 additions and 7 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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")

View File

@ -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
}