From 5e4018a742c904de9458e429b2997e86498de3ec Mon Sep 17 00:00:00 2001 From: ayn2op Date: Mon, 31 Aug 2026 18:41:04 -0400 Subject: [PATCH] refactor(config): simplify MIME type lookup --- internal/config/config.go | 13 ++++++------- internal/config/config_test.go | 6 +++--- internal/ui/chat/messages_list.go | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index f8bb785..566e47b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "slices" "sync" "unicode/utf8" @@ -198,18 +199,16 @@ func (types *MIMETypes) UnmarshalTOML(value any) error { return nil } -func (types MIMETypes) Allows(mediaType string) bool { +func (types MIMETypes) Has(mediaType string) bool { mediaType, _, err := mime.ParseMediaType(mediaType) if err != nil { return false } - for _, pattern := range types { - if matches, _ := path.Match(pattern, mediaType); matches { - return true - } - } - return false + return slices.ContainsFunc(types, func(pattern string) bool { + matches, _ := path.Match(pattern, mediaType) + return matches + }) } func applyDefaults(cfg *Config) { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index a66ca5a..0487beb 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -146,7 +146,7 @@ func TestLoad(t *testing.T) { }) } -func TestMIMETypesAllows(t *testing.T) { +func TestMIMETypesHas(t *testing.T) { tests := []struct { name string allowed MIMETypes @@ -164,8 +164,8 @@ func TestMIMETypesAllows(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.allowed.Allows(tt.mediaType); got != tt.wantAllowed { - t.Fatalf("Allows(%q) = %v, want %v", tt.mediaType, got, tt.wantAllowed) + if got := tt.allowed.Has(tt.mediaType); got != tt.wantAllowed { + t.Fatalf("Has(%q) = %v, want %v", tt.mediaType, got, tt.wantAllowed) } }) } diff --git a/internal/ui/chat/messages_list.go b/internal/ui/chat/messages_list.go index 303d22e..56c5bd3 100644 --- a/internal/ui/chat/messages_list.go +++ b/internal/ui/chat/messages_list.go @@ -1141,7 +1141,7 @@ func (ml *messagesList) openAttachment(attachment discord.Attachment) tview.Cmd } func (ml *messagesList) confirmAttachment(attachment discord.Attachment, action tview.Cmd) tview.Cmd { - if !ml.cfg.AllowedMIMETypes.Allows(attachment.ContentType) { + if !ml.cfg.AllowedMIMETypes.Has(attachment.ContentType) { return ui.ShowModal( "This attachment type is not allowed and may be unsafe. Continue anyway?", ui.ModalButton{Label: "No"},