2018-05-24 13:54:42 +01:00
|
|
|
// Copyright 2018 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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 appservice
|
|
|
|
|
|
|
|
import (
|
2018-07-17 17:31:40 +01:00
|
|
|
"context"
|
2022-11-02 10:17:53 +00:00
|
|
|
"sync"
|
2018-07-05 17:34:59 +01:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-02-16 17:55:38 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2018-07-17 15:36:04 +01:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2018-05-24 13:54:42 +01:00
|
|
|
"github.com/matrix-org/dendrite/appservice/consumers"
|
2018-07-17 15:36:04 +01:00
|
|
|
"github.com/matrix-org/dendrite/appservice/query"
|
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-16 17:39:56 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2018-05-24 13:54:42 +01:00
|
|
|
)
|
|
|
|
|
2020-06-08 15:51:07 +01:00
|
|
|
// 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.
|
|
|
|
func NewInternalAPI(
|
2023-03-22 08:21:32 +00:00
|
|
|
processContext *process.ProcessContext,
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
natsInstance *jetstream.NATSInstance,
|
2023-02-20 13:58:03 +00:00
|
|
|
userAPI userapi.AppserviceUserAPI,
|
2022-05-10 11:08:10 +01:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
2022-05-06 12:39:26 +01:00
|
|
|
) appserviceAPI.AppServiceInternalAPI {
|
2023-04-04 08:42:46 +01:00
|
|
|
|
2022-09-01 09:20:40 +01:00
|
|
|
// Create appserivce query API with an HTTP client that will be used for all
|
|
|
|
// outbound and inbound requests (inbound only for the internal API)
|
|
|
|
appserviceQueryAPI := &query.AppServiceQueryAPI{
|
2023-03-22 08:21:32 +00:00
|
|
|
Cfg: &cfg.AppServiceAPI,
|
2022-11-02 10:17:53 +00:00
|
|
|
ProtocolCache: map[string]appserviceAPI.ASProtocolResponse{},
|
|
|
|
CacheMu: sync.Mutex{},
|
2022-09-01 09:20:40 +01:00
|
|
|
}
|
2020-10-15 13:27:13 +01:00
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
if len(cfg.Derived.ApplicationServices) == 0 {
|
2022-09-01 09:20:40 +01:00
|
|
|
return appserviceQueryAPI
|
2018-07-05 17:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap application services in a type that relates the application service and
|
|
|
|
// a sync.Cond object that can be used to notify workers when there are new
|
|
|
|
// events to be sent out.
|
2023-03-22 08:21:32 +00:00
|
|
|
for _, appservice := range cfg.Derived.ApplicationServices {
|
2018-07-17 17:31:40 +01:00
|
|
|
// Create bot account for this AS if it doesn't already exist
|
2023-03-22 08:21:32 +00:00
|
|
|
if err := generateAppServiceAccount(userAPI, appservice, cfg.Global.ServerName); err != nil {
|
2018-07-17 17:31:40 +01:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"appservice": appservice.ID,
|
|
|
|
}).WithError(err).Panicf("failed to generate bot account for appservice")
|
|
|
|
}
|
2018-07-05 17:34:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:11:33 +01:00
|
|
|
// Only consume if we actually have ASes to track, else we'll just chew cycles needlessly.
|
|
|
|
// We can't add ASes at runtime so this is safe to do.
|
2023-03-22 08:21:32 +00:00
|
|
|
js, _ := natsInstance.Prepare(processContext, &cfg.Global.JetStream)
|
2022-09-01 09:20:40 +01:00
|
|
|
consumer := consumers.NewOutputRoomEventConsumer(
|
2023-03-22 08:21:32 +00:00
|
|
|
processContext, &cfg.AppServiceAPI,
|
2023-04-04 08:42:46 +01:00
|
|
|
js, rsAPI,
|
2022-09-01 09:20:40 +01:00
|
|
|
)
|
|
|
|
if err := consumer.Start(); err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to start appservice roomserver consumer")
|
2018-05-24 13:54:42 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:43:07 +01:00
|
|
|
return appserviceQueryAPI
|
2018-05-24 13:54:42 +01:00
|
|
|
}
|
2018-07-17 17:31:40 +01:00
|
|
|
|
|
|
|
// generateAppServiceAccounts creates a dummy account based off the
|
|
|
|
// `sender_localpart` field of each application service if it doesn't
|
|
|
|
// exist already
|
|
|
|
func generateAppServiceAccount(
|
2022-05-05 13:17:38 +01:00
|
|
|
userAPI userapi.AppserviceUserAPI,
|
2018-07-17 17:31:40 +01:00
|
|
|
as config.ApplicationService,
|
2023-04-19 15:50:33 +01:00
|
|
|
serverName spec.ServerName,
|
2018-07-17 17:31:40 +01:00
|
|
|
) error {
|
2020-06-16 17:39:56 +01:00
|
|
|
var accRes userapi.PerformAccountCreationResponse
|
|
|
|
err := userAPI.PerformAccountCreation(context.Background(), &userapi.PerformAccountCreationRequest{
|
2022-02-16 17:55:38 +00:00
|
|
|
AccountType: userapi.AccountTypeAppService,
|
2020-06-16 17:39:56 +01:00
|
|
|
Localpart: as.SenderLocalpart,
|
2022-11-11 16:41:37 +00:00
|
|
|
ServerName: serverName,
|
2020-06-16 17:39:56 +01:00
|
|
|
AppServiceID: as.ID,
|
|
|
|
OnConflict: userapi.ConflictUpdate,
|
|
|
|
}, &accRes)
|
2018-07-17 17:31:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-16 17:39:56 +01:00
|
|
|
var devRes userapi.PerformDeviceCreationResponse
|
|
|
|
err = userAPI.PerformDeviceCreation(context.Background(), &userapi.PerformDeviceCreationRequest{
|
2021-12-03 17:18:35 +00:00
|
|
|
Localpart: as.SenderLocalpart,
|
2022-11-11 16:41:37 +00:00
|
|
|
ServerName: serverName,
|
2021-12-03 17:18:35 +00:00
|
|
|
AccessToken: as.ASToken,
|
|
|
|
DeviceID: &as.SenderLocalpart,
|
|
|
|
DeviceDisplayName: &as.SenderLocalpart,
|
|
|
|
NoDeviceListUpdate: true,
|
2020-06-16 17:39:56 +01:00
|
|
|
}, &devRes)
|
2018-07-17 17:31:40 +01:00
|
|
|
return err
|
|
|
|
}
|