fix(channel): open an uncached post from a notification at once
- thread opens immediately with placeholder rows while the post loads - new Engine::open_post fetches the post and its replies in parallel - no second replies request after the jump when they came with the post - failed load returns to the post list with a notice Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -245,6 +245,7 @@ pub struct ConversationView {
245
245
reaction_details: Option<ReactionDetails>,
246
246
highlighted_message: Option<String>,
247
247
fetched_jump: Option<String>,
248
loading_post: Option<String>,
248
249
pin_index: usize,
249
250
channel_details_open: bool,
250
251
copilot: copilot::CopilotPane,
@@ -595,6 +596,7 @@ impl ConversationView {
595
596
reaction_details: None,
596
597
highlighted_message: None,
597
598
fetched_jump: None,
599
loading_post: None,
598
600
pin_index: 0,
599
601
channel_details_open: false,
600
602
copilot: copilot::CopilotPane::default(),
@@ -1190,6 +1192,16 @@ impl ConversationView {
1190
1192
}
1191
1193
1192
1194
fn jump_to_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) {
1195
self.jump_to_post(conversation_id, message_id, true, cx);
1196
}
1197
1198
fn jump_to_post(
1199
&mut self,
1200
conversation_id: &str,
1201
message_id: &str,
1202
refresh_replies: bool,
1203
cx: &mut Context<Self>,
1204
) {
1193
1205
let store = self.app.read(cx).store.clone();
1194
1206
let record = store
1195
1207
.messages_by_id(conversation_id, &[message_id.to_owned()])
@@ -1246,7 +1258,9 @@ impl ConversationView {
1246
1258
self.reload_pending(cx);
1247
1259
self.rebuild(true, cx);
1248
1260
if in_channel {
1249
self.refresh_thread(row_key.clone(), cx);
1261
if refresh_replies {
1262
self.refresh_thread(row_key.clone(), cx);
1263
}
1250
1264
let shown_alone = self
1251
1265
.rows
1252
1266
.borrow()
@@ -1276,24 +1290,68 @@ impl ConversationView {
1276
1290
let (task_conversation_id, task_message_id) = (conversation_id.clone(), message_id.to_owned());
1277
1291
let receiver = runtime::spawn(async move {
1278
1292
engine
1279
.refresh_message(&task_conversation_id, &task_message_id)
1293
.open_post(&task_conversation_id, &task_message_id)
1280
1294
.await
1281
1295
});
1282
1296
let message_id = message_id.to_owned();
1297
self.show_loading_post(message_id.clone(), cx);
1283
1298
cx.spawn(async move |this, cx| {
1284
let Ok(Ok(Some(_))) = receiver.await else {
1285
return;
1286
};
1299
let result = receiver.await;
1287
1300
this.update(cx, |this, cx| {
1288
if this.conversation_id().as_deref() == Some(conversation_id.as_str()) {
1289
this.jump_to_message(&conversation_id, &message_id, cx);
1290
}
1301
this.finish_loading_post(&conversation_id, &message_id, result, cx)
1291
1302
})
1292
1303
.ok();
1293
1304
})
1294
1305
.detach();
1295
1306
}
1296
1307
1308
fn show_loading_post(&mut self, message_id: String, cx: &mut Context<Self>) {
1309
self.pane = ChannelPane::Posts;
1310
self.enter_feed_mode();
1311
self.stop_typing(cx);
1312
self.enter_thread_mode(message_id.clone(), cx);
1313
self.loading_post = Some(message_id);
1314
self.rebuild(true, cx);
1315
}
1316
1317
fn finish_loading_post(
1318
&mut self,
1319
conversation_id: &str,
1320
message_id: &str,
1321
result: Result<
1322
teams_core::Result<Option<teams_core::OpenedPost>>,
1323
tokio::sync::oneshot::error::RecvError,
1324
>,
1325
cx: &mut Context<Self>,
1326
) {
1327
if self.loading_post.as_deref() != Some(message_id) {
1328
return;
1329
}
1330
self.loading_post = None;
1331
if self.conversation_id().as_deref() != Some(conversation_id) {
1332
return;
1333
}
1334
let still_waiting = self.current.as_ref().is_some_and(|current| {
1335
current.mode == ViewMode::Thread(message_id.to_owned())
1336
});
1337
match result {
1338
Ok(Ok(Some(post))) if still_waiting => {
1339
self.jump_to_post(conversation_id, message_id, !post.replies_loaded, cx)
1340
}
1341
Ok(Ok(Some(_))) => self.rebuild(true, cx),
1342
failure => {
1343
if let Ok(Err(error)) = &failure {
1344
self.notice = Some(format!("Could not load the post: {}", short_error(error)));
1345
}
1346
if still_waiting {
1347
self.back_to_threads(cx);
1348
} else {
1349
cx.notify();
1350
}
1351
}
1352
}
1353
}
1354
1297
1355
fn reveal_row(&mut self, row_key: String, message_key: String, cx: &mut Context<Self>) {
1298
1356
let index = self
1299
1357
.rows
@@ -1632,6 +1690,7 @@ impl ConversationView {
1632
1690
ViewMode::Thread(root_id) => Some(root_id.clone()),
1633
1691
ViewMode::Flat | ViewMode::ThreadList => None,
1634
1692
};
1693
let loading_post = thread_root.is_some() && thread_root == self.loading_post;
1635
1694
let sync_state = store.sync_state(&conversation_id).ok().flatten();
1636
1695
let window = current.detached.as_ref().map(|detached| {
1637
1696
(
@@ -1689,9 +1748,8 @@ impl ConversationView {
1689
1748
current.loaded_count = records.len();
1690
1749
}
1691
1750
let has_older = sync_has_more && !current.older_blocked && can_load_older;
1692
let placeholders = current.sync.messages
1693
&& records.is_empty()
1694
&& !matches!(current.mode, ViewMode::Thread(_));
1751
let placeholders = records.is_empty()
1752
&& (loading_post || current.sync.messages && thread_root.is_none());
1695
1753
let in_feed = current.mode == ViewMode::ThreadList;
1696
1754
let mut rows = match ¤t.mode {
1697
1755
_ if placeholders => {
@@ -245,6 +245,7 @@ pub struct ConversationView {245
reaction_details: Option<ReactionDetails>,245
reaction_details: Option<ReactionDetails>,246
highlighted_message: Option<String>,246
highlighted_message: Option<String>,247
fetched_jump: Option<String>,247
fetched_jump: Option<String>,248
loading_post: Option<String>,248
pin_index: usize,249
pin_index: usize,249
channel_details_open: bool,250
channel_details_open: bool,250
copilot: copilot::CopilotPane,251
copilot: copilot::CopilotPane,@@ -595,6 +596,7 @@ impl ConversationView {595
reaction_details: None,596
reaction_details: None,596
highlighted_message: None,597
highlighted_message: None,597
fetched_jump: None,598
fetched_jump: None,599
loading_post: None,598
pin_index: 0,600
pin_index: 0,599
channel_details_open: false,601
channel_details_open: false,600
copilot: copilot::CopilotPane::default(),602
copilot: copilot::CopilotPane::default(),@@ -1190,6 +1192,16 @@ impl ConversationView {1190
}1192
}1191
1193
1192
fn jump_to_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) {1194
fn jump_to_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) {1195
self.jump_to_post(conversation_id, message_id, true, cx);1196
}1197
1198
fn jump_to_post(1199
&mut self,1200
conversation_id: &str,1201
message_id: &str,1202
refresh_replies: bool,1203
cx: &mut Context<Self>,1204
) {1193
let store = self.app.read(cx).store.clone();1205
let store = self.app.read(cx).store.clone();1194
let record = store1206
let record = store1195
.messages_by_id(conversation_id, &[message_id.to_owned()])1207
.messages_by_id(conversation_id, &[message_id.to_owned()])@@ -1246,7 +1258,9 @@ impl ConversationView {1246
self.reload_pending(cx);1258
self.reload_pending(cx);1247
self.rebuild(true, cx);1259
self.rebuild(true, cx);1248
if in_channel {1260
if in_channel {1249
self.refresh_thread(row_key.clone(), cx);1261
if refresh_replies {1262
self.refresh_thread(row_key.clone(), cx);1263
}1250
let shown_alone = self1264
let shown_alone = self1251
.rows1265
.rows1252
.borrow()1266
.borrow()@@ -1276,24 +1290,68 @@ impl ConversationView {1276
let (task_conversation_id, task_message_id) = (conversation_id.clone(), message_id.to_owned());1290
let (task_conversation_id, task_message_id) = (conversation_id.clone(), message_id.to_owned());1277
let receiver = runtime::spawn(async move {1291
let receiver = runtime::spawn(async move {1278
engine1292
engine1279
.refresh_message(&task_conversation_id, &task_message_id)1293
.open_post(&task_conversation_id, &task_message_id)1280
.await1294
.await1281
});1295
});1282
let message_id = message_id.to_owned();1296
let message_id = message_id.to_owned();1297
self.show_loading_post(message_id.clone(), cx);1283
cx.spawn(async move |this, cx| {1298
cx.spawn(async move |this, cx| {1284
let Ok(Ok(Some(_))) = receiver.await else {1299
let result = receiver.await;1285
return;1286
};1287
this.update(cx, |this, cx| {1300
this.update(cx, |this, cx| {1288
if this.conversation_id().as_deref() == Some(conversation_id.as_str()) {1301
this.finish_loading_post(&conversation_id, &message_id, result, cx)1289
this.jump_to_message(&conversation_id, &message_id, cx);1290
}1291
})1302
})1292
.ok();1303
.ok();1293
})1304
})1294
.detach();1305
.detach();1295
}1306
}1296
1307
1308
fn show_loading_post(&mut self, message_id: String, cx: &mut Context<Self>) {1309
self.pane = ChannelPane::Posts;1310
self.enter_feed_mode();1311
self.stop_typing(cx);1312
self.enter_thread_mode(message_id.clone(), cx);1313
self.loading_post = Some(message_id);1314
self.rebuild(true, cx);1315
}1316
1317
fn finish_loading_post(1318
&mut self,1319
conversation_id: &str,1320
message_id: &str,1321
result: Result<1322
teams_core::Result<Option<teams_core::OpenedPost>>,1323
tokio::sync::oneshot::error::RecvError,1324
>,1325
cx: &mut Context<Self>,1326
) {1327
if self.loading_post.as_deref() != Some(message_id) {1328
return;1329
}1330
self.loading_post = None;1331
if self.conversation_id().as_deref() != Some(conversation_id) {1332
return;1333
}1334
let still_waiting = self.current.as_ref().is_some_and(|current| {1335
current.mode == ViewMode::Thread(message_id.to_owned())1336
});1337
match result {1338
Ok(Ok(Some(post))) if still_waiting => {1339
self.jump_to_post(conversation_id, message_id, !post.replies_loaded, cx)1340
}1341
Ok(Ok(Some(_))) => self.rebuild(true, cx),1342
failure => {1343
if let Ok(Err(error)) = &failure {1344
self.notice = Some(format!("Could not load the post: {}", short_error(error)));1345
}1346
if still_waiting {1347
self.back_to_threads(cx);1348
} else {1349
cx.notify();1350
}1351
}1352
}1353
}1354
1297
fn reveal_row(&mut self, row_key: String, message_key: String, cx: &mut Context<Self>) {1355
fn reveal_row(&mut self, row_key: String, message_key: String, cx: &mut Context<Self>) {1298
let index = self1356
let index = self1299
.rows1357
.rows@@ -1632,6 +1690,7 @@ impl ConversationView {1632
ViewMode::Thread(root_id) => Some(root_id.clone()),1690
ViewMode::Thread(root_id) => Some(root_id.clone()),1633
ViewMode::Flat | ViewMode::ThreadList => None,1691
ViewMode::Flat | ViewMode::ThreadList => None,1634
};1692
};1693
let loading_post = thread_root.is_some() && thread_root == self.loading_post;1635
let sync_state = store.sync_state(&conversation_id).ok().flatten();1694
let sync_state = store.sync_state(&conversation_id).ok().flatten();1636
let window = current.detached.as_ref().map(|detached| {1695
let window = current.detached.as_ref().map(|detached| {1637
(1696
(@@ -1689,9 +1748,8 @@ impl ConversationView {1689
current.loaded_count = records.len();1748
current.loaded_count = records.len();1690
}1749
}1691
let has_older = sync_has_more && !current.older_blocked && can_load_older;1750
let has_older = sync_has_more && !current.older_blocked && can_load_older;1692
let placeholders = current.sync.messages1751
let placeholders = records.is_empty()1693
&& records.is_empty()1752
&& (loading_post || current.sync.messages && thread_root.is_none());1694
&& !matches!(current.mode, ViewMode::Thread(_));1695
let in_feed = current.mode == ViewMode::ThreadList;1753
let in_feed = current.mode == ViewMode::ThreadList;1696
let mut rows = match ¤t.mode {1754
let mut rows = match ¤t.mode {1697
_ if placeholders => {1755
_ if placeholders => {@@ -373,6 +373,57 @@ impl<R: Remote> SyncEngine<R> {
373
373
Ok(Some(record))
374
374
}
375
375
376
pub async fn open_post(
377
&self,
378
conversation_id: &str,
379
message_id: &str,
380
) -> Result<Option<OpenedPost>> {
381
let Conversation::Channel { team_id } = self.resolve(conversation_id).await? else {
382
return Err(Error::Unsupported("opening a post of a chat"));
383
};
384
let root_ids = [message_id.to_owned()];
385
let (fetched, replies) = tokio::join!(
386
self.fetch_channel_message(&team_id, conversation_id, message_id),
387
self.remote
388
.channel_replies(&team_id, conversation_id, &root_ids)
389
);
390
let message = match fetched {
391
Ok(message) => message,
392
Err(error) => {
393
self.report(format!("cannot open a post: {error}"));
394
return Err(error);
395
}
396
};
397
let Some(record) = message_record(conversation_id, &message) else {
398
return Ok(None);
399
};
400
let replies_loaded = record.reply_to_id.is_none()
401
&& replies
402
.as_ref()
403
.is_ok_and(|listings| listings.iter().all(Result::is_ok));
404
let mut records = vec![record];
405
if replies_loaded {
406
records.extend(
407
replies
408
.into_iter()
409
.flatten()
410
.flatten()
411
.flatten()
412
.filter_map(|reply| message_record(conversation_id, &reply)),
413
);
414
}
415
self.apply_chat_emotions(conversation_id, &mut records).await;
416
let mut record = records[0].clone();
417
let delta = self.ingest(conversation_id, records)?;
418
self.announce_stored_late(conversation_id, &delta);
419
let links_changed = self.attach_links(conversation_id, &mut [&mut record]).await;
420
self.announce(conversation_id, !delta.is_empty() || links_changed);
421
Ok(Some(OpenedPost {
422
record,
423
replies_loaded,
424
}))
425
}
426
376
427
pub async fn attach_link_preview(
377
428
&self,
378
429
conversation_id: &str,
@@ -893,6 +944,12 @@ impl<R: Remote> SyncEngine<R> {
893
944
}
894
945
}
895
946
947
#[derive(Debug, Clone)]
948
pub struct OpenedPost {
949
pub record: MessageRecord,
950
pub replies_loaded: bool,
951
}
952
896
953
fn outgoing(
897
954
conversation: &Conversation,
898
955
html: &str,
@@ -373,6 +373,57 @@ impl<R: Remote> SyncEngine<R> {373
Ok(Some(record))373
Ok(Some(record))374
}374
}375
375
376
pub async fn open_post(377
&self,378
conversation_id: &str,379
message_id: &str,380
) -> Result<Option<OpenedPost>> {381
let Conversation::Channel { team_id } = self.resolve(conversation_id).await? else {382
return Err(Error::Unsupported("opening a post of a chat"));383
};384
let root_ids = [message_id.to_owned()];385
let (fetched, replies) = tokio::join!(386
self.fetch_channel_message(&team_id, conversation_id, message_id),387
self.remote388
.channel_replies(&team_id, conversation_id, &root_ids)389
);390
let message = match fetched {391
Ok(message) => message,392
Err(error) => {393
self.report(format!("cannot open a post: {error}"));394
return Err(error);395
}396
};397
let Some(record) = message_record(conversation_id, &message) else {398
return Ok(None);399
};400
let replies_loaded = record.reply_to_id.is_none()401
&& replies402
.as_ref()403
.is_ok_and(|listings| listings.iter().all(Result::is_ok));404
let mut records = vec![record];405
if replies_loaded {406
records.extend(407
replies408
.into_iter()409
.flatten()410
.flatten()411
.flatten()412
.filter_map(|reply| message_record(conversation_id, &reply)),413
);414
}415
self.apply_chat_emotions(conversation_id, &mut records).await;416
let mut record = records[0].clone();417
let delta = self.ingest(conversation_id, records)?;418
self.announce_stored_late(conversation_id, &delta);419
let links_changed = self.attach_links(conversation_id, &mut [&mut record]).await;420
self.announce(conversation_id, !delta.is_empty() || links_changed);421
Ok(Some(OpenedPost {422
record,423
replies_loaded,424
}))425
}426
376
pub async fn attach_link_preview(427
pub async fn attach_link_preview(377
&self,428
&self,378
conversation_id: &str,429
conversation_id: &str,@@ -893,6 +944,12 @@ impl<R: Remote> SyncEngine<R> {893
}944
}894
}945
}895
946
947
#[derive(Debug, Clone)]948
pub struct OpenedPost {949
pub record: MessageRecord,950
pub replies_loaded: bool,951
}952
896
fn outgoing(953
fn outgoing(897
conversation: &Conversation,954
conversation: &Conversation,898
html: &str,955
html: &str,@@ -70,6 +70,7 @@ mod translate_sync;
70
70
mod translation;
71
71
mod youtube;
72
72
73
pub use actions::OpenedPost;
73
74
pub use activity_sync::stored_activity_feed;
74
75
pub use announcement::{
75
76
ANNOUNCEMENT_APP_ID, ANNOUNCEMENT_BANNER_TYPE, AnnouncementBanner, BannerBackground,
@@ -70,6 +70,7 @@ mod translate_sync;70
mod translation;70
mod translation;71
mod youtube;71
mod youtube;72
72
73
pub use actions::OpenedPost;73
pub use activity_sync::stored_activity_feed;74
pub use activity_sync::stored_activity_feed;74
pub use announcement::{75
pub use announcement::{75
ANNOUNCEMENT_APP_ID, ANNOUNCEMENT_BANNER_TYPE, AnnouncementBanner, BannerBackground,76
ANNOUNCEMENT_APP_ID, ANNOUNCEMENT_BANNER_TYPE, AnnouncementBanner, BannerBackground,@@ -2035,6 +2035,24 @@ async fn refreshing_a_thread_stores_every_reply_of_that_root() {
2035
2035
assert_eq!(thread.len(), 4);
2036
2036
}
2037
2037
2038
#[tokio::test]
2039
async fn opening_an_uncached_post_stores_it_with_its_replies_in_one_go() {
2040
let fake = Arc::new(Fake::default());
2041
let engine = channel_engine(&fake).await;
2042
let root_id = fake.channel_pages.lock().unwrap()[0][0].id.clone();
2043
let reply = |id: u32, minute: i64| -> Message {
2044
let mut reply = message(id, minute, &format!("<p>reply {id}</p>"));
2045
reply.reply_to_id = Some(root_id.clone());
2046
reply
2047
};
2048
*fake.thread_replies.lock().unwrap() = vec![reply(7001, 90), reply(7002, 91)];
2049
let post = engine.open_post(CHANNEL, &root_id).await.unwrap().unwrap();
2050
assert_eq!(post.record.message_id, root_id);
2051
assert!(post.replies_loaded);
2052
let thread = engine.store().thread_messages(CHANNEL, &root_id).unwrap();
2053
assert_eq!(thread.len(), 3);
2054
}
2055
2038
2056
#[tokio::test]
2039
2057
async fn channel_delta_picks_up_new_and_edited_posts_and_stores_the_link() {
2040
2058
let fake = Arc::new(Fake::default());
@@ -2035,6 +2035,24 @@ async fn refreshing_a_thread_stores_every_reply_of_that_root() {2035
assert_eq!(thread.len(), 4);2035
assert_eq!(thread.len(), 4);2036
}2036
}2037
2037
2038
#[tokio::test]2039
async fn opening_an_uncached_post_stores_it_with_its_replies_in_one_go() {2040
let fake = Arc::new(Fake::default());2041
let engine = channel_engine(&fake).await;2042
let root_id = fake.channel_pages.lock().unwrap()[0][0].id.clone();2043
let reply = |id: u32, minute: i64| -> Message {2044
let mut reply = message(id, minute, &format!("<p>reply {id}</p>"));2045
reply.reply_to_id = Some(root_id.clone());2046
reply2047
};2048
*fake.thread_replies.lock().unwrap() = vec![reply(7001, 90), reply(7002, 91)];2049
let post = engine.open_post(CHANNEL, &root_id).await.unwrap().unwrap();2050
assert_eq!(post.record.message_id, root_id);2051
assert!(post.replies_loaded);2052
let thread = engine.store().thread_messages(CHANNEL, &root_id).unwrap();2053
assert_eq!(thread.len(), 3);2054
}2055
2038
#[tokio::test]2056
#[tokio::test]2039
async fn channel_delta_picks_up_new_and_edited_posts_and_stores_the_link() {2057
async fn channel_delta_picks_up_new_and_edited_posts_and_stores_the_link() {2040
let fake = Arc::new(Fake::default());2058
let fake = Arc::new(Fake::default());