diff --git a/js/channelProfile.js b/js/channelProfile.js index abd03eff..4c752719 100644 --- a/js/channelProfile.js +++ b/js/channelProfile.js @@ -3389,13 +3389,7 @@ function setupChannelMessageControls() { } try { - const isSubscribed = - subscriptions.isSubscribed(resolvedHex) === true; - if (isSubscribed) { - await subscriptions.removeChannel(resolvedHex, app.pubkey); - } else { - await subscriptions.addChannel(resolvedHex, app.pubkey); - } + await subscriptions.toggleChannel(resolvedHex, app.pubkey); } catch (error) { userLogger.error("Failed to toggle channel notifications:", error); const permissionErrorCodes = new Set([ @@ -4218,11 +4212,7 @@ function renderSubscribeButton(channelHex) { return; } try { - if (alreadySubscribed) { - await subscriptions.removeChannel(channelHex, currentApp.pubkey); - } else { - await subscriptions.addChannel(channelHex, currentApp.pubkey); - } + await subscriptions.toggleChannel(channelHex, currentApp.pubkey); // Re-render the button so it toggles state renderSubscribeButton(channelHex); } catch (err) { diff --git a/js/subscriptions.js b/js/subscriptions.js index bd2d7217..e4d28053 100644 --- a/js/subscriptions.js +++ b/js/subscriptions.js @@ -535,6 +535,25 @@ class SubscriptionsManager { return this.subscribedPubkeys.has(normalized); } + async toggleChannel(channelHex, userPubkey) { + if (!userPubkey) { + throw new Error("No user pubkey => cannot toggleChannel."); + } + await this.ensureLoaded(userPubkey); + const isSubscribed = this.isSubscribed(channelHex) === true; + try { + if (isSubscribed) { + await this.removeChannel(channelHex, userPubkey); + } else { + await this.addChannel(channelHex, userPubkey); + } + return { ok: true, subscribed: !isSubscribed }; + } catch (error) { + userLogger.error("[SubscriptionsManager] Failed to toggle subscription:", error); + throw error; + } + } + getSubscribedAuthors() { return Array.from(this.subscribedPubkeys); }