2020-05-01 11:48:17 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2020-09-02 14:47:31 +02:00
|
|
|
"context"
|
2020-05-01 11:48:17 +02:00
|
|
|
|
2021-03-24 11:25:24 +01:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-12-22 13:05:59 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-18 14:33:28 +01:00
|
|
|
asAPI "github.com/matrix-org/dendrite/appservice/api"
|
2021-11-24 11:45:23 +01:00
|
|
|
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-09-03 18:20:54 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/acls"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 18:13:15 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/perform"
|
2020-09-02 18:13:15 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
2022-07-01 11:54:07 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/producers"
|
2020-05-01 11:48:17 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2022-07-05 14:50:56 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-07-01 11:54:07 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2022-02-17 16:58:54 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2022-02-18 16:05:03 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-05-01 11:48:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverInternalAPI is an implementation of api.RoomserverInternalAPI
|
|
|
|
type RoomserverInternalAPI struct {
|
2020-09-02 18:13:15 +02:00
|
|
|
*input.Inputer
|
|
|
|
*query.Queryer
|
|
|
|
*perform.Inviter
|
|
|
|
*perform.Joiner
|
2020-09-10 15:39:18 +02:00
|
|
|
*perform.Peeker
|
2021-01-22 15:55:08 +01:00
|
|
|
*perform.InboundPeeker
|
2020-12-03 12:11:46 +01:00
|
|
|
*perform.Unpeeker
|
2020-09-02 18:13:15 +02:00
|
|
|
*perform.Leaver
|
|
|
|
*perform.Publisher
|
|
|
|
*perform.Backfiller
|
2020-11-05 11:19:23 +01:00
|
|
|
*perform.Forgetter
|
2022-04-05 11:04:08 +02:00
|
|
|
*perform.Upgrader
|
2022-04-28 17:02:30 +02:00
|
|
|
*perform.Admin
|
2022-02-17 16:58:54 +01:00
|
|
|
ProcessContext *process.ProcessContext
|
2022-07-05 14:50:56 +02:00
|
|
|
Base *base.BaseDendrite
|
2020-10-06 19:09:02 +02:00
|
|
|
DB storage.Database
|
|
|
|
Cfg *config.RoomServer
|
|
|
|
Cache caching.RoomServerCaches
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
KeyRing gomatrixserverlib.JSONVerifier
|
2022-01-27 15:29:14 +01:00
|
|
|
ServerACLs *acls.ServerACLs
|
2022-05-05 20:30:38 +02:00
|
|
|
fsAPI fsAPI.RoomserverFederationAPI
|
2022-05-06 13:39:26 +02:00
|
|
|
asAPI asAPI.AppServiceInternalAPI
|
2022-03-16 15:21:11 +01:00
|
|
|
NATSClient *nats.Conn
|
2022-01-27 15:29:14 +01:00
|
|
|
JetStream nats.JetStreamContext
|
2022-02-02 14:32:48 +01:00
|
|
|
Durable string
|
2022-01-05 18:44:49 +01:00
|
|
|
InputRoomEventTopic string // JetStream topic for new input room events
|
2022-07-01 11:54:07 +02:00
|
|
|
OutputProducer *producers.RoomEventProducer
|
2020-10-06 19:09:02 +02:00
|
|
|
PerspectiveServerNames []gomatrixserverlib.ServerName
|
2020-05-01 11:48:17 +02:00
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
|
|
|
|
func NewRoomserverAPI(
|
2022-07-05 14:50:56 +02:00
|
|
|
base *base.BaseDendrite, roomserverDB storage.Database,
|
|
|
|
js nats.JetStreamContext, nc *nats.Conn,
|
2020-09-02 14:47:31 +02:00
|
|
|
) *RoomserverInternalAPI {
|
2022-07-05 14:50:56 +02:00
|
|
|
var perspectiveServerNames []gomatrixserverlib.ServerName
|
|
|
|
for _, kp := range base.Cfg.FederationAPI.KeyPerspectives {
|
|
|
|
perspectiveServerNames = append(perspectiveServerNames, kp.ServerName)
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:40:58 +02:00
|
|
|
serverACLs := acls.NewServerACLs(roomserverDB)
|
2022-07-01 11:54:07 +02:00
|
|
|
producer := &producers.RoomEventProducer{
|
2022-07-05 14:50:56 +02:00
|
|
|
Topic: string(base.Cfg.Global.JetStream.Prefixed(jetstream.OutputRoomEvent)),
|
2022-07-01 11:54:07 +02:00
|
|
|
JetStream: js,
|
|
|
|
ACLs: serverACLs,
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
a := &RoomserverInternalAPI{
|
2022-07-05 14:50:56 +02:00
|
|
|
ProcessContext: base.ProcessContext,
|
2020-10-06 19:09:02 +02:00
|
|
|
DB: roomserverDB,
|
2022-07-05 14:50:56 +02:00
|
|
|
Base: base,
|
|
|
|
Cfg: &base.Cfg.RoomServer,
|
|
|
|
Cache: base.Caches,
|
|
|
|
ServerName: base.Cfg.Global.ServerName,
|
2020-10-06 19:09:02 +02:00
|
|
|
PerspectiveServerNames: perspectiveServerNames,
|
2022-07-05 14:50:56 +02:00
|
|
|
InputRoomEventTopic: base.Cfg.Global.JetStream.Prefixed(jetstream.InputRoomEvent),
|
2022-07-01 11:54:07 +02:00
|
|
|
OutputProducer: producer,
|
|
|
|
JetStream: js,
|
2022-03-16 15:21:11 +01:00
|
|
|
NATSClient: nc,
|
2022-07-05 14:50:56 +02:00
|
|
|
Durable: base.Cfg.Global.JetStream.Durable("RoomserverInputConsumer"),
|
2022-01-27 15:29:14 +01:00
|
|
|
ServerACLs: serverACLs,
|
2020-09-02 18:13:15 +02:00
|
|
|
Queryer: &query.Queryer{
|
2022-11-15 16:05:23 +01:00
|
|
|
DB: roomserverDB,
|
|
|
|
Cache: base.Caches,
|
|
|
|
IsLocalServerName: base.Cfg.Global.IsLocalServerName,
|
|
|
|
ServerACLs: serverACLs,
|
2020-09-02 18:13:15 +02:00
|
|
|
},
|
2020-09-02 14:47:31 +02:00
|
|
|
// perform-er structs get initialised when we have a federation sender to use
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
// SetFederationInputAPI passes in a federation input API reference so that we can
|
|
|
|
// avoid the chicken-and-egg problem of both the roomserver input API and the
|
|
|
|
// federation input API being interdependent.
|
2022-05-05 20:30:38 +02:00
|
|
|
func (r *RoomserverInternalAPI) SetFederationAPI(fsAPI fsAPI.RoomserverFederationAPI, keyRing *gomatrixserverlib.KeyRing) {
|
2020-09-02 14:47:31 +02:00
|
|
|
r.fsAPI = fsAPI
|
2021-12-13 14:24:49 +01:00
|
|
|
r.KeyRing = keyRing
|
2020-09-02 14:47:31 +02:00
|
|
|
|
2022-12-22 13:05:59 +01:00
|
|
|
identity, err := r.Cfg.Matrix.SigningIdentityFor(r.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
r.Inputer = &input.Inputer{
|
2022-07-05 14:50:56 +02:00
|
|
|
Cfg: &r.Base.Cfg.RoomServer,
|
|
|
|
Base: r.Base,
|
|
|
|
ProcessContext: r.Base.ProcessContext,
|
2022-07-01 11:54:07 +02:00
|
|
|
DB: r.DB,
|
|
|
|
InputRoomEventTopic: r.InputRoomEventTopic,
|
|
|
|
OutputProducer: r.OutputProducer,
|
|
|
|
JetStream: r.JetStream,
|
|
|
|
NATSClient: r.NATSClient,
|
|
|
|
Durable: nats.Durable(r.Durable),
|
2022-12-22 13:05:59 +01:00
|
|
|
ServerName: r.ServerName,
|
|
|
|
SigningIdentity: identity,
|
2022-07-01 11:54:07 +02:00
|
|
|
FSAPI: fsAPI,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
ACLs: r.ServerACLs,
|
|
|
|
Queryer: r.Queryer,
|
2022-01-27 15:29:14 +01:00
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
r.Inviter = &perform.Inviter{
|
2020-09-02 18:13:15 +02:00
|
|
|
DB: r.DB,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
r.Joiner = &perform.Joiner{
|
2022-11-15 16:05:23 +01:00
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
RSAPI: r,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
Queryer: r.Queryer,
|
2020-09-10 15:39:18 +02:00
|
|
|
}
|
|
|
|
r.Peeker = &perform.Peeker{
|
2022-12-22 13:05:59 +01:00
|
|
|
ServerName: r.ServerName,
|
2020-09-10 15:39:18 +02:00
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
2020-09-02 18:13:15 +02:00
|
|
|
Inputer: r.Inputer,
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
2021-01-22 15:55:08 +01:00
|
|
|
r.InboundPeeker = &perform.InboundPeeker{
|
|
|
|
DB: r.DB,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
}
|
2020-12-03 12:11:46 +01:00
|
|
|
r.Unpeeker = &perform.Unpeeker{
|
2022-12-22 13:05:59 +01:00
|
|
|
ServerName: r.ServerName,
|
2020-12-03 12:11:46 +01:00
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
r.Leaver = &perform.Leaver{
|
2020-09-02 18:13:15 +02:00
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
r.Publisher = &perform.Publisher{
|
|
|
|
DB: r.DB,
|
|
|
|
}
|
|
|
|
r.Backfiller = &perform.Backfiller{
|
2022-11-15 16:05:23 +01:00
|
|
|
IsLocalServerName: r.Cfg.Matrix.IsLocalServerName,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
KeyRing: r.KeyRing,
|
2020-10-06 19:09:02 +02:00
|
|
|
// Perspective servers are trusted to not lie about server keys, so we will also
|
|
|
|
// prefer these servers when backfilling (assuming they are in the room) rather
|
|
|
|
// than trying random servers
|
|
|
|
PreferServers: r.PerspectiveServerNames,
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
2020-11-05 11:19:23 +01:00
|
|
|
r.Forgetter = &perform.Forgetter{
|
|
|
|
DB: r.DB,
|
|
|
|
}
|
2022-04-05 11:04:08 +02:00
|
|
|
r.Upgrader = &perform.Upgrader{
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
URSAPI: r,
|
|
|
|
}
|
2022-04-28 17:02:30 +02:00
|
|
|
r.Admin = &perform.Admin{
|
|
|
|
DB: r.DB,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
Queryer: r.Queryer,
|
2022-06-29 16:29:39 +02:00
|
|
|
Leaver: r.Leaver,
|
2022-04-28 17:02:30 +02:00
|
|
|
}
|
2022-01-07 14:41:53 +01:00
|
|
|
|
|
|
|
if err := r.Inputer.Start(); err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to start roomserver input API")
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
func (r *RoomserverInternalAPI) SetUserAPI(userAPI userapi.RoomserverUserAPI) {
|
2022-02-18 16:05:03 +01:00
|
|
|
r.Leaver.UserAPI = userAPI
|
2022-12-22 13:05:59 +01:00
|
|
|
r.Inputer.UserAPI = userAPI
|
2022-02-18 16:05:03 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 13:39:26 +02:00
|
|
|
func (r *RoomserverInternalAPI) SetAppserviceAPI(asAPI asAPI.AppServiceInternalAPI) {
|
2020-12-18 14:33:28 +01:00
|
|
|
r.asAPI = asAPI
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:47:31 +02:00
|
|
|
func (r *RoomserverInternalAPI) PerformInvite(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformInviteRequest,
|
|
|
|
res *api.PerformInviteResponse,
|
|
|
|
) error {
|
|
|
|
outputEvents, err := r.Inviter.PerformInvite(ctx, req, res)
|
|
|
|
if err != nil {
|
2021-03-24 11:25:24 +01:00
|
|
|
sentry.CaptureException(err)
|
2020-09-02 14:47:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(outputEvents) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-01 11:54:07 +02:00
|
|
|
return r.OutputProducer.ProduceRoomEvents(req.Event.RoomID(), outputEvents)
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomserverInternalAPI) PerformLeave(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformLeaveRequest,
|
|
|
|
res *api.PerformLeaveResponse,
|
|
|
|
) error {
|
|
|
|
outputEvents, err := r.Leaver.PerformLeave(ctx, req, res)
|
|
|
|
if err != nil {
|
2021-03-24 11:25:24 +01:00
|
|
|
sentry.CaptureException(err)
|
2020-09-02 14:47:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(outputEvents) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-01 11:54:07 +02:00
|
|
|
return r.OutputProducer.ProduceRoomEvents(req.RoomID, outputEvents)
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
2020-11-05 11:19:23 +01:00
|
|
|
|
|
|
|
func (r *RoomserverInternalAPI) PerformForget(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformForgetRequest,
|
|
|
|
resp *api.PerformForgetResponse,
|
|
|
|
) error {
|
|
|
|
return r.Forgetter.PerformForget(ctx, req, resp)
|
|
|
|
}
|