Re-send pending messages after reconnecting
This commit is contained in:
@@ -73,7 +73,7 @@ async fn main() {
|
||||
for timestamp in timestamps {
|
||||
let sender_id = *zed_user_ids.choose(&mut rng).unwrap();
|
||||
let body = lipsum::lipsum_words(rng.gen_range(1..=50));
|
||||
db.create_channel_message(channel_id, sender_id, &body, timestamp)
|
||||
db.create_channel_message(channel_id, sender_id, &body, timestamp, rng.gen())
|
||||
.await
|
||||
.expect("failed to insert message");
|
||||
}
|
||||
|
||||
+39
-5
@@ -1,7 +1,7 @@
|
||||
use anyhow::Context;
|
||||
use async_std::task::{block_on, yield_now};
|
||||
use serde::Serialize;
|
||||
use sqlx::{FromRow, Result};
|
||||
use sqlx::{types::Uuid, FromRow, Result};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
pub use async_sqlx_session::PostgresSessionStore as SessionStore;
|
||||
@@ -402,11 +402,13 @@ impl Db {
|
||||
sender_id: UserId,
|
||||
body: &str,
|
||||
timestamp: OffsetDateTime,
|
||||
nonce: u128,
|
||||
) -> Result<MessageId> {
|
||||
test_support!(self, {
|
||||
let query = "
|
||||
INSERT INTO channel_messages (channel_id, sender_id, body, sent_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO channel_messages (channel_id, sender_id, body, sent_at, nonce)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (nonce) DO UPDATE SET nonce = excluded.nonce
|
||||
RETURNING id
|
||||
";
|
||||
sqlx::query_scalar(query)
|
||||
@@ -414,6 +416,7 @@ impl Db {
|
||||
.bind(sender_id.0)
|
||||
.bind(body)
|
||||
.bind(timestamp)
|
||||
.bind(Uuid::from_u128(nonce))
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map(MessageId)
|
||||
@@ -430,7 +433,7 @@ impl Db {
|
||||
let query = r#"
|
||||
SELECT * FROM (
|
||||
SELECT
|
||||
id, sender_id, body, sent_at AT TIME ZONE 'UTC' as sent_at
|
||||
id, sender_id, body, sent_at AT TIME ZONE 'UTC' as sent_at, nonce
|
||||
FROM
|
||||
channel_messages
|
||||
WHERE
|
||||
@@ -514,6 +517,7 @@ pub struct ChannelMessage {
|
||||
pub sender_id: UserId,
|
||||
pub body: String,
|
||||
pub sent_at: OffsetDateTime,
|
||||
pub nonce: Uuid,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -677,7 +681,7 @@ pub mod tests {
|
||||
let org = db.create_org("org", "org").await.unwrap();
|
||||
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
||||
for i in 0..10 {
|
||||
db.create_channel_message(channel, user, &i.to_string(), OffsetDateTime::now_utc())
|
||||
db.create_channel_message(channel, user, &i.to_string(), OffsetDateTime::now_utc(), i)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
@@ -697,4 +701,34 @@ pub mod tests {
|
||||
["1", "2", "3", "4"]
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_channel_message_nonces() {
|
||||
let test_db = TestDb::new();
|
||||
let db = test_db.db();
|
||||
let user = db.create_user("user", false).await.unwrap();
|
||||
let org = db.create_org("org", "org").await.unwrap();
|
||||
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
||||
|
||||
let msg1_id = db
|
||||
.create_channel_message(channel, user, "1", OffsetDateTime::now_utc(), 1)
|
||||
.await
|
||||
.unwrap();
|
||||
let msg2_id = db
|
||||
.create_channel_message(channel, user, "2", OffsetDateTime::now_utc(), 2)
|
||||
.await
|
||||
.unwrap();
|
||||
let msg3_id = db
|
||||
.create_channel_message(channel, user, "3", OffsetDateTime::now_utc(), 1)
|
||||
.await
|
||||
.unwrap();
|
||||
let msg4_id = db
|
||||
.create_channel_message(channel, user, "4", OffsetDateTime::now_utc(), 2)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_ne!(msg1_id, msg2_id);
|
||||
assert_eq!(msg1_id, msg3_id);
|
||||
assert_eq!(msg2_id, msg4_id);
|
||||
}
|
||||
}
|
||||
|
||||
+43
-2
@@ -602,6 +602,7 @@ impl Server {
|
||||
body: msg.body,
|
||||
timestamp: msg.sent_at.unix_timestamp() as u64,
|
||||
sender_id: msg.sender_id.to_proto(),
|
||||
nonce: Some(msg.nonce.as_u128().into()),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
self.peer
|
||||
@@ -687,10 +688,24 @@ impl Server {
|
||||
}
|
||||
|
||||
let timestamp = OffsetDateTime::now_utc();
|
||||
let nonce = if let Some(nonce) = request.payload.nonce {
|
||||
nonce
|
||||
} else {
|
||||
self.peer
|
||||
.respond_with_error(
|
||||
receipt,
|
||||
proto::Error {
|
||||
message: "nonce can't be blank".to_string(),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let message_id = self
|
||||
.app_state
|
||||
.db
|
||||
.create_channel_message(channel_id, user_id, &body, timestamp)
|
||||
.create_channel_message(channel_id, user_id, &body, timestamp, nonce.clone().into())
|
||||
.await?
|
||||
.to_proto();
|
||||
let message = proto::ChannelMessage {
|
||||
@@ -698,6 +713,7 @@ impl Server {
|
||||
id: message_id,
|
||||
body,
|
||||
timestamp: timestamp.unix_timestamp() as u64,
|
||||
nonce: Some(nonce),
|
||||
};
|
||||
broadcast(request.sender_id, connection_ids, |conn_id| {
|
||||
self.peer.send(
|
||||
@@ -754,6 +770,7 @@ impl Server {
|
||||
body: msg.body,
|
||||
timestamp: msg.sent_at.unix_timestamp() as u64,
|
||||
sender_id: msg.sender_id.to_proto(),
|
||||
nonce: Some(msg.nonce.as_u128().into()),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
self.peer
|
||||
@@ -1513,6 +1530,7 @@ mod tests {
|
||||
current_user_id(&user_store_b),
|
||||
"hello A, it's B.",
|
||||
OffsetDateTime::now_utc(),
|
||||
1,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1707,6 +1725,7 @@ mod tests {
|
||||
current_user_id(&user_store_b),
|
||||
"hello A, it's B.",
|
||||
OffsetDateTime::now_utc(),
|
||||
2,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1787,6 +1806,24 @@ mod tests {
|
||||
)
|
||||
});
|
||||
|
||||
// Send a message from client B while it is disconnected.
|
||||
channel_b
|
||||
.update(&mut cx_b, |channel, cx| {
|
||||
let task = channel
|
||||
.send_message("can you see this?".to_string(), cx)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
channel_messages(channel),
|
||||
&[
|
||||
("user_b".to_string(), "hello A, it's B.".to_string(), false),
|
||||
("user_b".to_string(), "can you see this?".to_string(), true)
|
||||
]
|
||||
);
|
||||
task
|
||||
})
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
// Send a message from client A while B is disconnected.
|
||||
channel_a
|
||||
.update(&mut cx_a, |channel, cx| {
|
||||
@@ -1812,7 +1849,8 @@ mod tests {
|
||||
server.allow_connections();
|
||||
cx_b.foreground().advance_clock(Duration::from_secs(10));
|
||||
|
||||
// Verify that B sees the new messages upon reconnection.
|
||||
// Verify that B sees the new messages upon reconnection, as well as the message client B
|
||||
// sent while offline.
|
||||
channel_b
|
||||
.condition(&cx_b, |channel, _| {
|
||||
channel_messages(channel)
|
||||
@@ -1820,6 +1858,7 @@ mod tests {
|
||||
("user_b".to_string(), "hello A, it's B.".to_string(), false),
|
||||
("user_a".to_string(), "oh, hi B.".to_string(), false),
|
||||
("user_a".to_string(), "sup".to_string(), false),
|
||||
("user_b".to_string(), "can you see this?".to_string(), false),
|
||||
]
|
||||
})
|
||||
.await;
|
||||
@@ -1838,6 +1877,7 @@ mod tests {
|
||||
("user_b".to_string(), "hello A, it's B.".to_string(), false),
|
||||
("user_a".to_string(), "oh, hi B.".to_string(), false),
|
||||
("user_a".to_string(), "sup".to_string(), false),
|
||||
("user_b".to_string(), "can you see this?".to_string(), false),
|
||||
("user_a".to_string(), "you online?".to_string(), false),
|
||||
]
|
||||
})
|
||||
@@ -1856,6 +1896,7 @@ mod tests {
|
||||
("user_b".to_string(), "hello A, it's B.".to_string(), false),
|
||||
("user_a".to_string(), "oh, hi B.".to_string(), false),
|
||||
("user_a".to_string(), "sup".to_string(), false),
|
||||
("user_b".to_string(), "can you see this?".to_string(), false),
|
||||
("user_a".to_string(), "you online?".to_string(), false),
|
||||
("user_b".to_string(), "yep".to_string(), false),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user