NewInternalAPI returns a concerete implementation of the internal API. Callers can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes.
( base *base.BaseDendrite, cfg *config.UserAPI, appServices []config.ApplicationService, keyAPI keyapi.UserKeyAPI, rsAPI rsapi.UserRoomserverAPI, pgClient pushgateway.Client, )
| 43 | // NewInternalAPI returns a concerete implementation of the internal API. Callers |
| 44 | // can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. |
| 45 | func NewInternalAPI( |
| 46 | base *base.BaseDendrite, cfg *config.UserAPI, |
| 47 | appServices []config.ApplicationService, keyAPI keyapi.UserKeyAPI, |
| 48 | rsAPI rsapi.UserRoomserverAPI, pgClient pushgateway.Client, |
| 49 | ) api.UserInternalAPI { |
| 50 | js, _ := base.NATS.Prepare(base.ProcessContext, &cfg.Matrix.JetStream) |
| 51 | |
| 52 | db, err := storage.NewUserAPIDatabase( |
| 53 | base, |
| 54 | &cfg.AccountDatabase, |
| 55 | cfg.Matrix.ServerName, |
| 56 | cfg.BCryptCost, |
| 57 | cfg.OpenIDTokenLifetimeMS, |
| 58 | api.DefaultLoginTokenLifetime, |
| 59 | cfg.Matrix.ServerNotices.LocalPart, |
| 60 | ) |
| 61 | if err != nil { |
| 62 | logrus.WithError(err).Panicf("failed to connect to accounts db") |
| 63 | } |
| 64 | |
| 65 | syncProducer := producers.NewSyncAPI( |
| 66 | db, js, |
| 67 | // TODO: user API should handle syncs for account data. Right now, |
| 68 | // it's handled by clientapi, and hence uses its topic. When user |
| 69 | // API handles it for all account data, we can remove it from |
| 70 | // here. |
| 71 | cfg.Matrix.JetStream.Prefixed(jetstream.OutputClientData), |
| 72 | cfg.Matrix.JetStream.Prefixed(jetstream.OutputNotificationData), |
| 73 | ) |
| 74 | |
| 75 | userAPI := &internal.UserInternalAPI{ |
| 76 | DB: db, |
| 77 | SyncProducer: syncProducer, |
| 78 | ServerName: cfg.Matrix.ServerName, |
| 79 | AppServices: appServices, |
| 80 | KeyAPI: keyAPI, |
| 81 | RSAPI: rsAPI, |
| 82 | DisableTLSValidation: cfg.PushGatewayDisableTLSValidation, |
| 83 | } |
| 84 | |
| 85 | readConsumer := consumers.NewOutputReadUpdateConsumer( |
| 86 | base.ProcessContext, cfg, js, db, pgClient, userAPI, syncProducer, |
| 87 | ) |
| 88 | if err := readConsumer.Start(); err != nil { |
| 89 | logrus.WithError(err).Panic("failed to start user API read update consumer") |
| 90 | } |
| 91 | |
| 92 | eventConsumer := consumers.NewOutputStreamEventConsumer( |
| 93 | base.ProcessContext, cfg, js, db, pgClient, userAPI, rsAPI, syncProducer, |
| 94 | ) |
| 95 | if err := eventConsumer.Start(); err != nil { |
| 96 | logrus.WithError(err).Panic("failed to start user API streamed event consumer") |
| 97 | } |
| 98 | |
| 99 | var cleanOldNotifs func() |
| 100 | cleanOldNotifs = func() { |
| 101 | logrus.Infof("Cleaning old notifications") |
| 102 | if err := db.DeleteOldNotifications(base.Context()); err != nil { |