From 5a4dfc01b63a1cfcccc2d8a34835c7a4e1cc4aa6 Mon Sep 17 00:00:00 2001 From: Alexander Horner <33007665+alexhorner@users.noreply.github.com> Date: Tue, 6 Dec 2022 20:14:45 +0000 Subject: [PATCH] Fix topic uniqueness issue --- ntfysh_client/Form1.cs | 19 ++++++++++--------- ntfysh_client/NotificationListener.cs | 21 +++++++++++---------- ntfysh_client/SubscribeDialog.cs | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/ntfysh_client/Form1.cs b/ntfysh_client/Form1.cs index cd451c5..6c3b9e1 100644 --- a/ntfysh_client/Form1.cs +++ b/ntfysh_client/Form1.cs @@ -31,14 +31,14 @@ namespace ntfysh_client private void subscribeNewTopic_Click(object sender, EventArgs e) { - using (var dialog = new SubscribeDialog()) + using (var dialog = new SubscribeDialog(notificationTopics)) { var result = dialog.ShowDialog(); if (result == DialogResult.OK) { - notificationListener.SubscribeToTopic(dialog.getTopicId(), dialog.getServerUrl(), dialog.getUsername(), dialog.getPassword()); - notificationTopics.Items.Add(dialog.getTopicId()); + notificationListener.SubscribeToTopic(dialog.getUniqueString(), dialog.getTopicId(), dialog.getServerUrl(), dialog.getUsername(), dialog.getPassword()); + notificationTopics.Items.Add(dialog.getUniqueString()); this.SaveTopicsToFile(); } } @@ -48,9 +48,10 @@ namespace ntfysh_client { while (notificationTopics.SelectedIndex > -1) { - var topicId = notificationTopics.Items[notificationTopics.SelectedIndex]; - notificationListener.RemoveTopic((string)topicId); - notificationTopics.Items.RemoveAt(notificationTopics.SelectedIndex); + string topicUniqueString = (string)notificationTopics.Items[notificationTopics.SelectedIndex]; + + notificationListener.RemoveTopicByUniqueString(topicUniqueString); + notificationTopics.Items.Remove(topicUniqueString); } this.SaveTopicsToFile(); @@ -107,7 +108,7 @@ namespace ntfysh_client private void SaveTopicsToFile() { - string topicsSerialised = JsonConvert.SerializeObject(notificationListener.SubscribedTopics.Select(st => st.Value).ToList(), Formatting.Indented); + string topicsSerialised = JsonConvert.SerializeObject(notificationListener.SubscribedTopicsByUnique.Select(st => st.Value).ToList(), Formatting.Indented); File.WriteAllText(GetTopicsFilePath(), topicsSerialised); } @@ -170,8 +171,8 @@ namespace ntfysh_client //Load them in foreach (SubscribedTopic topic in topics) { - notificationListener.SubscribeToTopic(topic.TopicId, topic.ServerUrl, topic.Username, topic.Password); - notificationTopics.Items.Add(topic.TopicId); + notificationListener.SubscribeToTopic($"{topic.TopicId}@{topic.ServerUrl}", topic.TopicId, topic.ServerUrl, topic.Username, topic.Password); + notificationTopics.Items.Add($"{topic.TopicId}@{topic.ServerUrl}"); } } diff --git a/ntfysh_client/NotificationListener.cs b/ntfysh_client/NotificationListener.cs index 54e5bf1..a7057ca 100644 --- a/ntfysh_client/NotificationListener.cs +++ b/ntfysh_client/NotificationListener.cs @@ -20,7 +20,7 @@ namespace ntfysh_client private bool disposedValue; - public readonly Dictionary SubscribedTopics = new Dictionary(); + public readonly Dictionary SubscribedTopicsByUnique = new Dictionary(); public delegate void NotificationReceiveHandler(object sender, NotificationReceiveEventArgs e); public event NotificationReceiveHandler OnNotificationReceive; @@ -33,7 +33,7 @@ namespace ntfysh_client ServicePointManager.DefaultConnectionLimit = 100; } - public async Task SubscribeToTopic(string topicId, string serverUrl, string username, string password) + public async Task SubscribeToTopic(string unique, string topicId, string serverUrl, string username, string password) { if (string.IsNullOrWhiteSpace(username)) username = null; if (string.IsNullOrWhiteSpace(password)) password = null; @@ -53,7 +53,7 @@ namespace ntfysh_client { using (StreamReader reader = new StreamReader(body)) { - SubscribedTopics.Add(topicId, new SubscribedTopic(topicId, serverUrl, username, password, reader)); + SubscribedTopicsByUnique.Add(unique, new SubscribedTopic(topicId, serverUrl, username, password, reader)); try { @@ -82,9 +82,9 @@ namespace ntfysh_client // If the topic is still registered, then that stream wasn't mean to be closed (maybe network failure?) // Restart it - if (SubscribedTopics.ContainsKey(topicId)) + if (SubscribedTopicsByUnique.ContainsKey(unique)) { - SubscribeToTopic(topicId, serverUrl, username, password); + SubscribeToTopic(unique, topicId, serverUrl, username, password); } } } @@ -92,16 +92,17 @@ namespace ntfysh_client } } - public void RemoveTopic(string topicId) + public void RemoveTopicByUniqueString(string topicUniqueString) { - Debug.WriteLine($"Removing topic {topicId}"); + Debug.WriteLine($"Removing topic {topicUniqueString}"); - if (SubscribedTopics.ContainsKey(topicId)) + if (SubscribedTopicsByUnique.ContainsKey(topicUniqueString)) { // Not moronic to store it in a variable; this solves a race condition in SubscribeToTopic - var topic = SubscribedTopics[topicId]; - SubscribedTopics.Remove(topicId); + SubscribedTopic topic = SubscribedTopicsByUnique[topicUniqueString]; topic.Stream.Close(); + + SubscribedTopicsByUnique.Remove(topicUniqueString); } } diff --git a/ntfysh_client/SubscribeDialog.cs b/ntfysh_client/SubscribeDialog.cs index 861f3fe..f5c697e 100644 --- a/ntfysh_client/SubscribeDialog.cs +++ b/ntfysh_client/SubscribeDialog.cs @@ -5,8 +5,11 @@ namespace ntfysh_client { public partial class SubscribeDialog : Form { - public SubscribeDialog() + private readonly ListBox _notificationTopics; + + public SubscribeDialog(ListBox notificationTopics) { + _notificationTopics = notificationTopics; InitializeComponent(); } @@ -30,6 +33,11 @@ namespace ntfysh_client return password.Text; } + public string getUniqueString() + { + return $"{topicId.Text}@{serverUrl.Text}"; + } + private void button1_Click(object sender, EventArgs e) { if (topicId.Text.Length < 1) @@ -64,6 +72,14 @@ namespace ntfysh_client return; } + if (_notificationTopics.Items.Contains(getUniqueString())) + { + MessageBox.Show($"The specified topic '{topicId.Text}' on the server '{serverUrl.Text}' is already subscribed", "Topic already subscribed", MessageBoxButtons.OK, MessageBoxIcon.Error); + DialogResult = DialogResult.None; + username.Focus(); + return; + } + DialogResult = DialogResult.OK; }