mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
remove unneeded sql-roundtrip on getting archived state
This commit is contained in:
@@ -1,13 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## Unreleased
|
|
||||||
|
|
||||||
- pinned chats #1248
|
|
||||||
|
|
||||||
#### Api Changes
|
|
||||||
|
|
||||||
- ChatInfo.archived is now a tri-state
|
|
||||||
|
|
||||||
## 1.0.0-beta.24
|
## 1.0.0-beta.24
|
||||||
|
|
||||||
- fix oauth2/gmail bug introduced in beta23 (not used in releases) #1219
|
- fix oauth2/gmail bug introduced in beta23 (not used in releases) #1219
|
||||||
|
|||||||
95
src/chat.rs
95
src/chat.rs
@@ -177,20 +177,16 @@ impl ChatId {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_archive_state(self, context: &Context) -> ArchiveState {
|
// note that unarchive() is not the same as set_archived(false) -
|
||||||
if self.is_special() {
|
// eg. unarchive() does not send events as done for set_archived(false).
|
||||||
return ArchiveState::Normal;
|
pub fn unarchive(self, context: &Context) -> Result<(), Error> {
|
||||||
}
|
sql::execute(
|
||||||
if let Some(archive_state) = context.sql.query_get_value::<_, ArchiveState>(
|
|
||||||
context,
|
context,
|
||||||
"SELECT archived FROM chats WHERE id=?;",
|
&context.sql,
|
||||||
|
"UPDATE chats SET archived=0 WHERE id=? and archived=1",
|
||||||
params![self],
|
params![self],
|
||||||
) {
|
)?;
|
||||||
archive_state
|
Ok(())
|
||||||
} else {
|
|
||||||
// if it failed return normal to be safe
|
|
||||||
ArchiveState::Normal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes a chat.
|
/// Deletes a chat.
|
||||||
@@ -434,6 +430,7 @@ pub struct Chat {
|
|||||||
pub id: ChatId,
|
pub id: ChatId,
|
||||||
pub typ: Chattype,
|
pub typ: Chattype,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub visibility: ArchiveState,
|
||||||
pub grpid: String,
|
pub grpid: String,
|
||||||
blocked: Blocked,
|
blocked: Blocked,
|
||||||
pub param: Params,
|
pub param: Params,
|
||||||
@@ -457,6 +454,7 @@ impl Chat {
|
|||||||
name: row.get::<_, String>(1)?,
|
name: row.get::<_, String>(1)?,
|
||||||
grpid: row.get::<_, String>(2)?,
|
grpid: row.get::<_, String>(2)?,
|
||||||
param: row.get::<_, String>(3)?.parse().unwrap_or_default(),
|
param: row.get::<_, String>(3)?.parse().unwrap_or_default(),
|
||||||
|
visibility: row.get(4)?,
|
||||||
blocked: row.get::<_, Option<_>>(5)?.unwrap_or_default(),
|
blocked: row.get::<_, Option<_>>(5)?.unwrap_or_default(),
|
||||||
is_sending_locations: row.get(6)?,
|
is_sending_locations: row.get(6)?,
|
||||||
mute_duration: row.get(7)?,
|
mute_duration: row.get(7)?,
|
||||||
@@ -663,12 +661,11 @@ impl Chat {
|
|||||||
Some(message) => message.text.unwrap_or_else(String::new),
|
Some(message) => message.text.unwrap_or_else(String::new),
|
||||||
_ => String::new(),
|
_ => String::new(),
|
||||||
};
|
};
|
||||||
let archive_state = self.id.get_archive_state(context);
|
|
||||||
Ok(ChatInfo {
|
Ok(ChatInfo {
|
||||||
id: self.id,
|
id: self.id,
|
||||||
type_: self.typ as u32,
|
type_: self.typ as u32,
|
||||||
name: self.name.clone(),
|
name: self.name.clone(),
|
||||||
archived: archive_state,
|
archived: self.visibility == ArchiveState::Archived,
|
||||||
param: self.param.to_string(),
|
param: self.param.to_string(),
|
||||||
gossiped_timestamp: self.get_gossiped_timestamp(context),
|
gossiped_timestamp: self.get_gossiped_timestamp(context),
|
||||||
is_sending_locations: self.is_sending_locations,
|
is_sending_locations: self.is_sending_locations,
|
||||||
@@ -680,6 +677,10 @@ impl Chat {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_visibility(&self) -> ArchiveState {
|
||||||
|
self.visibility
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_unpromoted(&self) -> bool {
|
pub fn is_unpromoted(&self) -> bool {
|
||||||
self.param.get_int(Param::Unpromoted).unwrap_or_default() == 1
|
self.param.get_int(Param::Unpromoted).unwrap_or_default() == 1
|
||||||
}
|
}
|
||||||
@@ -935,7 +936,7 @@ impl Chat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)]
|
||||||
pub enum ArchiveState {
|
pub enum ArchiveState {
|
||||||
/// Neither archived or pinned
|
/// Neither archived or pinned
|
||||||
Normal = 0,
|
Normal = 0,
|
||||||
@@ -993,7 +994,7 @@ pub struct ChatInfo {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// Whether the chat is archived.
|
/// Whether the chat is archived.
|
||||||
pub archived: ArchiveState,
|
pub archived: bool,
|
||||||
|
|
||||||
/// The "params" of the chat.
|
/// The "params" of the chat.
|
||||||
///
|
///
|
||||||
@@ -2531,7 +2532,7 @@ mod tests {
|
|||||||
"id": 10,
|
"id": 10,
|
||||||
"type": 100,
|
"type": 100,
|
||||||
"name": "bob",
|
"name": "bob",
|
||||||
"archived": 0,
|
"archived": false,
|
||||||
"param": "",
|
"param": "",
|
||||||
"gossiped_timestamp": 0,
|
"gossiped_timestamp": 0,
|
||||||
"is_sending_locations": false,
|
"is_sending_locations": false,
|
||||||
@@ -2605,7 +2606,7 @@ mod tests {
|
|||||||
let chat = Chat::load_from_db(&t.ctx, chat_id).unwrap();
|
let chat = Chat::load_from_db(&t.ctx, chat_id).unwrap();
|
||||||
assert_eq!(chat.id, chat_id);
|
assert_eq!(chat.id, chat_id);
|
||||||
assert!(chat.is_self_talk());
|
assert!(chat.is_self_talk());
|
||||||
assert!(chat_id.get_archive_state(&t.ctx) == ArchiveState::Normal);
|
assert!(chat.visibility == ArchiveState::Normal);
|
||||||
assert!(!chat.is_device_talk());
|
assert!(!chat.is_device_talk());
|
||||||
assert!(chat.can_send());
|
assert!(chat.can_send());
|
||||||
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::SavedMessages));
|
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::SavedMessages));
|
||||||
@@ -2619,7 +2620,7 @@ mod tests {
|
|||||||
assert_eq!(DC_CHAT_ID_DEADDROP, 1);
|
assert_eq!(DC_CHAT_ID_DEADDROP, 1);
|
||||||
assert!(chat.id.is_deaddrop());
|
assert!(chat.id.is_deaddrop());
|
||||||
assert!(!chat.is_self_talk());
|
assert!(!chat.is_self_talk());
|
||||||
assert!(chat.get_id().get_archive_state(&t.ctx) == ArchiveState::Normal);
|
assert!(chat.visibility == ArchiveState::Normal);
|
||||||
assert!(!chat.is_device_talk());
|
assert!(!chat.is_device_talk());
|
||||||
assert!(!chat.can_send());
|
assert!(!chat.can_send());
|
||||||
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::DeadDrop));
|
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::DeadDrop));
|
||||||
@@ -2828,8 +2829,18 @@ mod tests {
|
|||||||
assert!(chat_id1
|
assert!(chat_id1
|
||||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(chat_id1.get_archive_state(&t.ctx) == ArchiveState::Archived);
|
assert!(
|
||||||
assert!(chat_id2.get_archive_state(&t.ctx) == ArchiveState::Normal);
|
Chat::load_from_db(&t.ctx, chat_id1)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Archived
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
Chat::load_from_db(&t.ctx, chat_id2)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Normal
|
||||||
|
);
|
||||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||||
assert_eq!(chatlist_len(&t.ctx, 0), 2); // including DC_CHAT_ID_ARCHIVED_LINK now
|
assert_eq!(chatlist_len(&t.ctx, 0), 2); // including DC_CHAT_ID_ARCHIVED_LINK now
|
||||||
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 1);
|
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 1);
|
||||||
@@ -2839,8 +2850,18 @@ mod tests {
|
|||||||
assert!(chat_id2
|
assert!(chat_id2
|
||||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(chat_id1.get_archive_state(&t.ctx) == ArchiveState::Archived);
|
assert!(
|
||||||
assert!(chat_id2.get_archive_state(&t.ctx) == ArchiveState::Archived);
|
Chat::load_from_db(&t.ctx, chat_id1)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Archived
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
Chat::load_from_db(&t.ctx, chat_id2)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Archived
|
||||||
|
);
|
||||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||||
assert_eq!(chatlist_len(&t.ctx, 0), 1); // only DC_CHAT_ID_ARCHIVED_LINK now
|
assert_eq!(chatlist_len(&t.ctx, 0), 1); // only DC_CHAT_ID_ARCHIVED_LINK now
|
||||||
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 0);
|
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 0);
|
||||||
@@ -2856,8 +2877,18 @@ mod tests {
|
|||||||
assert!(chat_id2
|
assert!(chat_id2
|
||||||
.set_archive_state(&t.ctx, ArchiveState::Normal)
|
.set_archive_state(&t.ctx, ArchiveState::Normal)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(chat_id1.get_archive_state(&t.ctx) == ArchiveState::Archived);
|
assert!(
|
||||||
assert!(chat_id2.get_archive_state(&t.ctx) == ArchiveState::Normal);
|
Chat::load_from_db(&t.ctx, chat_id1)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Archived
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
Chat::load_from_db(&t.ctx, chat_id2)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility()
|
||||||
|
== ArchiveState::Normal
|
||||||
|
);
|
||||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||||
assert_eq!(chatlist_len(&t.ctx, 0), 2);
|
assert_eq!(chatlist_len(&t.ctx, 0), 2);
|
||||||
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 1);
|
assert_eq!(chatlist_len(&t.ctx, DC_GCL_NO_SPECIALS), 1);
|
||||||
@@ -2895,7 +2926,12 @@ mod tests {
|
|||||||
.is_ok()
|
.is_ok()
|
||||||
== true
|
== true
|
||||||
);
|
);
|
||||||
assert_eq!(chat_id1.get_archive_state(&t.ctx), ArchiveState::Pinned);
|
assert_eq!(
|
||||||
|
Chat::load_from_db(&t.ctx, chat_id1)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility(),
|
||||||
|
ArchiveState::Pinned
|
||||||
|
);
|
||||||
|
|
||||||
// check if chat order changed
|
// check if chat order changed
|
||||||
let chatlist = get_chats_from_chat_list(&t.ctx, DC_GCL_NO_SPECIALS);
|
let chatlist = get_chats_from_chat_list(&t.ctx, DC_GCL_NO_SPECIALS);
|
||||||
@@ -2908,7 +2944,12 @@ mod tests {
|
|||||||
.is_ok()
|
.is_ok()
|
||||||
== true
|
== true
|
||||||
);
|
);
|
||||||
assert_eq!(chat_id1.get_archive_state(&t.ctx), ArchiveState::Normal);
|
assert_eq!(
|
||||||
|
Chat::load_from_db(&t.ctx, chat_id1)
|
||||||
|
.unwrap()
|
||||||
|
.get_visibility(),
|
||||||
|
ArchiveState::Normal
|
||||||
|
);
|
||||||
|
|
||||||
// check if chat order changed back
|
// check if chat order changed back
|
||||||
let chatlist = get_chats_from_chat_list(&t.ctx, DC_GCL_NO_SPECIALS);
|
let chatlist = get_chats_from_chat_list(&t.ctx, DC_GCL_NO_SPECIALS);
|
||||||
|
|||||||
@@ -548,9 +548,7 @@ fn add_parts(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// unarchive chat
|
// unarchive chat
|
||||||
if chat_id.get_archive_state(context) == ArchiveState::Archived {
|
chat_id.unarchive(context)?;
|
||||||
chat_id.set_archive_state(context, ArchiveState::Normal)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the mime-headers should be saved, find out its size
|
// if the mime-headers should be saved, find out its size
|
||||||
// (the mime-header ends with an empty line)
|
// (the mime-header ends with an empty line)
|
||||||
|
|||||||
Reference in New Issue
Block a user