From 8f8dec553931ae2a5a7002636de14dd596a7d5aa Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Aug 2016 16:44:50 +0100 Subject: [PATCH] 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.