Error out on network name conflict

Closes: https://todo.sr.ht/~emersion/soju/29
This commit is contained in:
Simon Ser 2021-04-13 19:31:39 +02:00
parent a2c207d357
commit be2825595d

17
user.go
View File

@ -671,11 +671,24 @@ func (u *user) removeNetwork(network *network) {
panic("tried to remove a non-existing network") panic("tried to remove a non-existing network")
} }
func (u *user) checkNetwork(record *Network) error {
for _, net := range u.networks {
if net.GetName() == record.GetName() && net.ID != record.ID {
return fmt.Errorf("a network with the name %q already exists", record.GetName())
}
}
return nil
}
func (u *user) createNetwork(record *Network) (*network, error) { func (u *user) createNetwork(record *Network) (*network, error) {
if record.ID != 0 { if record.ID != 0 {
panic("tried creating an already-existing network") panic("tried creating an already-existing network")
} }
if err := u.checkNetwork(record); err != nil {
return nil, err
}
network := newNetwork(u, record, nil) network := newNetwork(u, record, nil)
err := u.srv.db.StoreNetwork(u.ID, &network.Network) err := u.srv.db.StoreNetwork(u.ID, &network.Network)
if err != nil { if err != nil {
@ -692,6 +705,10 @@ func (u *user) updateNetwork(record *Network) (*network, error) {
panic("tried updating a new network") panic("tried updating a new network")
} }
if err := u.checkNetwork(record); err != nil {
return nil, err
}
network := u.getNetworkByID(record.ID) network := u.getNetworkByID(record.ID)
if network == nil { if network == nil {
panic("tried updating a non-existing network") panic("tried updating a non-existing network")