rename to ChatVisibility, simplify ffi

This commit is contained in:
B. Petersen
2020-02-13 19:14:44 +01:00
parent 2813e01e61
commit 0303ea7f57
6 changed files with 63 additions and 68 deletions

View File

@@ -2693,7 +2693,6 @@ dc_context_t* dc_chatlist_get_context (dc_chatlist_t* chatlist);
* *
* id: chat id * id: chat id
* name: chat/group name * name: chat/group name
* visibility: one of @ref DC_CHAT_VISIBILITY
* color: color of this chat * color: color of this chat
* last-message-from: who sent the last message * last-message-from: who sent the last message
* last-message-text: message (truncated) * last-message-text: message (truncated)

View File

@@ -25,7 +25,7 @@ use std::time::{Duration, SystemTime};
use libc::uintptr_t; use libc::uintptr_t;
use num_traits::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive};
use deltachat::chat::{ArchiveState, ChatId, MuteDuration}; use deltachat::chat::{ChatId, ChatVisibility, MuteDuration};
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL; use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
use deltachat::contact::Contact; use deltachat::contact::Contact;
use deltachat::context::Context; use deltachat::context::Context;
@@ -1198,21 +1198,22 @@ pub unsafe extern "C" fn dc_set_chat_visibility(
return; return;
} }
let ffi_context = &*context; let ffi_context = &*context;
let archive_state = match archive { let visibility = match archive {
0 => ArchiveState::Normal, 0 => ChatVisibility::Normal,
1 => ArchiveState::Archived, 1 => ChatVisibility::Archived,
2 => ArchiveState::Pinned, 2 => ChatVisibility::Pinned,
_ => { _ => {
ffi_context ffi_context.warning(
.warning("ignoring careless call to dc_set_chat_visibility(): unknown archived state"); "ignoring careless call to dc_set_chat_visibility(): unknown archived state",
);
return; return;
} }
}; };
ffi_context ffi_context
.with_inner(|ctx| { .with_inner(|ctx| {
ChatId::new(chat_id) ChatId::new(chat_id)
.set_archive_state(ctx, archive_state) .set_visibility(ctx, visibility)
.log_err(ffi_context, "Failed archive chat") .log_err(ffi_context, "Failed setting chat visibility")
.unwrap_or(()) .unwrap_or(())
}) })
.unwrap_or(()) .unwrap_or(())
@@ -2462,14 +2463,11 @@ pub unsafe extern "C" fn dc_chat_get_visibility(chat: *mut dc_chat_t) -> libc::c
return 0; return 0;
} }
let ffi_chat = &*chat; let ffi_chat = &*chat;
let ffi_context = &*ffi_chat.context; match ffi_chat.chat.visibility {
ffi_context ChatVisibility::Normal => 0,
.with_inner(|ctx| match ffi_chat.chat.get_id().get_archive_state(ctx) { ChatVisibility::Archived => 1,
ArchiveState::Normal => 0, ChatVisibility::Pinned => 2,
ArchiveState::Archived => 1, }
ArchiveState::Pinned => 2,
} as libc::c_int)
.unwrap_or_else(|_| 0)
} }
#[no_mangle] #[no_mangle]

View File

