Fix topic uniqueness issue
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -20,7 +20,7 @@ namespace ntfysh_client
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user