mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 13:22:46 +00:00
Add support for config "auto_join_rooms" (#2823)
Add support for config "auto_join_rooms". Now new accounts can join the rooms in config file automatically. ### Pull Request Checklist <!-- Please read https://matrix-org.github.io/dendrite/development/contributing before submitting your pull request --> * [x] I have justified why this PR doesn't need tests. * [x] Pull request includes a [sign off below using a legally identifiable name](https://matrix-org.github.io/dendrite/development/contributing#sign-off) Signed-off-by: `Rubin Poster <rubinposter@gmail.com>`
This commit is contained in:
parent
c62ac3d6ad
commit
2a4c7f45b3
6 changed files with 64 additions and 0 deletions
|
@ -54,6 +54,7 @@ type UserInternalAPI struct {
|
|||
KeyAPI keyapi.UserKeyAPI
|
||||
RSAPI rsapi.UserRoomserverAPI
|
||||
PgClient pushgateway.Client
|
||||
Cfg *config.UserAPI
|
||||
}
|
||||
|
||||
func (a *UserInternalAPI) InputAccountData(ctx context.Context, req *api.InputAccountDataRequest, res *api.InputAccountDataResponse) error {
|
||||
|
@ -130,6 +131,45 @@ func (a *UserInternalAPI) setFullyRead(ctx context.Context, req *api.InputAccoun
|
|||
return nil
|
||||
}
|
||||
|
||||
func postRegisterJoinRooms(cfg *config.UserAPI, acc *api.Account, rsAPI rsapi.UserRoomserverAPI) {
|
||||
// POST register behaviour: check if the user is a normal user.
|
||||
// If the user is a normal user, add user to room specified in the configuration "auto_join_rooms".
|
||||
if acc.AccountType != api.AccountTypeAppService && acc.AppServiceID == "" {
|
||||
for room := range cfg.AutoJoinRooms {
|
||||
userID := userutil.MakeUserID(acc.Localpart, cfg.Matrix.ServerName)
|
||||
err := addUserToRoom(context.Background(), rsAPI, cfg.AutoJoinRooms[room], acc.Localpart, userID)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"user_id": userID,
|
||||
"room": cfg.AutoJoinRooms[room],
|
||||
}).WithError(err).Errorf("user failed to auto-join room")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add user to a room. This function currently working for auto_join_rooms config,
|
||||
// which can add a newly registered user to a specified room.
|
||||
func addUserToRoom(
|
||||
ctx context.Context,
|
||||
rsAPI rsapi.UserRoomserverAPI,
|
||||
roomID string,
|
||||
username string,
|
||||
userID string,
|
||||
) error {
|
||||
addGroupContent := make(map[string]interface{})
|
||||
// This make sure the user's username can be displayed correctly.
|
||||
// Because the newly-registered user doesn't have an avatar, the avatar_url is not needed.
|
||||
addGroupContent["displayname"] = username
|
||||
joinReq := rsapi.PerformJoinRequest{
|
||||
RoomIDOrAlias: roomID,
|
||||
UserID: userID,
|
||||
Content: addGroupContent,
|
||||
}
|
||||
joinRes := rsapi.PerformJoinResponse{}
|
||||
return rsAPI.PerformJoin(ctx, &joinReq, &joinRes)
|
||||
}
|
||||
|
||||
func (a *UserInternalAPI) PerformAccountCreation(ctx context.Context, req *api.PerformAccountCreationRequest, res *api.PerformAccountCreationResponse) error {
|
||||
acc, err := a.DB.CreateAccount(ctx, req.Localpart, req.Password, req.AppServiceID, req.AccountType)
|
||||
if err != nil {
|
||||
|
@ -174,6 +214,8 @@ func (a *UserInternalAPI) PerformAccountCreation(ctx context.Context, req *api.P
|
|||
return err
|
||||
}
|
||||
|
||||
postRegisterJoinRooms(a.Cfg, acc, a.RSAPI)
|
||||
|
||||
res.AccountCreated = true
|
||||
res.Account = acc
|
||||
return nil
|
||||
|
|
|
@ -82,6 +82,7 @@ func NewInternalAPI(
|
|||
RSAPI: rsAPI,
|
||||
DisableTLSValidation: cfg.PushGatewayDisableTLSValidation,
|
||||
PgClient: pgClient,
|
||||
Cfg: cfg,
|
||||
}
|
||||
|
||||
receiptConsumer := consumers.NewOutputReceiptEventConsumer(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue