RoomAliasToID converts the queried alias into a room ID and returns it
( httpReq *http.Request, federation federationAPI.FederationClient, cfg *config.FederationAPI, rsAPI roomserverAPI.FederationRoomserverAPI, senderAPI federationAPI.FederationInternalAPI, )
| 29 | |
| 30 | // RoomAliasToID converts the queried alias into a room ID and returns it |
| 31 | func RoomAliasToID( |
| 32 | httpReq *http.Request, |
| 33 | federation federationAPI.FederationClient, |
| 34 | cfg *config.FederationAPI, |
| 35 | rsAPI roomserverAPI.FederationRoomserverAPI, |
| 36 | senderAPI federationAPI.FederationInternalAPI, |
| 37 | ) util.JSONResponse { |
| 38 | roomAlias := httpReq.FormValue("room_alias") |
| 39 | if roomAlias == "" { |
| 40 | return util.JSONResponse{ |
| 41 | Code: http.StatusBadRequest, |
| 42 | JSON: jsonerror.BadJSON("Must supply room alias parameter."), |
| 43 | } |
| 44 | } |
| 45 | _, domain, err := gomatrixserverlib.SplitID('#', roomAlias) |
| 46 | if err != nil { |
| 47 | return util.JSONResponse{ |
| 48 | Code: http.StatusBadRequest, |
| 49 | JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | var resp gomatrixserverlib.RespDirectory |
| 54 | |
| 55 | if domain == cfg.Matrix.ServerName { |
| 56 | queryReq := &roomserverAPI.GetRoomIDForAliasRequest{ |
| 57 | Alias: roomAlias, |
| 58 | IncludeAppservices: true, |
| 59 | } |
| 60 | queryRes := &roomserverAPI.GetRoomIDForAliasResponse{} |
| 61 | if err = rsAPI.GetRoomIDForAlias(httpReq.Context(), queryReq, queryRes); err != nil { |
| 62 | util.GetLogger(httpReq.Context()).WithError(err).Error("aliasAPI.GetRoomIDForAlias failed") |
| 63 | return jsonerror.InternalServerError() |
| 64 | } |
| 65 | |
| 66 | if queryRes.RoomID != "" { |
| 67 | serverQueryReq := federationAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: queryRes.RoomID} |
| 68 | var serverQueryRes federationAPI.QueryJoinedHostServerNamesInRoomResponse |
| 69 | if err = senderAPI.QueryJoinedHostServerNamesInRoom(httpReq.Context(), &serverQueryReq, &serverQueryRes); err != nil { |
| 70 | util.GetLogger(httpReq.Context()).WithError(err).Error("senderAPI.QueryJoinedHostServerNamesInRoom failed") |
| 71 | return jsonerror.InternalServerError() |
| 72 | } |
| 73 | |
| 74 | resp = gomatrixserverlib.RespDirectory{ |
| 75 | RoomID: queryRes.RoomID, |
| 76 | Servers: serverQueryRes.ServerNames, |
| 77 | } |
| 78 | } else { |
| 79 | // If no alias was found, return an error |
| 80 | return util.JSONResponse{ |
| 81 | Code: http.StatusNotFound, |
| 82 | JSON: jsonerror.NotFound(fmt.Sprintf("Room alias %s not found", roomAlias)), |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | resp, err = federation.LookupRoomAlias(httpReq.Context(), domain, roomAlias) |
| 87 | if err != nil { |
| 88 | switch x := err.(type) { |
no test coverage detected