database: add NewNetwork

This commit is contained in:
Simon Ser 2023-10-31 23:51:04 +01:00
parent bb8397057e
commit e19dd8f0da
5 changed files with 18 additions and 17 deletions

View File

@ -175,7 +175,8 @@ func main() {
if ok { if ok {
logger.Printf("updating existing network") logger.Printf("updating existing network")
} else { } else {
n = &database.Network{Name: netName} n = database.NewNetwork("")
n.Name = netName
logger.Printf("creating new network") logger.Printf("creating new network")
} }

View File

@ -161,6 +161,13 @@ type Network struct {
Enabled bool Enabled bool
} }
func NewNetwork(addr string) *Network {
return &Network{
Addr: addr,
Enabled: true,
}
}
func (net *Network) GetName() string { func (net *Network) GetName() string {
if net.Name != "" { if net.Name != "" {
return net.Name return net.Name

View File

@ -1403,13 +1403,12 @@ func (dc *downstreamConn) loadNetwork(ctx context.Context) error {
}} }}
} }
record := database.NewNetwork(dc.registration.networkName)
record.Nick = nick
dc.logger.Printf("auto-saving network %q", dc.registration.networkName) dc.logger.Printf("auto-saving network %q", dc.registration.networkName)
var err error var err error
network, err = dc.user.createNetwork(ctx, &database.Network{ network, err = dc.user.createNetwork(ctx, record)
Addr: dc.registration.networkName,
Nick: nick,
Enabled: true,
})
if err != nil { if err != nil {
return err return err
} }
@ -3069,7 +3068,8 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
} }
attrs := irc.ParseTags(attrsStr) attrs := irc.ParseTags(attrsStr)
record := &database.Network{Nick: dc.nick, Enabled: true} record := database.NewNetwork("")
record.Nick = dc.nick
if err := updateNetworkAttrs(record, attrs, subcommand); err != nil { if err := updateNetworkAttrs(record, attrs, subcommand); err != nil {
return err return err
} }

View File

@ -74,12 +74,8 @@ func createTestUpstream(t *testing.T, db database.Database, user *database.User)
t.Fatalf("failed to create TCP listener: %v", err) t.Fatalf("failed to create TCP listener: %v", err)
} }
network := &database.Network{ network := database.NewNetwork("irc+insecure://" + ln.Addr().String())
Name: "testnet", network.Name = "testnet"
Addr: "irc+insecure://" + ln.Addr().String(),
Nick: user.Username,
Enabled: true,
}
if err := db.StoreNetwork(context.Background(), user.ID, network); err != nil { if err := db.StoreNetwork(context.Background(), user.ID, network); err != nil {
t.Fatalf("failed to store test network: %v", err) t.Fatalf("failed to store test network: %v", err)
} }

View File

@ -577,10 +577,7 @@ func handleServiceNetworkCreate(ctx *serviceContext, params []string) error {
return fmt.Errorf("flag -addr is required") return fmt.Errorf("flag -addr is required")
} }
record := &database.Network{ record := database.NewNetwork(*fs.Addr)
Addr: *fs.Addr,
Enabled: true,
}
if err := fs.update(record); err != nil { if err := fs.update(record); err != nil {
return err return err
} }