mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-08 15:33:09 -06:00
* Correctly redact events over federation (#2526) * Ensure we check powerlevel/origin before redacting an event * Add passing test * Use pl.UserLevel * Make check more readable, also check for the sender * Add new next steps page to the documentation * Highlighting in docs * Rename the page to "Optimise your installation" * Attempt to raise the file descriptor limit at startup (#2527) * Add `--difference` to `resolve-state` tool * Make the linter happy again * generic CaddyFile in front of Dendrite (monolith) (#2531) for Caddy 2.5.x Co-authored-by: emanuele.aliberti <emanuele.aliberti@mtka.eu> * Handle state before, send history visibility in output (#2532) * Check state before event * Tweaks * Refactor a bit, include in output events * Don't waste time if soft failed either * Tweak control flow, comments, use GMSL history visibility type * Fix rare panic when returning user devices over federation (#2534) * Add `InputDeviceListUpdate` to the keyserver, remove old input API (#2536) * Add `InputDeviceListUpdate` to the keyserver, remove old input API * Fix copyright * Log more information when a device list update fails * Fix nats.go commit (#2540) Signed-off-by: Jean Lucas <jean@4ray.co> * Don't return `end` if there are not more messages (#2542) * Be more spec compliant * Move lazyLoadMembers to own method * Return an error if trying to invite a malformed user ID (#2543) * Add `evacuateUser` endpoint, use it when deactivating accounts (#2545) * Add `evacuateUser` endpoint, use it when deactivating accounts * Populate the API * Clean up user devices when deactivating * Include invites, delete pushers * Silence presence logs (#2547) * Blacklist `Guest users can join guest_access rooms` test until it can be investigated * Disable WebAssembly builds for now * Try to fix backfilling (#2548) * Try to fix backfilling * Return start/end to not confuse clients * Update GMSL * Update GMSL * Roomserver producers package (#2546) * Give the roomserver a producers package * Change init point * Populate ACLs API * Fix build issues * `RoomEventProducer` naming * Version 0.8.9 (#2549) * Version 0.8.9 * Update changelog * feat+fix: Ignore unknown keys and verify required fields are present in appservice registration files (#2550) * fix: ignore unknown keys in appservice configs fixes matrix-org/dendrite#1567 * feat: verify required fields in appservice configs * Use new testrig for key changes tests (#2552) * Use new testrig for tests * Log the error message * Fix QuerySharedUsers for the SyncAPI keychange consumer (#2554) * Make more use of base.BaseDendrite * Fix QuerySharedUsers if no UserIDs are supplied * Return clearer error when no state NID exists for an event (#2555) * Wrap error from `SnapshotNIDFromEventID` * Hopefully fix read receipts timestamps (#2557) This should avoid coercions between signed and unsigned ints which might fix problems like `sql: converting argument $5 type: uint64 values with high bit set are not supported`. * Fix nil pointer access when redacting events (#2560) * Fix issue `uint64 values with high bit are not supported` in presence (#2562) * Fix issue #2528 * Use gomatrixserverlib.Timestamp * Use ParseUint instead of ParseInt * Update Pinecone to matrix-org/pinecone@1ce778f * Ristretto cache (#2563) * Try Ristretto cache * Tweak * It's beautiful * Update GMSL * More strict keyable interface * Fix that some more * Make less panicky * Don't enforce mutability checks for now * Determine mutability using deep equality * Tweaks * Namespace keys * Make federation caches mutable * Update cost estimation, add metric * Update GMSL * Estimate cost for metrics better * Reduce counters a bit * Try caching events * Some guards * Try again * Try this * Use separate caches for hopefully better hash distribution * Fix bug with admitting events into cache * Try to fix bugs * Check nil * Try that again * Preserve order jeezo this is messy * thanks VS Code for doing exactly the wrong thing * Try this again * Be more specific * aaaaargh * One more time * That might be better * Stronger sorting * Cache expiries, async publishing of EDUs * Put it back * Use a shared cache again * Cost estimation fixes * Update ristretto * Reduce counters a bit * Clean up a bit * Update GMSL * 1GB * Configurable cache sizees * Tweaks * Add `config.DataUnit` for specifying friendly cache sizes * Various tweaks * Update GMSL * Add back some lazy loading caching * Include key in cost * Include key in cost * Tweak max age handling, config key name * Only register prometheus metrics if requested * Review comments @S7evinK * Don't return errors when creating caches (it is better just to crash since otherwise we'll `nil`-pointer exception everywhere) * Review comments * Update sample configs * Update GHA Workflow * Update Complement images to Go 1.18 * Remove the cache test from the federation API as we no longer guarantee immediate cache admission * Don't check the caches in the renewal test * Possibly fix the upgrade tests * Update to matrix-org/gomatrixserverlib#322 * Update documentation to refer to Go 1.18 * Minor SendToDevice fix (#2565) * Avoid unnecessary marshalling if sending to the local server * Fix ordering of ToDevice messages * Revive SendToDevice test * Use `/v3` to request media from remote servers (update to matrix-org/gomatrixserverlib#324) * Pointerise `types.RoomInfo` in the cache so we can update it in-place in the latest events updater * Add a Troubleshooting page * Update `sytest-whitelist` * Use sync API database in `filterSharedUsers` (#2572) * Add function to the sync API storage package for filtering shared users * Use the database instead of asking the RS API * Fix unit tests * Fix map handling in `filterSharedUsers` * Update 1_createusers.md (#2571) * Update 1_createusers.md Added description on how to create user accounts when running in docker. * Update 1_createusers.md Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Fix connection_string format in dendrite-sample.polylith.yaml (#2574) * History visibility database changes (#2533) * Add new history_visibility column * Update SQL queries to include history_visibility * Store the history visibilty calculated by the roomserver * Update GMSL * Update migrations * Fix migration * Update GMSL * Fix `go.sum` * Update GMSL to use sql.Scanner & sql.Valuer * Re-order migration/table creation * Update gomatrixserverlib * Add history_visibility column to current_room_state * Fix migrations * Return error instead of Fatal log Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Tweak cache counters (#2575) * Tweak cache counters This makes the number of counters relative to the maximum cache size. Since the counters effectively manage the size of the bloom filter, larger caches need more counters and smaller caches need less. 10 counters per 1KB data means that the default cache size of 1GB should result in a bloom filter and TinyLRU admission set of about 16MB estimated. * Remove line left by accident * Set historyVisibility in rowsToStreamEvents * Update FAQ * Add event state key cache (#2576) * Explain how SRV works in Matrix and discourage using it (#2577) * Explain how SRV works in Matrix and discourage using it * Minor tweaks to formatting Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Fix issue with membership event_nid being 0 (#2580) * docs: Add build page; correct proxy info; fix Caddy example (#2579) * Add build page; correct proxy info; fix Caddy example * Improve Caddyfile example * Apply review comments; add polylith Caddyfile * Bump tzinfo from 1.2.9 to 1.2.10 in /docs (#2584) Bumps [tzinfo](https://github.com/tzinfo/tzinfo) from 1.2.9 to 1.2.10. - [Release notes](https://github.com/tzinfo/tzinfo/releases) - [Changelog](https://github.com/tzinfo/tzinfo/blob/master/CHANGES.md) - [Commits](https://github.com/tzinfo/tzinfo/compare/v1.2.9...v1.2.10) --- updated-dependencies: - dependency-name: tzinfo dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Membership updater refactoring (#2541) * Membership updater refactoring * Pass in membership state * Use membership check rather than referring to state directly * Delete irrelevant membership states * We don't need the leave event after all * Tweaks * Put a log entry in that I might stand a chance of finding * Be less panicky * Tweak invite handling * Don't freak if we can't find the event NID * Use event NID from `types.Event` * Clean up * Better invite handling * Placate the almighty linter * Blacklist a Sytest which is otherwise fine under Complement for reasons I don't understand * Fix the sytest after all (thanks @S7evinK for the spot) * Try to fix HTTP 500s on `/members` (#2581) * Update database migrations, remove goose (#2264) * Add new db migration * Update migrations Remove goose * Add possibility to test direct upgrades * Try to fix WASM test * Add checks for specific migrations * Remove AddMigration Use WithTransaction Add Dendrite version to table * Fix linter issues * Update tests * Update comments, outdent if * Namespace migrations * Add direct upgrade tests, skipping over one version * Split migrations * Update go version in CI * Fix copy&paste mistake * Use contexts in migrations Co-authored-by: kegsay <kegan@matrix.org> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Add .well-known/matrix/client to clientapi (#2551) Signed-off-by: Jonathan Bartlett <jonathan@jonnobrow.co.uk> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> * Remove `room_id` field from MSC2946 stripped events (closes #2588) * Remove `goose` from Dockerfiles * Make the User API responsible for sending account data output events (#2592) * Make the User API responsible for sending account data output events * Clean up producer * Review comments * Update NATS Server and nats.go to use upstream * Set CORS headers for HTTP 404 and 405 errors (#2599) * Set CORS headers for the 404s * Use custom handlers, plus one for HTTP 405 too * Tweak setup * Add to muxes too * Tidy up some more * Use built-in HTTP 404 handler * Don't bother setting it for federation-facing * Optimise checking other servers allowed to see events (#2596) * Try optimising checking if server is allowed to see event * Fix error * Handle case where snapshot NID is 0 * Fix query * Update SQL * Clean up `CheckServerAllowedToSeeEvent` * Not supported on SQLite * Maybe placate the unit tests * Review comments * De-race `types.RoomInfo` (#2600) * De-race `CompleteSync` (#2601) The `err` was coming from outside of the goroutine and being written to by concurrent goroutines. * Version 0.9.0 (#2602) Co-authored-by: Till <2353100+S7evinK@users.noreply.github.com> Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com> Co-authored-by: Till Faelligen <davidf@element.io> Co-authored-by: Emanuele Aliberti <dev@mtka.eu> Co-authored-by: emanuele.aliberti <emanuele.aliberti@mtka.eu> Co-authored-by: Jean Lucas <jean@4ray.co> Co-authored-by: Kabir Kwatra <kabir@kwatra.me> Co-authored-by: andreever <52261463+andreever@users.noreply.github.com> Co-authored-by: Maximilian Gaedig <38767445+MaximilianGaedig@users.noreply.github.com> Co-authored-by: Tulir Asokan <tulir@maunium.net> Co-authored-by: Matt Holt <mholt@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: kegsay <kegan@matrix.org> Co-authored-by: Jonathan Bartlett <34320158+Jonnobrow@users.noreply.github.com>
722 lines
23 KiB
Go
722 lines
23 KiB
Go
// 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"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/mux"
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
fs "github.com/matrix-org/dendrite/federationapi/api"
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
roomserver "github.com/matrix-org/dendrite/roomserver/api"
|
|
"github.com/matrix-org/dendrite/setup/base"
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
"github.com/matrix-org/util"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
const (
|
|
ConstCreateEventContentKey = "type"
|
|
ConstCreateEventContentValueSpace = "m.space"
|
|
ConstSpaceChildEventType = "m.space.child"
|
|
ConstSpaceParentEventType = "m.space.parent"
|
|
)
|
|
|
|
type MSC2946ClientResponse struct {
|
|
Rooms []gomatrixserverlib.MSC2946Room `json:"rooms"`
|
|
NextBatch string `json:"next_batch,omitempty"`
|
|
}
|
|
|
|
// Enable this MSC
|
|
func Enable(
|
|
base *base.BaseDendrite, rsAPI roomserver.RoomserverInternalAPI, userAPI userapi.UserInternalAPI,
|
|
fsAPI fs.FederationInternalAPI, keyRing gomatrixserverlib.JSONVerifier, cache caching.SpaceSummaryRoomsCache,
|
|
) error {
|
|
clientAPI := httputil.MakeAuthAPI("spaces", userAPI, spacesHandler(rsAPI, fsAPI, cache, base.Cfg.Global.ServerName))
|
|
base.PublicClientAPIMux.Handle("/v1/rooms/{roomID}/hierarchy", clientAPI).Methods(http.MethodGet, http.MethodOptions)
|
|
base.PublicClientAPIMux.Handle("/unstable/org.matrix.msc2946/rooms/{roomID}/hierarchy", clientAPI).Methods(http.MethodGet, http.MethodOptions)
|
|
|
|
fedAPI := httputil.MakeExternalAPI(
|
|
"msc2946_fed_spaces", func(req *http.Request) util.JSONResponse {
|
|
fedReq, errResp := gomatrixserverlib.VerifyHTTPRequest(
|
|
req, time.Now(), base.Cfg.Global.ServerName, keyRing,
|
|
)
|
|
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"]
|
|
return federatedSpacesHandler(req.Context(), fedReq, roomID, cache, rsAPI, fsAPI, base.Cfg.Global.ServerName)
|
|
},
|
|
)
|
|
base.PublicFederationAPIMux.Handle("/unstable/org.matrix.msc2946/hierarchy/{roomID}", fedAPI).Methods(http.MethodGet)
|
|
base.PublicFederationAPIMux.Handle("/v1/hierarchy/{roomID}", fedAPI).Methods(http.MethodGet)
|
|
return nil
|
|
}
|
|
|
|
func federatedSpacesHandler(
|
|
ctx context.Context, fedReq *gomatrixserverlib.FederationRequest, roomID string,
|
|
cache caching.SpaceSummaryRoomsCache,
|
|
rsAPI roomserver.RoomserverInternalAPI, fsAPI fs.FederationInternalAPI,
|
|
thisServer gomatrixserverlib.ServerName,
|
|
) util.JSONResponse {
|
|
u, err := url.Parse(fedReq.RequestURI())
|
|
if err != nil {
|
|
return util.JSONResponse{
|
|
Code: 400,
|
|
JSON: jsonerror.InvalidParam("bad request uri"),
|
|
}
|
|
}
|
|
|
|
w := walker{
|
|
rootRoomID: roomID,
|
|
serverName: fedReq.Origin(),
|
|
thisServer: thisServer,
|
|
ctx: ctx,
|
|
cache: cache,
|
|
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()
|
|
}
|
|
|
|
func spacesHandler(
|
|
rsAPI roomserver.RoomserverInternalAPI,
|
|
fsAPI fs.FederationInternalAPI,
|
|
cache caching.SpaceSummaryRoomsCache,
|
|
thisServer gomatrixserverlib.ServerName,
|
|
) func(*http.Request, *userapi.Device) util.JSONResponse {
|
|
// declared outside the returned handler so it persists between calls
|
|
// TODO: clear based on... time?
|
|
paginationCache := make(map[string]paginationInfo)
|
|
|
|
return func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
|
// 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"]
|
|
w := walker{
|
|
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(),
|
|
cache: cache,
|
|
|
|
rsAPI: rsAPI,
|
|
fsAPI: fsAPI,
|
|
paginationCache: paginationCache,
|
|
}
|
|
return w.walk()
|
|
}
|
|
}
|
|
|
|
type paginationInfo struct {
|
|
processed set
|
|
unvisited []roomVisit
|
|
}
|
|
|
|
type walker struct {
|
|
rootRoomID string
|
|
caller *userapi.Device
|
|
serverName gomatrixserverlib.ServerName
|
|
thisServer gomatrixserverlib.ServerName
|
|
rsAPI roomserver.RoomserverInternalAPI
|
|
fsAPI fs.FederationInternalAPI
|
|
ctx context.Context
|
|
cache caching.SpaceSummaryRoomsCache
|
|
suggestedOnly bool
|
|
limit int
|
|
maxDepth int
|
|
paginationToken string
|
|
|
|
paginationCache map[string]paginationInfo
|
|
mu sync.Mutex
|
|
}
|
|
|
|
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 {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
p := w.paginationCache[paginationToken]
|
|
return &p
|
|
}
|
|
|
|
func (w *walker) storePaginationCache(paginationToken string, cache paginationInfo) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.paginationCache[paginationToken] = cache
|
|
}
|
|
|
|
type roomVisit struct {
|
|
roomID string
|
|
parentRoomID string
|
|
depth int
|
|
vias []string // vias to query this room by
|
|
}
|
|
|
|
func (w *walker) walk() util.JSONResponse {
|
|
if authorised, _ := w.authorised(w.rootRoomID, ""); !authorised {
|
|
if w.caller != nil {
|
|
// CS API format
|
|
return util.JSONResponse{
|
|
Code: 403,
|
|
JSON: jsonerror.Forbidden("room is unknown/forbidden"),
|
|
}
|
|
} else {
|
|
// SS API format
|
|
return util.JSONResponse{
|
|
Code: 404,
|
|
JSON: jsonerror.NotFound("room is unknown/forbidden"),
|
|
}
|
|
}
|
|
}
|
|
|
|
var discoveredRooms []gomatrixserverlib.MSC2946Room
|
|
|
|
var cache *paginationInfo
|
|
if w.paginationToken != "" {
|
|
cache = w.loadPaginationCache(w.paginationToken)
|
|
if cache == nil {
|
|
return util.JSONResponse{
|
|
Code: 400,
|
|
JSON: jsonerror.InvalidArgumentValue("invalid from"),
|
|
}
|
|
}
|
|
} 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{
|
|
roomID: w.rootRoomID,
|
|
parentRoomID: "",
|
|
depth: 0,
|
|
})
|
|
}
|
|
|
|
processed := cache.processed
|
|
unvisited := cache.unvisited
|
|
|
|
// Depth first -> stack data structure
|
|
for len(unvisited) > 0 {
|
|
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) {
|
|
continue
|
|
}
|
|
|
|
// Mark this room as processed.
|
|
processed.set(rv.roomID)
|
|
|
|
// if this room is not a space room, skip.
|
|
var roomType string
|
|
create := w.stateEvent(rv.roomID, gomatrixserverlib.MRoomCreate, "")
|
|
if create != nil {
|
|
// escape the `.`s so gjson doesn't think it's nested
|
|
roomType = gjson.GetBytes(create.Content(), strings.ReplaceAll(ConstCreateEventContentKey, ".", `\.`)).Str
|
|
}
|
|
|
|
// Collect rooms/events to send back (either locally or fetched via federation)
|
|
var discoveredChildEvents []gomatrixserverlib.MSC2946StrippedEvent
|
|
|
|
// If we know about this room and the caller is authorised (joined/world_readable) then pull
|
|
// events locally
|
|
roomExists := w.roomExists(rv.roomID)
|
|
if !roomExists {
|
|
// 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)
|
|
fedRes := w.federatedRoomInfo(rv.roomID, rv.vias)
|
|
if fedRes != nil {
|
|
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
|
|
}
|
|
} 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)
|
|
|
|
discoveredRooms = append(discoveredRooms, gomatrixserverlib.MSC2946Room{
|
|
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
|
|
}
|
|
|
|
// don't walk the children
|
|
// if the parent is not a space room
|
|
if roomType != ConstCreateEventContentValueSpace {
|
|
continue
|
|
}
|
|
|
|
// 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{
|
|
roomID: ev.StateKey,
|
|
parentRoomID: rv.roomID,
|
|
depth: rv.depth + 1,
|
|
vias: spaceContent.Via,
|
|
})
|
|
}
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
}
|
|
// 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,
|
|
JSON: jsonerror.NotFound("room is unknown/forbidden"),
|
|
}
|
|
}
|
|
return util.JSONResponse{
|
|
Code: 200,
|
|
JSON: gomatrixserverlib.MSC2946SpacesResponse{
|
|
Room: discoveredRooms[0],
|
|
Children: discoveredRooms[1:],
|
|
},
|
|
}
|
|
}
|
|
|
|
func (w *walker) stateEvent(roomID, evType, stateKey string) *gomatrixserverlib.HeaderedEvent {
|
|
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]
|
|
}
|
|
|
|
func (w *walker) publicRoomsChunk(roomID string) *gomatrixserverlib.PublicRoom {
|
|
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]
|
|
}
|
|
|
|
// federatedRoomInfo returns more of the spaces graph from another server. Returns nil if this was
|
|
// unsuccessful.
|
|
func (w *walker) federatedRoomInfo(roomID string, vias []string) *gomatrixserverlib.MSC2946SpacesResponse {
|
|
// only do federated requests for client requests
|
|
if w.caller == nil {
|
|
return nil
|
|
}
|
|
resp, ok := w.cache.GetSpaceSummary(roomID)
|
|
if ok {
|
|
util.GetLogger(w.ctx).Debugf("Returning cached response for %s", roomID)
|
|
return &resp
|
|
}
|
|
util.GetLogger(w.ctx).Debugf("Querying %s via %+v", roomID, vias)
|
|
ctx := context.Background()
|
|
// query more of the spaces graph using these servers
|
|
for _, serverName := range vias {
|
|
if serverName == string(w.thisServer) {
|
|
continue
|
|
}
|
|
res, err := w.fsAPI.MSC2946Spaces(ctx, gomatrixserverlib.ServerName(serverName), roomID, w.suggestedOnly)
|
|
if err != nil {
|
|
util.GetLogger(w.ctx).WithError(err).Warnf("failed to call MSC2946Spaces on server %s", serverName)
|
|
continue
|
|
}
|
|
// ensure nil slices are empty as we send this to the client sometimes
|
|
if res.Room.ChildrenState == nil {
|
|
res.Room.ChildrenState = []gomatrixserverlib.MSC2946StrippedEvent{}
|
|
}
|
|
for i := 0; i < len(res.Children); i++ {
|
|
child := res.Children[i]
|
|
if child.ChildrenState == nil {
|
|
child.ChildrenState = []gomatrixserverlib.MSC2946StrippedEvent{}
|
|
}
|
|
res.Children[i] = child
|
|
}
|
|
w.cache.StoreSpaceSummary(roomID, res)
|
|
|
|
return &res
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// authorised returns true iff the user is joined this room or the room is world_readable
|
|
func (w *walker) authorised(roomID, parentRoomID string) (authed, isJoinedOrInvited bool) {
|
|
if w.caller != nil {
|
|
return w.authorisedUser(roomID, parentRoomID)
|
|
}
|
|
return w.authorisedServer(roomID), false
|
|
}
|
|
|
|
// authorisedServer returns true iff the server is joined this room or the room is world_readable
|
|
func (w *walker) authorisedServer(roomID string) bool {
|
|
// Check history visibility / join rules first
|
|
hisVisTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomHistoryVisibility,
|
|
StateKey: "",
|
|
}
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomJoinRules,
|
|
StateKey: "",
|
|
}
|
|
var queryRoomRes roomserver.QueryCurrentStateResponse
|
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
RoomID: roomID,
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
hisVisTuple, joinRuleTuple,
|
|
},
|
|
}, &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
|
|
}
|
|
}
|
|
|
|
// 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]
|
|
if joinRuleEv != nil {
|
|
allowJoinedToRoomIDs = append(allowJoinedToRoomIDs, w.restrictedJoinRuleAllowedRooms(joinRuleEv, "m.room_membership")...)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// authorisedUser returns true iff the user is invited/joined this room or the room is world_readable.
|
|
// 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) {
|
|
hisVisTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomHistoryVisibility,
|
|
StateKey: "",
|
|
}
|
|
joinRuleTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomJoinRules,
|
|
StateKey: "",
|
|
}
|
|
roomMemberTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomMember,
|
|
StateKey: w.caller.UserID,
|
|
}
|
|
var queryRes roomserver.QueryCurrentStateResponse
|
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
|
RoomID: roomID,
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
|
hisVisTuple, joinRuleTuple, roomMemberTuple,
|
|
},
|
|
}, &queryRes)
|
|
if err != nil {
|
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryCurrentState")
|
|
return false, false
|
|
}
|
|
memberEv := queryRes.StateEvents[roomMemberTuple]
|
|
if memberEv != nil {
|
|
membership, _ := memberEv.Membership()
|
|
if membership == gomatrixserverlib.Join || membership == gomatrixserverlib.Invite {
|
|
return true, true
|
|
}
|
|
}
|
|
hisVisEv := queryRes.StateEvents[hisVisTuple]
|
|
if hisVisEv != nil {
|
|
hisVis, _ := hisVisEv.HistoryVisibility()
|
|
if hisVis == "world_readable" {
|
|
return true, false
|
|
}
|
|
}
|
|
joinRuleEv := queryRes.StateEvents[joinRuleTuple]
|
|
if parentRoomID != "" && joinRuleEv != nil {
|
|
allowedRoomIDs := w.restrictedJoinRuleAllowedRooms(joinRuleEv, "m.room_membership")
|
|
// check parent is in the allowed set
|
|
var allowed bool
|
|
for _, a := range allowedRoomIDs {
|
|
if parentRoomID == a {
|
|
allowed = true
|
|
break
|
|
}
|
|
}
|
|
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()
|
|
if membership == gomatrixserverlib.Join {
|
|
return true, false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false, false
|
|
}
|
|
|
|
func (w *walker) restrictedJoinRuleAllowedRooms(joinRuleEv *gomatrixserverlib.HeaderedEvent, allowType string) (allows []string) {
|
|
rule, _ := joinRuleEv.JoinRule()
|
|
if rule != "restricted" {
|
|
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
|
|
}
|
|
|
|
// references returns all child references pointing to or from this room.
|
|
func (w *walker) childReferences(roomID string) ([]gomatrixserverlib.MSC2946StrippedEvent, error) {
|
|
createTuple := gomatrixserverlib.StateKeyTuple{
|
|
EventType: gomatrixserverlib.MRoomCreate,
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 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 {
|
|
return []gomatrixserverlib.MSC2946StrippedEvent{}, nil
|
|
}
|
|
}
|
|
delete(res.StateEvents, createTuple)
|
|
|
|
el := make([]gomatrixserverlib.MSC2946StrippedEvent, 0, len(res.StateEvents))
|
|
for _, ev := range res.StateEvents {
|
|
content := gjson.ParseBytes(ev.Content())
|
|
// 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)
|
|
if content.Get("via").Exists() {
|
|
strip := stripped(ev.Event)
|
|
if strip == nil {
|
|
continue
|
|
}
|
|
// 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
|
|
}
|
|
el = append(el, *strip)
|
|
}
|
|
}
|
|
// sort by origin_server_ts as per MSC2946
|
|
sort.Slice(el, func(i, j int) bool {
|
|
return el[i].OriginServerTS < el[j].OriginServerTS
|
|
})
|
|
|
|
return el, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func stripped(ev *gomatrixserverlib.Event) *gomatrixserverlib.MSC2946StrippedEvent {
|
|
if ev.StateKey() == nil {
|
|
return nil
|
|
}
|
|
return &gomatrixserverlib.MSC2946StrippedEvent{
|
|
Type: ev.Type(),
|
|
StateKey: *ev.StateKey(),
|
|
Content: ev.Content(),
|
|
Sender: ev.Sender(),
|
|
OriginServerTS: ev.OriginServerTS(),
|
|
}
|
|
}
|
|
|
|
func parseInt(intstr string, defaultVal int) int {
|
|
i, err := strconv.ParseInt(intstr, 10, 32)
|
|
if err != nil {
|
|
return defaultVal
|
|
}
|
|
return int(i)
|
|
}
|