From 4c5bd91e1cbf53985b0328c2f68a2daaff232c69 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Wed, 16 Jan 2019 02:20:46 +0800 Subject: [PATCH] POST /join/{roomId}: Allow joining even when not invited #663 (#680) * POST /join/{roomId}: Allow joining even when not invited #663 Signed-off-by: Alex Chen * POST /join/{roomId}: Use server in roomID as last resort to join Signed-off-by: Alex Chen --- .../dendrite/clientapi/routing/joinroom.go | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go index 98c7cd6a7..c98688de0 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go @@ -118,30 +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 { - // TODO: We might need to support clients which erroneously try to join - // the room by ID even when they are not invited. - // This can be done by removing this check and falling through to - // joinRoomUsingServers passing an empty list since joinRoomUserServers - // will check if we are already in the room first. - return util.JSONResponse{ - Code: http.StatusForbidden, - JSON: jsonerror.Forbidden("You are not invited to the room"), - } - } + 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) }