diff options
| author | Marvin W <git@larma.de> | 2025-10-14 15:13:36 +0200 |
|---|---|---|
| committer | Marvin W <git@larma.de> | 2025-10-14 15:14:29 +0200 |
| commit | ed62a6689394f01b635236d548a379ad80321b93 (patch) | |
| tree | c1f1faba8f00979d92d14b70433c780844fb6fa1 | |
| parent | d0e8582565d347183e38bfeccfafeeba3f2d82cc (diff) | |
History: Fix sync issues when resuming from partial sync
| -rw-r--r-- | libdino/src/service/history_sync.vala | 48 | ||||
| -rw-r--r-- | xmpp-vala/src/module/xep/0313_2_message_archive_management.vala | 2 |
2 files changed, 35 insertions, 15 deletions
diff --git a/libdino/src/service/history_sync.vala b/libdino/src/service/history_sync.vala index 8ab6d7bb..514788a4 100644 --- a/libdino/src/service/history_sync.vala +++ b/libdino/src/service/history_sync.vala @@ -121,7 +121,7 @@ public class Dino.HistorySync { } public async void fetch_everything(Account account, Jid mam_server, Cancellable? cancellable = null, DateTime until_earliest_time = new DateTime.from_unix_utc(0)) { - debug("Fetch everything for %s %s", mam_server.to_string(), until_earliest_time != null ? @"(until $until_earliest_time)" : ""); + debug("[%s | %s] Fetch everything %s", account.bare_jid.to_string(), mam_server.to_string(), until_earliest_time != null ? @"(until $until_earliest_time)" : ""); RowOption latest_row_opt = db.mam_catchup.select() .with(db.mam_catchup.account_id, "=", account.id) .with(db.mam_catchup.server_jid, "=", mam_server.to_string()) @@ -158,9 +158,12 @@ public class Dino.HistorySync { // Fetch messages between two db ranges and merge them while (current_row != null && previous_row != null) { - if (current_row[db.mam_catchup.from_end]) return; + if (current_row[db.mam_catchup.from_end]) { + debug("[%s | %s] No logs on server before %s, aborting sync.", account.bare_jid.to_string(), mam_server.to_string(), current_row[db.mam_catchup.from_time].to_string()); + return; + } - debug("[%s] Fetching between ranges %s - %s", mam_server.to_string(), previous_row[db.mam_catchup.to_time].to_string(), current_row[db.mam_catchup.from_time].to_string()); + debug("[%s | %s] Fetching between ranges %s - %s", account.bare_jid.to_string(), mam_server.to_string(), previous_row[db.mam_catchup.to_time].to_string(), current_row[db.mam_catchup.from_time].to_string()); current_row = yield fetch_between_ranges(account, mam_server, previous_row, current_row, cancellable); if (current_row == null) return; @@ -175,11 +178,19 @@ public class Dino.HistorySync { } // We're at the earliest range. Try to expand it even further back. - if (current_row == null || current_row[db.mam_catchup.from_end]) return; + if (current_row == null) { + debug("[%s | %s] No current range, aborting sync.", account.bare_jid.to_string(), mam_server.to_string()); + return; + } else if (current_row[db.mam_catchup.from_end]) { + debug("[%s | %s] No logs on server before %s, aborting sync.", account.bare_jid.to_string(), mam_server.to_string(), current_row[db.mam_catchup.from_time].to_string()); + return; + } // We don't want to fetch before the earliest range over and over again in MUCs if it's after until_earliest_time. // For now, don't query if we are within a week of until_earliest_time - if (until_earliest_time != null && - current_row[db.mam_catchup.from_time] > until_earliest_time.add(-TimeSpan.DAY * 7).to_unix()) return; + if (until_earliest_time != null && current_row[db.mam_catchup.from_time] <= until_earliest_time.to_unix()) { + debug("[%s | %s] Current range starting %s is before limit %s, aborting sync.", account.bare_jid.to_string(), mam_server.to_string(), current_row[db.mam_catchup.from_time].to_string(), until_earliest_time.to_unix().to_string()); + return; + } yield fetch_before_range(account, mam_server, current_row, until_earliest_time); } @@ -328,20 +339,29 @@ public class Dino.HistorySync { page_result = yield get_mam_page(account, query_params, page_result, cancellable); debug("[%s | %s] Page result %s (got stanzas: %s)", account.bare_jid.to_string(), query_params.mam_server.to_string(), page_result.page_result.to_string(), (page_result.stanzas != null).to_string()); - if (page_result.page_result == PageResult.Error || page_result.page_result == PageResult.Cancelled || page_result.query_result.first == null) return page_result; + if (page_result.page_result == PageResult.Error || page_result.page_result == PageResult.Cancelled) return page_result; string earliest_mam_id = page_result.query_result.first; - long earliest_mam_time = (long)mam_times[account][earliest_mam_id].to_unix(); + long earliest_mam_time = earliest_mam_id != null ? (long)mam_times[account][earliest_mam_id].to_unix() : 0; - debug("Updating %s to %s, %s", query_params.mam_server.to_string(), earliest_mam_time.to_string(), earliest_mam_id); var query = db.mam_catchup.update() - .with(db.mam_catchup.id, "=", db_id) - .set(db.mam_catchup.from_time, earliest_mam_time) - .set(db.mam_catchup.from_id, earliest_mam_id); + .with(db.mam_catchup.id, "=", db_id); + if (earliest_mam_id != null) { + debug("[%s | %s] Updating to %s, %s", account.bare_jid.to_string(), query_params.mam_server.to_string(), earliest_mam_time.to_string(), earliest_mam_id); + query.set(db.mam_catchup.from_id, earliest_mam_id); + if (page_result.page_result != PageResult.NoMoreMessages || query_params.start != null || earliest_mam_time < query_params.start.to_unix()) { + query.set(db.mam_catchup.from_time, earliest_mam_time); + } + } if (page_result.page_result == PageResult.NoMoreMessages) { - // If the server doesn't have more messages, store that this range is at its end. - query.set(db.mam_catchup.from_end, true); + // If the server doesn't have more messages, store that this range is at its end or update the from timestamp + if (query_params.start != null) { + debug("[%s | %s] Updating to %s based on query", account.bare_jid.to_string(), query_params.mam_server.to_string(), query_params.start.to_string()); + query.set(db.mam_catchup.from_time, (long) query_params.start.to_unix()); + } else { + query.set(db.mam_catchup.from_end, true); + } } query.perform(); } while (page_result.page_result == PageResult.MorePagesAvailable); diff --git a/xmpp-vala/src/module/xep/0313_2_message_archive_management.vala b/xmpp-vala/src/module/xep/0313_2_message_archive_management.vala index dde7d9b0..b25f6106 100644 --- a/xmpp-vala/src/module/xep/0313_2_message_archive_management.vala +++ b/xmpp-vala/src/module/xep/0313_2_message_archive_management.vala @@ -52,7 +52,7 @@ namespace Xmpp.MessageArchiveManagement.V2 { field.set_value_string(DateTimeProfiles.to_datetime(mam_params.start)); fields.add(field); } - if (mam_params.end != null) { + if (mam_params.end != null && mam_params.end_id == null && !mam_params.use_ns2_extended) { DataForms.DataForm.Field field = new DataForms.DataForm.Field() { var="end" }; field.set_value_string(DateTimeProfiles.to_datetime(mam_params.end)); fields.add(field); |
