[geary/mjog/composer-context-loading-fixes: 3/9] Geary.RFC822.MessageIDList: Support merging message id lists
- From: Michael Gratton <mjog src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geary/mjog/composer-context-loading-fixes: 3/9] Geary.RFC822.MessageIDList: Support merging message id lists
- Date: Thu, 13 Aug 2020 01:46:35 +0000 (UTC)
commit ed216697e595a13998d903fa00c1e52aee2e4e18
Author: Michael Gratton <mike vee net>
Date: Thu Aug 13 10:32:26 2020 +1000
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 ++++++++++++++++++++++++
test/engine/rfc822/rfc822-message-data-test.vala | 13 +++++++++++
2 files changed, 41 insertions(+)
---
diff --git a/src/engine/rfc822/rfc822-message-data.vala b/src/engine/rfc822/rfc822-message-data.vala
index 09aa1d4ed..43cc0223a 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 acc08cb1b..35c80e9fe 100644
--- a/test/engine/rfc822/rfc822-message-data-test.vala
+++ b/test/engine/rfc822/rfc822-message-data-test.vala
@@ -15,6 +15,7 @@ class Geary.RFC822.MessageDataTest : TestCase {
add_test("header_from_rfc822", header_from_rfc822);
add_test("header_names_from_rfc822", header_names_from_rfc822);
add_test("PreviewText.with_header", preview_text_with_header);
+ add_test("MessageIdList.merge", message_id_list_merge);
}
public void preview_text_with_header() throws GLib.Error {
@@ -108,6 +109,18 @@ class Geary.RFC822.MessageDataTest : TestCase {
assert_equal(neg_half_hour_tz.to_rfc822_string(), NEG_HALF_HOUR_TZ);
}
+ 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<int?>(list.merge_id(b).size, 2);
+ assert_equal<int?>(list.merge_id(a2).size, 1);
+
+ assert_equal<int?>(list.merge_list(new MessageIDList.single(b)).size, 2);
+ assert_equal<int?>(list.merge_list(new MessageIDList.single(a2)).size, 1);
+ }
private const string HEADER_FIXTURE = """From: Test <test example com>
Subject: test
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]