Use string constants for options, not magic strings

This also adds `Mailbox()` and `Owner()` getters for completeness.

Wording has been changed a bit to avoid saying "that room". It sounds
better if it's "this room" or just "the room".
This commit is contained in:
Slavi Pantaleev
2022-08-24 10:46:42 +03:00
parent 79ed440fdc
commit 26edcdadbc
2 changed files with 42 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ package bot
import (
"context"
"fmt"
"strings"
"github.com/getsentry/sentry-go"
@@ -18,20 +19,26 @@ var (
commands = map[string]string{
// special commands
"help": "Get help",
"stop": "Disable bridge for that room and clear all configuration",
"stop": "Disable bridge for the room and clear all configuration",
// options commands
"mailbox": "Get or set mailbox of that room",
"owner": "Get or set owner of that room",
"nosender": "Get or set `nosender` of that room (`true` - hide email sender; `false` - show email sender)",
"nosubject": "Get or set `nosubject` of that room (`true` - hide email subject; `false` - show email subject)",
optionMailbox: "Get or set mailbox of the room",
optionOwner: "Get or set owner of the room",
optionNoSender: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)",
optionNoSender,
),
optionNoSubject: fmt.Sprintf(
"Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)",
optionNoSubject,
),
}
// sanitizers is map of option name => sanitizer function
sanitizers = map[string]sanitizerFunc{
"mailbox": utils.Mailbox,
"nosender": utils.SanitizeBoolString,
"nosubject": utils.SanitizeBoolString,
optionMailbox: utils.Mailbox,
optionNoSender: utils.SanitizeBoolString,
optionNoSubject: utils.SanitizeBoolString,
}
)
@@ -106,7 +113,7 @@ func (b *Bot) runStop(ctx context.Context, evt *event.Event) {
return
}
mailbox := cfg.Get("mailbox")
mailbox := cfg.Get(optionMailbox)
if mailbox == "" {
b.Notice(span.Context(), evt.RoomID, "that room is not configured yet")
return
@@ -150,7 +157,7 @@ func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
return
}
if name == "mailbox" {
if name == optionMailbox {
msg = msg + "@" + b.domain
}
@@ -167,7 +174,7 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
value = sanitizer(value)
}
if name == "mailbox" {
if name == optionMailbox {
existingID, ok := b.GetMapping(ctx, value)
if ok && existingID != "" && existingID != evt.RoomID {
b.Notice(span.Context(), evt.RoomID, "Mailbox %s@%s already taken", value, b.domain)
@@ -187,9 +194,9 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
}
cfg.Set(name, value)
if name == "mailbox" {
if name == optionMailbox {
msg = msg + "@" + b.domain
cfg.Set("owner", evt.Sender.String())
cfg.Set(optionOwner, evt.Sender.String())
b.roomsmu.Lock()
b.rooms[value] = evt.RoomID
b.roomsmu.Unlock()

View File

@@ -13,6 +13,13 @@ import (
const settingskey = "cc.etke.postmoogle.settings"
const (
optionOwner = "owner"
optionMailbox = "mailbox"
optionNoSender = "nosender"
optionNoSubject = "nosubject"
)
var migrations = []string{}
// settings of a room
@@ -31,7 +38,7 @@ func (s settings) Allowed(noowner bool, userID id.UserID) bool {
return true
}
owner := s.Get("owner")
owner := s.Owner()
if owner == "" {
return true
}
@@ -50,12 +57,20 @@ func (s settings) Get(key string) string {
return value
}
func (s settings) Mailbox() string {
return s.Get(optionMailbox)
}
func (s settings) Owner() string {
return s.Get(optionOwner)
}
func (s settings) NoSender() bool {
return utils.Bool(s.Get("nosender"))
return utils.Bool(s.Get(optionNoSender))
}
func (s settings) NoSubject() bool {
return utils.Bool(s.Get("nosubject"))
return utils.Bool(s.Get(optionNoSubject))
}
// Set option
@@ -110,7 +125,7 @@ func (b *Bot) syncRooms(ctx context.Context) error {
b.log.Warn("cannot get %s settings: %v", roomID, err)
continue
}
mailbox := cfg.Get("mailbox")
mailbox := cfg.Mailbox()
if mailbox != "" {
b.rooms[mailbox] = roomID
}
@@ -132,9 +147,9 @@ func (b *Bot) migrateSettings(ctx context.Context, roomID id.RoomID) {
return
}
cfg := settings{}
cfg.Set("mailbox", config.Mailbox)
cfg.Set("owner", config.Owner.String())
cfg.Set("nosender", strconv.FormatBool(config.NoSender))
cfg.Set(optionMailbox, config.Mailbox)
cfg.Set(optionOwner, config.Owner.String())
cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender))
err = b.setSettings(ctx, roomID, cfg)
if err != nil {