Skip to content

Commit 4226b9d

Browse files
committed
fix: unread count
1 parent 835067d commit 4226b9d

File tree

13 files changed

+880
-821
lines changed

13 files changed

+880
-821
lines changed

crates/restsend-wasm/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-wasm"
3-
version = "1.2.1"
3+
version = "1.2.2"
44
edition = "2021"
55
description = "Restsend Instant Messaging Javascript/Wasm SDK"
66
authors = ["Restsend Team <[email protected]>"]

crates/restsend-wasm/src/account.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use restsend_sdk::models::conversation::Extra;
12
use serde::Serialize;
23
use wasm_bindgen::prelude::*;
34
use wasm_bindgen::JsValue;
@@ -59,6 +60,23 @@ pub async fn signup(
5960
.map_err(|e| e.into())
6061
}
6162

63+
/// Signup with userId and password
64+
#[allow(non_snake_case)]
65+
#[wasm_bindgen]
66+
pub async fn guestLogin(
67+
endpoint: String,
68+
userId: String,
69+
extra: JsValue,
70+
) -> Result<JsValue, JsValue> {
71+
let endpoint = get_endpoint(endpoint);
72+
let serializer = &serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
73+
let extra = serde_wasm_bindgen::from_value::<Extra>(extra).ok();
74+
restsend_sdk::services::auth::guest_login(endpoint, userId, extra)
75+
.await
76+
.map(|v| v.serialize(serializer).unwrap_or(JsValue::UNDEFINED))
77+
.map_err(|e| e.into())
78+
}
79+
6280
/// Logout with token
6381
#[allow(non_snake_case)]
6482
#[wasm_bindgen]

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.1.7"
3+
version = "1.1.8"
44
edition = "2021"
55

66
[lib]

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ impl ClientStore {
208208

209209
conversation.last_read_at = old_conversation.last_read_at;
210210
conversation.last_read_seq = old_conversation.last_read_seq;
211-
conversation.unread = old_conversation.unread;
212211
}
213212

214213
if let Some(log) = get_conversation_last_readable_message(
@@ -226,15 +225,12 @@ impl ClientStore {
226225
conversation.is_partial = false;
227226
conversation.cached_at = now;
228227

229-
if conversation.unread == 0 {
230-
let start_seq = conversation.start_seq.max(conversation.last_read_seq);
231-
let diff = conversation
232-
.last_message_seq
233-
.unwrap_or(conversation.last_seq)
234-
- start_seq;
235-
conversation.unread = diff.max(0);
236-
}
237-
228+
let start_seq = conversation.start_seq.max(conversation.last_read_seq);
229+
let diff = conversation
230+
.last_message_seq
231+
.unwrap_or(conversation.last_seq)
232+
- start_seq;
233+
conversation.unread = diff.max(0);
238234
results.push(ValueItem {
239235
partition: "".to_string(),
240236
sort_key: conversation.sort_key(),

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use crate::client::store::conversations::merge_conversation_from_chat;
33
use crate::client::store::is_cache_expired;
44
use crate::models::ChatLogStatus;
55
use crate::utils::now_millis;
6-
use crate::REMOVED_CONVERSATION_CACHE_EXPIRE_SECS;
76
use crate::{
87
callback::MessageCallback,
98
request::{ChatRequest, ChatRequestType},
109
};
10+
use crate::{PING_TIMEOUT_SECS, REMOVED_CONVERSATION_CACHE_EXPIRE_SECS};
1111
use http::StatusCode;
1212
use log::{info, warn};
1313
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
@@ -61,6 +61,7 @@ impl ClientStore {
6161
} else {
6262
ChatLogStatus::SendFailed
6363
};
64+
6465
if let Some(pending) = self.peek_pending_request(&req.chat_id).await {
6566
match status {
6667
ChatLogStatus::Sent => {
@@ -75,6 +76,28 @@ impl ClientStore {
7576
}
7677
_ => {}
7778
}
79+
} else {
80+
if content_type == "ping" && req.seq == 0 {
81+
match req.content.as_ref() {
82+
Some(content) => {
83+
let data = match serde_json::from_str::<serde_json::Value>(
84+
&content.text,
85+
) {
86+
Ok(data) => data,
87+
Err(_) => return vec![],
88+
};
89+
let timestamp = match data["timestamp"].as_i64() {
90+
Some(timestamp) => timestamp,
91+
None => return vec![],
92+
};
93+
let diff = now_millis() - timestamp;
94+
if diff >= PING_TIMEOUT_SECS * 1000 {
95+
warn!("ping timeout:{}", diff);
96+
}
97+
}
98+
_ => {}
99+
}
100+
}
78101
}
79102

80103
self.update_outoing_chat_log_state(&topic_id, &chat_id, status, Some(ack_seq))

crates/restsend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const MEDIA_PROGRESS_INTERVAL: u128 = 300; // 300ms to update progress
4040
const CONVERSATION_CACHE_EXPIRE_SECS: i64 = 60; // 60 seconds
4141
const USER_CACHE_EXPIRE_SECS: i64 = 60; // 60 seconds
4242
const REMOVED_CONVERSATION_CACHE_EXPIRE_SECS: i64 = 10; // 10 seconds
43+
const PING_TIMEOUT_SECS: i64 = 5; // 5 seconds
4344

4445
#[cfg(not(target_family = "wasm"))]
4546
const WORKER_THREADS: usize = 4;

crates/restsend/src/services/conversation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use serde::Serialize;
2-
31
use super::{api_call, response::APISendResponse};
42
use crate::Result;
53
use crate::{
@@ -8,6 +6,7 @@ use crate::{
86
services::LOGS_LIMIT,
97
utils::now_millis,
108
};
9+
use serde::Serialize;
1110

1211
#[derive(Serialize)]
1312
#[serde(rename_all = "camelCase")]
@@ -39,7 +38,7 @@ pub async fn get_conversations(
3938
if !updated_at.is_empty() {
4039
data["updatedAt"] = serde_json::json!(updated_at);
4140
}
42-
if let Some(last_updated_at) = last_updated_at.filter(|s| !s.is_empty()) {
41+
if let Some(last_updated_at) = last_updated_at.filter(|s| !s.is_empty()) {
4342
data["lastUpdatedAt"] = serde_json::json!(last_updated_at);
4443
}
4544
if let Some(last_removed_at) = last_removed_at.filter(|s| !s.is_empty()) {

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"Restsend Team <[email protected]>"
66
],
77
"description": "Restsend Instant Messaging Javascript/Wasm SDK",
8-
"version": "1.2.1",
8+
"version": "1.2.2",
99
"files": [
1010
"restsend_wasm_bg.wasm",
1111
"restsend_wasm.js",

0 commit comments

Comments
 (0)