Changeset 89 in code for trunk


Ignore:
Timestamp:
Mar 12, 2020, 5:33:03 PM (5 years ago)
Author:
contact
Message:

Update DB on JOIN and PART

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/db.go

    r85 r89  
    7878        defer db.lock.Unlock()
    7979
    80         tx, err := db.db.Begin()
    81         if err != nil {
    82                 return err
    83         }
    84         defer tx.Rollback()
    85 
    8680        var password *string
    8781        if user.Password != "" {
    8882                password = &user.Password
    8983        }
    90         _, err = tx.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
    91         if err != nil {
    92                 return err
    93         }
    94 
    95         return tx.Commit()
     84        _, err := db.db.Exec("INSERT INTO User(username, password) VALUES (?, ?)", user.Username, password)
     85        return err
    9686}
    9787
     
    152142        return channels, nil
    153143}
     144
     145func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
     146        db.lock.Lock()
     147        defer db.lock.Unlock()
     148
     149        _, err := db.db.Exec("INSERT OR REPLACE INTO Channel(network, name) VALUES (?, ?)", networkID, ch.Name)
     150        return err
     151}
     152
     153func (db *DB) DeleteChannel(networkID int64, name string) error {
     154        db.lock.Lock()
     155        defer db.lock.Unlock()
     156
     157        _, err := db.db.Exec("DELETE FROM Channel WHERE network = ? AND name = ?", networkID, name)
     158        return err
     159}
  • trunk/downstream.go

    r88 r89  
    115115}
    116116
     117// upstream returns the upstream connection, if any. If there are zero or if
     118// there are multiple upstream connections, it returns nil.
     119func (dc *downstreamConn) upstream() *upstreamConn {
     120        if dc.network == nil {
     121                return nil
     122        }
     123
     124        var upstream *upstreamConn
     125        dc.forEachUpstream(func(uc *upstreamConn) {
     126                upstream = uc
     127        })
     128        return upstream
     129}
     130
    117131func (dc *downstreamConn) unmarshalChannel(name string) (*upstreamConn, string, error) {
     132        if uc := dc.upstream(); uc != nil {
     133                return uc, name, nil
     134        }
     135
    118136        // TODO: extract network name from channel name if dc.upstream == nil
    119137        var channel *upstreamChannel
     
    462480                        Params:  []string{upstreamName},
    463481                })
    464                 // TODO: add/remove channel from upstream config
     482
     483                switch msg.Command {
     484                case "JOIN":
     485                        err := dc.srv.db.StoreChannel(uc.network.ID, &Channel{
     486                                Name: upstreamName,
     487                        })
     488                        if err != nil {
     489                                dc.logger.Printf("failed to create channel %q in DB: %v", upstreamName, err)
     490                        }
     491                case "PART":
     492                        if err := dc.srv.db.DeleteChannel(uc.network.ID, upstreamName); err != nil {
     493                                dc.logger.Printf("failed to delete channel %q in DB: %v", upstreamName, err)
     494                        }
     495                }
    465496        case "MODE":
    466497                if msg.Prefix == nil {
Note: See TracChangeset for help on using the changeset viewer.