Get rid of various duplicated optionMailbox formatting
`formatOptionValue` takes care of these now. Besides fixing the duplication, this also fixes the lint error affecting `sendHelp` - high complexity.
This commit is contained in:
@@ -182,10 +182,7 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
|||||||
if value == "" {
|
if value == "" {
|
||||||
msg.WriteString(" (currently not set)")
|
msg.WriteString(" (currently not set)")
|
||||||
} else {
|
} else {
|
||||||
if command.key == optionMailbox {
|
msg.WriteString(fmt.Sprintf(" (currently `%v`)", b.formatOptionValue(command.key, value)))
|
||||||
value = fmt.Sprintf("%s@%s", value, b.domain)
|
|
||||||
}
|
|
||||||
msg.WriteString(fmt.Sprintf(" (currently `%v`)", value))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,8 +234,6 @@ 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) {
|
||||||
msg := "`%s` of this room is `%s`"
|
|
||||||
|
|
||||||
evt := eventFromContext(ctx)
|
evt := eventFromContext(ctx)
|
||||||
cfg, err := b.getSettings(evt.RoomID)
|
cfg, err := b.getSettings(evt.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -252,16 +247,14 @@ func (b *Bot) getOption(ctx context.Context, name string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if name == optionMailbox {
|
b.Notice(ctx, evt.RoomID, fmt.Sprintf(
|
||||||
value = fmt.Sprintf("%s@%s", value, b.domain)
|
"`%s` of this room is `%s`",
|
||||||
}
|
name,
|
||||||
|
b.formatOptionValue(name, value),
|
||||||
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) {
|
||||||
msg := "`%s` of this room set to `%s`"
|
|
||||||
|
|
||||||
sanitizer, ok := sanitizers[name]
|
sanitizer, ok := sanitizers[name]
|
||||||
if ok {
|
if ok {
|
||||||
value = sanitizer(value)
|
value = sanitizer(value)
|
||||||
@@ -271,7 +264,7 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
if name == optionMailbox {
|
if name == optionMailbox {
|
||||||
existingID, ok := b.GetMapping(value)
|
existingID, ok := b.GetMapping(value)
|
||||||
if ok && existingID != "" && existingID != evt.RoomID {
|
if ok && existingID != "" && existingID != evt.RoomID {
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain))
|
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` already taken, kupo", b.formatOptionValue(name, value)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,9 +282,8 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
|
|
||||||
cfg.Set(name, value)
|
cfg.Set(name, value)
|
||||||
if name == optionMailbox {
|
if name == optionMailbox {
|
||||||
value = fmt.Sprintf("%s@%s", value, b.domain)
|
|
||||||
cfg.Set(optionOwner, evt.Sender.String())
|
cfg.Set(optionOwner, evt.Sender.String())
|
||||||
b.rooms.Store(value, evt.RoomID)
|
b.rooms.Store(b.formatOptionValue(name, value), evt.RoomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.setSettings(evt.RoomID, cfg)
|
err = b.setSettings(evt.RoomID, cfg)
|
||||||
@@ -300,5 +292,9 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value))
|
b.Notice(ctx, evt.RoomID, fmt.Sprintf(
|
||||||
|
"`%s` of this room set to `%s`",
|
||||||
|
name,
|
||||||
|
b.formatOptionValue(name, value),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -118,3 +119,15 @@ func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) formatOptionValue(name string, value string) string {
|
||||||
|
if value == "" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == optionMailbox {
|
||||||
|
value = fmt.Sprintf("%s@%s", value, b.domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user