2020-07-02 09:41:18 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2022-08-05 04:12:41 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 03:55:01 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2022-08-05 04:12:41 -05:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
|
2020-07-02 11:11:33 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/api"
|
2020-07-02 09:41:18 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-07-02 09:41:18 -05:00
|
|
|
)
|
|
|
|
|
2020-07-03 06:59:00 -05:00
|
|
|
var (
|
|
|
|
cacheMu sync.Mutex
|
2023-04-06 03:55:01 -05:00
|
|
|
publicRoomsCache []fclient.PublicRoom
|
2020-07-03 06:59:00 -05:00
|
|
|
)
|
|
|
|
|
2020-07-02 09:41:18 -05:00
|
|
|
type PublicRoomReq struct {
|
2022-10-27 07:40:35 -05:00
|
|
|
Since string `json:"since,omitempty"`
|
|
|
|
Limit int64 `json:"limit,omitempty"`
|
|
|
|
Filter filter `json:"filter,omitempty"`
|
|
|
|
Server string `json:"server,omitempty"`
|
|
|
|
IncludeAllNetworks bool `json:"include_all_networks,omitempty"`
|
|
|
|
NetworkID string `json:"third_party_instance_id,omitempty"`
|
2020-07-02 09:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type filter struct {
|
2022-10-27 07:40:35 -05:00
|
|
|
SearchTerms string `json:"generic_search_term,omitempty"`
|
|
|
|
RoomTypes []string `json:"room_types,omitempty"` // TODO: Implement filter on this
|
2020-07-02 09:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPostPublicRooms implements GET and POST /publicRooms
|
|
|
|
func GetPostPublicRooms(
|
2022-05-05 07:17:38 -05:00
|
|
|
req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI,
|
2020-07-03 06:59:00 -05:00
|
|
|
extRoomsProvider api.ExtraPublicRoomsProvider,
|
2023-04-06 03:55:01 -05:00
|
|
|
federation *fclient.FederationClient,
|
2020-09-01 04:26:34 -05:00
|
|
|
cfg *config.ClientAPI,
|
2020-07-02 09:41:18 -05:00
|
|
|
) util.JSONResponse {
|
|
|
|
var request PublicRoomReq
|
|
|
|
if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil {
|
|
|
|
return *fillErr
|
|
|
|
}
|
2020-09-01 04:26:34 -05:00
|
|
|
|
2022-10-27 07:40:35 -05:00
|
|
|
if request.IncludeAllNetworks && request.NetworkID != "" {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidParam("include_all_networks and third_party_instance_id can not be used together"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 04:26:34 -05:00
|
|
|
serverName := gomatrixserverlib.ServerName(request.Server)
|
2022-10-26 06:59:19 -05:00
|
|
|
if serverName != "" && !cfg.Matrix.IsLocalServerName(serverName) {
|
2022-01-28 05:07:47 -06:00
|
|
|
res, err := federation.GetPublicRoomsFiltered(
|
2022-11-15 09:05:23 -06:00
|
|
|
req.Context(), cfg.Matrix.ServerName, serverName,
|
2022-01-28 05:07:47 -06:00
|
|
|
int(request.Limit), request.Since,
|
|
|
|
request.Filter.SearchTerms, false,
|
|
|
|
"",
|
|
|
|
)
|
2020-09-01 04:26:34 -05:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:38:09 -05:00
|
|
|
response, err := publicRooms(req.Context(), request, rsAPI, extRoomsProvider)
|
2020-07-02 09:41:18 -05:00
|
|
|
if err != nil {
|
2020-07-03 06:59:00 -05:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Errorf("failed to work out public rooms")
|
2020-07-02 09:41:18 -05:00
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: response,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:38:09 -05:00
|
|
|
func publicRooms(
|
2022-05-05 07:17:38 -05:00
|
|
|
ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI.ClientRoomserverAPI, extRoomsProvider api.ExtraPublicRoomsProvider,
|
2023-04-06 03:55:01 -05:00
|
|
|
) (*fclient.RespPublicRooms, error) {
|
2020-07-02 09:41:18 -05:00
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
response := fclient.RespPublicRooms{
|
|
|
|
Chunk: []fclient.PublicRoom{},
|
2020-07-03 08:28:43 -05:00
|
|
|
}
|
2022-10-27 07:40:35 -05:00
|
|
|
var limit int64
|
2020-07-02 09:41:18 -05:00
|
|
|
var offset int64
|
|
|
|
limit = request.Limit
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 50
|
|
|
|
}
|
|
|
|
offset, err := strconv.ParseInt(request.Since, 10, 64)
|
|
|
|
// ParseInt returns 0 and an error when trying to parse an empty string
|
|
|
|
// In that case, we want to assign 0 so we ignore the error
|
|
|
|
if err != nil && len(request.Since) > 0 {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("strconv.ParseInt failed")
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-03 06:59:00 -05:00
|
|
|
err = nil
|
2020-07-02 09:41:18 -05:00
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
var rooms []fclient.PublicRoom
|
2020-07-03 06:59:00 -05:00
|
|
|
if request.Since == "" {
|
2022-10-27 07:40:35 -05:00
|
|
|
rooms = refreshPublicRoomCache(ctx, rsAPI, extRoomsProvider, request)
|
2020-07-03 06:59:00 -05:00
|
|
|
} else {
|
|
|
|
rooms = getPublicRoomsFromCache()
|
2020-07-02 09:41:18 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 06:59:00 -05:00
|
|
|
response.TotalRoomCountEstimate = len(rooms)
|
|
|
|
|
2020-09-01 04:26:34 -05:00
|
|
|
rooms = filterRooms(rooms, request.Filter.SearchTerms)
|
|
|
|
|
2020-07-03 06:59:00 -05:00
|
|
|
chunk, prev, next := sliceInto(rooms, offset, limit)
|
2020-07-02 09:41:18 -05:00
|
|
|
if prev >= 0 {
|
|
|
|
response.PrevBatch = "T" + strconv.Itoa(prev)
|
|
|
|
}
|
|
|
|
if next >= 0 {
|
|
|
|
response.NextBatch = "T" + strconv.Itoa(next)
|
|
|
|
}
|
2020-07-03 08:28:43 -05:00
|
|
|
if chunk != nil {
|
|
|
|
response.Chunk = chunk
|
|
|
|
}
|
2020-07-02 09:41:18 -05:00
|
|
|
return &response, err
|
|
|
|
}
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
func filterRooms(rooms []fclient.PublicRoom, searchTerm string) []fclient.PublicRoom {
|
2020-09-01 04:26:34 -05:00
|
|
|
if searchTerm == "" {
|
|
|
|
return rooms
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizedTerm := strings.ToLower(searchTerm)
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
result := make([]fclient.PublicRoom, 0)
|
2020-09-01 04:26:34 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:41:18 -05:00
|
|
|
// 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
|
|
|
|
func fillPublicRoomsReq(httpReq *http.Request, request *PublicRoomReq) *util.JSONResponse {
|
|
|
|
if httpReq.Method != "GET" && httpReq.Method != "POST" {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusMethodNotAllowed,
|
|
|
|
JSON: jsonerror.NotFound("Bad method"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if httpReq.Method == "GET" {
|
|
|
|
limit, err := strconv.Atoi(httpReq.FormValue("limit"))
|
|
|
|
// Atoi returns 0 and an error when trying to parse an empty string
|
|
|
|
// In that case, we want to assign 0 so we ignore the error
|
|
|
|
if err != nil && len(httpReq.FormValue("limit")) > 0 {
|
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("strconv.Atoi failed")
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.BadJSON("limit param is not a number"),
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 07:40:35 -05:00
|
|
|
request.Limit = int64(limit)
|
2020-07-02 09:41:18 -05:00
|
|
|
request.Since = httpReq.FormValue("since")
|
2020-09-01 04:26:34 -05:00
|
|
|
request.Server = httpReq.FormValue("server")
|
2020-07-02 09:41:18 -05:00
|
|
|
} else {
|
|
|
|
resErr := httputil.UnmarshalJSONRequest(httpReq, request)
|
|
|
|
if resErr != nil {
|
|
|
|
return resErr
|
|
|
|
}
|
2020-09-01 04:26:34 -05:00
|
|
|
request.Server = httpReq.FormValue("server")
|
2020-07-02 09:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// strip the 'T' which is only required because when sytest does pagination tests it stops
|
|
|
|
// iterating when !prev_batch which then fails if prev_batch==0, so add arbitrary text to
|
|
|
|
// make it truthy not falsey.
|
|
|
|
request.Since = strings.TrimPrefix(request.Since, "T")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sliceInto returns a subslice of `slice` which honours the since/limit values given.
|
|
|
|
//
|
2022-08-05 04:12:41 -05:00
|
|
|
// 0 1 2 3 4 5 6 index
|
|
|
|
// [A, B, C, D, E, F, G] slice
|
2020-07-02 09:41:18 -05:00
|
|
|
//
|
2022-08-05 04:12:41 -05:00
|
|
|
// limit=3 => A,B,C (prev='', next='3')
|
|
|
|
// limit=3&since=3 => D,E,F (prev='0', next='6')
|
|
|
|
// limit=3&since=6 => G (prev='3', next='')
|
2020-07-02 09:41:18 -05:00
|
|
|
//
|
2022-08-05 04:12:41 -05:00
|
|
|
// A value of '-1' for prev/next indicates no position.
|
2023-04-06 03:55:01 -05:00
|
|
|
func sliceInto(slice []fclient.PublicRoom, since int64, limit int64) (subset []fclient.PublicRoom, prev, next int) {
|
2020-07-02 09:41:18 -05:00
|
|
|
prev = -1
|
|
|
|
next = -1
|
|
|
|
|
|
|
|
if since > 0 {
|
|
|
|
prev = int(since) - int(limit)
|
|
|
|
}
|
|
|
|
nextIndex := int(since) + int(limit)
|
|
|
|
if len(slice) > nextIndex { // there are more rooms ahead of us
|
|
|
|
next = nextIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply sanity caps
|
|
|
|
if since < 0 {
|
|
|
|
since = 0
|
|
|
|
}
|
|
|
|
if nextIndex > len(slice) {
|
|
|
|
nextIndex = len(slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
subset = slice[since:nextIndex]
|
|
|
|
return
|
|
|
|
}
|
2020-07-03 06:59:00 -05:00
|
|
|
|
|
|
|
func refreshPublicRoomCache(
|
2022-05-05 07:17:38 -05:00
|
|
|
ctx context.Context, rsAPI roomserverAPI.ClientRoomserverAPI, extRoomsProvider api.ExtraPublicRoomsProvider,
|
2022-10-27 07:40:35 -05:00
|
|
|
request PublicRoomReq,
|
2023-04-06 03:55:01 -05:00
|
|
|
) []fclient.PublicRoom {
|
2020-07-03 06:59:00 -05:00
|
|
|
cacheMu.Lock()
|
|
|
|
defer cacheMu.Unlock()
|
2023-04-06 03:55:01 -05:00
|
|
|
var extraRooms []fclient.PublicRoom
|
2020-07-03 06:59:00 -05:00
|
|
|
if extRoomsProvider != nil {
|
|
|
|
extraRooms = extRoomsProvider.Rooms()
|
|
|
|
}
|
|
|
|
|
2022-10-27 07:40:35 -05:00
|
|
|
// TODO: this is only here to make Sytest happy, for now.
|
|
|
|
ns := strings.Split(request.NetworkID, "|")
|
|
|
|
if len(ns) == 2 {
|
|
|
|
request.NetworkID = ns[1]
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:59:00 -05:00
|
|
|
var queryRes roomserverAPI.QueryPublishedRoomsResponse
|
2022-10-27 07:40:35 -05:00
|
|
|
err := rsAPI.QueryPublishedRooms(ctx, &roomserverAPI.QueryPublishedRoomsRequest{
|
|
|
|
NetworkID: request.NetworkID,
|
|
|
|
IncludeAllNetworks: request.IncludeAllNetworks,
|
|
|
|
}, &queryRes)
|
2020-07-03 06:59:00 -05:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("QueryPublishedRooms failed")
|
|
|
|
return publicRoomsCache
|
|
|
|
}
|
2020-09-07 06:38:09 -05:00
|
|
|
pubRooms, err := roomserverAPI.PopulatePublicRooms(ctx, queryRes.RoomIDs, rsAPI)
|
2020-07-03 06:59:00 -05:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("PopulatePublicRooms failed")
|
|
|
|
return publicRoomsCache
|
|
|
|
}
|
2023-04-06 03:55:01 -05:00
|
|
|
publicRoomsCache = []fclient.PublicRoom{}
|
2020-07-03 06:59:00 -05:00
|
|
|
publicRoomsCache = append(publicRoomsCache, pubRooms...)
|
|
|
|
publicRoomsCache = append(publicRoomsCache, extraRooms...)
|
|
|
|
publicRoomsCache = dedupeAndShuffle(publicRoomsCache)
|
|
|
|
|
|
|
|
// sort by total joined member count (big to small)
|
|
|
|
sort.SliceStable(publicRoomsCache, func(i, j int) bool {
|
|
|
|
return publicRoomsCache[i].JoinedMembersCount > publicRoomsCache[j].JoinedMembersCount
|
|
|
|
})
|
|
|
|
return publicRoomsCache
|
|
|
|
}
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
func getPublicRoomsFromCache() []fclient.PublicRoom {
|
2020-07-03 06:59:00 -05:00
|
|
|
cacheMu.Lock()
|
|
|
|
defer cacheMu.Unlock()
|
|
|
|
return publicRoomsCache
|
|
|
|
}
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
func dedupeAndShuffle(in []fclient.PublicRoom) []fclient.PublicRoom {
|
2020-07-03 06:59:00 -05:00
|
|
|
// de-duplicate rooms with the same room ID. We can join the room via any of these aliases as we know these servers
|
|
|
|
// are alive and well, so we arbitrarily pick one (purposefully shuffling them to spread the load a bit)
|
2023-04-06 03:55:01 -05:00
|
|
|
var publicRooms []fclient.PublicRoom
|
2020-07-03 06:59:00 -05:00
|
|
|
haveRoomIDs := make(map[string]bool)
|
|
|
|
rand.Shuffle(len(in), func(i, j int) {
|
|
|
|
in[i], in[j] = in[j], in[i]
|
|
|
|
})
|
|
|
|
for _, r := range in {
|
|
|
|
if haveRoomIDs[r.RoomID] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
haveRoomIDs[r.RoomID] = true
|
|
|
|
publicRooms = append(publicRooms, r)
|
|
|
|
}
|
|
|
|
return publicRooms
|
|
|
|
}
|