Browse Source

Move database/types.go to types/types.go in its own 'types' package

There is nothing specific to the types to warrant them being under the database
package.
kegan/webhooks
Kegan Dougal 9 years ago
parent
commit
edf3f87674
  1. 13
      src/github.com/matrix-org/go-neb/api.go
  2. 9
      src/github.com/matrix-org/go-neb/clients/clients.go
  3. 11
      src/github.com/matrix-org/go-neb/database/db.go
  4. 15
      src/github.com/matrix-org/go-neb/database/schema.go
  5. 4
      src/github.com/matrix-org/go-neb/services/echo/echo.go
  6. 4
      src/github.com/matrix-org/go-neb/services/github/github.go
  7. 4
      src/github.com/matrix-org/go-neb/types/types.go

13
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/clients"
"github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/errors" "github.com/matrix-org/go-neb/errors"
"github.com/matrix-org/go-neb/types"
"net/http" "net/http"
) )
@ -24,7 +25,7 @@ func (s *configureClientHandler) OnIncomingRequest(req *http.Request) (interface
return nil, &errors.HTTPError{nil, "Unsupported Method", 405} 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 { if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
return nil, &errors.HTTPError{err, "Error parsing request JSON", 400} return nil, &errors.HTTPError{err, "Error parsing request JSON", 400}
} }
@ -39,8 +40,8 @@ func (s *configureClientHandler) OnIncomingRequest(req *http.Request) (interface
} }
return &struct { return &struct {
OldClient database.ClientConfig
NewClient database.ClientConfig
OldClient types.ClientConfig
NewClient types.ClientConfig
}{oldClient, body}, nil }{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} 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 { if service == nil {
return nil, &errors.HTTPError{nil, "Unknown service type", 400} return nil, &errors.HTTPError{nil, "Unknown service type", 400}
} }
@ -89,7 +90,7 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac
return &struct { return &struct {
ID string ID string
Type string Type string
OldConfig database.Service
NewConfig database.Service
OldConfig types.Service
NewConfig types.Service
}{body.ID, body.Type, oldService, service}, nil }{body.ID, body.Type, oldService, service}, nil
} }

