From 8f8dec553931ae2a5a7002636de14dd596a7d5aa Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Aug 2016 16:44:50 +0100 Subject: [PATCH 1/2] Sync rooms on startup --- .../matrix-org/go-neb/clients/clients.go | 15 ++++++++++++ .../matrix-org/go-neb/database/db.go | 9 +++++++ .../matrix-org/go-neb/database/schema.go | 24 +++++++++++++++++++ src/github.com/matrix-org/go-neb/goneb.go | 3 +++ .../matrix-org/go-neb/types/types.go | 2 ++ 5 files changed, 53 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 6c6c0fa..79e23c4 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,21 @@ func (c *Clients) Update(config types.ClientConfig) (types.ClientConfig, error) return old.config, err } +func (c *Clients) Start() error { + configs, err := c.db.LoadMatrixClientConfigs() + if err != nil { + return err + } + for _, cfg := range configs { + if cfg.Sync { + if _, err := c.Client(cfg.UserID); err != nil { + return err + } + } + } + return nil +} + type clientEntry struct { config types.ClientConfig client *matrix.Client diff --git a/src/github.com/matrix-org/go-neb/database/db.go b/src/github.com/matrix-org/go-neb/database/db.go index bab866e..7b4c394 100644 --- a/src/github.com/matrix-org/go-neb/database/db.go +++ b/src/github.com/matrix-org/go-neb/database/db.go @@ -59,6 +59,15 @@ func (d *ServiceDB) StoreMatrixClientConfig(config types.ClientConfig) (oldConfi return } +// LoadMatrixClientConfigs loads all Matrix client configs from the database. +func (d *ServiceDB) LoadMatrixClientConfigs() (configs []types.ClientConfig, err error) { + err = runTransaction(d.db, func(txn *sql.Tx) error { + configs, err = selectMatrixClientConfigsTxn(txn) + return err + }) + return +} + // LoadMatrixClientConfig loads a Matrix client config from the database. // Returns sql.ErrNoRows if the client isn't in the database. func (d *ServiceDB) LoadMatrixClientConfig(userID string) (config types.ClientConfig, err error) { diff --git a/src/github.com/matrix-org/go-neb/database/schema.go b/src/github.com/matrix-org/go-neb/database/schema.go index 892882b..6b48eb0 100644 --- a/src/github.com/matrix-org/go-neb/database/schema.go +++ b/src/github.com/matrix-org/go-neb/database/schema.go @@ -64,6 +64,30 @@ func selectMatrixClientConfigTxn(txn *sql.Tx, userID string) (config types.Clien return } +const selectMatrixClientConfigsSQL = ` +SELECT client_json FROM matrix_clients +` + +func selectMatrixClientConfigsTxn(txn *sql.Tx) (configs []types.ClientConfig, err error) { + rows, err := txn.Query(selectMatrixClientConfigsSQL) + if err != nil { + return + } + defer rows.Close() + for rows.Next() { + var config types.ClientConfig + var configJSON []byte + if err = rows.Scan(&configJSON); err != nil { + return + } + if err = json.Unmarshal(configJSON, &config); err != nil { + return + } + configs = append(configs, config) + } + return +} + const insertMatrixClientConfigSQL = ` INSERT INTO matrix_clients( user_id, client_json, next_batch, time_added_ms, time_updated_ms diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index dd9c50c..607ad42 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -51,6 +51,9 @@ func main() { database.SetServiceDB(db) clients := clients.New(db) + if err := clients.Start(); err != nil { + log.Panic(err) + } http.Handle("/test", server.MakeJSONAPI(&heartbeatHandler{})) http.Handle("/admin/getService", server.MakeJSONAPI(&getServiceHandler{db: db})) diff --git a/src/github.com/matrix-org/go-neb/types/types.go b/src/github.com/matrix-org/go-neb/types/types.go index c223096..f58eb61 100644 --- a/src/github.com/matrix-org/go-neb/types/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -16,6 +16,8 @@ type ClientConfig struct { UserID string // The matrix UserId to connect with. HomeserverURL string // A URL with the host and port of the matrix server. E.g. https://matrix.org:8448 AccessToken string // The matrix access token to authenticate the requests with. + Sync bool // True to start a sync stream for this user + AutoJoinRooms bool // True to automatically join all rooms for this user } // Check that the client has the correct fields. From c7114907f6565d68b06153cced25fb9fc10df291 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Aug 2016 16:50:47 +0100 Subject: [PATCH 2/2] 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