Do not call getSettings() for each option in help

Retrieving the settings object for each and every option was wasteful

I don't like how we're doing custom formatting for optionMailbox in many
different places. This should be redone.
This commit is contained in:
Slavi Pantaleev
2022-08-27 19:21:18 +03:00
parent 0d88de9f77
commit 972b4c11c5
2 changed files with 25 additions and 32 deletions

View File

@@ -162,6 +162,11 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
evt := eventFromContext(ctx)
settings, settingsErr := b.getSettings(evt.RoomID)
if settingsErr != nil {
b.Error(ctx, evt.RoomID, settingsErr.Error())
}
var msg strings.Builder
msg.WriteString("The following commands are supported:\n\n")
for _, command := range commands {
@@ -171,16 +176,16 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
msg.WriteString(command.key)
msg.WriteString("`**")
if command.isOption {
value, err := b.getSettingsOption(evt.RoomID, command.key)
if err != nil {
b.Error(ctx, evt.RoomID, err.Error())
if command.isOption && settingsErr == nil {
value := settings.Get(command.key)
if value == "" {
msg.WriteString(" (currently not set)")
} else {
if value == nil {
msg.WriteString(" (currently not set)")
} else {
msg.WriteString(fmt.Sprintf(" (currently `%v`)", value))
if command.key == optionMailbox {
value = fmt.Sprintf("%s@%s", value, b.domain)
}
msg.WriteString(fmt.Sprintf(" (currently `%v`)", value))
}
}
@@ -232,19 +237,26 @@ func (b *Bot) handleOption(ctx context.Context, command []string) {
}
func (b *Bot) getOption(ctx context.Context, name string) {
evt := eventFromContext(ctx)
msg := "`%s` of this room is `%s`"
value, err := b.getSettingsOption(evt.RoomID, name)
evt := eventFromContext(ctx)
cfg, err := b.getSettings(evt.RoomID)
if err != nil {
b.Error(ctx, evt.RoomID, err.Error())
b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err)
return
}
if value == nil {
value := cfg.Get(name)
if value == "" {
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name))
return
}
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value))
if name == optionMailbox {
value = fmt.Sprintf("%s@%s", value, b.domain)
}
b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value))
}
func (b *Bot) setOption(ctx context.Context, name, value string) {

View File

@@ -1,7 +1,6 @@
package bot
import (
"fmt"
"strconv"
"strings"
@@ -116,24 +115,6 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
return config, err
}
func (b *Bot) getSettingsOption(roomID id.RoomID, name string) (any, error) {
cfg, err := b.getSettings(roomID)
if err != nil {
return nil, fmt.Errorf("failed to retrieve settings: %v", err)
}
value := cfg.Get(name)
if value == "" {
return nil, nil
}
if name == optionMailbox {
value = fmt.Sprintf("%s@%s", value, b.domain)
}
return value, nil
}
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
}