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) { func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
evt := eventFromContext(ctx) evt := eventFromContext(ctx)
settings, settingsErr := b.getSettings(evt.RoomID)
if settingsErr != nil {
b.Error(ctx, evt.RoomID, settingsErr.Error())
}
var msg strings.Builder var msg strings.Builder
msg.WriteString("The following commands are supported:\n\n") msg.WriteString("The following commands are supported:\n\n")
for _, command := range commands { for _, command := range commands {
@@ -171,16 +176,16 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
msg.WriteString(command.key) msg.WriteString(command.key)
msg.WriteString("`**") msg.WriteString("`**")
if command.isOption { if command.isOption && settingsErr == nil {
value, err := b.getSettingsOption(evt.RoomID, command.key) value := settings.Get(command.key)
if err != nil {
b.Error(ctx, evt.RoomID, err.Error()) if value == "" {
msg.WriteString(" (currently not set)")
} else { } else {
if value == nil { if command.key == optionMailbox {
msg.WriteString(" (currently not set)") value = fmt.Sprintf("%s@%s", value, b.domain)
} else {
msg.WriteString(fmt.Sprintf(" (currently `%v`)", value))
} }
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) { 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 { if err != nil {
b.Error(ctx, evt.RoomID, err.Error()) b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err)
return return
} }
if value == nil {
value := cfg.Get(name)
if value == "" {
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name)) b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name))
return 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) { func (b *Bot) setOption(ctx context.Context, name, value string) {

View File

@@ -1,7 +1,6 @@
package bot package bot
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
@@ -116,24 +115,6 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
return config, err 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 { func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg) return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
} }