Validate admin targets before actions
This commit is contained in:
committed by
Brandon McGinty
parent
cf9e1c6d0a
commit
461f172035
@@ -169,7 +169,9 @@ func (b *Barnard) AdminItemKeyPress(ui *uiterm.Ui, tree *uiterm.Tree, item uiter
|
||||
if !ok || admin.action == nil {
|
||||
return
|
||||
}
|
||||
admin.action()
|
||||
if !b.withValidAdminTargets(admin.action) {
|
||||
b.AddOutputLine("Admin: action target is no longer available")
|
||||
}
|
||||
b.UiAdmin.Rebuild()
|
||||
b.Ui.Refresh()
|
||||
}
|
||||
@@ -549,13 +551,37 @@ func (b *Barnard) handleAdminPrompt(text string) bool {
|
||||
}
|
||||
prompt := b.pendingAdminPrompt
|
||||
b.pendingAdminPrompt = nil
|
||||
prompt.action(strings.TrimSpace(text))
|
||||
if !b.withValidAdminTargets(func() { prompt.action(strings.TrimSpace(text)) }) {
|
||||
b.AddOutputLine("Admin: action target is no longer available")
|
||||
}
|
||||
if b.Client != nil && b.Client.Self != nil {
|
||||
b.UpdateInputStatus(fmt.Sprintf("[%s]", b.Client.Self.Channel.Name))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// withValidAdminTargets runs an action only while its selected targets are
|
||||
// still members of the current connection. Menu actions can outlive server
|
||||
// removal events while a prompt is open.
|
||||
func (b *Barnard) withValidAdminTargets(action func()) bool {
|
||||
if b.Client == nil {
|
||||
return false
|
||||
}
|
||||
valid := true
|
||||
b.Client.Do(func() {
|
||||
if u := b.adminTargetUser; u != nil && b.Client.Users[u.Session] != u {
|
||||
valid = false
|
||||
}
|
||||
if ch := b.adminTargetChan; ch != nil && b.Client.Channels[ch.ID] != ch {
|
||||
valid = false
|
||||
}
|
||||
if valid {
|
||||
action()
|
||||
}
|
||||
})
|
||||
return valid
|
||||
}
|
||||
|
||||
func (b *Barnard) CommandAdmin(ui *uiterm.Ui, cmd string) {
|
||||
b.executeAdminCommand(cmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user