mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
rename to ChatVisibility, simplify ffi
This commit is contained in:
@@ -2693,7 +2693,6 @@ dc_context_t* dc_chatlist_get_context (dc_chatlist_t* chatlist);
|
||||
*
|
||||
* id: chat id
|
||||
* name: chat/group name
|
||||
* visibility: one of @ref DC_CHAT_VISIBILITY
|
||||
* color: color of this chat
|
||||
* last-message-from: who sent the last message
|
||||
* last-message-text: message (truncated)
|
||||
|
||||
@@ -25,7 +25,7 @@ use std::time::{Duration, SystemTime};
|
||||
use libc::uintptr_t;
|
||||
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::contact::Contact;
|
||||
use deltachat::context::Context;
|
||||
@@ -1198,21 +1198,22 @@ pub unsafe extern "C" fn dc_set_chat_visibility(
|
||||
return;
|
||||
}
|
||||
let ffi_context = &*context;
|
||||
let archive_state = match archive {
|
||||
0 => ArchiveState::Normal,
|
||||
1 => ArchiveState::Archived,
|
||||
2 => ArchiveState::Pinned,
|
||||
let visibility = match archive {
|
||||
0 => ChatVisibility::Normal,
|
||||
1 => ChatVisibility::Archived,
|
||||
2 => ChatVisibility::Pinned,
|
||||
_ => {
|
||||
ffi_context
|
||||
.warning("ignoring careless call to dc_set_chat_visibility(): unknown archived state");
|
||||
ffi_context.warning(
|
||||
"ignoring careless call to dc_set_chat_visibility(): unknown archived state",
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
ffi_context
|
||||
.with_inner(|ctx| {
|
||||
ChatId::new(chat_id)
|
||||
.set_archive_state(ctx, archive_state)
|
||||
.log_err(ffi_context, "Failed archive chat")
|
||||
.set_visibility(ctx, visibility)
|
||||
.log_err(ffi_context, "Failed setting chat visibility")
|
||||
.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;
|
||||
}
|
||||
let ffi_chat = &*chat;
|
||||
let ffi_context = &*ffi_chat.context;
|
||||
ffi_context
|
||||
.with_inner(|ctx| match ffi_chat.chat.get_id().get_archive_state(ctx) {
|
||||
ArchiveState::Normal => 0,
|
||||
ArchiveState::Archived => 1,
|
||||
ArchiveState::Pinned => 2,
|
||||
} as libc::c_int)
|
||||
.unwrap_or_else(|_| 0)
|
||||
match ffi_chat.chat.visibility {
|
||||
ChatVisibility::Normal => 0,
|
||||
ChatVisibility::Archived => 1,
|
||||
ChatVisibility::Pinned => 2,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use deltachat::chat::{self, ArchiveState, Chat, ChatId};
|
||||
use deltachat::chat::{self, Chat, ChatId, ChatVisibility};
|
||||
use deltachat::chatlist::*;
|
||||
use deltachat::constants::*;
|
||||
use deltachat::contact::*;
|
||||
@@ -518,15 +518,15 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> {
|
||||
chat.get_id(),
|
||||
chat.get_name(),
|
||||
chat.get_id().get_fresh_msg_cnt(context),
|
||||
match chat.get_id().get_archive_state(context) {
|
||||
ArchiveState::Normal => "",
|
||||
ArchiveState::Archived => "📦",
|
||||
ArchiveState::Pinned => "📌",
|
||||
match chat.visibility {
|
||||
ChatVisibility::Normal => "",
|
||||
ChatVisibility::Archived => "📦",
|
||||
ChatVisibility::Pinned => "📌",
|
||||
},
|
||||
);
|
||||
let lot = chatlist.get_summary(context, i, Some(&chat));
|
||||
let statestr =
|
||||
if chat.get_id().get_archive_state(context) == ArchiveState::Archived {
|
||||
if chat.get_id().get_visibility(context) == ChatVisibility::Archived {
|
||||
" [Archived]"
|
||||
} else {
|
||||
match lot.get_state() {
|
||||
@@ -853,12 +853,12 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> {
|
||||
"archive" | "unarchive" | "pin" | "unpin" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
|
||||
let chat_id = ChatId::new(arg1.parse()?);
|
||||
chat_id.set_archive_state(
|
||||
chat_id.set_visibility(
|
||||
context,
|
||||
match arg0 {
|
||||
"archive" => ArchiveState::Archived,
|
||||
"unarchive" | "unpin" => ArchiveState::Normal,
|
||||
"pin" => ArchiveState::Pinned,
|
||||
"archive" => ChatVisibility::Archived,
|
||||
"unarchive" | "unpin" => ChatVisibility::Normal,
|
||||
"pin" => ChatVisibility::Pinned,
|
||||
_ => panic!("Unexpected command (This should never happen)"),
|
||||
},
|
||||
)?;
|
||||
|
||||
74
src/chat.rs
74
src/chat.rs
@@ -137,10 +137,10 @@ impl ChatId {
|
||||
}
|
||||
|
||||
/// Archives or unarchives a chat.
|
||||
pub fn set_archive_state(
|
||||
pub fn set_visibility(
|
||||
self,
|
||||
context: &Context,
|
||||
new_archive_state: ArchiveState,
|
||||
visibility: ChatVisibility,
|
||||
) -> Result<(), Error> {
|
||||
ensure!(
|
||||
!self.is_special(),
|
||||
@@ -148,7 +148,7 @@ impl ChatId {
|
||||
self
|
||||
);
|
||||
|
||||
let send_event = if new_archive_state == ArchiveState::Archived {
|
||||
let send_event = if visibility == ChatVisibility::Archived {
|
||||
sql::execute(
|
||||
context,
|
||||
&context.sql,
|
||||
@@ -164,7 +164,7 @@ impl ChatId {
|
||||
context,
|
||||
&context.sql,
|
||||
"UPDATE chats SET archived=? WHERE id=?;",
|
||||
params![new_archive_state, self],
|
||||
params![visibility, self],
|
||||
)?;
|
||||
|
||||
if send_event {
|
||||
@@ -430,7 +430,7 @@ pub struct Chat {
|
||||
pub id: ChatId,
|
||||
pub typ: Chattype,
|
||||
pub name: String,
|
||||
pub visibility: ArchiveState,
|
||||
pub visibility: ChatVisibility,
|
||||
pub grpid: String,
|
||||
blocked: Blocked,
|
||||
pub param: Params,
|
||||
@@ -665,7 +665,7 @@ impl Chat {
|
||||
id: self.id,
|
||||
type_: self.typ as u32,
|
||||
name: self.name.clone(),
|
||||
archived: self.visibility == ArchiveState::Archived,
|
||||
archived: self.visibility == ChatVisibility::Archived,
|
||||
param: self.param.to_string(),
|
||||
gossiped_timestamp: self.get_gossiped_timestamp(context),
|
||||
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
|
||||
}
|
||||
|
||||
@@ -937,20 +937,18 @@ impl Chat {
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub enum ArchiveState {
|
||||
/// Neither archived or pinned
|
||||
pub enum ChatVisibility {
|
||||
Normal = 0,
|
||||
Archived = 1,
|
||||
/// Pinned (formaly known as sticky)
|
||||
Pinned = 2,
|
||||
}
|
||||
|
||||
impl rusqlite::types::ToSql for ArchiveState {
|
||||
impl rusqlite::types::ToSql for ChatVisibility {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput> {
|
||||
let duration = match &self {
|
||||
ArchiveState::Normal => 0,
|
||||
ArchiveState::Archived => 1,
|
||||
ArchiveState::Pinned => 2,
|
||||
ChatVisibility::Normal => 0,
|
||||
ChatVisibility::Archived => 1,
|
||||
ChatVisibility::Pinned => 2,
|
||||
};
|
||||
let val = rusqlite::types::Value::Integer(duration as i64);
|
||||
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> {
|
||||
i64::column_result(value).and_then(|val| {
|
||||
match val {
|
||||
2 => Ok(ArchiveState::Pinned),
|
||||
1 => Ok(ArchiveState::Archived),
|
||||
0 => Ok(ArchiveState::Normal),
|
||||
2 => Ok(ChatVisibility::Pinned),
|
||||
1 => Ok(ChatVisibility::Archived),
|
||||
0 => Ok(ChatVisibility::Normal),
|
||||
n => {
|
||||
// unknown archived state, falling back to normal state (was this db opened with a newer deltachat version?)
|
||||
Err(rusqlite::types::FromSqlError::OutOfRange(n))
|
||||
@@ -1347,7 +1345,7 @@ fn prepare_msg_common(
|
||||
) -> Result<MsgId, Error> {
|
||||
msg.id = MsgId::new_unset();
|
||||
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)?;
|
||||
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 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) {
|
||||
ensure!(chat.can_send(), "cannot send to {}", chat_id);
|
||||
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");
|
||||
msg.try_calc_and_set_dimensions(context).ok();
|
||||
prepare_msg_blob(context, msg)?;
|
||||
chat_id.set_archive_state(context, ArchiveState::Normal)?;
|
||||
chat_id.set_visibility(context, ChatVisibility::Normal)?;
|
||||
|
||||
context.sql.execute(
|
||||
"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();
|
||||
assert_eq!(chat.id, chat_id);
|
||||
assert!(chat.is_self_talk());
|
||||
assert!(chat.visibility == ArchiveState::Normal);
|
||||
assert!(chat.visibility == ChatVisibility::Normal);
|
||||
assert!(!chat.is_device_talk());
|
||||
assert!(chat.can_send());
|
||||
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::SavedMessages));
|
||||
@@ -2620,7 +2618,7 @@ mod tests {
|
||||
assert_eq!(DC_CHAT_ID_DEADDROP, 1);
|
||||
assert!(chat.id.is_deaddrop());
|
||||
assert!(!chat.is_self_talk());
|
||||
assert!(chat.visibility == ArchiveState::Normal);
|
||||
assert!(chat.visibility == ChatVisibility::Normal);
|
||||
assert!(!chat.is_device_talk());
|
||||
assert!(!chat.can_send());
|
||||
assert_eq!(chat.name, t.ctx.stock_str(StockMessage::DeadDrop));
|
||||
@@ -2827,19 +2825,19 @@ mod tests {
|
||||
|
||||
// archive first chat
|
||||
assert!(chat_id1
|
||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Archived)
|
||||
.is_ok());
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id1)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Archived
|
||||
== ChatVisibility::Archived
|
||||
);
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id2)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Normal
|
||||
== ChatVisibility::Normal
|
||||
);
|
||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||
assert_eq!(chatlist_len(&t.ctx, 0), 2); // including DC_CHAT_ID_ARCHIVED_LINK now
|
||||
@@ -2848,19 +2846,19 @@ mod tests {
|
||||
|
||||
// archive second chat
|
||||
assert!(chat_id2
|
||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Archived)
|
||||
.is_ok());
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id1)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Archived
|
||||
== ChatVisibility::Archived
|
||||
);
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id2)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Archived
|
||||
== ChatVisibility::Archived
|
||||
);
|
||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||
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
|
||||
assert!(chat_id1
|
||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Archived)
|
||||
.is_ok());
|
||||
assert!(chat_id2
|
||||
.set_archive_state(&t.ctx, ArchiveState::Normal)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Normal)
|
||||
.is_ok());
|
||||
assert!(chat_id2
|
||||
.set_archive_state(&t.ctx, ArchiveState::Normal)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Normal)
|
||||
.is_ok());
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id1)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Archived
|
||||
== ChatVisibility::Archived
|
||||
);
|
||||
assert!(
|
||||
Chat::load_from_db(&t.ctx, chat_id2)
|
||||
.unwrap()
|
||||
.get_visibility()
|
||||
== ArchiveState::Normal
|
||||
== ChatVisibility::Normal
|
||||
);
|
||||
assert_eq!(get_chat_cnt(&t.ctx), 2);
|
||||
assert_eq!(chatlist_len(&t.ctx, 0), 2);
|
||||
@@ -2922,7 +2920,7 @@ mod tests {
|
||||
// pin
|
||||
assert!(
|
||||
chat_id1
|
||||
.set_archive_state(&t.ctx, ArchiveState::Pinned)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Pinned)
|
||||
.is_ok()
|
||||
== true
|
||||
);
|
||||
@@ -2930,7 +2928,7 @@ mod tests {
|
||||
Chat::load_from_db(&t.ctx, chat_id1)
|
||||
.unwrap()
|
||||
.get_visibility(),
|
||||
ArchiveState::Pinned
|
||||
ChatVisibility::Pinned
|
||||
);
|
||||
|
||||
// check if chat order changed
|
||||
@@ -2940,7 +2938,7 @@ mod tests {
|
||||
// unpin
|
||||
assert!(
|
||||
chat_id1
|
||||
.set_archive_state(&t.ctx, ArchiveState::Normal)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Normal)
|
||||
.is_ok()
|
||||
== true
|
||||
);
|
||||
@@ -2948,7 +2946,7 @@ mod tests {
|
||||
Chat::load_from_db(&t.ctx, chat_id1)
|
||||
.unwrap()
|
||||
.get_visibility(),
|
||||
ArchiveState::Normal
|
||||
ChatVisibility::Normal
|
||||
);
|
||||
|
||||
// check if chat order changed back
|
||||
|
||||
@@ -393,7 +393,7 @@ mod tests {
|
||||
assert_eq!(chats.len(), 0);
|
||||
|
||||
chat_id1
|
||||
.set_archive_state(&t.ctx, ArchiveState::Archived)
|
||||
.set_visibility(&t.ctx, ChatVisibility::Archived)
|
||||
.ok();
|
||||
let chats = Chatlist::try_load(&t.ctx, DC_GCL_ARCHIVED_ONLY, None, None).unwrap();
|
||||
assert_eq!(chats.len(), 1);
|
||||
|
||||
@@ -3,7 +3,7 @@ use sha2::{Digest, Sha256};
|
||||
|
||||
use num_traits::FromPrimitive;
|
||||
|
||||
use crate::chat::{self, ArchiveState, Chat, ChatId};
|
||||
use crate::chat::{self, Chat, ChatId};
|
||||
use crate::config::Config;
|
||||
use crate::constants::*;
|
||||
use crate::contact::*;
|
||||
|
||||
Reference in New Issue
Block a user