Update DB on JOIN and PART
This commit is contained in:
parent
461de13ecc
commit
0c4e9b539c
28
db.go
28
db.go
@ -77,24 +77,14 @@ func (db *DB) CreateUser(user *User) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var password *string
|
||||
if user.Password != "" {
|
||||
password = &user.Password
|
||||
}
|
||||
_, err = tx.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
|
||||
if err != nil {
|
||||
_, err := db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *DB) ListNetworks(username string) ([]Network, error) {
|
||||
db.lock.RLock()
|
||||
defer db.lock.RUnlock()
|
||||
@ -151,3 +141,19 @@ func (db *DB) ListChannels(networkID int64) ([]Channel, error) {
|
||||
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
_, err := db.db.Exec("INSERT OR REPLACE INTO Channel(network, name) VALUES (?, ?)", networkID, ch.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) DeleteChannel(networkID int64, name string) error {
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
|
||||
_, err := db.db.Exec("DELETE FROM Channel WHERE network = ? AND name = ?", networkID, name)
|
||||
return err
|
||||
}
|
||||
|
@ -114,7 +114,25 @@ func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
||||
})
|
||||
}
|
||||
|
||||
// upstream returns the upstream connection, if any. If there are zero or if
|
||||
// there are multiple upstream connections, it returns nil.
|
||||
func (dc *downstreamConn) upstream() *upstreamConn {
|
||||
if dc.network == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var upstream *upstreamConn
|
||||
dc.forEachUpstream(func(uc *upstreamConn) {
|
||||
upstream = uc
|
||||
})
|
||||
return upstream
|
||||
}
|
||||
|
||||
func (dc *downstreamConn) unmarshalChannel(name string) (*upstreamConn, string, error) {
|
||||
if uc := dc.upstream(); uc != nil {
|
||||
return uc, name, nil
|
||||
}
|
||||
|
||||
// TODO: extract network name from channel name if dc.upstream == nil
|
||||
var channel *upstreamChannel
|
||||
var err error
|
||||
@ -461,7 +479,20 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
||||
Command: msg.Command,
|
||||
Params: []string{upstreamName},
|
||||
})
|
||||
// TODO: add/remove channel from upstream config
|
||||
|
||||
switch msg.Command {
|
||||
case "JOIN":
|
||||
err := dc.srv.db.StoreChannel(uc.network.ID, &Channel{
|
||||
Name: upstreamName,
|
||||
})
|
||||
if err != nil {
|
||||
dc.logger.Printf("failed to create channel %q in DB: %v", upstreamName, err)
|
||||
}
|
||||
case "PART":
|
||||
if err := dc.srv.db.DeleteChannel(uc.network.ID, upstreamName); err != nil {
|
||||
dc.logger.Printf("failed to delete channel %q in DB: %v", upstreamName, err)
|
||||
}
|
||||
}
|
||||
case "MODE":
|
||||
if msg.Prefix == nil {
|
||||
return fmt.Errorf("missing prefix")
|
||||
|
Loading…
Reference in New Issue
Block a user