Browse Source

Sync rooms on startup

pull/38/head
Kegan Dougal 8 years ago
parent
commit
8f8dec5539
  1. 15
      src/github.com/matrix-org/go-neb/clients/clients.go
  2. 9
      src/github.com/matrix-org/go-neb/database/db.go
  3. 24
      src/github.com/matrix-org/go-neb/database/schema.go
  4. 3
      src/github.com/matrix-org/go-neb/goneb.go
  5. 2
      src/github.com/matrix-org/go-neb/types/types.go

15
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

9
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) {

24
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

3
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}))

2
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.

Loading…
Cancel
Save