2021-01-13 19:00:38 +01:00
|
|
|
// Copyright 2021 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 msc2946 'Spaces Summary' implements https://github.com/matrix-org/matrix-doc/pull/2946
|
|
|
|
package msc2946
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-19 18:14:25 +01:00
|
|
|
"encoding/json"
|
2021-01-13 19:00:38 +01:00
|
|
|
"net/http"
|
2022-03-01 14:40:07 +01:00
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-01-15 19:49:21 +01:00
|
|
|
"strings"
|
2021-01-13 19:00:38 +01:00
|
|
|
"sync"
|
2021-01-19 18:14:25 +01:00
|
|
|
"time"
|
2021-01-13 19:00:38 +01:00
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
"github.com/google/uuid"
|
2021-01-13 19:00:38 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-11-24 11:45:23 +01:00
|
|
|
fs "github.com/matrix-org/dendrite/federationapi/api"
|
2022-03-01 17:32:48 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2021-01-13 19:00:38 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
roomserver "github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 13:54:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2023-03-22 09:21:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2021-01-13 19:00:38 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 10:55:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2021-01-13 19:00:38 +01:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-01 14:40:07 +01:00
|
|
|
ConstCreateEventContentKey = "type"
|
|
|
|
ConstCreateEventContentValueSpace = "m.space"
|
|
|
|
ConstSpaceChildEventType = "m.space.child"
|
|
|
|
ConstSpaceParentEventType = "m.space.parent"
|
2021-01-13 19:00:38 +01:00
|
|
|
)
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
type MSC2946ClientResponse struct {
|
2023-04-06 10:55:01 +02:00
|
|
|
Rooms []fclient.MSC2946Room `json:"rooms"`
|
|
|
|
NextBatch string `json:"next_batch,omitempty"`
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable this MSC
|
|
|
|
func Enable(
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg *config.Dendrite, routers httputil.Routers, rsAPI roomserver.RoomserverInternalAPI, userAPI userapi.UserInternalAPI,
|
2022-03-01 17:32:48 +01:00
|
|
|
fsAPI fs.FederationInternalAPI, keyRing gomatrixserverlib.JSONVerifier, cache caching.SpaceSummaryRoomsCache,
|
2021-01-13 19:00:38 +01:00
|
|
|
) error {
|
2023-03-22 09:21:32 +01:00
|
|
|
clientAPI := httputil.MakeAuthAPI("spaces", userAPI, spacesHandler(rsAPI, fsAPI, cache, cfg.Global.ServerName), httputil.WithAllowGuests())
|
|
|
|
routers.Client.Handle("/v1/rooms/{roomID}/hierarchy", clientAPI).Methods(http.MethodGet, http.MethodOptions)
|
|
|
|
routers.Client.Handle("/unstable/org.matrix.msc2946/rooms/{roomID}/hierarchy", clientAPI).Methods(http.MethodGet, http.MethodOptions)
|
2021-01-13 19:00:38 +01:00
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
fedAPI := httputil.MakeExternalAPI(
|
2021-01-19 18:14:25 +01:00
|
|
|
"msc2946_fed_spaces", func(req *http.Request) util.JSONResponse {
|
2023-04-19 16:50:33 +02:00
|
|
|
fedReq, errResp := fclient.VerifyHTTPRequest(
|
2023-03-22 09:21:32 +01:00
|
|
|
req, time.Now(), cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
|
2021-01-19 18:14:25 +01:00
|
|
|
)
|
|
|
|
if fedReq == nil {
|
|
|
|
return errResp
|
|
|
|
}
|
|
|
|
// Extract the room ID from the request. Sanity check request data.
|
|
|
|
params, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
roomID := params["roomID"]
|
2023-03-22 09:21:32 +01:00
|
|
|
return federatedSpacesHandler(req.Context(), fedReq, roomID, cache, rsAPI, fsAPI, cfg.Global.ServerName)
|
2021-01-19 18:14:25 +01:00
|
|
|
},
|
2022-03-01 14:40:07 +01:00
|
|
|
)
|
2023-03-22 09:21:32 +01:00
|
|
|
routers.Federation.Handle("/unstable/org.matrix.msc2946/hierarchy/{roomID}", fedAPI).Methods(http.MethodGet)
|
|
|
|
routers.Federation.Handle("/v1/hierarchy/{roomID}", fedAPI).Methods(http.MethodGet)
|
2021-01-13 19:00:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:14:25 +01:00
|
|
|
func federatedSpacesHandler(
|
2023-04-19 16:50:33 +02:00
|
|
|
ctx context.Context, fedReq *fclient.FederationRequest, roomID string,
|
2022-03-01 17:32:48 +01:00
|
|
|
cache caching.SpaceSummaryRoomsCache,
|
2021-11-24 11:45:23 +01:00
|
|
|
rsAPI roomserver.RoomserverInternalAPI, fsAPI fs.FederationInternalAPI,
|
2023-04-19 16:50:33 +02:00
|
|
|
thisServer spec.ServerName,
|
2021-01-19 18:14:25 +01:00
|
|
|
) util.JSONResponse {
|
2022-03-01 14:40:07 +01:00
|
|
|
u, err := url.Parse(fedReq.RequestURI())
|
|
|
|
if err != nil {
|
2021-01-19 18:14:25 +01:00
|
|
|
return util.JSONResponse{
|
2022-03-01 14:40:07 +01:00
|
|
|
Code: 400,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidParam("bad request uri"),
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
|
2021-01-19 18:14:25 +01:00
|
|
|
w := walker{
|
2022-03-01 14:40:07 +01:00
|
|
|
rootRoomID: roomID,
|
|
|
|
serverName: fedReq.Origin(),
|
|
|
|
thisServer: thisServer,
|
|
|
|
ctx: ctx,
|
2022-03-01 17:32:48 +01:00
|
|
|
cache: cache,
|
2022-03-01 14:40:07 +01:00
|
|
|
suggestedOnly: u.Query().Get("suggested_only") == "true",
|
|
|
|
limit: 1000,
|
|
|
|
// The main difference is that it does not recurse into spaces and does not support pagination.
|
|
|
|
// This is somewhat equivalent to a Client-Server request with a max_depth=1.
|
|
|
|
maxDepth: 1,
|
|
|
|
|
|
|
|
rsAPI: rsAPI,
|
|
|
|
fsAPI: fsAPI,
|
|
|
|
// inline cache as we don't have pagination in federation mode
|
|
|
|
paginationCache: make(map[string]paginationInfo),
|
|
|
|
}
|
|
|
|
return w.walk()
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func spacesHandler(
|
2022-03-01 17:32:48 +01:00
|
|
|
rsAPI roomserver.RoomserverInternalAPI,
|
|
|
|
fsAPI fs.FederationInternalAPI,
|
|
|
|
cache caching.SpaceSummaryRoomsCache,
|
2023-04-19 16:50:33 +02:00
|
|
|
thisServer spec.ServerName,
|
2021-01-19 18:14:25 +01:00
|
|
|
) func(*http.Request, *userapi.Device) util.JSONResponse {
|
2022-03-01 14:40:07 +01:00
|
|
|
// declared outside the returned handler so it persists between calls
|
|
|
|
// TODO: clear based on... time?
|
|
|
|
paginationCache := make(map[string]paginationInfo)
|
|
|
|
|
2021-01-13 19:00:38 +01:00
|
|
|
return func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
|
|
|
// Extract the room ID from the request. Sanity check request data.
|
2021-01-13 23:27:04 +01:00
|
|
|
params, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2021-01-13 19:00:38 +01:00
|
|
|
roomID := params["roomID"]
|
|
|
|
w := walker{
|
2022-03-01 14:40:07 +01:00
|
|
|
suggestedOnly: req.URL.Query().Get("suggested_only") == "true",
|
|
|
|
limit: parseInt(req.URL.Query().Get("limit"), 1000),
|
|
|
|
maxDepth: parseInt(req.URL.Query().Get("max_depth"), -1),
|
|
|
|
paginationToken: req.URL.Query().Get("from"),
|
|
|
|
rootRoomID: roomID,
|
|
|
|
caller: device,
|
|
|
|
thisServer: thisServer,
|
|
|
|
ctx: req.Context(),
|
2022-03-01 17:32:48 +01:00
|
|
|
cache: cache,
|
2022-03-01 14:40:07 +01:00
|
|
|
|
|
|
|
rsAPI: rsAPI,
|
|
|
|
fsAPI: fsAPI,
|
|
|
|
paginationCache: paginationCache,
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
return w.walk()
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
type paginationInfo struct {
|
|
|
|
processed set
|
|
|
|
unvisited []roomVisit
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
type walker struct {
|
|
|
|
rootRoomID string
|
|
|
|
caller *userapi.Device
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName
|
|
|
|
thisServer spec.ServerName
|
2022-03-01 14:40:07 +01:00
|
|
|
rsAPI roomserver.RoomserverInternalAPI
|
|
|
|
fsAPI fs.FederationInternalAPI
|
|
|
|
ctx context.Context
|
2022-03-01 17:32:48 +01:00
|
|
|
cache caching.SpaceSummaryRoomsCache
|
2022-03-01 14:40:07 +01:00
|
|
|
suggestedOnly bool
|
|
|
|
limit int
|
|
|
|
maxDepth int
|
|
|
|
paginationToken string
|
|
|
|
|
|
|
|
paginationCache map[string]paginationInfo
|
|
|
|
mu sync.Mutex
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
func (w *walker) newPaginationCache() (string, paginationInfo) {
|
|
|
|
p := paginationInfo{
|
|
|
|
processed: make(set),
|
|
|
|
unvisited: nil,
|
|
|
|
}
|
|
|
|
tok := uuid.NewString()
|
|
|
|
return tok, p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *walker) loadPaginationCache(paginationToken string) *paginationInfo {
|
2021-01-13 19:00:38 +01:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2022-03-01 14:40:07 +01:00
|
|
|
p := w.paginationCache[paginationToken]
|
|
|
|
return &p
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
func (w *walker) storePaginationCache(paginationToken string, cache paginationInfo) {
|
2021-01-13 19:00:38 +01:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2022-03-01 14:40:07 +01:00
|
|
|
w.paginationCache[paginationToken] = cache
|
|
|
|
}
|
|
|
|
|
|
|
|
type roomVisit struct {
|
2022-03-08 14:24:32 +01:00
|
|
|
roomID string
|
|
|
|
parentRoomID string
|
|
|
|
depth int
|
|
|
|
vias []string // vias to query this room by
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
func (w *walker) walk() util.JSONResponse {
|
2022-03-08 14:24:32 +01:00
|
|
|
if authorised, _ := w.authorised(w.rootRoomID, ""); !authorised {
|
2022-03-01 14:40:07 +01:00
|
|
|
if w.caller != nil {
|
|
|
|
// CS API format
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 403,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.Forbidden("room is unknown/forbidden"),
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SS API format
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 404,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotFound("room is unknown/forbidden"),
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
var discoveredRooms []fclient.MSC2946Room
|
2022-03-01 14:40:07 +01:00
|
|
|
|
|
|
|
var cache *paginationInfo
|
|
|
|
if w.paginationToken != "" {
|
|
|
|
cache = w.loadPaginationCache(w.paginationToken)
|
|
|
|
if cache == nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.InvalidParam("invalid from"),
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tok, c := w.newPaginationCache()
|
|
|
|
cache = &c
|
|
|
|
w.paginationToken = tok
|
|
|
|
// Begin walking the graph starting with the room ID in the request in a queue of unvisited rooms
|
|
|
|
c.unvisited = append(c.unvisited, roomVisit{
|
2022-03-08 14:24:32 +01:00
|
|
|
roomID: w.rootRoomID,
|
|
|
|
parentRoomID: "",
|
|
|
|
depth: 0,
|
2022-03-01 14:40:07 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
processed := cache.processed
|
|
|
|
unvisited := cache.unvisited
|
|
|
|
|
|
|
|
// Depth first -> stack data structure
|
2021-01-13 19:00:38 +01:00
|
|
|
for len(unvisited) > 0 {
|
2022-03-01 14:40:07 +01:00
|
|
|
if len(discoveredRooms) >= w.limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop the stack
|
|
|
|
rv := unvisited[len(unvisited)-1]
|
|
|
|
unvisited = unvisited[:len(unvisited)-1]
|
|
|
|
// If this room has already been processed, skip.
|
|
|
|
// If this room exceeds the specified depth, skip.
|
|
|
|
if processed.isSet(rv.roomID) || rv.roomID == "" || (w.maxDepth > 0 && rv.depth > w.maxDepth) {
|
2021-01-13 19:00:38 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
|
2021-01-13 19:00:38 +01:00
|
|
|
// Mark this room as processed.
|
2022-03-01 14:40:07 +01:00
|
|
|
processed.set(rv.roomID)
|
|
|
|
|
|
|
|
// if this room is not a space room, skip.
|
|
|
|
var roomType string
|
2023-04-19 16:50:33 +02:00
|
|
|
create := w.stateEvent(rv.roomID, spec.MRoomCreate, "")
|
2022-03-01 14:40:07 +01:00
|
|
|
if create != nil {
|
|
|
|
// escape the `.`s so gjson doesn't think it's nested
|
|
|
|
roomType = gjson.GetBytes(create.Content(), strings.ReplaceAll(ConstCreateEventContentKey, ".", `\.`)).Str
|
|
|
|
}
|
2021-01-19 18:14:25 +01:00
|
|
|
|
2021-01-20 18:03:35 +01:00
|
|
|
// Collect rooms/events to send back (either locally or fetched via federation)
|
2023-04-06 10:55:01 +02:00
|
|
|
var discoveredChildEvents []fclient.MSC2946StrippedEvent
|
2021-01-20 18:03:35 +01:00
|
|
|
|
|
|
|
// If we know about this room and the caller is authorised (joined/world_readable) then pull
|
|
|
|
// events locally
|
2022-03-08 14:24:32 +01:00
|
|
|
roomExists := w.roomExists(rv.roomID)
|
|
|
|
if !roomExists {
|
2021-01-20 18:03:35 +01:00
|
|
|
// attempt to query this room over federation, as either we've never heard of it before
|
|
|
|
// or we've left it and hence are not authorised (but info may be exposed regardless)
|
2022-03-24 11:03:22 +01:00
|
|
|
fedRes := w.federatedRoomInfo(rv.roomID, rv.vias)
|
2021-01-20 18:03:35 +01:00
|
|
|
if fedRes != nil {
|
2022-03-01 14:40:07 +01:00
|
|
|
discoveredChildEvents = fedRes.Room.ChildrenState
|
|
|
|
discoveredRooms = append(discoveredRooms, fedRes.Room)
|
|
|
|
if len(fedRes.Children) > 0 {
|
|
|
|
discoveredRooms = append(discoveredRooms, fedRes.Children...)
|
|
|
|
}
|
|
|
|
// mark this room as a space room as the federated server responded.
|
|
|
|
// we need to do this so we add the children of this room to the unvisited stack
|
|
|
|
// as these children may be rooms we do know about.
|
|
|
|
roomType = ConstCreateEventContentValueSpace
|
2021-01-20 18:03:35 +01:00
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
} else if authorised, isJoinedOrInvited := w.authorised(rv.roomID, rv.parentRoomID); authorised {
|
|
|
|
// Get all `m.space.child` state events for this room
|
|
|
|
events, err := w.childReferences(rv.roomID)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).WithField("room_id", rv.roomID).Error("failed to extract references for room")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
discoveredChildEvents = events
|
|
|
|
|
|
|
|
pubRoom := w.publicRoomsChunk(rv.roomID)
|
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
discoveredRooms = append(discoveredRooms, fclient.MSC2946Room{
|
2022-03-08 14:24:32 +01:00
|
|
|
PublicRoom: *pubRoom,
|
|
|
|
RoomType: roomType,
|
|
|
|
ChildrenState: events,
|
|
|
|
})
|
|
|
|
// don't walk children if the user is not joined/invited to the space
|
|
|
|
if !isJoinedOrInvited {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// room exists but user is not authorised
|
|
|
|
continue
|
2021-01-20 18:03:35 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
// don't walk the children
|
|
|
|
// if the parent is not a space room
|
|
|
|
if roomType != ConstCreateEventContentValueSpace {
|
|
|
|
continue
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
// For each referenced room ID in the child events being returned to the caller
|
|
|
|
// add the room ID to the queue of unvisited rooms. Loop from the beginning.
|
|
|
|
// We need to invert the order here because the child events are lo->hi on the timestamp,
|
|
|
|
// so we need to ensure we pop in the same lo->hi order, which won't be the case if we
|
|
|
|
// insert the highest timestamp last in a stack.
|
|
|
|
for i := len(discoveredChildEvents) - 1; i >= 0; i-- {
|
|
|
|
spaceContent := struct {
|
|
|
|
Via []string `json:"via"`
|
|
|
|
}{}
|
|
|
|
ev := discoveredChildEvents[i]
|
|
|
|
_ = json.Unmarshal(ev.Content, &spaceContent)
|
|
|
|
unvisited = append(unvisited, roomVisit{
|
2022-03-08 14:24:32 +01:00
|
|
|
roomID: ev.StateKey,
|
|
|
|
parentRoomID: rv.roomID,
|
|
|
|
depth: rv.depth + 1,
|
|
|
|
vias: spaceContent.Via,
|
2022-03-01 14:40:07 +01:00
|
|
|
})
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
2021-01-13 19:00:38 +01:00
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
if len(unvisited) > 0 {
|
|
|
|
// we still have more rooms so we need to send back a pagination token,
|
|
|
|
// we probably hit a room limit
|
|
|
|
cache.processed = processed
|
|
|
|
cache.unvisited = unvisited
|
|
|
|
w.storePaginationCache(w.paginationToken, *cache)
|
|
|
|
} else {
|
|
|
|
// clear the pagination token so we don't send it back to the client
|
|
|
|
// Note we do NOT nuke the cache just in case this response is lost
|
|
|
|
// and the client retries it.
|
|
|
|
w.paginationToken = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.caller != nil {
|
|
|
|
// return CS API format
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: MSC2946ClientResponse{
|
|
|
|
Rooms: discoveredRooms,
|
|
|
|
NextBatch: w.paginationToken,
|
|
|
|
},
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
// return SS API format
|
|
|
|
// the first discovered room will be the room asked for, and subsequent ones the depth=1 children
|
|
|
|
if len(discoveredRooms) == 0 {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 404,
|
2023-05-10 00:46:49 +02:00
|
|
|
JSON: spec.NotFound("room is unknown/forbidden"),
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
2023-04-06 10:55:01 +02:00
|
|
|
JSON: fclient.MSC2946SpacesResponse{
|
2022-03-01 14:40:07 +01:00
|
|
|
Room: discoveredRooms[0],
|
|
|
|
Children: discoveredRooms[1:],
|
|
|
|
},
|
|
|
|
}
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
func (w *walker) stateEvent(roomID, evType, stateKey string) *types.HeaderedEvent {
|
2021-01-13 19:00:38 +01:00
|
|
|
var queryRes roomserver.QueryCurrentStateResponse
|
|
|
|
tuple := gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: evType,
|
|
|
|
StateKey: stateKey,
|
|
|
|
}
|
|
|
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{tuple},
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return queryRes.StateEvents[tuple]
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
func (w *walker) publicRoomsChunk(roomID string) *fclient.PublicRoom {
|
2021-01-13 19:00:38 +01:00
|
|
|
pubRooms, err := roomserver.PopulatePublicRooms(w.ctx, []string{roomID}, w.rsAPI)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to PopulatePublicRooms")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(pubRooms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &pubRooms[0]
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:14:25 +01:00
|
|
|
// federatedRoomInfo returns more of the spaces graph from another server. Returns nil if this was
|
|
|
|
// unsuccessful.
|
2023-04-06 10:55:01 +02:00
|
|
|
func (w *walker) federatedRoomInfo(roomID string, vias []string) *fclient.MSC2946SpacesResponse {
|
2021-01-19 18:14:25 +01:00
|
|
|
// only do federated requests for client requests
|
|
|
|
if w.caller == nil {
|
2022-03-24 11:03:22 +01:00
|
|
|
return nil
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
2022-03-01 17:32:48 +01:00
|
|
|
resp, ok := w.cache.GetSpaceSummary(roomID)
|
|
|
|
if ok {
|
|
|
|
util.GetLogger(w.ctx).Debugf("Returning cached response for %s", roomID)
|
2022-03-24 11:03:22 +01:00
|
|
|
return &resp
|
2022-03-01 17:32:48 +01:00
|
|
|
}
|
|
|
|
util.GetLogger(w.ctx).Debugf("Querying %s via %+v", roomID, vias)
|
2021-01-19 18:14:25 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
// query more of the spaces graph using these servers
|
2022-03-01 14:40:07 +01:00
|
|
|
for _, serverName := range vias {
|
2021-01-19 18:14:25 +01:00
|
|
|
if serverName == string(w.thisServer) {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-19 16:50:33 +02:00
|
|
|
res, err := w.fsAPI.MSC2946Spaces(ctx, w.thisServer, spec.ServerName(serverName), roomID, w.suggestedOnly)
|
2021-01-19 18:14:25 +01:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Warnf("failed to call MSC2946Spaces on server %s", serverName)
|
|
|
|
continue
|
|
|
|
}
|
2022-03-01 15:22:59 +01:00
|
|
|
// ensure nil slices are empty as we send this to the client sometimes
|
|
|
|
if res.Room.ChildrenState == nil {
|
2023-04-06 10:55:01 +02:00
|
|
|
res.Room.ChildrenState = []fclient.MSC2946StrippedEvent{}
|
2022-03-01 15:22:59 +01:00
|
|
|
}
|
|
|
|
for i := 0; i < len(res.Children); i++ {
|
|
|
|
child := res.Children[i]
|
|
|
|
if child.ChildrenState == nil {
|
2023-04-06 10:55:01 +02:00
|
|
|
child.ChildrenState = []fclient.MSC2946StrippedEvent{}
|
2022-03-01 15:22:59 +01:00
|
|
|
}
|
|
|
|
res.Children[i] = child
|
|
|
|
}
|
2022-03-01 17:32:48 +01:00
|
|
|
w.cache.StoreSpaceSummary(roomID, res)
|
2022-03-01 15:22:59 +01:00
|
|
|
|
2022-03-24 11:03:22 +01:00
|
|
|
return &res
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
2022-03-24 11:03:22 +01:00
|
|
|
return nil
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *walker) roomExists(roomID string) bool {
|
|
|
|
var queryRes roomserver.QueryServerJoinedToRoomResponse
|
|
|
|
err := w.rsAPI.QueryServerJoinedToRoom(w.ctx, &roomserver.QueryServerJoinedToRoomRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
ServerName: w.thisServer,
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryServerJoinedToRoom")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// if the room exists but we aren't in the room then we might have stale data so we want to fetch
|
|
|
|
// it fresh via federation
|
|
|
|
return queryRes.RoomExists && queryRes.IsInRoom
|
|
|
|
}
|
|
|
|
|
2021-01-13 19:00:38 +01:00
|
|
|
// authorised returns true iff the user is joined this room or the room is world_readable
|
2022-03-08 14:24:32 +01:00
|
|
|
func (w *walker) authorised(roomID, parentRoomID string) (authed, isJoinedOrInvited bool) {
|
2021-01-19 18:14:25 +01:00
|
|
|
if w.caller != nil {
|
2022-03-08 14:24:32 +01:00
|
|
|
return w.authorisedUser(roomID, parentRoomID)
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
return w.authorisedServer(roomID), false
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:07:45 +02:00
|
|
|
// authorisedServer returns true iff the server is joined this room or the room is world_readable, public, or knockable
|
2021-01-19 18:14:25 +01:00
|
|
|
func (w *walker) authorisedServer(roomID string) bool {
|
2022-03-08 14:24:32 +01:00
|
|
|
// Check history visibility / join rules first
|
2021-01-19 18:14:25 +01:00
|
|
|
hisVisTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomHistoryVisibility,
|
2021-01-19 18:14:25 +01:00
|
|
|
StateKey: "",
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomJoinRules,
|
2022-03-08 14:24:32 +01:00
|
|
|
StateKey: "",
|
|
|
|
}
|
2021-01-19 18:14:25 +01:00
|
|
|
var queryRoomRes roomserver.QueryCurrentStateResponse
|
|
|
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
2022-03-08 14:24:32 +01:00
|
|
|
hisVisTuple, joinRuleTuple,
|
2021-01-19 18:14:25 +01:00
|
|
|
},
|
|
|
|
}, &queryRoomRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryCurrentState")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hisVisEv := queryRoomRes.StateEvents[hisVisTuple]
|
|
|
|
if hisVisEv != nil {
|
|
|
|
hisVis, _ := hisVisEv.HistoryVisibility()
|
|
|
|
if hisVis == "world_readable" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
|
|
|
|
// check if this room is a restricted room and if so, we need to check if the server is joined to an allowed room ID
|
|
|
|
// in addition to the actual room ID (but always do the actual one first as it's quicker in the common case)
|
|
|
|
allowJoinedToRoomIDs := []string{roomID}
|
|
|
|
joinRuleEv := queryRoomRes.StateEvents[joinRuleTuple]
|
2022-08-12 15:07:45 +02:00
|
|
|
|
2022-03-08 14:24:32 +01:00
|
|
|
if joinRuleEv != nil {
|
2022-08-12 15:07:45 +02:00
|
|
|
rule, ruleErr := joinRuleEv.JoinRule()
|
|
|
|
if ruleErr != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(ruleErr).WithField("parent_room_id", roomID).Warn("failed to get join rule")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
if rule == spec.Public || rule == spec.Knock {
|
2022-08-12 15:07:45 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:50:33 +02:00
|
|
|
if rule == spec.Restricted {
|
2022-08-12 15:07:45 +02:00
|
|
|
allowJoinedToRoomIDs = append(allowJoinedToRoomIDs, w.restrictedJoinRuleAllowedRooms(joinRuleEv, "m.room_membership")...)
|
|
|
|
}
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
|
|
|
|
// check if server is joined to any allowed room
|
|
|
|
for _, allowedRoomID := range allowJoinedToRoomIDs {
|
|
|
|
var queryRes fs.QueryJoinedHostServerNamesInRoomResponse
|
|
|
|
err = w.fsAPI.QueryJoinedHostServerNamesInRoom(w.ctx, &fs.QueryJoinedHostServerNamesInRoomRequest{
|
|
|
|
RoomID: allowedRoomID,
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryJoinedHostServerNamesInRoom")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, srv := range queryRes.ServerNames {
|
|
|
|
if srv == w.serverName {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
|
2021-01-19 18:14:25 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:07:45 +02:00
|
|
|
// authorisedUser returns true iff the user is invited/joined this room or the room is world_readable
|
|
|
|
// or if the room has a public or knock join rule.
|
2022-03-08 14:24:32 +01:00
|
|
|
// Failing that, if the room has a restricted join rule and belongs to the space parent listed, it will return true.
|
|
|
|
func (w *walker) authorisedUser(roomID, parentRoomID string) (authed bool, isJoinedOrInvited bool) {
|
2021-01-13 19:00:38 +01:00
|
|
|
hisVisTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomHistoryVisibility,
|
2021-01-13 19:00:38 +01:00
|
|
|
StateKey: "",
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomJoinRules,
|
2022-03-08 14:24:32 +01:00
|
|
|
StateKey: "",
|
|
|
|
}
|
2021-01-13 19:00:38 +01:00
|
|
|
roomMemberTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomMember,
|
2021-01-13 19:00:38 +01:00
|
|
|
StateKey: w.caller.UserID,
|
|
|
|
}
|
|
|
|
var queryRes roomserver.QueryCurrentStateResponse
|
|
|
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
2022-03-08 14:24:32 +01:00
|
|
|
hisVisTuple, joinRuleTuple, roomMemberTuple,
|
2021-01-13 19:00:38 +01:00
|
|
|
},
|
|
|
|
}, &queryRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryCurrentState")
|
2022-03-08 14:24:32 +01:00
|
|
|
return false, false
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
memberEv := queryRes.StateEvents[roomMemberTuple]
|
|
|
|
if memberEv != nil {
|
|
|
|
membership, _ := memberEv.Membership()
|
2023-04-19 16:50:33 +02:00
|
|
|
if membership == spec.Join || membership == spec.Invite {
|
2022-03-08 14:24:32 +01:00
|
|
|
return true, true
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
hisVisEv := queryRes.StateEvents[hisVisTuple]
|
2021-01-13 19:00:38 +01:00
|
|
|
if hisVisEv != nil {
|
|
|
|
hisVis, _ := hisVisEv.HistoryVisibility()
|
|
|
|
if hisVis == "world_readable" {
|
2022-03-08 14:24:32 +01:00
|
|
|
return true, false
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
joinRuleEv := queryRes.StateEvents[joinRuleTuple]
|
|
|
|
if parentRoomID != "" && joinRuleEv != nil {
|
|
|
|
var allowed bool
|
2022-08-12 15:07:45 +02:00
|
|
|
rule, ruleErr := joinRuleEv.JoinRule()
|
|
|
|
if ruleErr != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(ruleErr).WithField("parent_room_id", parentRoomID).Warn("failed to get join rule")
|
2023-04-19 16:50:33 +02:00
|
|
|
} else if rule == spec.Public || rule == spec.Knock {
|
2022-08-12 15:07:45 +02:00
|
|
|
allowed = true
|
2023-04-19 16:50:33 +02:00
|
|
|
} else if rule == spec.Restricted {
|
2022-08-12 15:07:45 +02:00
|
|
|
allowedRoomIDs := w.restrictedJoinRuleAllowedRooms(joinRuleEv, "m.room_membership")
|
|
|
|
// check parent is in the allowed set
|
|
|
|
for _, a := range allowedRoomIDs {
|
|
|
|
if parentRoomID == a {
|
|
|
|
allowed = true
|
|
|
|
break
|
|
|
|
}
|
2022-03-08 14:24:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if allowed {
|
|
|
|
// ensure caller is joined to the parent room
|
|
|
|
var queryRes2 roomserver.QueryCurrentStateResponse
|
|
|
|
err = w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
|
|
RoomID: parentRoomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
roomMemberTuple,
|
|
|
|
},
|
|
|
|
}, &queryRes2)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(w.ctx).WithError(err).WithField("parent_room_id", parentRoomID).Warn("failed to check user is joined to parent room")
|
|
|
|
} else {
|
|
|
|
memberEv = queryRes2.StateEvents[roomMemberTuple]
|
|
|
|
if memberEv != nil {
|
|
|
|
membership, _ := memberEv.Membership()
|
2023-04-19 16:50:33 +02:00
|
|
|
if membership == spec.Join {
|
2022-03-08 14:24:32 +01:00
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
func (w *walker) restrictedJoinRuleAllowedRooms(joinRuleEv *types.HeaderedEvent, allowType string) (allows []string) {
|
2022-03-08 14:24:32 +01:00
|
|
|
rule, _ := joinRuleEv.JoinRule()
|
2023-04-19 16:50:33 +02:00
|
|
|
if rule != spec.Restricted {
|
2022-03-08 14:24:32 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var jrContent gomatrixserverlib.JoinRuleContent
|
|
|
|
if err := json.Unmarshal(joinRuleEv.Content(), &jrContent); err != nil {
|
|
|
|
util.GetLogger(w.ctx).Warnf("failed to check join_rule on room %s: %s", joinRuleEv.RoomID(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, allow := range jrContent.Allow {
|
|
|
|
if allow.Type == allowType {
|
|
|
|
allows = append(allows, allow.RoomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
// references returns all child references pointing to or from this room.
|
2023-04-06 10:55:01 +02:00
|
|
|
func (w *walker) childReferences(roomID string) ([]fclient.MSC2946StrippedEvent, error) {
|
2022-03-01 14:40:07 +01:00
|
|
|
createTuple := gomatrixserverlib.StateKeyTuple{
|
2023-04-19 16:50:33 +02:00
|
|
|
EventType: spec.MRoomCreate,
|
2022-03-01 14:40:07 +01:00
|
|
|
StateKey: "",
|
|
|
|
}
|
|
|
|
var res roomserver.QueryCurrentStateResponse
|
|
|
|
err := w.rsAPI.QueryCurrentState(context.Background(), &roomserver.QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
AllowWildcards: true,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
createTuple, {
|
|
|
|
EventType: ConstSpaceChildEventType,
|
|
|
|
StateKey: "*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &res)
|
2021-01-13 19:00:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
|
|
|
|
// don't return any child refs if the room is not a space room
|
|
|
|
if res.StateEvents[createTuple] != nil {
|
|
|
|
// escape the `.`s so gjson doesn't think it's nested
|
|
|
|
roomType := gjson.GetBytes(res.StateEvents[createTuple].Content(), strings.ReplaceAll(ConstCreateEventContentKey, ".", `\.`)).Str
|
|
|
|
if roomType != ConstCreateEventContentValueSpace {
|
2023-04-06 10:55:01 +02:00
|
|
|
return []fclient.MSC2946StrippedEvent{}, nil
|
2022-03-01 14:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(res.StateEvents, createTuple)
|
|
|
|
|
2023-04-06 10:55:01 +02:00
|
|
|
el := make([]fclient.MSC2946StrippedEvent, 0, len(res.StateEvents))
|
2022-03-01 14:40:07 +01:00
|
|
|
for _, ev := range res.StateEvents {
|
|
|
|
content := gjson.ParseBytes(ev.Content())
|
2021-01-15 12:44:36 +01:00
|
|
|
// only return events that have a `via` key as per MSC1772
|
|
|
|
// else we'll incorrectly walk redacted events (as the link
|
|
|
|
// is in the state_key)
|
2022-03-01 14:40:07 +01:00
|
|
|
if content.Get("via").Exists() {
|
2023-05-02 16:03:16 +02:00
|
|
|
strip := stripped(ev.PDU)
|
2021-01-20 18:03:35 +01:00
|
|
|
if strip == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
// if suggested only and this child isn't suggested, skip it.
|
|
|
|
// if suggested only = false we include everything so don't need to check the content.
|
|
|
|
if w.suggestedOnly && !content.Get("suggested").Bool() {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-20 18:03:35 +01:00
|
|
|
el = append(el, *strip)
|
2021-01-15 12:44:36 +01:00
|
|
|
}
|
2021-01-13 19:00:38 +01:00
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
// sort by origin_server_ts as per MSC2946
|
|
|
|
sort.Slice(el, func(i, j int) bool {
|
|
|
|
return el[i].OriginServerTS < el[j].OriginServerTS
|
|
|
|
})
|
|
|
|
|
2021-01-13 19:00:38 +01:00
|
|
|
return el, nil
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
type set map[string]struct{}
|
|
|
|
|
|
|
|
func (s set) set(val string) {
|
|
|
|
s[val] = struct{}{}
|
|
|
|
}
|
|
|
|
func (s set) isSet(val string) bool {
|
|
|
|
_, ok := s[val]
|
|
|
|
return ok
|
|
|
|
}
|
2021-01-19 18:14:25 +01:00
|
|
|
|
2023-05-02 16:03:16 +02:00
|
|
|
func stripped(ev gomatrixserverlib.PDU) *fclient.MSC2946StrippedEvent {
|
2021-01-19 18:14:25 +01:00
|
|
|
if ev.StateKey() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-06 10:55:01 +02:00
|
|
|
return &fclient.MSC2946StrippedEvent{
|
2022-03-01 14:40:07 +01:00
|
|
|
Type: ev.Type(),
|
|
|
|
StateKey: *ev.StateKey(),
|
|
|
|
Content: ev.Content(),
|
2023-06-06 22:55:18 +02:00
|
|
|
Sender: ev.SenderID(),
|
2022-03-01 14:40:07 +01:00
|
|
|
OriginServerTS: ev.OriginServerTS(),
|
2021-01-19 18:14:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
func parseInt(intstr string, defaultVal int) int {
|
|
|
|
i, err := strconv.ParseInt(intstr, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return int(i)
|
|
|
|
}
|