unoptimal refactoring
This commit is contained in:
@@ -98,11 +98,11 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, plaintext, html strin
|
|||||||
|
|
||||||
settings, err := b.getSettings(ctx, roomID)
|
settings, err := b.getSettings(ctx, roomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
b.Error(ctx, roomID, "cannot get settings: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var text strings.Builder
|
var text strings.Builder
|
||||||
if !settings.HideSenderAddress {
|
if !settings.NoSender {
|
||||||
text.WriteString("From: ")
|
text.WriteString("From: ")
|
||||||
text.WriteString(from)
|
text.WriteString(from)
|
||||||
text.WriteString("\n\n")
|
text.WriteString("\n\n")
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var commands = map[string]string{
|
var commands = map[string]string{
|
||||||
"mailbox": "Get or set mailbox of that room",
|
"mailbox": "Get or set mailbox of that room",
|
||||||
"owner": "Get or set owner of that room",
|
"owner": "Get or set owner of that room",
|
||||||
"hide-sender-address": "Get or set the `hide-sender-address` setting (controls if the sender's email address is displayed or not; default `false`)",
|
"nosender": "Get or set `nosender` of that room (`true` - hide email sender; `false` - show email sender)",
|
||||||
"help": "Get help",
|
"help": "Get help",
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) {
|
||||||
@@ -34,8 +34,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []str
|
|||||||
b.handleOwner(ctx, evt, command)
|
b.handleOwner(ctx, evt, command)
|
||||||
case "mailbox":
|
case "mailbox":
|
||||||
b.handleMailbox(ctx, evt, command)
|
b.handleMailbox(ctx, evt, command)
|
||||||
case "hide-sender-address":
|
case "nosender":
|
||||||
b.handleHideSenderAddress(ctx, evt, command)
|
b.handleNoSender(ctx, evt, command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ func (b *Bot) parseCommand(message string) []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
message = strings.TrimSpace(strings.Replace(message, b.prefix, "", 1))
|
message = strings.ToLower(strings.TrimSpace(strings.Replace(message, b.prefix, "", 1)))
|
||||||
return strings.Split(message, " ")
|
return strings.Split(message, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ var migrations = []string{}
|
|||||||
|
|
||||||
// settings of a room
|
// settings of a room
|
||||||
type settings struct {
|
type settings struct {
|
||||||
Mailbox string
|
Mailbox string
|
||||||
Owner id.UserID
|
Owner id.UserID
|
||||||
HideSenderAddress bool
|
NoSender bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allowed checks if change is allowed
|
// Allowed checks if change is allowed
|
||||||
|
|||||||
104
bot/mailbox.go
104
bot/mailbox.go
@@ -2,7 +2,6 @@ package bot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
@@ -111,106 +110,3 @@ func (b *Bot) setMailbox(ctx context.Context, evt *event.Event, mailbox string)
|
|||||||
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleHideSenderAddress(ctx context.Context, evt *event.Event, command []string) {
|
|
||||||
getter := func(entity settings) bool {
|
|
||||||
return entity.HideSenderAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
setter := func(entity *settings, value bool) error {
|
|
||||||
entity.HideSenderAddress = value
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
b.handleBooleanConfigurationKey(ctx, evt, command, "hide-sender-address", getter, setter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) handleBooleanConfigurationKey(
|
|
||||||
ctx context.Context,
|
|
||||||
evt *event.Event,
|
|
||||||
command []string,
|
|
||||||
configKey string,
|
|
||||||
getter func(entity settings) bool,
|
|
||||||
setter func(entity *settings, value bool) error,
|
|
||||||
) {
|
|
||||||
if len(command) == 1 {
|
|
||||||
b.getBooleanConfigurationKey(ctx, evt, configKey, getter, setter)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b.setBooleanConfigurationKey(ctx, evt, configKey, command[1], getter, setter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) getBooleanConfigurationKey(
|
|
||||||
ctx context.Context,
|
|
||||||
evt *event.Event,
|
|
||||||
configKey string,
|
|
||||||
getter func(entity settings) bool,
|
|
||||||
setter func(entity *settings, value bool) error,
|
|
||||||
) {
|
|
||||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName(fmt.Sprintf("getBooleanConfigurationKey.%s", configKey)))
|
|
||||||
defer span.Finish()
|
|
||||||
|
|
||||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
|
||||||
if err != nil {
|
|
||||||
b.log.Warn("cannot get %s settings: %v", evt.RoomID, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
value := getter(cfg)
|
|
||||||
|
|
||||||
content := format.RenderMarkdown(fmt.Sprintf("`%s` configuration setting for this room is currently set to `%v`", configKey, value), true, true)
|
|
||||||
content.MsgType = event.MsgNotice
|
|
||||||
_, err = b.lp.Send(evt.RoomID, content)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) setBooleanConfigurationKey(
|
|
||||||
ctx context.Context,
|
|
||||||
evt *event.Event,
|
|
||||||
configKey string,
|
|
||||||
value string,
|
|
||||||
getter func(entity settings) bool,
|
|
||||||
setter func(entity *settings, value bool) error,
|
|
||||||
) {
|
|
||||||
var actualValue bool
|
|
||||||
if value == "true" {
|
|
||||||
actualValue = true
|
|
||||||
} else if value == "false" {
|
|
||||||
actualValue = false
|
|
||||||
} else {
|
|
||||||
b.Notice(ctx, evt.RoomID, "you are supposed to send a true or false value")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName(fmt.Sprintf("setBooleanConfigurationKey.%s", configKey)))
|
|
||||||
defer span.Finish()
|
|
||||||
|
|
||||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve setting: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !cfg.Allowed(b.noowner, evt.Sender) {
|
|
||||||
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setter(&cfg, actualValue)
|
|
||||||
|
|
||||||
err = b.setSettings(span.Context(), evt.RoomID, cfg)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
content := format.RenderMarkdown(fmt.Sprintf("`%s` configuration setting for this room has been set to `%v`", configKey, actualValue), true, true)
|
|
||||||
content.MsgType = event.MsgNotice
|
|
||||||
_, err = b.lp.Send(evt.RoomID, content)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
68
bot/nosender.go
Normal file
68
bot/nosender.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/getsentry/sentry-go"
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
|
"maunium.net/go/mautrix/event"
|
||||||
|
"maunium.net/go/mautrix/format"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Bot) handleNoSender(ctx context.Context, evt *event.Event, command []string) {
|
||||||
|
if len(command) == 1 {
|
||||||
|
b.getNoSender(ctx, evt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.setNoSender(ctx, evt, command[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) getNoSender(ctx context.Context, evt *event.Event) {
|
||||||
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getNoSender"))
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "failed to retrieve setting: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content := format.RenderMarkdown(fmt.Sprintf("`nosender` of this room is **%t**", cfg.NoSender), true, true)
|
||||||
|
content.MsgType = event.MsgNotice
|
||||||
|
_, err = b.lp.Send(evt.RoomID, content)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) setNoSender(ctx context.Context, evt *event.Event, value string) {
|
||||||
|
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setNoSender"))
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
nosender := utils.Bool(value)
|
||||||
|
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "failed to retrieve setting: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cfg.Allowed(b.noowner, evt.Sender) {
|
||||||
|
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.NoSender = nosender
|
||||||
|
err = b.setSettings(span.Context(), evt.RoomID, cfg)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content := format.RenderMarkdown(fmt.Sprintf("`nosender` of this room set to **%t**", nosender), true, true)
|
||||||
|
content.MsgType = event.MsgNotice
|
||||||
|
_, err = b.lp.Send(evt.RoomID, content)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
// Mailbox returns mailbox part from email address
|
||||||
func Mailbox(email string) string {
|
func Mailbox(email string) string {
|
||||||
index := strings.LastIndex(email, "@")
|
index := strings.LastIndex(email, "@")
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
@@ -10,6 +11,17 @@ func Mailbox(email string) string {
|
|||||||
return email[:strings.LastIndex(email, "@")]
|
return email[:strings.LastIndex(email, "@")]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hostname returns hostname part from email address
|
||||||
func Hostname(email string) string {
|
func Hostname(email string) string {
|
||||||
return email[strings.LastIndex(email, "@")+1:]
|
return email[strings.LastIndex(email, "@")+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bool converts string to boolean
|
||||||
|
func Bool(str string) bool {
|
||||||
|
str = strings.ToLower(str)
|
||||||
|
if str == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return (str == "1" || str == "true" || str == "yes")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user