From 6dc8b483b5045a94e72e631a8faee388713c3c05 Mon Sep 17 00:00:00 2001
From: Bron Gondwana <brong@fastmail.fm>
Date: Wed, 21 Sep 2022 16:08:07 +1000
Subject: [PATCH] index: track changes for modseq bump when setting seen on
 shared folders

---
 imap/index.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/imap/index.c b/imap/index.c
index af06e94e6..5c96216ad 100644
--- a/imap/index.c
+++ b/imap/index.c
@@ -4778,6 +4778,7 @@ static int index_storeflag(struct index_state *state,
     int dirty = 0;
     modseq_t oldmodseq;
     struct index_map *im = &state->map[msgno-1];
+    int seen_dirty = 0;
     int r;
 
     memset(modified_flags, 0, sizeof(struct index_modified_flags));
@@ -4803,6 +4804,7 @@ static int index_storeflag(struct index_state *state,
             im->isseen = new;
             state->seen_dirty = 1;
             dirty++;
+            seen_dirty = 1;
         }
     }
 
@@ -4925,6 +4927,7 @@ static int index_storeflag(struct index_state *state,
         else
             system_flags &= ~FLAG_SEEN;
     }
+
     /* add back the internal tracking flags */
     system_flags |= keep;
 
@@ -4942,6 +4945,18 @@ static int index_storeflag(struct index_state *state,
     r = msgrecord_set_userflags(msgrec, user_flags);
     if (r) return r;
 
+    // patch back in seen state for non-internal-seen
+    if (seen_dirty && !state->internalseen) {
+        if (im->isseen) {
+            modified_flags->added_system_flags |= FLAG_SEEN;
+            modified_flags->added_flags++;
+        }
+        else {
+            modified_flags->removed_system_flags |= FLAG_SEEN;
+            modified_flags->removed_flags++;
+        }
+    }
+
     /* if it's silent and unchanged, update the seen value, but
      * not if qresync is enabled - RFC 4551 says that the MODSEQ
      * must always been told, and we prefer just to tell flags
-- 
2.43.0

