Skip to content

Commit 0410f79

Browse files
committed
add CountableCallback
1 parent 694b928 commit 0410f79

File tree

10 files changed

+404
-31
lines changed

10 files changed

+404
-31
lines changed

crates/restsend/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "restsend-sdk"
3-
version = "1.0.7"
3+
version = "1.0.9"
44
edition = "2021"
55

66
[lib]

crates/restsend/src/callback.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ pub trait RsCallback: Send + Sync {
5050
fn on_conversations_updated(&self, conversations: Vec<Conversation>) {}
5151
fn on_conversation_removed(&self, conversation_id: String) {}
5252
}
53-
53+
#[allow(unused_variables)]
54+
#[export_wasm_or_ffi(#[uniffi::export(callback_interface)])]
55+
pub trait CountableCallback: Send + Sync {
56+
fn is_countable(&self, content: Content) -> bool {
57+
!content.unreadable
58+
}
59+
}
5460
#[allow(unused_variables)]
5561
#[export_wasm_or_ffi(#[uniffi::export(callback_interface)])]
5662
pub trait UploadCallback: Send + Sync {

crates/restsend/src/client/conversation.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ impl Client {
9191
.ok()?;
9292

9393
for c in r.items.iter() {
94-
if !c.content.unreadable {
94+
let is_countable =
95+
if let Some(cb) = self.store.countable_callback.lock().unwrap().as_ref() {
96+
cb.is_countable(c.content.clone())
97+
} else {
98+
!c.content.unreadable
99+
};
100+
if is_countable {
95101
conversation.last_message_at = c.created_at.clone();
96102
conversation.last_message = Some(c.content.clone());
97103
conversation.last_sender_id = c.sender_id.clone();
@@ -117,7 +123,12 @@ impl Client {
117123
ensure_conversation_last_version: Option<bool>,
118124
) {
119125
let st = now_millis();
120-
let limit = if limit == 0 { MAX_LOGS_LIMIT/2 } else { limit }.min(MAX_LOGS_LIMIT);
126+
let limit = if limit == 0 {
127+
MAX_LOGS_LIMIT / 2
128+
} else {
129+
limit
130+
}
131+
.min(MAX_LOGS_LIMIT);
121132
let conversation = self
122133
.store
123134
.get_conversation(
@@ -127,9 +138,13 @@ impl Client {
127138
)
128139
.await
129140
.unwrap_or_default();
130-
141+
131142
let store_st = now_millis();
132-
match self.store.get_chat_logs(&topic_id, conversation.start_seq, last_seq, limit).await {
143+
match self
144+
.store
145+
.get_chat_logs(&topic_id, conversation.start_seq, last_seq, limit)
146+
.await
147+
{
133148
Ok((local_logs, need_fetch)) => {
134149
info!(
135150
"sync_chat_logs local_logs.len: {} start_seq: {} last_seq: {:?} limit: {} local_logs.start_sort_value:{} local_logs.end_sort_value:{} need_fetch:{} store_cost:{:?} total_cost:{:?}",
@@ -165,10 +180,7 @@ impl Client {
165180
}
166181
}
167182

168-
pub async fn save_chat_logs(
169-
&self,
170-
logs: &Vec<ChatLog>,
171-
) -> Result<()> {
183+
pub async fn save_chat_logs(&self, logs: &Vec<ChatLog>) -> Result<()> {
172184
self.store.save_chat_logs(logs).await
173185
}
174186

@@ -437,17 +449,32 @@ impl Client {
437449
None => continue,
438450
};
439451

452+
if self.store.countable_callback.lock().unwrap().is_some() {
453+
conversation.unread = 0
454+
}
455+
440456
for c in lr.items.iter() {
441457
if c.seq <= conversation.last_seq {
442458
continue;
443459
}
444460
conversation.last_seq = c.seq;
445-
if !c.content.unreadable {
461+
462+
let is_countable =
463+
if let Some(cb) = self.store.countable_callback.lock().unwrap().as_ref() {
464+
cb.is_countable(c.content.clone())
465+
} else {
466+
!c.content.unreadable
467+
};
468+
469+
if is_countable {
446470
conversation.updated_at = c.created_at.clone();
447471
conversation.last_message_at = c.created_at.clone();
448472
conversation.last_message = Some(c.content.clone());
449473
conversation.last_sender_id = c.sender_id.clone();
450474
conversation.last_message_seq = Some(c.seq);
475+
if conversation.last_read_seq < c.seq {
476+
conversation.unread += 1;
477+
}
451478
}
452479
break;
453480
}
@@ -457,6 +484,11 @@ impl Client {
457484
if let Some(cb) = self.store.callback.lock().unwrap().as_ref() {
458485
cb.on_conversations_updated(updated_conversations.clone());
459486
}
487+
// sync to store
488+
let t = self.store.message_storage.table::<Conversation>().await;
489+
for c in updated_conversations.iter_mut() {
490+
t.set("", &c.topic_id, Some(c)).await.ok();
491+
}
460492
Ok(())
461493
}
462494

@@ -534,10 +566,7 @@ impl Client {
534566
) -> Result<Conversation> {
535567
self.store.set_conversation_extra(&topic_id, extra).await
536568
}
537-
pub async fn clear_conversation(
538-
&self,
539-
topic_id: String
540-
) -> Result<()> {
569+
pub async fn clear_conversation(&self, topic_id: String) -> Result<()> {
541570
self.store.clear_conversation(&topic_id).await
542571
}
543572
}

crates/restsend/src/client/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use self::{
33
store::{ClientStore, ClientStoreRef},
44
};
55
use crate::{
6-
callback::{DownloadCallback, RsCallback},
6+
callback::{CountableCallback, DownloadCallback, RsCallback},
77
error::ClientError,
88
media::{build_download_url, download_file},
99
models::{AuthInfo, User},
@@ -92,6 +92,9 @@ impl Client {
9292
pub fn set_callback(&self, callback: Option<Box<dyn RsCallback>>) {
9393
*self.store.callback.lock().unwrap() = callback;
9494
}
95+
pub fn set_countable_callback(&self, callback: Option<Box<dyn CountableCallback>>) {
96+
*self.store.countable_callback.lock().unwrap() = callback;
97+
}
9598

9699
pub async fn get_user(&self, user_id: String, blocking: bool) -> Option<User> {
97100
self.store.get_user(&user_id, blocking).await

crates/restsend/src/client/store/conversations.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ impl ClientStore {
228228

229229
if conversation.unread == 0 {
230230
let start_seq = conversation.start_seq.max(conversation.last_read_seq);
231-
let diff = conversation.last_seq - start_seq;
231+
let diff = conversation
232+
.last_message_seq
233+
.unwrap_or(conversation.last_seq)
234+
- start_seq;
232235
conversation.unread = diff.max(0);
233236
}
234237

crates/restsend/src/client/store/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use self::attachments::UploadTask;
2-
use crate::callback::RsCallback;
2+
use crate::callback::{CountableCallback, RsCallback};
33
use crate::models::Attachment;
44
use crate::storage::Storage;
55
use crate::utils::{elapsed, now_millis};
@@ -99,6 +99,7 @@ type PendingRequests = Arc<Mutex<HashMap<String, PendingRequest>>>;
9999

100100
pub(super) type ClientStoreRef = Arc<ClientStore>;
101101
pub(super) type CallbackRef = Arc<Mutex<Option<Box<dyn RsCallback>>>>;
102+
pub(super) type CountableCallbackRef = Arc<Mutex<Option<Box<dyn CountableCallback>>>>;
102103
pub(super) struct ClientStore {
103104
user_id: String,
104105
endpoint: String,
@@ -110,6 +111,7 @@ pub(super) struct ClientStore {
110111
removed_conversations: Mutex<HashMap<String, i64>>,
111112
pub(crate) message_storage: Arc<Storage>,
112113
pub(crate) callback: CallbackRef,
114+
pub(crate) countable_callback: CountableCallbackRef,
113115
incoming_logs: Mutex<HashMap<String, Vec<String>>>,
114116
}
115117

@@ -132,6 +134,7 @@ impl ClientStore {
132134
removed_conversations: Mutex::new(HashMap::new()),
133135
message_storage: Arc::new(Storage::new(db_path)),
134136
callback: Arc::new(Mutex::new(None)),
137+
countable_callback: Arc::new(Mutex::new(None)),
135138
incoming_logs: Mutex::new(HashMap::new()),
136139
}
137140
}

0 commit comments

Comments
 (0)