@@ -1,7 +1,7 @@
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use deltachat::chat::{self, ArchiveState, Chat, ChatId}; use deltachat::chat::{self, Chat, ChatId, ChatVisibility};
use deltachat::chatlist::*; use deltachat::chatlist::*;
use deltachat::constants::*; use deltachat::constants::*;
use deltachat::contact::*; use deltachat::contact::*;
@@ -518,15 +518,15 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> {
chat.get_id(), chat.get_id(),
chat.get_name(), chat.get_name(),
chat.get_id().get_fresh_msg_cnt(context), chat.get_id().get_fresh_msg_cnt(context),
match chat.get_id().get_archive_state(context) { match chat.visibility {
ArchiveState::Normal => "", ChatVisibility::Normal => "",
ArchiveState::Archived => "📦", ChatVisibility::Archived => "📦",
ArchiveState::Pinned => "📌", ChatVisibility::Pinned => "📌",
}, },
); );
let lot = chatlist.get_summary(context, i, Some(&chat)); let lot = chatlist.get_summary(context, i, Some(&chat));
let statestr = let statestr =
if chat.get_id().get_archive_state(context) == ArchiveState::Archived { if chat.get_id().get_visibility(context) == ChatVisibility::Archived {
" [Archived]" " [Archived]"
} else { } else {
match lot.get_state() { match lot.get_state() {
@@ -853,12 +853,12 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> {
"archive" | "unarchive" | "pin" | "unpin" => { "archive" | "unarchive" | "pin" | "unpin" => {
ensure!(!arg1.is_empty(), "Argument <chat-id> missing."); ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
let chat_id = ChatId::new(arg1.parse()?); let chat_id = ChatId::new(arg1.parse()?);
chat_id.set_archive_state( chat_id.set_visibility(
context, context,
match arg0 { match arg0 {
"archive" => ArchiveState::Archived, "archive" => ChatVisibility::Archived,
"unarchive" | "unpin" => ArchiveState::Normal, "unarchive" | "unpin" => ChatVisibility::Normal,
"pin" => ArchiveState::Pinned, "pin" => ChatVisibility::Pinned,
_ => panic!("Unexpected command (This should never happen)"), _ => panic!("Unexpected command (This should never happen)"),
}, },
)?; )?;

View File

@@ -137,10 +137,10 @@ impl ChatId {
} }
/// Archives or unarchives a chat. /// Archives or unarchives a chat.
pub fn set_archive_state( pub fn set_visibility(
self, self,
context: &Context, context: &Context,
new_archive_state: ArchiveState, visibility: ChatVisibility,
) -> Result<(), Error> { ) -> Result<(), Error> {
ensure!( ensure!(
!self.is_special(), !self.is_special(),
@@ -148,7 +148,7 @@ impl ChatId {
self self
); );
let send_event = if new_archive_state == ArchiveState::Archived { let send_event = if visibility == ChatVisibility::Archived {
sql::execute( sql::execute(
context, context,
&context.sql, &context.sql,
@@ -164,7 +164,7 @@ impl ChatId {
context, context,
&context.sql, &context.sql,
"UPDATE chats SET archived=? WHERE id=?;", "UPDATE chats SET archived=? WHERE id=?;",
params![new_archive_state, self], params![visibility, self],
)?; )?;
if send_event { if send_event {
@@ -430,7 +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 visibility: ChatVisibility,
pub grpid: String, pub grpid: String,
blocked: Blocked, blocked: Blocked,
pub param: Params, pub param: Params,
@@ -665,7 +665,7 @@ impl Chat {
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: self.visibility == ArchiveState::Archived, archived: self.visibility == ChatVisibility::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,
@@ -677,7 +677,7 @@ impl Chat {
}) })
} }
pub fn get_visibility(&self) -> ArchiveState { pub fn get_visibility(&self) -> ChatVisibility {
self.visibility self.visibility
} }
@@ -937,20 +937,18 @@ impl Chat {
} }
#[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum ArchiveState { pub enum ChatVisibility {
/// Neither archived or pinned
Normal = 0, Normal = 0,
Archived = 1, Archived = 1,
/// Pinned (formaly known as sticky)
Pinned = 2, Pinned = 2,
} }
impl rusqlite::types::ToSql for ArchiveState { impl rusqlite::types::ToSql for ChatVisibility {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput> { fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput> {
let duration = match &self { let duration = match &self {
ArchiveState::Normal => 0, ChatVisibility::Normal => 0,
ArchiveState::Archived => 1, ChatVisibility::Archived => 1,
ArchiveState::Pinned => 2, ChatVisibility::Pinned => 2,
}; };
let val = rusqlite::types::Value::Integer(duration as i64); let val = rusqlite::types::Value::Integer(duration as i64);
let out = rusqlite::types::ToSqlOutput::Owned(val); let out = rusqlite::types::ToSqlOutput::Owned(val);
@@ -958,13 +956,13 @@ impl rusqlite::types::ToSql for ArchiveState {
} }
} }
impl rusqlite::types::FromSql for ArchiveState { impl rusqlite::types::FromSql for ChatVisibility {
fn column_result(value: rusqlite::types::ValueRef) -> rusqlite::types::FromSqlResult<Self> { fn column_result(value: rusqlite::types::ValueRef) -> rusqlite::types::FromSqlResult<Self> {
i64::column_result(value).and_then(|val| { i64::column_result(value).and_then(|val| {
match val { match val {
2 => Ok(ArchiveState::Pinned), 2 => Ok(ChatVisibility::Pinned),
1 => Ok(ArchiveState::Archived), 1 => Ok(ChatVisibility::Archived),
0 => Ok(ArchiveState::Normal), 0 => Ok(ChatVisibility::Normal),
n => { n => {
// unknown archived state, falling back to normal state (was this db opened with a newer deltachat version?) // unknown archived state, falling back to normal state (was this db opened with a newer deltachat version?)
Err(rusqlite::types::FromSqlError::OutOfRange(n)) Err(rusqlite::types::FromSqlError::OutOfRange(n))
@@ -1347,7 +1345,7 @@ fn prepare_msg_common(
) -> Result<MsgId, Error> { ) -> Result<MsgId, Error> {
msg.id = MsgId::new_unset(); msg.id = MsgId::new_unset();
prepare_msg_blob(context, msg)?; prepare_msg_blob(context, msg)?;
chat_id.set_archive_state(context, ArchiveState::Normal)?; chat_id.set_visibility(context, ChatVisibility::Normal)?;
let mut chat = Chat::load_from_db(context, chat_id)?; let mut chat = Chat::load_from_db(context, chat_id)?;
ensure!(chat.can_send(), "cannot send to {}", chat_id); ensure!(chat.can_send(), "cannot send to {}", chat_id);
@@ -2272,7 +2270,7 @@ pub fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId) -> Re
let mut created_msgs: Vec<MsgId> = Vec::new(); let mut created_msgs: Vec<MsgId> = Vec::new();
let mut curr_timestamp: i64; let mut curr_timestamp: i64;
chat_id.set_archive_state(context, ArchiveState::Normal)?; chat_id.set_visibility(context, ChatVisibility::Normal)?;
if let Ok(mut chat) = Chat::load_from_db(context, chat_id) { if let Ok(mut chat) = Chat::load_from_db(context, chat_id) {
ensure!(chat.can_send(), "cannot send to {}", chat_id); ensure!(chat.can_send(), "cannot send to {}", chat_id);
curr_timestamp = dc_create_smeared_timestamps(context, msg_ids.len()); curr_timestamp = dc_create_smeared_timestamps(context, msg_ids.len());
@@ -2415,7 +2413,7 @@ pub fn add_device_msg(
let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device"); let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device");
msg.try_calc_and_set_dimensions(context).ok(); msg.try_calc_and_set_dimensions(context).ok();
prepare_msg_blob(context, msg)?; prepare_msg_blob(context, msg)?;
chat_id.set_archive_state(context, ArchiveState::Normal)?; chat_id.set_visibility(context, ChatVisibility::Normal)?;
context.sql.execute( context.sql.execute(
"INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,param,rfc724_mid) \ "INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,param,rfc724_mid) \
@@ -2606,7 +2604,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.visibility == ArchiveState::Normal); assert!(chat.visibility == ChatVisibility::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));
@@ -2620,7 +2618,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.visibility == ArchiveState::Normal); assert!(chat.visibility == ChatVisibility::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));
@@ -2827,19 +2825,19 @@ mod tests {
// archive first chat // archive first chat
assert!(chat_id1 assert!(chat_id1
.set_archive_state(&t.ctx, ArchiveState::Archived) .set_visibility(&t.ctx, ChatVisibility::Archived)
.is_ok()); .is_ok());
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id1) Chat::load_from_db(&t.ctx, chat_id1)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Archived == ChatVisibility::Archived
); );
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id2) Chat::load_from_db(&t.ctx, chat_id2)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Normal == ChatVisibility::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
@@ -2848,19 +2846,19 @@ mod tests {
// archive second chat // archive second chat
assert!(chat_id2 assert!(chat_id2
.set_archive_state(&t.ctx, ArchiveState::Archived) .set_visibility(&t.ctx, ChatVisibility::Archived)
.is_ok()); .is_ok());
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id1) Chat::load_from_db(&t.ctx, chat_id1)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Archived == ChatVisibility::Archived
); );
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id2) Chat::load_from_db(&t.ctx, chat_id2)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Archived == ChatVisibility::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
@@ -2869,25 +2867,25 @@ mod tests {
// archive already archived first chat, unarchive second chat two times // archive already archived first chat, unarchive second chat two times
assert!(chat_id1 assert!(chat_id1
.set_archive_state(&t.ctx, ArchiveState::Archived) .set_visibility(&t.ctx, ChatVisibility::Archived)
.is_ok()); .is_ok());
assert!(chat_id2 assert!(chat_id2
.set_archive_state(&t.ctx, ArchiveState::Normal) .set_visibility(&t.ctx, ChatVisibility::Normal)
.is_ok()); .is_ok());
assert!(chat_id2 assert!(chat_id2
.set_archive_state(&t.ctx, ArchiveState::Normal) .set_visibility(&t.ctx, ChatVisibility::Normal)
.is_ok()); .is_ok());
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id1) Chat::load_from_db(&t.ctx, chat_id1)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Archived == ChatVisibility::Archived
); );
assert!( assert!(
Chat::load_from_db(&t.ctx, chat_id2) Chat::load_from_db(&t.ctx, chat_id2)
.unwrap() .unwrap()
.get_visibility() .get_visibility()
== ArchiveState::Normal == ChatVisibility::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);
@@ -2922,7 +2920,7 @@ mod tests {
// pin // pin
assert!( assert!(
chat_id1 chat_id1
.set_archive_state(&t.ctx, ArchiveState::Pinned) .set_visibility(&t.ctx, ChatVisibility::Pinned)
.is_ok() .is_ok()
== true == true
); );
@@ -2930,7 +2928,7 @@ mod tests {
Chat::load_from_db(&t.ctx, chat_id1) Chat::load_from_db(&t.ctx, chat_id1)
.unwrap() .unwrap()
.get_visibility(), .get_visibility(),
ArchiveState::Pinned ChatVisibility::Pinned
); );
// check if chat order changed // check if chat order changed
@@ -2940,7 +2938,7 @@ mod tests {
// unpin // unpin
assert!( assert!(
chat_id1 chat_id1
.set_archive_state(&t.ctx, ArchiveState::Normal) .set_visibility(&t.ctx, ChatVisibility::Normal)
.is_ok() .is_ok()
== true == true
); );
@@ -2948,7 +2946,7 @@ mod tests {
Chat::load_from_db(&t.ctx, chat_id1) Chat::load_from_db(&t.ctx, chat_id1)
.unwrap() .unwrap()
.get_visibility(), .get_visibility(),
ArchiveState::Normal ChatVisibility::Normal
); );
// check if chat order changed back // check if chat order changed back

View File

@@ -393,7 +393,7 @@ mod tests {
assert_eq!(chats.len(), 0); assert_eq!(chats.len(), 0);
chat_id1 chat_id1
.set_archive_state(&t.ctx, ArchiveState::Archived) .set_visibility(&t.ctx, ChatVisibility::Archived)
.ok(); .ok();
let chats = Chatlist::try_load(&t.ctx, DC_GCL_ARCHIVED_ONLY, None, None).unwrap(); let chats = Chatlist::try_load(&t.ctx, DC_GCL_ARCHIVED_ONLY, None, None).unwrap();
assert_eq!(chats.len(), 1); assert_eq!(chats.len(), 1);

View File

@@ -3,7 +3,7 @@ use sha2::{Digest, Sha256};
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use crate::chat::{self, ArchiveState, Chat, ChatId}; use crate::chat::{self, Chat, ChatId};
use crate::config::Config; use crate::config::Config;
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;