diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index a97ec7b..cfec0dc 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -5,6 +5,7 @@ import ( "github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/errors" + "github.com/matrix-org/go-neb/types" "net/http" ) @@ -24,7 +25,7 @@ func (s *configureClientHandler) OnIncomingRequest(req *http.Request) (interface return nil, &errors.HTTPError{nil, "Unsupported Method", 405} } - var body database.ClientConfig + var body types.ClientConfig if err := json.NewDecoder(req.Body).Decode(&body); err != nil { return nil, &errors.HTTPError{err, "Error parsing request JSON", 400} } @@ -39,8 +40,8 @@ func (s *configureClientHandler) OnIncomingRequest(req *http.Request) (interface } return &struct { - OldClient database.ClientConfig - NewClient database.ClientConfig + OldClient types.ClientConfig + NewClient types.ClientConfig }{oldClient, body}, nil } @@ -67,7 +68,7 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac return nil, &errors.HTTPError{nil, `Must supply a "ID", a "Type" and a "Config"`, 400} } - service := database.CreateService(body.ID, body.Type) + service := types.CreateService(body.ID, body.Type) if service == nil { return nil, &errors.HTTPError{nil, "Unknown service type", 400} } @@ -89,7 +90,7 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac return &struct { ID string Type string - OldConfig database.Service - NewConfig database.Service + OldConfig types.Service + NewConfig types.Service }{body.ID, body.Type, oldService, service}, nil } 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 29345d3..8aab734 100644 --- a/src/github.com/matrix-org/go-neb/clients/clients.go +++ b/src/github.com/matrix-org/go-neb/clients/clients.go @@ -5,6 +5,7 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/plugin" + "github.com/matrix-org/go-neb/types" "net/url" "sync" ) @@ -37,7 +38,7 @@ func (c *Clients) Client(userID string) (*matrix.Client, error) { } // Update updates the config for a matrix client -func (c *Clients) Update(config database.ClientConfig) (database.ClientConfig, error) { +func (c *Clients) Update(config types.ClientConfig) (types.ClientConfig, error) { _, old, err := c.updateClientInDB(config) return old.config, err } @@ -68,7 +69,7 @@ func (c *Clients) Start() error { } type clientEntry struct { - config database.ClientConfig + config types.ClientConfig client *matrix.Client } @@ -105,7 +106,7 @@ func (c *Clients) loadClientFromDB(userID string) (entry clientEntry, err error) return } -func (c *Clients) updateClientInDB(newConfig database.ClientConfig) (new clientEntry, old clientEntry, err error) { +func (c *Clients) updateClientInDB(newConfig types.ClientConfig) (new clientEntry, old clientEntry, err error) { c.dbMutex.Lock() defer c.dbMutex.Unlock() @@ -136,7 +137,7 @@ func (c *Clients) updateClientInDB(newConfig database.ClientConfig) (new clientE return } -func (c *Clients) newClient(config database.ClientConfig) (*matrix.Client, error) { +func (c *Clients) newClient(config types.ClientConfig) (*matrix.Client, error) { homeserverURL, err := url.Parse(config.HomeserverURL) if err != nil { 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 b850820..ece6d23 100644 --- a/src/github.com/matrix-org/go-neb/database/db.go +++ b/src/github.com/matrix-org/go-neb/database/db.go @@ -3,6 +3,7 @@ package database import ( "database/sql" "github.com/matrix-org/go-neb/matrix" + "github.com/matrix-org/go-neb/types" "sort" "time" ) @@ -29,7 +30,7 @@ func Open(databaseType, databaseURL string) (serviceDB *ServiceDB, err error) { // StoreMatrixClientConfig stores the Matrix client config for a bot service. // If a config already exists then it will be updated, otherwise a new config // will be inserted. The previous config is returned. -func (d *ServiceDB) StoreMatrixClientConfig(config ClientConfig) (oldConfig ClientConfig, err error) { +func (d *ServiceDB) StoreMatrixClientConfig(config types.ClientConfig) (oldConfig types.ClientConfig, err error) { err = runTransaction(d.db, func(txn *sql.Tx) error { oldConfig, err = selectMatrixClientConfigTxn(txn, config.UserID) now := time.Now() @@ -56,7 +57,7 @@ func (d *ServiceDB) LoadServiceUserIds() (userIDsToRooms map[string][]string, er // 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 ClientConfig, err error) { +func (d *ServiceDB) LoadMatrixClientConfig(userID string) (config types.ClientConfig, err error) { err = runTransaction(d.db, func(txn *sql.Tx) error { config, err = selectMatrixClientConfigTxn(txn, userID) return err @@ -66,7 +67,7 @@ func (d *ServiceDB) LoadMatrixClientConfig(userID string) (config ClientConfig, // LoadService loads a service from the database. // Returns sql.ErrNoRows if the service isn't in the database. -func (d *ServiceDB) LoadService(serviceID string) (service Service, err error) { +func (d *ServiceDB) LoadService(serviceID string) (service types.Service, err error) { err = runTransaction(d.db, func(txn *sql.Tx) error { service, err = selectServiceTxn(txn, serviceID) return err @@ -76,7 +77,7 @@ func (d *ServiceDB) LoadService(serviceID string) (service Service, err error) { // LoadServicesInRoom loads all the bot services configured for a room. // Returns the empty list if there aren't any services configured. -func (d *ServiceDB) LoadServicesInRoom(serviceUserID, roomID string) (services []Service, err error) { +func (d *ServiceDB) LoadServicesInRoom(serviceUserID, roomID string) (services []types.Service, err error) { err = runTransaction(d.db, func(txn *sql.Tx) error { serviceIDs, err := selectRoomServicesTxn(txn, serviceUserID, roomID) if err != nil { @@ -97,7 +98,7 @@ func (d *ServiceDB) LoadServicesInRoom(serviceUserID, roomID string) (services [ // StoreService stores a service into the database either by inserting a new // service or updating an existing service. Returns the old service if there // was one. -func (d *ServiceDB) StoreService(service Service, client *matrix.Client) (oldService Service, err error) { +func (d *ServiceDB) StoreService(service types.Service, client *matrix.Client) (oldService types.Service, err error) { err = runTransaction(d.db, func(txn *sql.Tx) error { oldService, err = selectServiceTxn(txn, service.ServiceID()) if err != nil && err != sql.ErrNoRows { 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 03817fa..420766f 100644 --- a/src/github.com/matrix-org/go-neb/database/schema.go +++ b/src/github.com/matrix-org/go-neb/database/schema.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "fmt" + "github.com/matrix-org/go-neb/types" "time" ) @@ -61,7 +62,7 @@ const selectMatrixClientConfigSQL = ` SELECT client_json FROM matrix_clients WHERE user_id = $1 ` -func selectMatrixClientConfigTxn(txn *sql.Tx, userID string) (config ClientConfig, err error) { +func selectMatrixClientConfigTxn(txn *sql.Tx, userID string) (config types.ClientConfig, err error) { var configJSON []byte err = txn.QueryRow(selectMatrixClientConfigSQL, userID).Scan(&configJSON) if err != nil { @@ -77,7 +78,7 @@ INSERT INTO matrix_clients( ) VALUES ($1, $2, '', $3, $4) ` -func insertMatrixClientConfigTxn(txn *sql.Tx, now time.Time, config ClientConfig) error { +func insertMatrixClientConfigTxn(txn *sql.Tx, now time.Time, config types.ClientConfig) error { t := now.UnixNano() / 1000000 configJSON, err := json.Marshal(&config) if err != nil { @@ -92,7 +93,7 @@ UPDATE matrix_clients SET client_json = $1, time_updated_ms = $2 WHERE user_id = $3 ` -func updateMatrixClientConfigTxn(txn *sql.Tx, now time.Time, config ClientConfig) error { +func updateMatrixClientConfigTxn(txn *sql.Tx, now time.Time, config types.ClientConfig) error { t := now.UnixNano() / 1000000 configJSON, err := json.Marshal(&config) if err != nil { @@ -107,14 +108,14 @@ SELECT service_type, service_json FROM services WHERE service_id = $1 ` -func selectServiceTxn(txn *sql.Tx, serviceID string) (Service, error) { +func selectServiceTxn(txn *sql.Tx, serviceID string) (types.Service, error) { var serviceType string var serviceJSON []byte row := txn.QueryRow(selectServiceSQL, serviceID) if err := row.Scan(&serviceType, &serviceJSON); err != nil { return nil, err } - service := CreateService(serviceID, serviceType) + service := types.CreateService(serviceID, serviceType) if service == nil { return nil, fmt.Errorf("Cannot create services of type %s", serviceType) } @@ -129,7 +130,7 @@ UPDATE services SET service_type=$1, service_json=$2, time_updated_ms=$3 WHERE service_id=$4 ` -func updateServiceTxn(txn *sql.Tx, now time.Time, service Service) error { +func updateServiceTxn(txn *sql.Tx, now time.Time, service types.Service) error { serviceJSON, err := json.Marshal(service) if err != nil { return err @@ -148,7 +149,7 @@ INSERT INTO services( ) VALUES ($1, $2, $3, $4, $5) ` -func insertServiceTxn(txn *sql.Tx, now time.Time, service Service) error { +func insertServiceTxn(txn *sql.Tx, now time.Time, service types.Service) error { serviceJSON, err := json.Marshal(service) if err != nil { return err diff --git a/src/github.com/matrix-org/go-neb/services/echo/echo.go b/src/github.com/matrix-org/go-neb/services/echo/echo.go index 6377a45..d102b20 100644 --- a/src/github.com/matrix-org/go-neb/services/echo/echo.go +++ b/src/github.com/matrix-org/go-neb/services/echo/echo.go @@ -1,9 +1,9 @@ package services import ( - "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/plugin" + "github.com/matrix-org/go-neb/types" "strings" ) @@ -31,7 +31,7 @@ func (e *echoService) Plugin(roomID string) plugin.Plugin { } func init() { - database.RegisterService(func(serviceID string) database.Service { + types.RegisterService(func(serviceID string) types.Service { return &echoService{id: serviceID} }) } diff --git a/src/github.com/matrix-org/go-neb/services/github/github.go b/src/github.com/matrix-org/go-neb/services/github/github.go index df58df0..c81d1a0 100644 --- a/src/github.com/matrix-org/go-neb/services/github/github.go +++ b/src/github.com/matrix-org/go-neb/services/github/github.go @@ -4,9 +4,9 @@ import ( "fmt" log "github.com/Sirupsen/logrus" "github.com/google/go-github/github" - "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/plugin" + "github.com/matrix-org/go-neb/types" "golang.org/x/oauth2" "regexp" "strconv" @@ -91,7 +91,7 @@ func ownerRepoNumberFromText(ownerRepoNumberText string) (string, string, int, e } func init() { - database.RegisterService(func(serviceID string) database.Service { + types.RegisterService(func(serviceID string) types.Service { return &githubService{id: serviceID} }) } diff --git a/src/github.com/matrix-org/go-neb/database/types.go b/src/github.com/matrix-org/go-neb/types/types.go similarity index 95% rename from src/github.com/matrix-org/go-neb/database/types.go rename to src/github.com/matrix-org/go-neb/types/types.go index 2028a9d..7bdc6a8 100644 --- a/src/github.com/matrix-org/go-neb/database/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -1,8 +1,9 @@ -package database +package types import ( "errors" "github.com/matrix-org/go-neb/plugin" + // "net/http" "net/url" ) @@ -31,6 +32,7 @@ type Service interface { ServiceType() string RoomIDs() []string Plugin(roomID string) plugin.Plugin + // OnReceiveWebhook(req http.Request) } var servicesByType = map[string]func(string) Service{}