From c7114907f6565d68b06153cced25fb9fc10df291 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Aug 2016 16:50:47 +0100 Subject: [PATCH] Auto-join rooms if set in config --- .../matrix-org/go-neb/clients/clients.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/github.com/matrix-org/go-neb/clients/clients.go b/src/github.com/matrix-org/go-neb/clients/clients.go index 79e23c4..be64037 100644 --- a/src/github.com/matrix-org/go-neb/clients/clients.go +++ b/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 } +// Start listening on client /sync streams func (c *Clients) Start() error { configs, err := c.db.LoadMatrixClientConfigs() if err != nil { @@ -154,6 +155,33 @@ func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) { 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() return client, nil