mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-18 04:13:10 -06:00
Implement PerformJoinRequest
This commit is contained in:
parent
a308e61331
commit
90d6baebad
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
commonHTTP "github.com/matrix-org/dendrite/common/http"
|
commonHTTP "github.com/matrix-org/dendrite/common/http"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/opentracing/opentracing-go"
|
"github.com/opentracing/opentracing-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,6 +18,9 @@ const (
|
||||||
|
|
||||||
type PerformJoinRequest struct {
|
type PerformJoinRequest struct {
|
||||||
RoomID string `json:"room_id"`
|
RoomID string `json:"room_id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
ServerName gomatrixserverlib.ServerName `json:"server_name"`
|
||||||
|
Content map[string]interface{} `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PerformJoinResponse struct {
|
type PerformJoinResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -61,10 +61,10 @@ func SetupFederationSenderComponent(
|
||||||
logrus.WithError(err).Panic("failed to start typing server consumer")
|
logrus.WithError(err).Panic("failed to start typing server consumer")
|
||||||
}
|
}
|
||||||
|
|
||||||
queryAPI := query.FederationSenderInternalAPI{
|
queryAPI := query.NewFederationSenderInternalAPI(
|
||||||
DB: federationSenderDB,
|
federationSenderDB, base.Cfg, roomserverProducer, federation,
|
||||||
}
|
)
|
||||||
queryAPI.SetupHTTP(http.DefaultServeMux)
|
queryAPI.SetupHTTP(http.DefaultServeMux)
|
||||||
|
|
||||||
return &queryAPI
|
return queryAPI
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,42 @@ func (c *RoomserverProducer) SendInviteResponse(
|
||||||
return c.SendInputRoomEvents(ctx, []api.InputRoomEvent{ire})
|
return c.SendInputRoomEvents(ctx, []api.InputRoomEvent{ire})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendEventWithState writes an event with KindNew to the roomserver input log
|
||||||
|
// with the state at the event as KindOutlier before it.
|
||||||
|
func (c *RoomserverProducer) SendEventWithState(
|
||||||
|
ctx context.Context, state gomatrixserverlib.RespState, event gomatrixserverlib.HeaderedEvent,
|
||||||
|
) error {
|
||||||
|
outliers, err := state.Events()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var ires []api.InputRoomEvent
|
||||||
|
for _, outlier := range outliers {
|
||||||
|
ires = append(ires, api.InputRoomEvent{
|
||||||
|
Kind: api.KindOutlier,
|
||||||
|
Event: outlier.Headered(event.RoomVersion),
|
||||||
|
AuthEventIDs: outlier.AuthEventIDs(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
stateEventIDs := make([]string, len(state.StateEvents))
|
||||||
|
for i := range state.StateEvents {
|
||||||
|
stateEventIDs[i] = state.StateEvents[i].EventID()
|
||||||
|
}
|
||||||
|
|
||||||
|
ires = append(ires, api.InputRoomEvent{
|
||||||
|
Kind: api.KindNew,
|
||||||
|
Event: event,
|
||||||
|
AuthEventIDs: event.AuthEventIDs(),
|
||||||
|
HasState: true,
|
||||||
|
StateEventIDs: stateEventIDs,
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err = c.SendInputRoomEvents(ctx, ires)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// SendInputRoomEvents writes the given input room events to the roomserver input API.
|
// SendInputRoomEvents writes the given input room events to the roomserver input API.
|
||||||
func (c *RoomserverProducer) SendInputRoomEvents(
|
func (c *RoomserverProducer) SendInputRoomEvents(
|
||||||
ctx context.Context, ires []api.InputRoomEvent,
|
ctx context.Context, ires []api.InputRoomEvent,
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,33 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/federationsender/api"
|
"github.com/matrix-org/dendrite/federationsender/api"
|
||||||
|
"github.com/matrix-org/dendrite/federationsender/producers"
|
||||||
"github.com/matrix-org/dendrite/federationsender/storage"
|
"github.com/matrix-org/dendrite/federationsender/storage"
|
||||||
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FederationSenderInternalAPI is an implementation of api.FederationSenderInternalAPI
|
// FederationSenderInternalAPI is an implementation of api.FederationSenderInternalAPI
|
||||||
type FederationSenderInternalAPI struct {
|
type FederationSenderInternalAPI struct {
|
||||||
api.FederationSenderInternalAPI
|
api.FederationSenderInternalAPI
|
||||||
DB storage.Database
|
db storage.Database
|
||||||
RoomserverInputAPI rsAPI.RoomserverInputAPI
|
cfg *config.Dendrite
|
||||||
|
producer *producers.RoomserverProducer
|
||||||
|
federation *gomatrixserverlib.FederationClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFederationSenderInternalAPI(
|
||||||
|
db storage.Database, cfg *config.Dendrite,
|
||||||
|
producer *producers.RoomserverProducer,
|
||||||
|
federation *gomatrixserverlib.FederationClient,
|
||||||
|
) *FederationSenderInternalAPI {
|
||||||
|
return &FederationSenderInternalAPI{
|
||||||
|
db: db,
|
||||||
|
producer: producer,
|
||||||
|
federation: federation,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupHTTP adds the FederationSenderInternalAPI handlers to the http.ServeMux.
|
// SetupHTTP adds the FederationSenderInternalAPI handlers to the http.ServeMux.
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,12 @@ package query
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/federationsender/api"
|
"github.com/matrix-org/dendrite/federationsender/api"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/version"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PerformJoinRequest implements api.FederationSenderInternalAPI
|
// PerformJoinRequest implements api.FederationSenderInternalAPI
|
||||||
|
|
@ -12,6 +16,84 @@ func (r *FederationSenderInternalAPI) PerformJoinRequest(
|
||||||
request *api.PerformJoinRequest,
|
request *api.PerformJoinRequest,
|
||||||
response *api.PerformJoinResponse,
|
response *api.PerformJoinResponse,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
|
// Look up the supported room versions.
|
||||||
|
var supportedVersions []gomatrixserverlib.RoomVersion
|
||||||
|
for version := range version.SupportedRoomVersions() {
|
||||||
|
supportedVersions = append(supportedVersions, version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to perform a make_join using the information supplied in the
|
||||||
|
// request.
|
||||||
|
respMakeJoin, err := r.federation.MakeJoin(
|
||||||
|
ctx,
|
||||||
|
request.ServerName,
|
||||||
|
request.RoomID,
|
||||||
|
request.UserID,
|
||||||
|
supportedVersions,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
// TODO: Check if the user was not allowed to join the room.
|
||||||
|
return fmt.Errorf("r.federation.MakeJoin: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set all the fields to be what they should be, this should be a no-op
|
||||||
|
// but it's possible that the remote server returned us something "odd"
|
||||||
|
respMakeJoin.JoinEvent.Type = "m.room.member"
|
||||||
|
respMakeJoin.JoinEvent.Sender = request.UserID
|
||||||
|
respMakeJoin.JoinEvent.StateKey = &request.UserID
|
||||||
|
respMakeJoin.JoinEvent.RoomID = request.RoomID
|
||||||
|
respMakeJoin.JoinEvent.Redacts = ""
|
||||||
|
if err = respMakeJoin.JoinEvent.SetContent(request.Content); err != nil {
|
||||||
|
return fmt.Errorf("respMakeJoin.JoinEvent.SetContent: %w", err)
|
||||||
|
}
|
||||||
|
if err = respMakeJoin.JoinEvent.SetUnsigned(struct{}{}); err != nil {
|
||||||
|
return fmt.Errorf("respMakeJoin.JoinEvent.SetUnsigned: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Work out if we support the room version that has been supplied in
|
||||||
|
// the make_join response.
|
||||||
|
if respMakeJoin.RoomVersion == "" {
|
||||||
|
respMakeJoin.RoomVersion = gomatrixserverlib.RoomVersionV1
|
||||||
|
}
|
||||||
|
if _, err = respMakeJoin.RoomVersion.EventFormat(); err != nil {
|
||||||
|
return fmt.Errorf("respMakeJoin.RoomVersion.EventFormat: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the join event.
|
||||||
|
event, err := respMakeJoin.JoinEvent.Build(
|
||||||
|
time.Now(),
|
||||||
|
r.cfg.Matrix.ServerName,
|
||||||
|
r.cfg.Matrix.KeyID,
|
||||||
|
r.cfg.Matrix.PrivateKey,
|
||||||
|
respMakeJoin.RoomVersion,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("respMakeJoin.JoinEvent.Build: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to perform a send_join using the newly built event.
|
||||||
|
respSendJoin, err := r.federation.SendJoin(
|
||||||
|
ctx,
|
||||||
|
request.ServerName,
|
||||||
|
event,
|
||||||
|
respMakeJoin.RoomVersion,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("r.federation.SendJoin: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we successfully performed a send_join above then the other
|
||||||
|
// server now thinks we're a part of the room. Send the newly
|
||||||
|
// returned state to the roomserver to update our local view.
|
||||||
|
if err = r.producer.SendEventWithState(
|
||||||
|
ctx,
|
||||||
|
respSendJoin.ToRespState(),
|
||||||
|
event.Headered(respMakeJoin.RoomVersion),
|
||||||
|
); err != nil {
|
||||||
|
return fmt.Errorf("r.producer.SendEventWithState: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything went to plan.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ func (f *FederationSenderInternalAPI) QueryJoinedHostsInRoom(
|
||||||
request *api.QueryJoinedHostsInRoomRequest,
|
request *api.QueryJoinedHostsInRoomRequest,
|
||||||
response *api.QueryJoinedHostsInRoomResponse,
|
response *api.QueryJoinedHostsInRoomResponse,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
response.JoinedHosts, err = f.DB.GetJoinedHosts(ctx, request.RoomID)
|
response.JoinedHosts, err = f.db.GetJoinedHosts(ctx, request.RoomID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ func (f *FederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom(
|
||||||
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
||||||
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
joinedHosts, err := f.DB.GetJoinedHosts(ctx, request.RoomID)
|
joinedHosts, err := f.db.GetJoinedHosts(ctx, request.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue