diff --git a/src/github.com/matrix-org/dendrite/appservice/appservice.go b/src/github.com/matrix-org/dendrite/appservice/appservice.go index 4d3c2d2f9..5d2fc085a 100644 --- a/src/github.com/matrix-org/dendrite/appservice/appservice.go +++ b/src/github.com/matrix-org/dendrite/appservice/appservice.go @@ -15,6 +15,7 @@ package appservice import ( + "context" "net/http" "sync" "time" @@ -27,7 +28,9 @@ import ( "github.com/matrix-org/dendrite/appservice/types" "github.com/matrix-org/dendrite/appservice/workers" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" + "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/common/basecomponent" + "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/common/transactions" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -39,6 +42,7 @@ import ( func SetupAppServiceAPIComponent( base *basecomponent.BaseDendrite, accountsDB *accounts.Database, + deviceDB *devices.Database, federation *gomatrixserverlib.FederationClient, roomserverAliasAPI roomserverAPI.RoomserverAliasAPI, roomserverQueryAPI roomserverAPI.RoomserverQueryAPI, @@ -61,6 +65,13 @@ func SetupAppServiceAPIComponent( Cond: sync.NewCond(&m), } workerStates[i] = ws + + // Create bot account for this AS if it doesn't already exist + if err = generateAppServiceAccount(accountsDB, deviceDB, appservice); err != nil { + logrus.WithFields(logrus.Fields{ + "appservice": appservice.ID, + }).WithError(err).Panicf("failed to generate bot account for appservice") + } } // Create a HTTP client that this component will use for all outbound and @@ -97,3 +108,27 @@ func SetupAppServiceAPIComponent( return &appserviceQueryAPI } + +// generateAppServiceAccounts creates a dummy account based off the +// `sender_localpart` field of each application service if it doesn't +// exist already +func generateAppServiceAccount( + accountsDB *accounts.Database, + deviceDB *devices.Database, + as config.ApplicationService, +) error { + ctx := context.Background() + + // Create an account for the application service + acc, err := accountsDB.CreateAccount(ctx, as.SenderLocalpart, "", as.ID) + if err != nil { + return err + } else if acc == nil { + // This account already exists + return nil + } + + // Create a dummy device with a dummy token for the application service + _, err = deviceDB.CreateDevice(ctx, as.SenderLocalpart, nil, as.ASToken, &as.SenderLocalpart) + return err +} diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-appservice-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-appservice-server/main.go index 347a04464..dcaea5138 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-appservice-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-appservice-server/main.go @@ -26,12 +26,13 @@ func main() { defer base.Close() // nolint: errcheck accountDB := base.CreateAccountsDB() + deviceDB := base.CreateDeviceDB() federation := base.CreateFederationClient() alias, _, query := base.CreateHTTPRoomserverAPIs() cache := transactions.New() appservice.SetupAppServiceAPIComponent( - base, accountDB, federation, alias, query, cache, + base, accountDB, deviceDB, federation, alias, query, cache, ) base.SetupAndServeHTTP(string(base.Cfg.Listen.FederationSender)) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go index 6cb93a310..e111f96a0 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-monolith-server/main.go @@ -21,6 +21,7 @@ import ( "github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/common/transactions" + "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/clientapi" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/basecomponent" @@ -65,6 +66,7 @@ func main() { mediaapi.SetupMediaAPIComponent(base, deviceDB) publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB) syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, query) + appservice.SetupAppServiceAPIComponent(base, accountDB, deviceDB, federation, alias, query, transactions.New()) httpHandler := common.WrapHandlerInCORS(base.APIMux)