Add user.notifyBouncerNetworkState

DRY.
This commit is contained in:
Simon Ser 2022-04-15 10:41:38 +02:00
parent f3932ab500
commit 42b8dac44e

91
user.go
View File

@ -561,7 +561,6 @@ func (u *user) run() {
uc.updateAway() uc.updateAway()
uc.updateMonitor() uc.updateMonitor()
netIDStr := fmt.Sprintf("%v", uc.network.ID)
uc.forEachDownstream(func(dc *downstreamConn) { uc.forEachDownstream(func(dc *downstreamConn) {
dc.updateSupportedCaps() dc.updateSupportedCaps()
@ -574,18 +573,10 @@ func (u *user) run() {
dc.updateRealname() dc.updateRealname()
dc.updateAccount() dc.updateAccount()
}) })
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(uc.network.ID, irc.Tags{
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { "state": "connected",
dc.SendMessage(&irc.Message{ "error": "",
Prefix: dc.srv.prefix(), })
Command: "BOUNCER",
Params: []string{"NETWORK", netIDStr, irc.Tags{
"state": "connected",
"error": "",
}.String()},
})
}
}
uc.network.lastError = nil uc.network.lastError = nil
case eventUpstreamDisconnected: case eventUpstreamDisconnected:
u.handleUpstreamDisconnected(e.uc) u.handleUpstreamDisconnected(e.uc)
@ -605,17 +596,9 @@ func (u *user) run() {
}) })
} }
net.lastError = e.err net.lastError = e.err
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(net.ID, irc.Tags{
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { "error": irc.TagValue(net.lastError.Error()),
dc.SendMessage(&irc.Message{ })
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", fmt.Sprintf("%v", net.ID), irc.Tags{
"error": irc.TagValue(net.lastError.Error()),
}.String()},
})
}
}
case eventUpstreamError: case eventUpstreamError:
uc := e.uc uc := e.uc
@ -623,17 +606,9 @@ func (u *user) run() {
sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err)) sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
}) })
uc.network.lastError = e.err uc.network.lastError = e.err
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(uc.network.ID, irc.Tags{
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { "error": irc.TagValue(uc.network.lastError.Error()),
dc.SendMessage(&irc.Message{ })
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", fmt.Sprintf("%v", uc.network.ID), irc.Tags{
"error": irc.TagValue(uc.network.lastError.Error()),
}.String()},
})
}
}
case eventUpstreamMessage: case eventUpstreamMessage:
msg, uc := e.msg, e.uc msg, uc := e.msg, e.uc
if uc.isClosed() { if uc.isClosed() {
@ -773,7 +748,6 @@ func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
uch.updateAutoDetach(0) uch.updateAutoDetach(0)
} }
netIDStr := fmt.Sprintf("%v", uc.network.ID)
uc.forEachDownstream(func(dc *downstreamConn) { uc.forEachDownstream(func(dc *downstreamConn) {
dc.updateSupportedCaps() dc.updateSupportedCaps()
}) })
@ -790,15 +764,7 @@ func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
return return
} }
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(uc.network.ID, irc.Tags{"state": "disconnected"})
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", netIDStr, "state=disconnected"},
})
}
}
if uc.network.lastError == nil { if uc.network.lastError == nil {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forEachDownstream(func(dc *downstreamConn) {
@ -809,6 +775,19 @@ func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
} }
} }
func (u *user) notifyBouncerNetworkState(netID int64, attrs irc.Tags) {
netIDStr := fmt.Sprintf("%v", netID)
for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", netIDStr, attrs.String()},
})
}
}
}
func (u *user) addNetwork(network *network) { func (u *user) addNetwork(network *network) {
u.networks = append(u.networks, network) u.networks = append(u.networks, network)
@ -909,17 +888,8 @@ func (u *user) createNetwork(ctx context.Context, record *Network) (*network, er
u.addNetwork(network) u.addNetwork(network)
idStr := fmt.Sprintf("%v", network.ID)
attrs := getNetworkAttrs(network) attrs := getNetworkAttrs(network)
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(network.ID, attrs)
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", idStr, attrs.String()},
})
}
}
return network, nil return network, nil
} }
@ -990,17 +960,8 @@ func (u *user) updateNetwork(ctx context.Context, record *Network) (*network, er
u.addNetwork(updatedNetwork) u.addNetwork(updatedNetwork)
// TODO: only broadcast attributes that have changed // TODO: only broadcast attributes that have changed
idStr := fmt.Sprintf("%v", updatedNetwork.ID)
attrs := getNetworkAttrs(updatedNetwork) attrs := getNetworkAttrs(updatedNetwork)
for _, dc := range u.downstreamConns { u.notifyBouncerNetworkState(updatedNetwork.ID, attrs)
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: "BOUNCER",
Params: []string{"NETWORK", idStr, attrs.String()},
})
}
}
return updatedNetwork, nil return updatedNetwork, nil
} }