From 021b087a129727d324d149dffa5e40fee757c9cf Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 17 Jan 2026 09:49:47 -0800 Subject: [PATCH] fix(mail): improve channel subscribe/unsubscribe feedback - Report "already subscribed" instead of false success on re-subscribe - Report "not subscribed" instead of false success on redundant unsubscribe - Add explicit channel existence check before subscribe/unsubscribe - Return empty JSON array [] instead of null for no subscribers Co-Authored-By: Claude Opus 4.5 --- internal/cmd/mail_channel.go | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/cmd/mail_channel.go b/internal/cmd/mail_channel.go index df592ea2..c3f50603 100644 --- a/internal/cmd/mail_channel.go +++ b/internal/cmd/mail_channel.go @@ -352,6 +352,23 @@ func runChannelSubscribe(cmd *cobra.Command, args []string) error { b := beads.New(townRoot) + // Check channel exists and current subscription status + _, fields, err := b.GetChannelBead(name) + if err != nil { + return fmt.Errorf("getting channel: %w", err) + } + if fields == nil { + return fmt.Errorf("channel not found: %s", name) + } + + // Check if already subscribed + for _, s := range fields.Subscribers { + if s == subscriber { + fmt.Printf("%s is already subscribed to channel %q\n", subscriber, name) + return nil + } + } + if err := b.SubscribeToChannel(name, subscriber); err != nil { return fmt.Errorf("subscribing to channel: %w", err) } @@ -375,6 +392,28 @@ func runChannelUnsubscribe(cmd *cobra.Command, args []string) error { b := beads.New(townRoot) + // Check channel exists and current subscription status + _, fields, err := b.GetChannelBead(name) + if err != nil { + return fmt.Errorf("getting channel: %w", err) + } + if fields == nil { + return fmt.Errorf("channel not found: %s", name) + } + + // Check if actually subscribed + found := false + for _, s := range fields.Subscribers { + if s == subscriber { + found = true + break + } + } + if !found { + fmt.Printf("%s is not subscribed to channel %q\n", subscriber, name) + return nil + } + if err := b.UnsubscribeFromChannel(name, subscriber); err != nil { return fmt.Errorf("unsubscribing from channel: %w", err) } @@ -402,9 +441,13 @@ func runChannelSubscribers(cmd *cobra.Command, args []string) error { } if channelJSON { + subs := fields.Subscribers + if subs == nil { + subs = []string{} + } enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") - return enc.Encode(fields.Subscribers) + return enc.Encode(subs) } if len(fields.Subscribers) == 0 {