POST /join/{roomId}: Use server in roomID as last resort to join

Signed-off-by: Alex Chen <minecnly@gmail.com>
This commit is contained in:
Cnly 2019-01-15 22:23:15 +08:00
parent efa98b35ee
commit c4af1e04a1

View file

@ -118,28 +118,32 @@ func (r joinRoomReq) joinRoomByID(roomID string) util.JSONResponse {
if err := r.queryAPI.QueryInvitesForUser(r.req.Context(), &queryReq, &queryRes); err != nil {
return httputil.LogThenError(r.req, err)
}
if len(queryRes.InviteSenderUserIDs) == 0 {
// Support clients erroneously trying to join without an invite
_, domain, err := gomatrixserverlib.SplitID('!', roomID)
if err != nil {
return httputil.LogThenError(r.req, err)
}
return r.joinRoomUsingServers(roomID, []gomatrixserverlib.ServerName{domain})
}
servers := []gomatrixserverlib.ServerName{}
seenBefore := map[gomatrixserverlib.ServerName]bool{}
seenInInviterIDs := map[gomatrixserverlib.ServerName]bool{}
for _, userID := range queryRes.InviteSenderUserIDs {
_, domain, err := gomatrixserverlib.SplitID('@', userID)
if err != nil {
return httputil.LogThenError(r.req, err)
}
if !seenBefore[domain] {
if !seenInInviterIDs[domain] {
servers = append(servers, domain)
seenBefore[domain] = true
seenInInviterIDs[domain] = true
}
}
// Also add the domain extracted from the roomID as a last resort to join
// in case the client is erroneously trying to join by ID without an invite
// or all previous attempts at domains extracted from the inviter IDs fail
// Note: It's no guarantee we'll succeed because a room isn't bound to the domain in its ID
_, domain, err := gomatrixserverlib.SplitID('!', roomID)
if err != nil {
return httputil.LogThenError(r.req, err)
}
if domain != r.cfg.Matrix.ServerName && !seenInInviterIDs[domain] {
servers = append(servers, domain)
}
return r.joinRoomUsingServers(roomID, servers)
}