2020-05-01 04:48:17 -05:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2020-09-02 07:47:31 -05:00
|
|
|
"context"
|
2020-05-01 04:48:17 -05:00
|
|
|
|
2021-03-24 05:25:24 -05:00
|
|
|
"github.com/getsentry/sentry-go"
|
2020-12-18 07:33:28 -06:00
|
|
|
asAPI "github.com/matrix-org/dendrite/appservice/api"
|
2021-11-24 04:45:23 -06:00
|
|
|
fsAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-09-03 11:20:54 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/acls"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 11:13:15 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/input"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/perform"
|
2020-09-02 11:13:15 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/query"
|
2020-05-01 04:48:17 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-02-17 09:58:54 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2022-02-18 09:05:03 -06:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-05-01 04:48:17 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-01-05 11:44:49 -06:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-01 04:48:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverInternalAPI is an implementation of api.RoomserverInternalAPI
|
|
|
|
type RoomserverInternalAPI struct {
|
2020-09-02 11:13:15 -05:00
|
|
|
*input.Inputer
|
|
|
|
*query.Queryer
|
|
|
|
*perform.Inviter
|
|
|
|
*perform.Joiner
|
2020-09-10 08:39:18 -05:00
|
|
|
*perform.Peeker
|
2021-01-22 08:55:08 -06:00
|
|
|
*perform.InboundPeeker
|
2020-12-03 05:11:46 -06:00
|
|
|
*perform.Unpeeker
|
2020-09-02 11:13:15 -05:00
|
|
|
*perform.Leaver
|
|
|
|
*perform.Publisher
|
|
|
|
*perform.Backfiller
|
2020-11-05 04:19:23 -06:00
|
|
|
*perform.Forgetter
|
2022-02-17 09:58:54 -06:00
|
|
|
ProcessContext *process.ProcessContext
|
2020-10-06 12:09:02 -05:00
|
|
|
DB storage.Database
|
|
|
|
Cfg *config.RoomServer
|
|
|
|
Cache caching.RoomServerCaches
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
KeyRing gomatrixserverlib.JSONVerifier
|
2022-01-27 08:29:14 -06:00
|
|
|
ServerACLs *acls.ServerACLs
|
2021-11-24 04:45:23 -06:00
|
|
|
fsAPI fsAPI.FederationInternalAPI
|
2020-12-18 07:33:28 -06:00
|
|
|
asAPI asAPI.AppServiceQueryAPI
|
2022-03-16 09:21:11 -05:00
|
|
|
NATSClient *nats.Conn
|
2022-01-27 08:29:14 -06:00
|
|
|
JetStream nats.JetStreamContext
|
2022-02-02 07:32:48 -06:00
|
|
|
Durable string
|
2022-01-05 11:44:49 -06:00
|
|
|
InputRoomEventTopic string // JetStream topic for new input room events
|
|
|
|
OutputRoomEventTopic string // JetStream topic for new output room events
|
2020-10-06 12:09:02 -05:00
|
|
|
PerspectiveServerNames []gomatrixserverlib.ServerName
|
2020-05-01 04:48:17 -05:00
|
|
|
}
|
2020-09-02 07:47:31 -05:00
|
|
|
|
|
|
|
func NewRoomserverAPI(
|
2022-02-17 09:58:54 -06:00
|
|
|
processCtx *process.ProcessContext, cfg *config.RoomServer, roomserverDB storage.Database,
|
2022-03-16 09:21:11 -05:00
|
|
|
consumer nats.JetStreamContext, nc *nats.Conn,
|
|
|
|
inputRoomEventTopic, outputRoomEventTopic string,
|
2022-02-17 09:58:54 -06:00
|
|
|
caches caching.RoomServerCaches, perspectiveServerNames []gomatrixserverlib.ServerName,
|
2020-09-02 07:47:31 -05:00
|
|
|
) *RoomserverInternalAPI {
|
2020-09-04 04:40:58 -05:00
|
|
|
serverACLs := acls.NewServerACLs(roomserverDB)
|
2020-09-02 07:47:31 -05:00
|
|
|
a := &RoomserverInternalAPI{
|
2022-02-17 09:58:54 -06:00
|
|
|
ProcessContext: processCtx,
|
2020-10-06 12:09:02 -05:00
|
|
|
DB: roomserverDB,
|
|
|
|
Cfg: cfg,
|
|
|
|
Cache: caches,
|
|
|
|
ServerName: cfg.Matrix.ServerName,
|
|
|
|
PerspectiveServerNames: perspectiveServerNames,
|
2022-01-27 08:29:14 -06:00
|
|
|
InputRoomEventTopic: inputRoomEventTopic,
|
|
|
|
OutputRoomEventTopic: outputRoomEventTopic,
|
|
|
|
JetStream: consumer,
|
2022-03-16 09:21:11 -05:00
|
|
|
NATSClient: nc,
|
2022-01-27 08:29:14 -06:00
|
|
|
Durable: cfg.Matrix.JetStream.Durable("RoomserverInputConsumer"),
|
|
|
|
ServerACLs: serverACLs,
|
2020-09-02 11:13:15 -05:00
|
|
|
Queryer: &query.Queryer{
|
2020-09-03 11:20:54 -05:00
|
|
|
DB: roomserverDB,
|
|
|
|
Cache: caches,
|
2021-07-09 10:36:45 -05:00
|
|
|
ServerName: cfg.Matrix.ServerName,
|
2020-09-04 04:40:58 -05:00
|
|
|
ServerACLs: serverACLs,
|
2020-09-02 11:13:15 -05:00
|
|
|
},
|
2020-09-02 07:47:31 -05:00
|
|
|
// perform-er structs get initialised when we have a federation sender to use
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-11-24 04:45:23 -06: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.
|
2021-12-13 07:24:49 -06:00
|
|
|
func (r *RoomserverInternalAPI) SetFederationAPI(fsAPI fsAPI.FederationInternalAPI, keyRing *gomatrixserverlib.KeyRing) {
|
2020-09-02 07:47:31 -05:00
|
|
|
r.fsAPI = fsAPI
|
2021-12-13 07:24:49 -06:00
|
|
|
r.KeyRing = keyRing
|
2020-09-02 07:47:31 -05:00
|
|
|
|
2022-01-27 08:29:14 -06:00
|
|
|
r.Inputer = &input.Inputer{
|
2022-02-17 09:58:54 -06:00
|
|
|
ProcessContext: r.ProcessContext,
|
2022-01-27 08:29:14 -06:00
|
|
|
DB: r.DB,
|
|
|
|
InputRoomEventTopic: r.InputRoomEventTopic,
|
|
|
|
OutputRoomEventTopic: r.OutputRoomEventTopic,
|
|
|
|
JetStream: r.JetStream,
|
2022-03-16 09:21:11 -05:00
|
|
|
NATSClient: r.NATSClient,
|
2022-02-02 07:32:48 -06:00
|
|
|
Durable: nats.Durable(r.Durable),
|
2022-01-27 08:29:14 -06:00
|
|
|
ServerName: r.Cfg.Matrix.ServerName,
|
|
|
|
FSAPI: fsAPI,
|
|
|
|
KeyRing: keyRing,
|
|
|
|
ACLs: r.ServerACLs,
|
|
|
|
Queryer: r.Queryer,
|
|
|
|
}
|
2020-09-02 07:47:31 -05:00
|
|
|
r.Inviter = &perform.Inviter{
|
2020-09-02 11:13:15 -05:00
|
|
|
DB: r.DB,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
2020-09-02 07:47:31 -05:00
|
|
|
}
|
|
|
|
r.Joiner = &perform.Joiner{
|
|
|
|
ServerName: r.Cfg.Matrix.ServerName,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
2021-03-03 11:00:31 -06:00
|
|
|
RSAPI: r,
|
2020-09-10 08:39:18 -05:00
|
|
|
Inputer: r.Inputer,
|
2021-07-09 11:49:59 -05:00
|
|
|
Queryer: r.Queryer,
|
2020-09-10 08:39:18 -05:00
|
|
|
}
|
|
|
|
r.Peeker = &perform.Peeker{
|
|
|
|
ServerName: r.Cfg.Matrix.ServerName,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
2020-09-02 11:13:15 -05:00
|
|
|
Inputer: r.Inputer,
|
2020-09-02 07:47:31 -05:00
|
|
|
}
|
2021-01-22 08:55:08 -06:00
|
|
|
r.InboundPeeker = &perform.InboundPeeker{
|
|
|
|
DB: r.DB,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
}
|
2020-12-03 05:11:46 -06:00
|
|
|
r.Unpeeker = &perform.Unpeeker{
|
|
|
|
ServerName: r.Cfg.Matrix.ServerName,
|
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
|
|
|
}
|
2020-09-02 07:47:31 -05:00
|
|
|
r.Leaver = &perform.Leaver{
|
2020-09-02 11:13:15 -05:00
|
|
|
Cfg: r.Cfg,
|
|
|
|
DB: r.DB,
|
|
|
|
FSAPI: r.fsAPI,
|
|
|
|
Inputer: r.Inputer,
|
2020-09-02 07:47:31 -05:00
|
|
|
}
|
|
|
|
r.Publisher = &perform.Publisher{
|
|
|
|
DB: r.DB,
|
|
|
|
}
|
|
|
|
r.Backfiller = &perform.Backfiller{
|
|
|
|
ServerName: r.ServerName,
|
|
|
|
DB: r.DB,
|
2020-09-02 09:26:30 -05:00
|
|
|
FSAPI: r.fsAPI,
|
2020-09-02 07:47:31 -05:00
|
|
|
KeyRing: r.KeyRing,
|
2020-10-06 12:09:02 -05: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 07:47:31 -05:00
|
|
|
}
|
2020-11-05 04:19:23 -06:00
|
|
|
r.Forgetter = &perform.Forgetter{
|
|
|
|
DB: r.DB,
|
|
|
|
}
|
2022-01-07 07:41:53 -06:00
|
|
|
|
|
|
|
if err := r.Inputer.Start(); err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to start roomserver input API")
|
|
|
|
}
|
2020-09-02 07:47:31 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:05:03 -06:00
|
|
|
func (r *RoomserverInternalAPI) SetUserAPI(userAPI userapi.UserInternalAPI) {
|
|
|
|
r.Leaver.UserAPI = userAPI
|
|
|
|
}
|
|
|
|
|
2020-12-18 07:33:28 -06:00
|
|
|
func (r *RoomserverInternalAPI) SetAppserviceAPI(asAPI asAPI.AppServiceQueryAPI) {
|
|
|
|
r.asAPI = asAPI
|
|
|
|
}
|
|
|
|
|
2020-09-02 07:47:31 -05: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 05:25:24 -05:00
|
|
|
sentry.CaptureException(err)
|
2020-09-02 07:47:31 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(outputEvents) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r.WriteOutputEvents(req.Event.RoomID(), outputEvents)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 05:25:24 -05:00
|
|
|
sentry.CaptureException(err)
|
2020-09-02 07:47:31 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(outputEvents) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r.WriteOutputEvents(req.RoomID, outputEvents)
|
|
|
|
}
|
2020-11-05 04:19:23 -06:00
|
|
|
|
|
|
|
func (r *RoomserverInternalAPI) PerformForget(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.PerformForgetRequest,
|
|
|
|
resp *api.PerformForgetResponse,
|
|
|
|
) error {
|
|
|
|
return r.Forgetter.PerformForget(ctx, req, resp)
|
|
|
|
}
|