diff --git a/downstream.go b/downstream.go index 0a40507..11bf43d 100644 --- a/downstream.go +++ b/downstream.go @@ -827,17 +827,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { Params: params, }) - ch, err := dc.srv.db.GetChannel(uc.network.ID, upstreamName) - if err == ErrNoSuchChannel { - ch = &Channel{Name: upstreamName} - } else if err != nil { - return err - } - - ch.Key = key - - if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil { - return err + ch := &Channel{Name: upstreamName, Key: key} + if err := uc.network.createUpdateChannel(ch); err != nil { + dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err) } } case "PART": @@ -866,8 +858,8 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { Params: params, }) - 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) + if err := uc.network.deleteChannel(upstreamName); err != nil { + dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err) } } case "KICK": diff --git a/user.go b/user.go index becf5b6..db8319c 100644 --- a/user.go +++ b/user.go @@ -139,6 +139,19 @@ func (net *network) Stop() { } } +func (net *network) createUpdateChannel(ch *Channel) error { + if dbCh, err := net.user.srv.db.GetChannel(net.ID, ch.Name); err == nil { + ch.ID = dbCh.ID + } else if err != ErrNoSuchChannel { + return err + } + return net.user.srv.db.StoreChannel(net.ID, ch) +} + +func (net *network) deleteChannel(name string) error { + return net.user.srv.db.DeleteChannel(net.ID, name) +} + type user struct { User srv *Server