|
| 1 | +// Copyright 2020 New Vector Ltd |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package routing |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + |
| 20 | + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" |
| 21 | + "github.com/matrix-org/dendrite/userapi/api" |
| 22 | + "github.com/matrix-org/dendrite/userapi/storage/accounts" |
| 23 | + "github.com/matrix-org/gomatrixserverlib" |
| 24 | + "github.com/matrix-org/util" |
| 25 | +) |
| 26 | + |
| 27 | +func PeekRoomByIDOrAlias( |
| 28 | + req *http.Request, |
| 29 | + device *api.Device, |
| 30 | + rsAPI roomserverAPI.RoomserverInternalAPI, |
| 31 | + accountDB accounts.Database, |
| 32 | + roomIDOrAlias string, |
| 33 | +) util.JSONResponse { |
| 34 | + // if this is a remote roomIDOrAlias, we have to ask the roomserver (or federation sender?) to |
| 35 | + // to call /peek and /state on the remote server. |
| 36 | + // TODO: in future we could skip this if we know we're already participating in the room, |
| 37 | + // but this is fiddly in case we stop participating in the room. |
| 38 | + |
| 39 | + // then we create a local peek. |
| 40 | + peekReq := roomserverAPI.PerformPeekRequest{ |
| 41 | + RoomIDOrAlias: roomIDOrAlias, |
| 42 | + UserID: device.UserID, |
| 43 | + DeviceID: device.ID, |
| 44 | + } |
| 45 | + peekRes := roomserverAPI.PerformPeekResponse{} |
| 46 | + |
| 47 | + // Check to see if any ?server_name= query parameters were |
| 48 | + // given in the request. |
| 49 | + if serverNames, ok := req.URL.Query()["server_name"]; ok { |
| 50 | + for _, serverName := range serverNames { |
| 51 | + peekReq.ServerNames = append( |
| 52 | + peekReq.ServerNames, |
| 53 | + gomatrixserverlib.ServerName(serverName), |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Ask the roomserver to perform the peek. |
| 59 | + rsAPI.PerformPeek(req.Context(), &peekReq, &peekRes) |
| 60 | + if peekRes.Error != nil { |
| 61 | + return peekRes.Error.JSONResponse() |
| 62 | + } |
| 63 | + |
| 64 | + // if this user is already joined to the room, we let them peek anyway |
| 65 | + // (given they might be about to part the room, and it makes things less fiddly) |
| 66 | + |
| 67 | + // Peeking stops if none of the devices who started peeking have been |
| 68 | + // /syncing for a while, or if everyone who was peeking calls /leave |
| 69 | + // (or /unpeek with a server_name param? or DELETE /peek?) |
| 70 | + // on the peeked room. |
| 71 | + |
| 72 | + return util.JSONResponse{ |
| 73 | + Code: http.StatusOK, |
| 74 | + // TODO: Put the response struct somewhere internal. |
| 75 | + JSON: struct { |
| 76 | + RoomID string `json:"room_id"` |
| 77 | + }{peekRes.RoomID}, |
| 78 | + } |
| 79 | +} |
0 commit comments