Merge branch 'master' into neilalexander/rsjoininput

This commit is contained in:
Neil Alexander 2020-09-01 10:33:55 +01:00 committed by GitHub
commit 746c113530
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 32 deletions

View file

@ -27,6 +27,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
currentstateAPI "github.com/matrix-org/dendrite/currentstateserver/api" currentstateAPI "github.com/matrix-org/dendrite/currentstateserver/api"
"github.com/matrix-org/dendrite/internal/config"
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
@ -41,6 +42,7 @@ type PublicRoomReq struct {
Since string `json:"since,omitempty"` Since string `json:"since,omitempty"`
Limit int16 `json:"limit,omitempty"` Limit int16 `json:"limit,omitempty"`
Filter filter `json:"filter,omitempty"` Filter filter `json:"filter,omitempty"`
Server string `json:"server,omitempty"`
} }
type filter struct { type filter struct {
@ -51,11 +53,28 @@ type filter struct {
func GetPostPublicRooms( func GetPostPublicRooms(
req *http.Request, rsAPI roomserverAPI.RoomserverInternalAPI, stateAPI currentstateAPI.CurrentStateInternalAPI, req *http.Request, rsAPI roomserverAPI.RoomserverInternalAPI, stateAPI currentstateAPI.CurrentStateInternalAPI,
extRoomsProvider api.ExtraPublicRoomsProvider, extRoomsProvider api.ExtraPublicRoomsProvider,
federation *gomatrixserverlib.FederationClient,
cfg *config.ClientAPI,
) util.JSONResponse { ) util.JSONResponse {
var request PublicRoomReq var request PublicRoomReq
if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil { if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil {
return *fillErr 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) response, err := publicRooms(req.Context(), request, rsAPI, stateAPI, extRoomsProvider)
if err != nil { if err != nil {
util.GetLogger(req.Context()).WithError(err).Errorf("failed to work out public rooms") 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) response.TotalRoomCountEstimate = len(rooms)
rooms = filterRooms(rooms, request.Filter.SearchTerms)
chunk, prev, next := sliceInto(rooms, offset, limit) chunk, prev, next := sliceInto(rooms, offset, limit)
if prev >= 0 { if prev >= 0 {
response.PrevBatch = "T" + strconv.Itoa(prev) response.PrevBatch = "T" + strconv.Itoa(prev)
@ -111,6 +132,25 @@ func publicRooms(ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI
return &response, err 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 // fillPublicRoomsReq fills the Limit, Since and Filter attributes of a GET or POST request
// on /publicRooms by parsing the incoming HTTP request // on /publicRooms by parsing the incoming HTTP request
// Filter is only filled for POST requests // 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.Limit = int16(limit)
request.Since = httpReq.FormValue("since") request.Since = httpReq.FormValue("since")
request.Server = httpReq.FormValue("server")
} else { } else {
resErr := httputil.UnmarshalJSONRequest(httpReq, request) resErr := httputil.UnmarshalJSONRequest(httpReq, request)
if resErr != nil { if resErr != nil {
return resErr return resErr
} }
request.Server = httpReq.FormValue("server")
} }
// strip the 'T' which is only required because when sytest does pagination tests it stops // strip the 'T' which is only required because when sytest does pagination tests it stops

View file

@ -314,7 +314,7 @@ func Setup(
).Methods(http.MethodPut, http.MethodOptions) ).Methods(http.MethodPut, http.MethodOptions)
r0mux.Handle("/publicRooms", r0mux.Handle("/publicRooms",
httputil.MakeExternalAPI("public_rooms", func(req *http.Request) util.JSONResponse { 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) ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)

View file

@ -104,8 +104,6 @@ type Database interface {
// matches the streamevent.transactionID device then the transaction ID gets // matches the streamevent.transactionID device then the transaction ID gets
// added to the unsigned section of the output event. // added to the unsigned section of the output event.
StreamEventsToEvents(device *userapi.Device, in []types.StreamEvent) []gomatrixserverlib.HeaderedEvent 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 increases the EDU position in the cache and returns the stream position.
AddSendToDevice() types.StreamPosition AddSendToDevice() types.StreamPosition
// SendToDeviceUpdatesForSync returns a list of send-to-device updates. It returns three lists: // SendToDeviceUpdatesForSync returns a list of send-to-device updates. It returns three lists:

View file

@ -133,35 +133,6 @@ func (d *Database) GetStateEventsForRoom(
return 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. // AddInviteEvent stores a new invite event for a user.
// If the invite was successfully stored this returns the stream ID it was stored at. // 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. // Returns an error if there was a problem communicating with the database.

View file

@ -457,3 +457,6 @@ Inbound /v1/send_leave rejects leaves from other servers
Guest users can accept invites to private rooms over federation Guest users can accept invites to private rooms over federation
AS user (not ghost) can join room without registering 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 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