mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-22 19:51:39 +00:00
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:
parent
532cc082a9
commit
79adba43f0
@ -20,6 +20,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"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/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"
|
||||||
@ -58,6 +59,11 @@ func main() {
|
|||||||
log.Panicf("Failed to setup key database(%q): %s", cfg.Database.ServerKey, err.Error())
|
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{
|
keyRing := gomatrixserverlib.KeyRing{
|
||||||
KeyFetchers: []gomatrixserverlib.KeyFetcher{
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{
|
||||||
// TODO: Use perspective key fetchers for production.
|
// TODO: Use perspective key fetchers for production.
|
||||||
@ -78,7 +84,7 @@ func main() {
|
|||||||
log.Info("Starting federation API server on ", cfg.Listen.FederationAPI)
|
log.Info("Starting federation API server on ", cfg.Listen.FederationAPI)
|
||||||
|
|
||||||
api := mux.NewRouter()
|
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)
|
common.SetupHTTPAPI(http.DefaultServeMux, api)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(string(cfg.Listen.FederationAPI), nil))
|
log.Fatal(http.ListenAndServe(string(cfg.Listen.FederationAPI), nil))
|
||||||
|
@ -333,6 +333,7 @@ func (m *monolith) setupAPIs() {
|
|||||||
|
|
||||||
federationapi_routing.Setup(
|
federationapi_routing.Setup(
|
||||||
m.api, *m.cfg, m.queryAPI, m.roomServerProducer, m.keyRing, m.federation,
|
m.api, *m.cfg, m.queryAPI, m.roomServerProducer, m.keyRing, m.federation,
|
||||||
|
m.accountDB,
|
||||||
)
|
)
|
||||||
|
|
||||||
publicroomsapi_routing.Setup(m.api, m.deviceDB, m.publicRoomsAPIDB)
|
publicroomsapi_routing.Setup(m.api, m.deviceDB, m.publicRoomsAPIDB)
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"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/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"
|
||||||
@ -42,6 +43,7 @@ func Setup(
|
|||||||
producer *producers.RoomserverProducer,
|
producer *producers.RoomserverProducer,
|
||||||
keys gomatrixserverlib.KeyRing,
|
keys gomatrixserverlib.KeyRing,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
|
accountDB *accounts.Database,
|
||||||
) {
|
) {
|
||||||
v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter()
|
v2keysmux := apiMux.PathPrefix(pathPrefixV2Keys).Subrouter()
|
||||||
v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter()
|
v1fedmux := apiMux.PathPrefix(pathPrefixV1Federation).Subrouter()
|
||||||
@ -81,7 +83,7 @@ func Setup(
|
|||||||
|
|
||||||
v1fedmux.Handle("/3pid/onbind", common.MakeAPI("3pid_onbind",
|
v1fedmux.Handle("/3pid/onbind", common.MakeAPI("3pid_onbind",
|
||||||
func(req *http.Request) util.JSONResponse {
|
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")
|
)).Methods("POST", "OPTIONS")
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||||
"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/clientapi/producers"
|
"github.com/matrix-org/dendrite/clientapi/producers"
|
||||||
@ -50,12 +51,16 @@ type invites struct {
|
|||||||
Invites []invite `json:"invites"`
|
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
|
// CreateInvitesFrom3PIDInvites implements POST /_matrix/federation/v1/3pid/onbind
|
||||||
func CreateInvitesFrom3PIDInvites(
|
func CreateInvitesFrom3PIDInvites(
|
||||||
req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite,
|
req *http.Request, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite,
|
||||||
producer *producers.RoomserverProducer, federation *gomatrixserverlib.FederationClient,
|
producer *producers.RoomserverProducer, federation *gomatrixserverlib.FederationClient,
|
||||||
|
accountDB *accounts.Database,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
var body invites
|
var body invites
|
||||||
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
|
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
|
||||||
@ -65,7 +70,7 @@ func CreateInvitesFrom3PIDInvites(
|
|||||||
evs := []gomatrixserverlib.Event{}
|
evs := []gomatrixserverlib.Event{}
|
||||||
for _, inv := range body.Invites {
|
for _, inv := range body.Invites {
|
||||||
event, err := createInviteFrom3PIDInvite(
|
event, err := createInviteFrom3PIDInvite(
|
||||||
req.Context(), queryAPI, cfg, inv, federation,
|
req.Context(), queryAPI, cfg, inv, federation, accountDB,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httputil.LogThenError(req, err)
|
return httputil.LogThenError(req, err)
|
||||||
@ -165,7 +170,17 @@ func ExchangeThirdPartyInvite(
|
|||||||
func createInviteFrom3PIDInvite(
|
func createInviteFrom3PIDInvite(
|
||||||
ctx context.Context, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite,
|
ctx context.Context, queryAPI api.RoomserverQueryAPI, cfg config.Dendrite,
|
||||||
inv invite, federation *gomatrixserverlib.FederationClient,
|
inv invite, federation *gomatrixserverlib.FederationClient,
|
||||||
|
accountDB *accounts.Database,
|
||||||
) (*gomatrixserverlib.Event, error) {
|
) (*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
|
// Build the event
|
||||||
builder := &gomatrixserverlib.EventBuilder{
|
builder := &gomatrixserverlib.EventBuilder{
|
||||||
Type: "m.room.member",
|
Type: "m.room.member",
|
||||||
@ -174,15 +189,21 @@ func createInviteFrom3PIDInvite(
|
|||||||
StateKey: &inv.MXID,
|
StateKey: &inv.MXID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
profile, err := accountDB.GetProfileByLocalpart(localpart)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
content := common.MemberContent{
|
content := common.MemberContent{
|
||||||
// TODO: Load the profile
|
AvatarURL: profile.AvatarURL,
|
||||||
Membership: "invite",
|
DisplayName: profile.DisplayName,
|
||||||
|
Membership: "invite",
|
||||||
ThirdPartyInvite: &common.TPInvite{
|
ThirdPartyInvite: &common.TPInvite{
|
||||||
Signed: inv.Signed,
|
Signed: inv.Signed,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := builder.SetContent(content); err != nil {
|
if err = builder.SetContent(content); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user