9
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/database"
"github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/plugin" "github.com/matrix-org/go-neb/plugin"
"github.com/matrix-org/go-neb/types"
"net/url" "net/url"
"sync" "sync"
) )
@ -37,7 +38,7 @@ func (c *Clients) Client(userID string) (*matrix.Client, error) {
} }
// Update updates the config for a matrix client // 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) _, old, err := c.updateClientInDB(config)
return old.config, err return old.config, err
} }
@ -68,7 +69,7 @@ func (c *Clients) Start() error {
} }
type clientEntry struct { type clientEntry struct {
config database.ClientConfig
config types.ClientConfig
client *matrix.Client client *matrix.Client
} }
@ -105,7 +106,7 @@ func (c *Clients) loadClientFromDB(userID string) (entry clientEntry, err error)
return 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() c.dbMutex.Lock()
defer c.dbMutex.Unlock() defer c.dbMutex.Unlock()
@ -136,7 +137,7 @@ func (c *Clients) updateClientInDB(newConfig database.ClientConfig) (new clientE
return 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) homeserverURL, err := url.Parse(config.HomeserverURL)
if err != nil { if err != nil {

11
src/github.com/matrix-org/go-neb/database/db.go

@ -3,6 +3,7 @@ package database
import ( import (
"database/sql" "database/sql"
"github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/types"
"sort" "sort"
"time" "time"
) )
@ -29,7 +30,7 @@ func Open(databaseType, databaseURL string) (serviceDB *ServiceDB, err error) {
// StoreMatrixClientConfig stores the Matrix client config for a bot service. // StoreMatrixClientConfig stores the Matrix client config for a bot service.
// If a config already exists then it will be updated, otherwise a new config // If a config already exists then it will be updated, otherwise a new config
// will be inserted. The previous config is returned. // 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 { err = runTransaction(d.db, func(txn *sql.Tx) error {
oldConfig, err = selectMatrixClientConfigTxn(txn, config.UserID) oldConfig, err = selectMatrixClientConfigTxn(txn, config.UserID)
now := time.Now() 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. // LoadMatrixClientConfig loads a Matrix client config from the database.
// Returns sql.ErrNoRows if the client isn't in 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 { err = runTransaction(d.db, func(txn *sql.Tx) error {
config, err = selectMatrixClientConfigTxn(txn, userID) config, err = selectMatrixClientConfigTxn(txn, userID)
return err return err
@ -66,7 +67,7 @@ func (d *ServiceDB) LoadMatrixClientConfig(userID string) (config ClientConfig,
// LoadService loads a service from the database. // LoadService loads a service from the database.
// Returns sql.ErrNoRows if the service isn't in 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 { err = runTransaction(d.db, func(txn *sql.Tx) error {
service, err = selectServiceTxn(txn, serviceID) service, err = selectServiceTxn(txn, serviceID)
return err 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. // LoadServicesInRoom loads all the bot services configured for a room.
// Returns the empty list if there aren't any services configured. // 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 { err = runTransaction(d.db, func(txn *sql.Tx) error {
serviceIDs, err := selectRoomServicesTxn(txn, serviceUserID, roomID) serviceIDs, err := selectRoomServicesTxn(txn, serviceUserID, roomID)
if err != nil { 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 // StoreService stores a service into the database either by inserting a new
// service or updating an existing service. Returns the old service if there // service or updating an existing service. Returns the old service if there
// was one. // 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 { err = runTransaction(d.db, func(txn *sql.Tx) error {
oldService, err = selectServiceTxn(txn, service.ServiceID()) oldService, err = selectServiceTxn(txn, service.ServiceID())
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {

15
src/github.com/matrix-org/go-neb/database/schema.go

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/matrix-org/go-neb/types"
"time" "time"
) )
@ -61,7 +62,7 @@ const selectMatrixClientConfigSQL = `
SELECT client_json FROM matrix_clients WHERE user_id = $1 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 var configJSON []byte
err = txn.QueryRow(selectMatrixClientConfigSQL, userID).Scan(&configJSON) err = txn.QueryRow(selectMatrixClientConfigSQL, userID).Scan(&configJSON)
if err != nil { if err != nil {
@ -77,7 +78,7 @@ INSERT INTO matrix_clients(
) VALUES ($1, $2, '', $3, $4) ) 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 t := now.UnixNano() / 1000000
configJSON, err := json.Marshal(&config) configJSON, err := json.Marshal(&config)
if err != nil { if err != nil {
@ -92,7 +93,7 @@ UPDATE matrix_clients SET client_json = $1, time_updated_ms = $2
WHERE user_id = $3 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 t := now.UnixNano() / 1000000
configJSON, err := json.Marshal(&config) configJSON, err := json.Marshal(&config)
if err != nil { if err != nil {
@ -107,14 +108,14 @@ SELECT service_type, service_json FROM services
WHERE service_id = $1 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 serviceType string
var serviceJSON []byte var serviceJSON []byte
row := txn.QueryRow(selectServiceSQL, serviceID) row := txn.QueryRow(selectServiceSQL, serviceID)
if err := row.Scan(&serviceType, &serviceJSON); err != nil { if err := row.Scan(&serviceType, &serviceJSON); err != nil {
return nil, err return nil, err
} }
service := CreateService(serviceID, serviceType)
service := types.CreateService(serviceID, serviceType)
if service == nil { if service == nil {
return nil, fmt.Errorf("Cannot create services of type %s", serviceType) 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 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) serviceJSON, err := json.Marshal(service)
if err != nil { if err != nil {
return err return err
@ -148,7 +149,7 @@ INSERT INTO services(
) VALUES ($1, $2, $3, $4, $5) ) 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) serviceJSON, err := json.Marshal(service)
if err != nil { if err != nil {
return err return err

4
src/github.com/matrix-org/go-neb/services/echo/echo.go

@ -1,9 +1,9 @@
package services package services
import ( import (
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/matrix" "github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/plugin" "github.com/matrix-org/go-neb/plugin"
"github.com/matrix-org/go-neb/types"
"strings" "strings"
) )
@ -31,7 +31,7 @@ func (e *echoService) Plugin(roomID string) plugin.Plugin {
} }
func init() { func init() {
database.RegisterService(func(serviceID string) database.Service {
types.RegisterService(func(serviceID string) types.Service {
return &echoService{id: serviceID} return &echoService{id: serviceID}
}) })
} }

4
src/github.com/matrix-org/go-neb/services/github/github.go

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/google/go-github/github" "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/matrix"
"github.com/matrix-org/go-neb/plugin" "github.com/matrix-org/go-neb/plugin"
"github.com/matrix-org/go-neb/types"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"regexp" "regexp"
"strconv" "strconv"
@ -91,7 +91,7 @@ func ownerRepoNumberFromText(ownerRepoNumberText string) (string, string, int, e
} }
func init() { func init() {
database.RegisterService(func(serviceID string) database.Service {
types.RegisterService(func(serviceID string) types.Service {
return &githubService{id: serviceID} return &githubService{id: serviceID}
}) })
} }

4
src/github.com/matrix-org/go-neb/database/types.go → src/github.com/matrix-org/go-neb/types/types.go

@ -1,8 +1,9 @@
package database
package types
import ( import (
"errors" "errors"
"github.com/matrix-org/go-neb/plugin" "github.com/matrix-org/go-neb/plugin"
// "net/http"
"net/url" "net/url"
) )
@ -31,6 +32,7 @@ type Service interface {
ServiceType() string ServiceType() string
RoomIDs() []string RoomIDs() []string
Plugin(roomID string) plugin.Plugin Plugin(roomID string) plugin.Plugin
// OnReceiveWebhook(req http.Request)
} }
var servicesByType = map[string]func(string) Service{} var servicesByType = map[string]func(string) Service{}
Loading…
Cancel
Save