From 76141df83128abf6dbdfc6ce76e8d9676c5004fb Mon Sep 17 00:00:00 2001 From: Andrei Zisu Date: Sun, 12 Nov 2023 17:23:21 +0100 Subject: [PATCH] Keep modseq as optional Some features might not support this feature --- .../down.sql | 24 +++++++++++++++++++ .../up.sql | 24 +++++++++++++++++++ app/src/backends/imap/sync.rs | 10 ++++++-- app/src/models/database.rs | 8 +++---- app/src/schema.rs | 2 +- 5 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 app/migrations/2023-11-12-160133_make_modification_sequence_nullable/down.sql create mode 100644 app/migrations/2023-11-12-160133_make_modification_sequence_nullable/up.sql diff --git a/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/down.sql b/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/down.sql new file mode 100644 index 0000000..acf93a6 --- /dev/null +++ b/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/down.sql @@ -0,0 +1,24 @@ +CREATE TABLE messages_new ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + message_id TEXT NOT NULL, + subject TEXT NOT NULL, + folder_id INTEGER NOT NULL, + time_received TIMESTAMP NOT NULL, + "from" TEXT NOT NULL, + "to" TEXT NOT NULL, + cc TEXT NOT NULL, + bcc TEXT NOT NULL, + content TEXT, + "references" TEXT NOT NULL, + in_reply_to TEXT NOT NULL, + uid INTEGER NOT NULL, + modification_sequence INTEGER NOT NULL, + seen BOOLEAN NOT NULL, + flagged BOOLEAN NOT NULL, + draft BOOLEAN NOT NULL, + deleted BOOLEAN NOT NULL +); + +INSERT INTO messages_new SELECT * FROM messages; +DROP TABLE messages; +ALTER TABLE messages_new RENAME TO messages; \ No newline at end of file diff --git a/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/up.sql b/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/up.sql new file mode 100644 index 0000000..71d7faf --- /dev/null +++ b/app/migrations/2023-11-12-160133_make_modification_sequence_nullable/up.sql @@ -0,0 +1,24 @@ +CREATE TABLE messages_new ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + message_id TEXT NOT NULL, + subject TEXT NOT NULL, + folder_id INTEGER NOT NULL, + time_received TIMESTAMP NOT NULL, + "from" TEXT NOT NULL, + "to" TEXT NOT NULL, + cc TEXT NOT NULL, + bcc TEXT NOT NULL, + content TEXT, + "references" TEXT NOT NULL, + in_reply_to TEXT NOT NULL, + uid INTEGER NOT NULL, + modification_sequence INTEGER NULL, + seen BOOLEAN NOT NULL, + flagged BOOLEAN NOT NULL, + draft BOOLEAN NOT NULL, + deleted BOOLEAN NOT NULL +); + +INSERT INTO messages_new SELECT * FROM messages; +DROP TABLE messages; +ALTER TABLE messages_new RENAME TO messages; \ No newline at end of file diff --git a/app/src/backends/imap/sync.rs b/app/src/backends/imap/sync.rs index 9bb82bb..368d73a 100644 --- a/app/src/backends/imap/sync.rs +++ b/app/src/backends/imap/sync.rs @@ -8,6 +8,7 @@ use melib::MeliError; use log::debug; +use std::borrow::Cow; use std::convert::TryInto; use std::time::Instant; @@ -174,8 +175,13 @@ async fn fetch_messages_overview_in_uid_range( // message_sequence_number: 0, // references: None, - //@TODO conversion from i64 to u64 - new_message.modification_sequence = message.modseq.unwrap().0.get().try_into().unwrap(); + new_message.modification_sequence = message + .modseq + // The DB uses i64 but the backend gives us NonZeroU64 so we have to do this conversion + .map(|modseq| TryInto::::try_into(u64::from(modseq.0))) + .transpose() + // Abusing the types a little bit to coerce into a valid MeliError + .or_else(|_| Err(Cow::from("Unable to convert modification sequence type")))?; messages_list.push(new_message); } diff --git a/app/src/models/database.rs b/app/src/models/database.rs index 15833ef..dd818f6 100644 --- a/app/src/models/database.rs +++ b/app/src/models/database.rs @@ -38,7 +38,7 @@ pub struct Message { pub references: String, pub in_reply_to: String, pub uid: i64, - pub modification_sequence: i64, + pub modification_sequence: Option, pub seen: bool, pub flagged: bool, pub draft: bool, @@ -153,7 +153,7 @@ pub struct NewMessage { pub references: String, pub in_reply_to: String, pub uid: i64, - pub modification_sequence: i64, + pub modification_sequence: Option, pub seen: bool, pub flagged: bool, pub draft: bool, @@ -179,8 +179,8 @@ impl From for NewMessage { in_reply_to: envelope .in_reply_to() .map_or("".to_string(), |x| String::from_utf8(x.0.clone()).unwrap()), - uid: 0, //@TODO - modification_sequence: 0, //@TODO + uid: 0, //@TODO + modification_sequence: Some(0), seen: flags.contains(melib::email::Flag::SEEN), flagged: flags.contains(melib::email::Flag::FLAGGED), draft: flags.contains(melib::email::Flag::DRAFT), diff --git a/app/src/schema.rs b/app/src/schema.rs index dea0eb1..cd1dfbb 100644 --- a/app/src/schema.rs +++ b/app/src/schema.rs @@ -57,7 +57,7 @@ table! { references -> Text, in_reply_to -> Text, uid -> BigInt, - modification_sequence -> BigInt, + modification_sequence -> Nullable, seen -> Bool, flagged -> Bool, draft -> Bool,