Drop user.forEachNetwork

It's a trivial for loop.
This commit is contained in:
Simon Ser 2022-02-04 14:01:27 +01:00
parent f2a03cf7a1
commit 0b5da29916
3 changed files with 17 additions and 18 deletions

View File

@ -351,7 +351,9 @@ func (dc *downstreamConn) forEachNetwork(f func(*network)) {
if dc.network != nil {
f(dc.network)
} else if dc.isMultiUpstream {
dc.user.forEachNetwork(f)
for _, network := range dc.user.networks {
f(network)
}
}
}
@ -775,11 +777,12 @@ func (dc *downstreamConn) handleMessageUnregistered(ctx context.Context, msg *ir
}
var match *network
dc.user.forEachNetwork(func(net *network) {
for _, net := range dc.user.networks {
if net.ID == id {
match = net
break
}
})
}
if match == nil {
return ircError{&irc.Message{
Command: "FAIL",
@ -1436,7 +1439,7 @@ func (dc *downstreamConn) welcome(ctx context.Context) error {
if dc.caps["soju.im/bouncer-networks-notify"] {
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
dc.user.forEachNetwork(func(network *network) {
for _, network := range dc.user.networks {
idStr := fmt.Sprintf("%v", network.ID)
attrs := getNetworkAttrs(network)
dc.SendMessage(&irc.Message{
@ -1445,7 +1448,7 @@ func (dc *downstreamConn) welcome(ctx context.Context) error {
Command: "BOUNCER",
Params: []string{"NETWORK", idStr, attrs.String()},
})
})
}
})
}
@ -2748,7 +2751,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
}}
case "LISTNETWORKS":
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
dc.user.forEachNetwork(func(network *network) {
for _, network := range dc.user.networks {
idStr := fmt.Sprintf("%v", network.ID)
attrs := getNetworkAttrs(network)
dc.SendMessage(&irc.Message{
@ -2757,7 +2760,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
Command: "BOUNCER",
Params: []string{"NETWORK", idStr, attrs.String()},
})
})
}
})
case "ADDNETWORK":
var attrsStr string

View File

@ -506,7 +506,7 @@ func handleServiceNetworkCreate(ctx context.Context, dc *downstreamConn, params
func handleServiceNetworkStatus(ctx context.Context, dc *downstreamConn, params []string) error {
n := 0
dc.user.forEachNetwork(func(net *network) {
for _, net := range dc.user.networks {
var statuses []string
var details string
if uc := net.conn; uc != nil {
@ -541,7 +541,7 @@ func handleServiceNetworkStatus(ctx context.Context, dc *downstreamConn, params
sendServicePRIVMSG(dc, s)
n++
})
}
if n == 0 {
sendServicePRIVMSG(dc, `No network configured, add one with "network create".`)
@ -969,7 +969,9 @@ func handleServiceChannelStatus(ctx context.Context, dc *downstreamConn, params
}
if *networkName == "" {
dc.user.forEachNetwork(sendNetwork)
for _, net := range dc.user.networks {
sendNetwork(net)
}
} else {
net := dc.user.getNetwork(*networkName)
if net == nil {

10
user.go
View File

@ -464,12 +464,6 @@ func newUser(srv *Server, record *User) *user {
}
}
func (u *user) forEachNetwork(f func(*network)) {
for _, network := range u.networks {
f(network)
}
}
func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
for _, network := range u.networks {
if network.conn == nil {
@ -992,11 +986,11 @@ func (u *user) updateUser(ctx context.Context, record *User) error {
if realnameUpdated {
// Re-connect to networks which use the default realname
var needUpdate []Network
u.forEachNetwork(func(net *network) {
for _, net := range u.networks {
if net.Realname == "" {
needUpdate = append(needUpdate, net.Network)
}
})
}
var netErr error
for _, net := range needUpdate {