Fix topic uniqueness issue

This commit is contained in:
Alexander Horner
2022-12-06 20:14:45 +00:00
parent 6aa48fdd2f
commit 5a4dfc01b6
3 changed files with 38 additions and 20 deletions

View File

@@ -31,14 +31,14 @@ namespace ntfysh_client
private void subscribeNewTopic_Click(object sender, EventArgs e) private void subscribeNewTopic_Click(object sender, EventArgs e)
{ {
using (var dialog = new SubscribeDialog()) using (var dialog = new SubscribeDialog(notificationTopics))
{ {
var result = dialog.ShowDialog(); var result = dialog.ShowDialog();
if (result == DialogResult.OK) if (result == DialogResult.OK)
{ {
notificationListener.SubscribeToTopic(dialog.getTopicId(), dialog.getServerUrl(), dialog.getUsername(), dialog.getPassword()); notificationListener.SubscribeToTopic(dialog.getUniqueString(), dialog.getTopicId(), dialog.getServerUrl(), dialog.getUsername(), dialog.getPassword());
notificationTopics.Items.Add(dialog.getTopicId()); notificationTopics.Items.Add(dialog.getUniqueString());
this.SaveTopicsToFile(); this.SaveTopicsToFile();
} }
} }
@@ -48,9 +48,10 @@ namespace ntfysh_client
{ {
while (notificationTopics.SelectedIndex > -1) while (notificationTopics.SelectedIndex > -1)
{ {
var topicId = notificationTopics.Items[notificationTopics.SelectedIndex]; string topicUniqueString = (string)notificationTopics.Items[notificationTopics.SelectedIndex];
notificationListener.RemoveTopic((string)topicId);
notificationTopics.Items.RemoveAt(notificationTopics.SelectedIndex); notificationListener.RemoveTopicByUniqueString(topicUniqueString);
notificationTopics.Items.Remove(topicUniqueString);
} }
this.SaveTopicsToFile(); this.SaveTopicsToFile();
@@ -107,7 +108,7 @@ namespace ntfysh_client
private void SaveTopicsToFile() 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); File.WriteAllText(GetTopicsFilePath(), topicsSerialised);
} }
@@ -170,8 +171,8 @@ namespace ntfysh_client
//Load them in //Load them in
foreach (SubscribedTopic topic in topics) foreach (SubscribedTopic topic in topics)
{ {
notificationListener.SubscribeToTopic(topic.TopicId, topic.ServerUrl, topic.Username, topic.Password); notificationListener.SubscribeToTopic($"{topic.TopicId}@{topic.ServerUrl}", topic.TopicId, topic.ServerUrl, topic.Username, topic.Password);
notificationTopics.Items.Add(topic.TopicId); notificationTopics.Items.Add($"{topic.TopicId}@{topic.ServerUrl}");
} }
} }

View File

@@ -20,7 +20,7 @@ namespace ntfysh_client
private bool disposedValue; private bool disposedValue;
public readonly Dictionary<string, SubscribedTopic> SubscribedTopics = new Dictionary<string, SubscribedTopic>(); public readonly Dictionary<string, SubscribedTopic> SubscribedTopicsByUnique = new Dictionary<string, SubscribedTopic>();
public delegate void NotificationReceiveHandler(object sender, NotificationReceiveEventArgs e); public delegate void NotificationReceiveHandler(object sender, NotificationReceiveEventArgs e);
public event NotificationReceiveHandler OnNotificationReceive; public event NotificationReceiveHandler OnNotificationReceive;
@@ -33,7 +33,7 @@ namespace ntfysh_client
ServicePointManager.DefaultConnectionLimit = 100; 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(username)) username = null;
if (string.IsNullOrWhiteSpace(password)) password = null; if (string.IsNullOrWhiteSpace(password)) password = null;
@@ -53,7 +53,7 @@ namespace ntfysh_client
{ {
using (StreamReader reader = new StreamReader(body)) 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 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?) // If the topic is still registered, then that stream wasn't mean to be closed (maybe network failure?)
// Restart it // 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 // Not moronic to store it in a variable; this solves a race condition in SubscribeToTopic
var topic = SubscribedTopics[topicId]; SubscribedTopic topic = SubscribedTopicsByUnique[topicUniqueString];
SubscribedTopics.Remove(topicId);
topic.Stream.Close(); topic.Stream.Close();
SubscribedTopicsByUnique.Remove(topicUniqueString);
} }
} }

View File

@@ -5,8 +5,11 @@ namespace ntfysh_client
{ {
public partial class SubscribeDialog : Form public partial class SubscribeDialog : Form
{ {
public SubscribeDialog() private readonly ListBox _notificationTopics;
public SubscribeDialog(ListBox notificationTopics)
{ {
_notificationTopics = notificationTopics;
InitializeComponent(); InitializeComponent();
} }
@@ -30,6 +33,11 @@ namespace ntfysh_client
return password.Text; return password.Text;
} }
public string getUniqueString()
{
return $"{topicId.Text}@{serverUrl.Text}";
}
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
if (topicId.Text.Length < 1) if (topicId.Text.Length < 1)
@@ -64,6 +72,14 @@ namespace ntfysh_client
return; 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; DialogResult = DialogResult.OK;
} }