Support multiple reminders for the same time

This is done by offsetting duplicate reminders by 1ms
This commit is contained in:
Starbeamrainbowlabs 2018-11-17 16:50:01 +00:00
parent 6f598d7520
commit 62c278a639
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 38 additions and 18 deletions

View file

@ -112,13 +112,16 @@ namespace RhinoReminds
case "list": case "list":
case "show": case "show":
if (parts.Select((n) => n.ToLower()).Contains("all")) { if (parts.Select((n) => n.ToLower()).Contains("all")) {
// TODO: Make sure that you can't see other people's reminders // Filter by reminders for this user.
IEnumerable<Reminder> userReminderList = reminderList.Reminders.Values.Where(
(Reminder next) => message.From.GetBareJid() == next.JidObj.GetBareJid()
);
StringBuilder listMessage = new StringBuilder("I've got the following reminders on my list:\n"); StringBuilder listMessage = new StringBuilder("I've got the following reminders on my list:\n");
foreach (Reminder nextReminder in reminderList.Reminders.Values) { foreach (Reminder nextReminder in userReminderList) {
listMessage.AppendLine($" - {nextReminder.Message} at {nextReminder.Time}"); listMessage.AppendLine($" - {nextReminder.Message} at {nextReminder.Time}");
} }
listMessage.AppendLine(); listMessage.AppendLine();
listMessage.AppendLine($"({reminderList.Reminders.Count} total)"); listMessage.AppendLine($"({userReminderList.Count()} total)");
sendChatReply(message, listMessage.ToString()); sendChatReply(message, listMessage.ToString());
return; return;
} }

View file

@ -23,19 +23,42 @@ namespace RhinoReminds
} }
public Reminder CreateReminder(Jid inJid, DateTime time, string message) { public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message); Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}"); Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
while (Reminders.ContainsKey(time)) {
Console.WriteLine($"[Rhino/ReminderList] Reminder list contained {time}, adding 1ms");
time = time.AddMilliseconds(1);
}
Reminders.Add(time, result); Reminders.Add(time, result);
OnReminderListUpdate(this, result); OnReminderListUpdate(this, result);
return result; return result;
} }
public Reminder GetNextReminder()
{
if (Reminders.Count == 0)
return null;
return Reminders.Values[0];
}
public void DeleteReminder(Reminder nextReminder)
{
Reminders.Remove(nextReminder.Time);
}
#region Persistence
public void Save(string filename) public void Save(string filename)
{ {
// Make sure that the reminder thread doesn't try to save the reminders at the exact same time // Make sure that the reminder thread doesn't try to save the reminders at the exact same time
// we receive a request for a new reminder // we receive a request for a new reminder
lock (saveLock) { // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead lock (saveLock)
{ // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead
XmlWriter xml = XmlWriter.Create( XmlWriter xml = XmlWriter.Create(
filename, filename,
new XmlWriterSettings() { Indent = true } new XmlWriterSettings() { Indent = true }
@ -57,18 +80,6 @@ namespace RhinoReminds
} }
} }
public Reminder GetNextReminder()
{
if (Reminders.Count == 0)
return null;
return Reminders.Values[0];
}
public void DeleteReminder(Reminder nextReminder) {
Reminders.Remove(nextReminder.Time);
}
public static ReminderList FromXmlFile(string filepath) public static ReminderList FromXmlFile(string filepath)
{ {
XmlDocument xml = new XmlDocument(); XmlDocument xml = new XmlDocument();
@ -76,12 +87,18 @@ namespace RhinoReminds
ReminderList result = new ReminderList(); ReminderList result = new ReminderList();
result.nextId = int.Parse(xml.GetElementsByTagName("NextId")[0].InnerText); result.nextId = int.Parse(xml.GetElementsByTagName("NextId")[0].InnerText);
foreach (XmlNode reminderXML in xml.GetElementsByTagName("Reminders")[0].ChildNodes) { foreach (XmlNode reminderXML in xml.GetElementsByTagName("Reminders")[0].ChildNodes)
{
Reminder nextReminder = Reminder.FromXml(reminderXML); Reminder nextReminder = Reminder.FromXml(reminderXML);
result.Reminders.Add(nextReminder.Time, nextReminder); DateTime timeKey = nextReminder.Time;
while (result.Reminders.ContainsKey(timeKey))
timeKey = timeKey.AddMilliseconds(1);
result.Reminders.Add(timeKey, nextReminder);
} }
return result; return result;
} }
#endregion
} }
} }