From b0d2b39739551a46698222ec62a3c5fb11efd7e5 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 1 Sep 2020 10:26:02 +0100 Subject: [PATCH 1/2] Remove unused SyncStreamPosition --- syncapi/storage/interface.go | 2 -- syncapi/storage/shared/syncserver.go | 29 ---------------------------- 2 files changed, 31 deletions(-) diff --git a/syncapi/storage/interface.go b/syncapi/storage/interface.go index a5e13b674..838fd547d 100644 --- a/syncapi/storage/interface.go +++ b/syncapi/storage/interface.go @@ -104,8 +104,6 @@ type Database interface { // matches the streamevent.transactionID device then the transaction ID gets // added to the unsigned section of the output event. StreamEventsToEvents(device *userapi.Device, in []types.StreamEvent) []gomatrixserverlib.HeaderedEvent - // SyncStreamPosition returns the latest position in the sync stream. Returns 0 if there are no events yet. - SyncStreamPosition(ctx context.Context) (types.StreamPosition, error) // AddSendToDevice increases the EDU position in the cache and returns the stream position. AddSendToDevice() types.StreamPosition // SendToDeviceUpdatesForSync returns a list of send-to-device updates. It returns three lists: diff --git a/syncapi/storage/shared/syncserver.go b/syncapi/storage/shared/syncserver.go index 401bae33b..37793ba2a 100644 --- a/syncapi/storage/shared/syncserver.go +++ b/syncapi/storage/shared/syncserver.go @@ -133,35 +133,6 @@ func (d *Database) GetStateEventsForRoom( return } -func (d *Database) SyncStreamPosition(ctx context.Context) (types.StreamPosition, error) { - var maxID int64 - var err error - err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error { - maxID, err = d.OutputEvents.SelectMaxEventID(ctx, txn) - if err != nil { - return err - } - var maxAccountDataID int64 - maxAccountDataID, err = d.AccountData.SelectMaxAccountDataID(ctx, txn) - if err != nil { - return err - } - if maxAccountDataID > maxID { - maxID = maxAccountDataID - } - var maxInviteID int64 - maxInviteID, err = d.Invites.SelectMaxInviteID(ctx, txn) - if err != nil { - return err - } - if maxInviteID > maxID { - maxID = maxInviteID - } - return nil - }) - return types.StreamPosition(maxID), err -} - // AddInviteEvent stores a new invite event for a user. // If the invite was successfully stored this returns the stream ID it was stored at. // Returns an error if there was a problem communicating with the database. From 3f9b829bc570d5f6353eda21ecf3d0088e4d9c50 Mon Sep 17 00:00:00 2001 From: Rohit Mohan Date: Tue, 1 Sep 2020 14:56:34 +0530 Subject: [PATCH 2/2] Public room client API changes (#1368) Signed-off-by: Rohit Mohan --- clientapi/routing/directory_public.go | 42 +++++++++++++++++++++++++++ clientapi/routing/routing.go | 2 +- sytest-whitelist | 3 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/clientapi/routing/directory_public.go b/clientapi/routing/directory_public.go index fcf3f656d..bae7e49bb 100644 --- a/clientapi/routing/directory_public.go +++ b/clientapi/routing/directory_public.go @@ -27,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" currentstateAPI "github.com/matrix-org/dendrite/currentstateserver/api" + "github.com/matrix-org/dendrite/internal/config" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" @@ -41,6 +42,7 @@ type PublicRoomReq struct { Since string `json:"since,omitempty"` Limit int16 `json:"limit,omitempty"` Filter filter `json:"filter,omitempty"` + Server string `json:"server,omitempty"` } type filter struct { @@ -51,11 +53,28 @@ type filter struct { func GetPostPublicRooms( req *http.Request, rsAPI roomserverAPI.RoomserverInternalAPI, stateAPI currentstateAPI.CurrentStateInternalAPI, extRoomsProvider api.ExtraPublicRoomsProvider, + federation *gomatrixserverlib.FederationClient, + cfg *config.ClientAPI, ) util.JSONResponse { var request PublicRoomReq if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil { return *fillErr } + + serverName := gomatrixserverlib.ServerName(request.Server) + + if serverName != "" && serverName != cfg.Matrix.ServerName { + res, err := federation.GetPublicRooms(req.Context(), serverName, int(request.Limit), request.Since, false, "") + if err != nil { + util.GetLogger(req.Context()).WithError(err).Error("failed to get public rooms") + return jsonerror.InternalServerError() + } + return util.JSONResponse{ + Code: http.StatusOK, + JSON: res, + } + } + response, err := publicRooms(req.Context(), request, rsAPI, stateAPI, extRoomsProvider) if err != nil { util.GetLogger(req.Context()).WithError(err).Errorf("failed to work out public rooms") @@ -98,6 +117,8 @@ func publicRooms(ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI response.TotalRoomCountEstimate = len(rooms) + rooms = filterRooms(rooms, request.Filter.SearchTerms) + chunk, prev, next := sliceInto(rooms, offset, limit) if prev >= 0 { response.PrevBatch = "T" + strconv.Itoa(prev) @@ -111,6 +132,25 @@ func publicRooms(ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI return &response, err } +func filterRooms(rooms []gomatrixserverlib.PublicRoom, searchTerm string) []gomatrixserverlib.PublicRoom { + if searchTerm == "" { + return rooms + } + + normalizedTerm := strings.ToLower(searchTerm) + + result := make([]gomatrixserverlib.PublicRoom, 0) + for _, room := range rooms { + if strings.Contains(strings.ToLower(room.Name), normalizedTerm) || + strings.Contains(strings.ToLower(room.Topic), normalizedTerm) || + strings.Contains(strings.ToLower(room.CanonicalAlias), normalizedTerm) { + result = append(result, room) + } + } + + return result +} + // fillPublicRoomsReq fills the Limit, Since and Filter attributes of a GET or POST request // on /publicRooms by parsing the incoming HTTP request // Filter is only filled for POST requests @@ -134,11 +174,13 @@ func fillPublicRoomsReq(httpReq *http.Request, request *PublicRoomReq) *util.JSO } request.Limit = int16(limit) request.Since = httpReq.FormValue("since") + request.Server = httpReq.FormValue("server") } else { resErr := httputil.UnmarshalJSONRequest(httpReq, request) if resErr != nil { return resErr } + request.Server = httpReq.FormValue("server") } // strip the 'T' which is only required because when sytest does pagination tests it stops diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index f2494dc78..24343ee19 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -314,7 +314,7 @@ func Setup( ).Methods(http.MethodPut, http.MethodOptions) r0mux.Handle("/publicRooms", httputil.MakeExternalAPI("public_rooms", func(req *http.Request) util.JSONResponse { - return GetPostPublicRooms(req, rsAPI, stateAPI, extRoomsProvider) + return GetPostPublicRooms(req, rsAPI, stateAPI, extRoomsProvider, federation, cfg) }), ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) diff --git a/sytest-whitelist b/sytest-whitelist index a17ed8407..7ce59fef6 100644 --- a/sytest-whitelist +++ b/sytest-whitelist @@ -457,3 +457,6 @@ Inbound /v1/send_leave rejects leaves from other servers Guest users can accept invites to private rooms over federation AS user (not ghost) can join room without registering If user leaves room, remote user changes device and rejoins we see update in /sync and /keys/changes +Can search public room list +Can get remote public room list +Asking for a remote rooms list, but supplying the local server's name, returns the local rooms list