Browse Source

Auto-join rooms if set in config

pull/38/head
Kegan Dougal 8 years ago
parent
commit
c7114907f6
  1. 28
      src/github.com/matrix-org/go-neb/clients/clients.go

28
src/github.com/matrix-org/go-neb/clients/clients.go

@ -43,6 +43,7 @@ func (c *Clients) Update(config types.ClientConfig) (types.ClientConfig, error)
return old.config, err return old.config, err
} }
// Start listening on client /sync streams
func (c *Clients) Start() error { func (c *Clients) Start() error {
configs, err := c.db.LoadMatrixClientConfigs() configs, err := c.db.LoadMatrixClientConfigs()
if err != nil { if err != nil {
@ -154,6 +155,33 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) {
plugin.OnMessage(plugins, client, event) plugin.OnMessage(plugins, client, event)
}) })
if config.AutoJoinRooms {
client.Worker.OnEventType("m.room.member", func(event *matrix.Event) {
if event.StateKey != config.UserID {
return // not our member event
}
m := event.Content["membership"]
membership, ok := m.(string)
if !ok {
return
}
if membership == "invite" {
logger := log.WithFields(log.Fields{
"room_id": event.RoomID,
"service_user_id": config.UserID,
"inviter": event.Sender,
})
logger.Print("Accepting invite from user")
if _, err := client.JoinRoom(event.RoomID, ""); err != nil {
logger.WithError(err).Print("Failed to join room")
} else {
logger.Print("Joined room")
}
}
})
}
go client.Sync() go client.Sync()
return client, nil return client, nil

Loading…
Cancel
Save