DirectoryRoom looks up a room alias
( req *http.Request, roomAlias string, federation *gomatrixserverlib.FederationClient, cfg *config.ClientAPI, rsAPI roomserverAPI.ClientRoomserverAPI, fedSenderAPI federationAPI.ClientFederationAPI, )
| 42 | |
| 43 | // DirectoryRoom looks up a room alias |
| 44 | func DirectoryRoom( |
| 45 | req *http.Request, |
| 46 | roomAlias string, |
| 47 | federation *gomatrixserverlib.FederationClient, |
| 48 | cfg *config.ClientAPI, |
| 49 | rsAPI roomserverAPI.ClientRoomserverAPI, |
| 50 | fedSenderAPI federationAPI.ClientFederationAPI, |
| 51 | ) util.JSONResponse { |
| 52 | _, domain, err := gomatrixserverlib.SplitID('#', roomAlias) |
| 53 | if err != nil { |
| 54 | return util.JSONResponse{ |
| 55 | Code: http.StatusBadRequest, |
| 56 | JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | var res roomDirectoryResponse |
| 61 | |
| 62 | // Query the roomserver API to check if the alias exists locally. |
| 63 | queryReq := &roomserverAPI.GetRoomIDForAliasRequest{ |
| 64 | Alias: roomAlias, |
| 65 | IncludeAppservices: true, |
| 66 | } |
| 67 | queryRes := &roomserverAPI.GetRoomIDForAliasResponse{} |
| 68 | if err = rsAPI.GetRoomIDForAlias(req.Context(), queryReq, queryRes); err != nil { |
| 69 | util.GetLogger(req.Context()).WithError(err).Error("rsAPI.GetRoomIDForAlias failed") |
| 70 | return jsonerror.InternalServerError() |
| 71 | } |
| 72 | |
| 73 | res.RoomID = queryRes.RoomID |
| 74 | |
| 75 | if res.RoomID == "" { |
| 76 | // If we don't know it locally, do a federation query. |
| 77 | // But don't send the query to ourselves. |
| 78 | if domain != cfg.Matrix.ServerName { |
| 79 | fedRes, fedErr := federation.LookupRoomAlias(req.Context(), domain, roomAlias) |
| 80 | if fedErr != nil { |
| 81 | // TODO: Return 502 if the remote server errored. |
| 82 | // TODO: Return 504 if the remote server timed out. |
| 83 | util.GetLogger(req.Context()).WithError(fedErr).Error("federation.LookupRoomAlias failed") |
| 84 | return jsonerror.InternalServerError() |
| 85 | } |
| 86 | res.RoomID = fedRes.RoomID |
| 87 | res.fillServers(fedRes.Servers) |
| 88 | } |
| 89 | |
| 90 | if res.RoomID == "" { |
| 91 | return util.JSONResponse{ |
| 92 | Code: http.StatusNotFound, |
| 93 | JSON: jsonerror.NotFound( |
| 94 | fmt.Sprintf("Room alias %s not found", roomAlias), |
| 95 | ), |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | joinedHostsReq := federationAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: res.RoomID} |
| 100 | var joinedHostsRes federationAPI.QueryJoinedHostServerNamesInRoomResponse |
| 101 | if err = fedSenderAPI.QueryJoinedHostServerNamesInRoom(req.Context(), &joinedHostsReq, &joinedHostsRes); err != nil { |
no test coverage detected