public async Task UpdateNotesAsync(string guid, string matchFile, Throttle throttle, ProgressData progressData) { var notes = ReadMatchFile(matchFile, progressData).ToList(); await MaybeUpdateFilesAsync(notes); notes = (await FilterModifiedNodesAsync(guid, notes, throttle, progressData)).ToList(); if (notes.Count == 0) { MessageBox.Show("No changed notes were found.", "No changed notes", MessageBoxButton.OK, MessageBoxImage.Information); return; } var toChangeCount = notes.Count(note => !string.IsNullOrEmpty(note.NewNotes)); var toRemoveCount = notes.Count - toChangeCount; var message = "Found " + (toChangeCount > 0 && toRemoveCount > 0 ? $"{toChangeCount} notes to change and {toRemoveCount} notes to remove." : toChangeCount > 0 ? $"{toChangeCount} notes to change." : $"{toRemoveCount} notes to remove.") + " Continue?"; if (MessageBox.Show( message, "Notes to change", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) { return; } progressData.Reset("Updating notes", notes.Count); var updateTasks = notes.Select(async note => { await _matchesRetriever.UpdateNotesAsync(guid, note.TestId, note.NewNotes, throttle); progressData.Increment(); }); await Task.WhenAll(updateTasks); await SaveUpdatedNotesToFileAsync(notes, progressData); }
public async Task UpdateNotesAsync(string guid, string matchFile, Throttle throttle, ProgressData progressData) { var originalTags = await _matchesRetriever.GetTagsAsync(guid, throttle); var originalTagIds = new HashSet <int>(originalTags.Select(tag => tag.TagId)); var notes = ReadMatchFile(matchFile, originalTags, progressData).ToList(); var originalNumNotes = notes.Count; await MaybeUpdateFilesAsync(notes, originalTags); notes = (await FilterModifiedNodesAsync(guid, notes, originalTagIds, throttle, progressData)).ToList(); if (notes.Count == 0) { MessageBox.Show("No changed notes were found.", "No changed notes", MessageBoxButton.OK, MessageBoxImage.Information); return; } var duplicateTags = originalTags .GroupBy(tag => tag.Label) .Where(g => g.Count() > 1) .SelectMany(g => g).ToList(); if (duplicateTags.Count > 1 && duplicateTags.Select(tag => tag.TagId).Intersect(notes.SelectMany(note => note.NewTags.Concat(note.NewTagsRemoved))).Any()) { MessageBox.Show($"Duplicate group names found: '{duplicateTags.First().Label}'. Cannot update groups when more than one group has a matching name.", "Duplicate groups", MessageBoxButton.OK, MessageBoxImage.Information); return; } var toChangeCount = notes.Count( note => (note.NewNotes != null && note.NewNotes != "") || (note.NewStarred != null && note.NewStarred != note.OldStarred) || note.NewTags.Except(note.OldTags).Any() || note.NewTagsRemoved.Intersect(note.OldTags).Any()); var toRemoveCount = notes.Count(note => note.NewNotes == ""); var message = "Found " + (toChangeCount > 0 && toRemoveCount > 0 ? $"{toChangeCount} matches to change and {toRemoveCount} notes to remove." : toChangeCount > 0 ? $"{toChangeCount} matches to change." : $"{toRemoveCount} notes to remove.") + " Are you sure that you want to make changes to your notes on the Ancestry website?"; if (MessageBox.Show( message, "Notes to change", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) { return; } progressData.Reset("Updating notes", notes.Count); var updateTasks = notes.Select(async note => { try { if (note.NewNotes != null) { await _matchesRetriever.UpdateNotesAsync(guid, note.TestId, note.NewNotes, throttle); } if (note.NewStarred != null) { await _matchesRetriever.UpdateStarredAsync(guid, note.TestId, note.NewStarred.Value, throttle); } foreach (var tagId in note.NewTags.Except(note.OldTags)) { await _matchesRetriever.AddTagAsync(guid, note.TestId, tagId, throttle); } foreach (var tagId in note.NewTagsRemoved.Intersect(note.OldTags)) { await _matchesRetriever.DeleteTagAsync(guid, note.TestId, tagId, throttle); } return(note); } catch (Exception ex) { FileUtils.LogException(new Exception($"Failed to update notes for {note.Name} ({note.TestId})", ex), false); return(null); } finally { progressData.Increment(); } }); var updatedNotes = await Task.WhenAll(updateTasks); await SaveUpdatedNotesToFileAsync(updatedNotes.Where(note => note != null).ToList(), originalTags, progressData); if (originalNumNotes > notes.Count * 100 || originalNumNotes > notes.Count + 1000) { MessageBox.Show($"Only {notes.Count} out of {originalNumNotes} were updated. The uploading process would be much quicker if you trim down your file to just the changed matches, before uploading the changes.", "Few notes updated", MessageBoxButton.OK, MessageBoxImage.Information); } }