From f57a67ea7e9beff28cc6745fe6a12d37f99ced12 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 8 Jan 2021 12:00:46 +0000 Subject: [PATCH] Add stub functions for MSC2946 --- dendrite-config.yaml | 3 +- setup/config/config_mscs.go | 2 +- setup/mscs/msc2946/msc2946.go | 87 +++++++++++++++++++++++++++++++++++ setup/mscs/msc2946/storage.go | 43 +++++++++++++++++ setup/mscs/mscs.go | 3 ++ 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 setup/mscs/msc2946/msc2946.go create mode 100644 setup/mscs/msc2946/storage.go diff --git a/dendrite-config.yaml b/dendrite-config.yaml index 585d466bc..978b18008 100644 --- a/dendrite-config.yaml +++ b/dendrite-config.yaml @@ -257,7 +257,8 @@ media_api: mscs: # A list of enabled MSC's # Currently valid values are: - # - msc2836 (Threading, see https://github.com/matrix-org/matrix-doc/pull/2836) + # - msc2836 (Threading, see https://github.com/matrix-org/matrix-doc/pull/2836) + # - msc2946 (Spaces Summary, see https://github.com/matrix-org/matrix-doc/pull/2946) mscs: [] database: connection_string: file:mscs.db diff --git a/setup/config/config_mscs.go b/setup/config/config_mscs.go index 776d0b641..4b53495f0 100644 --- a/setup/config/config_mscs.go +++ b/setup/config/config_mscs.go @@ -3,7 +3,7 @@ package config type MSCs struct { Matrix *Global `yaml:"-"` - // The MSCs to enable, currently only `msc2836` is supported. + // The MSCs to enable MSCs []string `yaml:"mscs"` Database DatabaseOptions `yaml:"database"` diff --git a/setup/mscs/msc2946/msc2946.go b/setup/mscs/msc2946/msc2946.go new file mode 100644 index 000000000..db798f58c --- /dev/null +++ b/setup/mscs/msc2946/msc2946.go @@ -0,0 +1,87 @@ +// 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" + "fmt" + "net/http" + + "github.com/matrix-org/dendrite/internal/hooks" + "github.com/matrix-org/dendrite/internal/httputil" + roomserver "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/setup" + userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/util" +) + +// SpacesRequest is the request body to POST /_matrix/client/r0/rooms/{roomID}/spaces +type SpacesRequest struct { + MaxRoomsPerSpace int `json:"max_rooms_per_space"` + Limit int `json:"limit"` + Batch string `json:"batch"` +} + +// SpacesResponse is the response body to POST /_matrix/client/r0/rooms/{roomID}/spaces +type SpacesResponse struct { + NextBatch string `json:"next_batch"` + // Rooms are nodes on the space graph. + Rooms []Room `json:"rooms"` + // Events are edges on the space graph, exclusively m.space.child or m.room.parent events + Events []gomatrixserverlib.ClientEvent `json:"events"` +} + +// Room is a node on the space graph +type Room struct { + gomatrixserverlib.PublicRoom + NumRefs int `json:"num_refs"` + RoomType string `json:"room_type"` +} + +// Enable this MSC +func Enable( + base *setup.BaseDendrite, rsAPI roomserver.RoomserverInternalAPI, userAPI userapi.UserInternalAPI, +) error { + db, err := NewDatabase(&base.Cfg.MSCs.Database) + if err != nil { + return fmt.Errorf("Cannot enable MSC2946: %w", err) + } + hooks.Enable() + hooks.Attach(hooks.KindNewEventPersisted, func(headeredEvent interface{}) { + he := headeredEvent.(*gomatrixserverlib.HeaderedEvent) + hookErr := db.StoreRelation(context.Background(), he) + if hookErr != nil { + util.GetLogger(context.Background()).WithError(hookErr).WithField("event_id", he.EventID()).Error( + "failed to StoreRelation", + ) + } + }) + + base.PublicClientAPIMux.Handle("/unstable/rooms/{roomID}/spaces", + httputil.MakeAuthAPI("spaces", userAPI, spacesHandler(db, rsAPI)), + ).Methods(http.MethodPost, http.MethodOptions) + return nil +} + +func spacesHandler(db Database, rsAPI roomserver.RoomserverInternalAPI) func(*http.Request, *userapi.Device) util.JSONResponse { + return func(req *http.Request, device *userapi.Device) util.JSONResponse { + return util.JSONResponse{ + Code: 200, + JSON: struct{}{}, + } + } +} diff --git a/setup/mscs/msc2946/storage.go b/setup/mscs/msc2946/storage.go new file mode 100644 index 000000000..68ac0bf45 --- /dev/null +++ b/setup/mscs/msc2946/storage.go @@ -0,0 +1,43 @@ +// 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 + +import ( + "context" + "database/sql" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/gomatrixserverlib" +) + +type Database interface { + StoreRelation(ctx context.Context, he *gomatrixserverlib.HeaderedEvent) error +} + +type DB struct { + db *sql.DB + writer sqlutil.Writer + insertEdgeStmt *sql.Stmt +} + +// NewDatabase loads the database for msc2836 +func NewDatabase(dbOpts *config.DatabaseOptions) (Database, error) { + return &DB{}, nil +} + +func (d *DB) StoreRelation(ctx context.Context, he *gomatrixserverlib.HeaderedEvent) error { + return nil +} diff --git a/setup/mscs/mscs.go b/setup/mscs/mscs.go index a8e5668ea..bf2103629 100644 --- a/setup/mscs/mscs.go +++ b/setup/mscs/mscs.go @@ -21,6 +21,7 @@ import ( "github.com/matrix-org/dendrite/setup" "github.com/matrix-org/dendrite/setup/mscs/msc2836" + "github.com/matrix-org/dendrite/setup/mscs/msc2946" "github.com/matrix-org/util" ) @@ -39,6 +40,8 @@ func EnableMSC(base *setup.BaseDendrite, monolith *setup.Monolith, msc string) e switch msc { case "msc2836": return msc2836.Enable(base, monolith.RoomserverAPI, monolith.FederationSenderAPI, monolith.UserAPI, monolith.KeyRing) + case "msc2946": + return msc2946.Enable(base, monolith.RoomserverAPI, monolith.UserAPI) default: return fmt.Errorf("EnableMSC: unknown msc '%s'", msc) }