Log errors on iteration and throw one if no server could be reached

This commit is contained in:
Brendan Abolivier 2017-09-11 18:39:56 +01:00
parent ebe91ac251
commit e1c8861c4a
No known key found for this signature in database
GPG key ID: 8EF1500759F70623

View file

@ -16,6 +16,7 @@ package writers
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
@ -28,6 +29,8 @@ import (
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
"github.com/sirupsen/logrus"
)
type invite struct {
@ -122,24 +125,7 @@ func createInviteFrom3PIDInvite(
if !queryRes.RoomExists {
// Use federation to auth the event
remoteServers := make([]gomatrixserverlib.ServerName, 2)
_, remoteServers[0], err = gomatrixserverlib.SplitID('@', inv.Sender)
if err != nil {
return nil, err
}
// Fallback to the room's server if the sender's domain is the same as
// the current server's
_, remoteServers[1], err = gomatrixserverlib.SplitID('!', inv.RoomID)
if err != nil {
return nil, err
}
for _, server := range remoteServers {
if server != cfg.Matrix.ServerName {
err = federation.ExchangeThirdPartyInvite(server, *builder)
return nil, err
}
}
return sendToRemoteServer(inv, federation, cfg)
}
// Auth the event locally
@ -172,6 +158,37 @@ func createInviteFrom3PIDInvite(
return &event, nil
}
// sendToRemoteServer uses federation to send an invite provided by an identity
// server to a remote server in case the current server isn't in the room the
// invite is for.
// Returns an error if it couldn't get the server names to reach or if all of
// them responded with an error.
func sendToRemoteServer(
inv invite, federation *gomatrixserverlib.FederationClient, cfg config.Dendrite,
) error {
remoteServers := make([]gomatrixserverlib.ServerName, 2)
_, remoteServers[0], err = gomatrixserverlib.SplitID('@', inv.Sender)
if err != nil {
return err
}
// Fallback to the room's server if the sender's domain is the same as
// the current server's
_, remoteServers[1], err = gomatrixserverlib.SplitID('!', inv.RoomID)
if err != nil {
return err
}
for _, server := range remoteServers {
err = federation.ExchangeThirdPartyInvite(server, *builder)
if err == nil {
return nil
}
logrus.WithError(err).Warn("Failed to send 3PID invite via %s.", server)
}
return errors.New("Failed to send 3PID invite via any server.")
}
// fillDisplayName looks in a list of auth events for a m.room.third_party_invite
// event with the state key matching a given m.room.member event's content's token.
// If such an event is found, fills the "display_name" attribute of the