[geary/mjog/composer-context-loading-fixes: 12/19] Geary.RFC822.MessageIDList: Support merging message id lists




commit c73888b2c4877154f0ed212e12369629cd1e1991
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 | 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 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 8c1eb157a..b3f5fd52d 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<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]