From c73888b2c4877154f0ed212e12369629cd1e1991 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Thu, 13 Aug 2020 10:32:26 +1000 Subject: [PATCH] Geary.RFC822.MessageIDList: Support merging message id lists Add support for merging a list with a single id or a another list, only appending the new id(s) if not already present. Add unit tests. --- src/engine/rfc822/rfc822-message-data.vala | 28 +++++++++++++++++++ .../rfc822/rfc822-message-data-test.vala | 14 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/engine/rfc822/rfc822-message-data.vala b/src/engine/rfc822/rfc822-message-data.vala index 09aa1d4e..43cc0223 100644 --- a/src/engine/rfc822/rfc822-message-data.vala +++ b/src/engine/rfc822/rfc822-message-data.vala @@ -210,6 +210,34 @@ public class Geary.RFC822.MessageIDList : return this.list.read_only_view; } + /** + * Returns a list with the given id appended if not already present. + * + * This list is returned if the given id is already present, + * otherwise the result of a call to {@link concatenate_id} is + * returned. + */ + public MessageIDList merge_id(MessageID other) { + return this.list.contains(other) ? this : this.concatenate_id(other); + } + + /** + * Returns a list with the given ids appended if not already present. + * + * This list is returned if all given ids are already present, + * otherwise the result of a call to {@link concatenate_id} for + * each not present is returned. + */ + public MessageIDList merge_list(MessageIDList other) { + var list = this; + foreach (var id in other) { + if (!this.list.contains(id)) { + list = list.concatenate_id(id); + } + } + return list; + } + /** * Returns a new list with the given list appended to this. */ diff --git a/test/engine/rfc822/rfc822-message-data-test.vala b/test/engine/rfc822/rfc822-message-data-test.vala index 8c1eb157..b3f5fd52 100644 --- a/test/engine/rfc822/rfc822-message-data-test.vala +++ b/test/engine/rfc822/rfc822-message-data-test.vala @@ -16,6 +16,7 @@ class Geary.RFC822.MessageDataTest : TestCase { add_test("header_names_from_rfc822", header_names_from_rfc822); add_test("PreviewText.with_header", preview_text_with_header); add_test("MessageIDList.from_rfc822_string", message_id_list_from_rfc822_string); + add_test("MessageIdList.merge", message_id_list_merge); } public void preview_text_with_header() throws GLib.Error { @@ -213,6 +214,19 @@ class Geary.RFC822.MessageDataTest : TestCase { .contains(new MessageID("id2@example.com")); } + public void message_id_list_merge() throws GLib.Error { + var a1 = new MessageID("a"); + var b = new MessageID("b"); + var a2 = new MessageID("a"); + var list = new MessageIDList.single(a1); + + assert_equal(list.merge_id(b).size, 2); + assert_equal(list.merge_id(a2).size, 1); + + assert_equal(list.merge_list(new MessageIDList.single(b)).size, 2); + assert_equal(list.merge_list(new MessageIDList.single(a2)).size, 1); + } + private const string HEADER_FIXTURE = """From: Test Subject: test