feat(chat): drag chats into a slot of Favorites or a section like Teams
- drop on a chat row places the chat at that slot: ReorderItems with the full order inside a folder, AddItem with itemIndex + RemoveItem across folders - moving a chat out of Favorites removes it there too, actions in the Teams order (AddItem, then RemoveItem) - no Pin menu item: Teams hides it while chat sections are on, pinning is Move to Favorites Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -685,19 +685,48 @@ impl AppState {
685
685
cx: &mut Context<Self>,
686
686
) {
687
687
if self.mode.read_only {
688
) {
689
self.place_chat(conversation_id, folder_id, None, cx);
690
}
691
692
pub fn drop_chat_on(
693
&mut self,
694
conversation_id: &str,
695
target_conversation_id: &str,
696
folder_id: &str,
697
cx: &mut Context<Self>,
698
) {
699
if conversation_id == target_conversation_id {
700
return;
701
}
702
let Some(slot) = self.directory.slot_of(folder_id, target_conversation_id) else {
703
return;
704
};
705
self.place_chat(conversation_id, Some(folder_id), Some(slot), cx);
706
}
707
708
pub fn place_chat(
709
&mut self,
710
conversation_id: &str,
711
folder_id: Option<&str>,
712
index: Option<usize>,
713
cx: &mut Context<Self>,
688
714
return;
689
715
}
690
716
let previous_folder = self
691
717
.directory
692
718
.folder_of(conversation_id)
693
719
.map(|folder| folder.id.clone());
694
self.directory.assign(conversation_id, folder_id);
720
self.directory.place(conversation_id, folder_id, index);
695
721
if let Some(engine) = self.engine.clone() {
696
722
let previous = previous_folder.clone();
697
723
let done = match (folder_id, previous.as_deref()) {
698
(Some(folder_id), _) => {
699
Some(data::move_to_folder(&engine, conversation_id, folder_id))
700
}
724
(Some(folder_id), _) => Some(data::move_to_folder(
725
&engine,
726
conversation_id,
727
folder_id,
728
index,
729
)),
701
730
(None, Some(previous)) => {
702
731
Some(data::remove_from_folder(&engine, conversation_id, previous))
703
732
}
@@ -685,19 +685,48 @@ impl AppState {685
cx: &mut Context<Self>,685
cx: &mut Context<Self>,686
) {686
) {687
if self.mode.read_only {687
if self.mode.read_only {688
) {689
self.place_chat(conversation_id, folder_id, None, cx);690
}691
692
pub fn drop_chat_on(693
&mut self,694
conversation_id: &str,695
target_conversation_id: &str,696
folder_id: &str,697
cx: &mut Context<Self>,698
) {699
if conversation_id == target_conversation_id {700
return;701
}702
let Some(slot) = self.directory.slot_of(folder_id, target_conversation_id) else {703
return;704
};705
self.place_chat(conversation_id, Some(folder_id), Some(slot), cx);706
}707
708
pub fn place_chat(709
&mut self,710
conversation_id: &str,711
folder_id: Option<&str>,712
index: Option<usize>,713
cx: &mut Context<Self>,688
return;714
return;689
}715
}690
let previous_folder = self716
let previous_folder = self691
.directory717
.directory692
.folder_of(conversation_id)718
.folder_of(conversation_id)693
.map(|folder| folder.id.clone());719
.map(|folder| folder.id.clone());694
self.directory.assign(conversation_id, folder_id);720
self.directory.place(conversation_id, folder_id, index);695
if let Some(engine) = self.engine.clone() {721
if let Some(engine) = self.engine.clone() {696
let previous = previous_folder.clone();722
let previous = previous_folder.clone();697
let done = match (folder_id, previous.as_deref()) {723
let done = match (folder_id, previous.as_deref()) {698
(Some(folder_id), _) => {724
(Some(folder_id), _) => Some(data::move_to_folder(699
Some(data::move_to_folder(&engine, conversation_id, folder_id))725
&engine,700
}726
conversation_id,727
folder_id,728
index,729
)),701
(None, Some(previous)) => {730
(None, Some(previous)) => {702
Some(data::remove_from_folder(&engine, conversation_id, previous))731
Some(data::remove_from_folder(&engine, conversation_id, previous))703
}732
}@@ -330,16 +330,34 @@ impl Directory {
330
330
}
331
331
332
332
pub fn assign(&mut self, conversation_id: &str, folder_id: Option<&str>) {
333
self.place(conversation_id, folder_id, None);
334
}
335
336
pub fn place(&mut self, conversation_id: &str, folder_id: Option<&str>, index: Option<usize>) {
333
337
for folder in &mut self.folders {
334
338
folder.conversation_ids.retain(|id| id != conversation_id);
335
339
}
336
340
if let Some(folder) =
337
341
folder_id.and_then(|id| self.folders.iter_mut().find(|folder| folder.id == id))
338
342
{
339
folder.conversation_ids.push(conversation_id.to_owned());
343
let position = index.map_or(folder.conversation_ids.len(), |index| {
344
index.min(folder.conversation_ids.len())
345
});
346
folder
347
.conversation_ids
348
.insert(position, conversation_id.to_owned());
340
349
}
341
350
}
342
351
352
pub fn slot_of(&self, folder_id: &str, conversation_id: &str) -> Option<usize> {
353
self.folders
354
.iter()
355
.find(|folder| folder.id == folder_id)?
356
.conversation_ids
357
.iter()
358
.position(|id| id == conversation_id)
359
}
360
343
361
pub fn move_targets(&self) -> Vec<(String, String)> {
344
362
let favorites = self.favorites().map(|folder| {
345
363
let name = folder.name.trim();
@@ -643,13 +661,22 @@ pub fn watch_presence(engine: &Arc<Engine>, user_ids: Vec<String>) -> Done {
643
661
run(async move { engine.watch_presence(&user_ids).await })
644
662
}
645
663
646
pub fn move_to_folder(engine: &Arc<Engine>, conversation_id: &str, folder_id: &str) -> Done {
664
pub fn move_to_folder(
665
engine: &Arc<Engine>,
666
conversation_id: &str,
667
folder_id: &str,
668
index: Option<usize>,
669
) -> Done {
647
670
let (engine, conversation_id, folder_id) = (
648
671
engine.clone(),
649
672
conversation_id.to_owned(),
650
673
folder_id.to_owned(),
651
674
);
652
run(async move { engine.move_to_folder(&conversation_id, &folder_id).await })
675
run(async move {
676
engine
677
.move_to_folder(&conversation_id, &folder_id, index)
678
.await
679
})
653
680
}
654
681
655
682
pub fn remove_from_folder(engine: &Arc<Engine>, conversation_id: &str, folder_id: &str) -> Done {
@@ -715,6 +742,29 @@ mod tests {
715
742
}
716
743
}
717
744
745
#[test]
746
fn place_puts_a_chat_at_the_slot_inside_and_across_folders() {
747
let mut directory = Directory {
748
folders: vec![
749
folder("fav", FolderKind::Favorites, &["a", "b", "c"]),
750
folder("work", FolderKind::UserCreated, &["d"]),
751
],
752
..Default::default()
753
};
754
directory.place("a", Some("fav"), Some(2));
755
assert_eq!(directory.folders[0].conversation_ids, vec!["b", "c", "a"]);
756
directory.place("d", Some("fav"), Some(0));
757
assert_eq!(
758
directory.folders[0].conversation_ids,
759
vec!["d", "b", "c", "a"]
760
);
761
assert!(directory.folders[1].conversation_ids.is_empty());
762
directory.place("b", Some("work"), Some(9));
763
assert_eq!(directory.folders[1].conversation_ids, vec!["b"]);
764
assert_eq!(directory.slot_of("fav", "c"), Some(1));
765
assert_eq!(directory.slot_of("work", "c"), None);
766
}
767
718
768
#[test]
719
769
fn assign_moves_a_chat_between_folders() {
720
770
let mut directory = Directory {
@@ -330,16 +330,34 @@ impl Directory {330
}330
}331
331
332
pub fn assign(&mut self, conversation_id: &str, folder_id: Option<&str>) {332
pub fn assign(&mut self, conversation_id: &str, folder_id: Option<&str>) {333
self.place(conversation_id, folder_id, None);334
}335
336
pub fn place(&mut self, conversation_id: &str, folder_id: Option<&str>, index: Option<usize>) {333
for folder in &mut self.folders {337
for folder in &mut self.folders {334
folder.conversation_ids.retain(|id| id != conversation_id);338
folder.conversation_ids.retain(|id| id != conversation_id);335
}339
}336
if let Some(folder) =340
if let Some(folder) =337
folder_id.and_then(|id| self.folders.iter_mut().find(|folder| folder.id == id))341
folder_id.and_then(|id| self.folders.iter_mut().find(|folder| folder.id == id))338
{342
{339
folder.conversation_ids.push(conversation_id.to_owned());343
let position = index.map_or(folder.conversation_ids.len(), |index| {344
index.min(folder.conversation_ids.len())345
});346
folder347
.conversation_ids348
.insert(position, conversation_id.to_owned());340
}349
}341
}350
}342
351
352
pub fn slot_of(&self, folder_id: &str, conversation_id: &str) -> Option<usize> {353
self.folders354
.iter()355
.find(|folder| folder.id == folder_id)?356
.conversation_ids357
.iter()358
.position(|id| id == conversation_id)359
}360
343
pub fn move_targets(&self) -> Vec<(String, String)> {361
pub fn move_targets(&self) -> Vec<(String, String)> {344
let favorites = self.favorites().map(|folder| {362
let favorites = self.favorites().map(|folder| {345
let name = folder.name.trim();363
let name = folder.name.trim();@@ -643,13 +661,22 @@ pub fn watch_presence(engine: &Arc<Engine>, user_ids: Vec<String>) -> Done {643
run(async move { engine.watch_presence(&user_ids).await })661
run(async move { engine.watch_presence(&user_ids).await })644
}662
}645
663
646
pub fn move_to_folder(engine: &Arc<Engine>, conversation_id: &str, folder_id: &str) -> Done {664
pub fn move_to_folder(665
engine: &Arc<Engine>,666
conversation_id: &str,667
folder_id: &str,668
index: Option<usize>,669
) -> Done {647
let (engine, conversation_id, folder_id) = (670
let (engine, conversation_id, folder_id) = (648
engine.clone(),671
engine.clone(),649
conversation_id.to_owned(),672
conversation_id.to_owned(),650
folder_id.to_owned(),673
folder_id.to_owned(),651
);674
);652
run(async move { engine.move_to_folder(&conversation_id, &folder_id).await })675
run(async move {676
engine677
.move_to_folder(&conversation_id, &folder_id, index)678
.await679
})653
}680
}654
681
655
pub fn remove_from_folder(engine: &Arc<Engine>, conversation_id: &str, folder_id: &str) -> Done {682
pub fn remove_from_folder(engine: &Arc<Engine>, conversation_id: &str, folder_id: &str) -> Done {@@ -715,6 +742,29 @@ mod tests {715
}742
}716
}743
}717
744
745
#[test]746
fn place_puts_a_chat_at_the_slot_inside_and_across_folders() {747
let mut directory = Directory {748
folders: vec![749
folder("fav", FolderKind::Favorites, &["a", "b", "c"]),750
folder("work", FolderKind::UserCreated, &["d"]),751
],752
..Default::default()753
};754
directory.place("a", Some("fav"), Some(2));755
assert_eq!(directory.folders[0].conversation_ids, vec!["b", "c", "a"]);756
directory.place("d", Some("fav"), Some(0));757
assert_eq!(758
directory.folders[0].conversation_ids,759
vec!["d", "b", "c", "a"]760
);761
assert!(directory.folders[1].conversation_ids.is_empty());762
directory.place("b", Some("work"), Some(9));763
assert_eq!(directory.folders[1].conversation_ids, vec!["b"]);764
assert_eq!(directory.slot_of("fav", "c"), Some(1));765
assert_eq!(directory.slot_of("work", "c"), None);766
}767
718
#[test]768
#[test]719
fn assign_moves_a_chat_between_folders() {769
fn assign_moves_a_chat_between_folders() {720
let mut directory = Directory {770
let mut directory = Directory {@@ -1004,6 +1004,10 @@ impl SidebarView {
1004
1004
title: item.title.clone(),
1005
1005
};
1006
1006
let menu_context = menu.clone();
1007
let reorder_folder = item
1008
.folder_id
1009
.clone()
1010
.filter(|folder_id| !is_pending_folder(folder_id));
1007
1011
let target = ChatMenuTarget {
1008
1012
id: item.id.clone(),
1009
1013
title: item.title.clone(),
@@ -1029,6 +1033,15 @@ impl SidebarView {
1029
1033
let title = dragged.title.clone();
1030
1034
cx.new(|_| DragGhost { title })
1031
1035
})
1036
.when_some(reorder_folder, |row, folder_id| {
1037
let (drop_state, target_id) = (self.state.clone(), item.id.clone());
1038
row.drag_over::<DraggedChat>(|style, _, _, _| style.bg(theme::drop_background()))
1039
.on_drop(move |dragged: &DraggedChat, _, cx| {
1040
drop_state.update(cx, |state, cx| {
1041
state.drop_chat_on(&dragged.id, &target_id, &folder_id, cx)
1042
});
1043
})
1044
})
1032
1045
.child(avatar)
1033
1046
.child(lines)
1034
1047
.on_click(move |_, _, cx| {
@@ -1004,6 +1004,10 @@ impl SidebarView {1004
title: item.title.clone(),1004
title: item.title.clone(),1005
};1005
};1006
let menu_context = menu.clone();1006
let menu_context = menu.clone();1007
let reorder_folder = item1008
.folder_id1009
.clone()1010
.filter(|folder_id| !is_pending_folder(folder_id));1007
let target = ChatMenuTarget {1011
let target = ChatMenuTarget {1008
id: item.id.clone(),1012
id: item.id.clone(),1009
title: item.title.clone(),1013
title: item.title.clone(),@@ -1029,6 +1033,15 @@ impl SidebarView {1029
let title = dragged.title.clone();1033
let title = dragged.title.clone();1030
cx.new(|_| DragGhost { title })1034
cx.new(|_| DragGhost { title })1031
})1035
})1036
.when_some(reorder_folder, |row, folder_id| {1037
let (drop_state, target_id) = (self.state.clone(), item.id.clone());1038
row.drag_over::<DraggedChat>(|style, _, _, _| style.bg(theme::drop_background()))1039
.on_drop(move |dragged: &DraggedChat, _, cx| {1040
drop_state.update(cx, |state, cx| {1041
state.drop_chat_on(&dragged.id, &target_id, &folder_id, cx)1042
});1043
})1044
})1032
.child(avatar)1045
.child(avatar)1033
.child(lines)1046
.child(lines)1034
.on_click(move |_, _, cx| {1047
.on_click(move |_, _, cx| {@@ -51,7 +51,7 @@ impl Folders {
51
51
52
52
fn containing(&self, conversation_id: &str) -> Option<&Folder> {
53
53
self.folders.iter().find(|folder| {
54
folder.kind == FolderKind::UserCreated
54
matches!(folder.kind, FolderKind::Favorites | FolderKind::UserCreated)
55
55
&& folder
56
56
.conversation_ids
57
57
.iter()
@@ -143,8 +143,54 @@ fn expanded_body(hierarchy_version: i64, expanded: bool) -> Value {
143
143
})
144
144
}
145
145
146
fn add_item(folder_id: &str, conversation_id: &str) -> Value {
147
json!({"action": "AddItem", "folderId": folder_id, "itemId": conversation_id})
146
fn add_item(folder_id: &str, conversation_id: &str, index: Option<usize>) -> Value {
147
let mut action = json!({"action": "AddItem", "folderId": folder_id, "itemId": conversation_id});
148
if let Some(index) = index {
149
action["itemIndex"] = json!(index);
150
}
151
action
152
}
153
154
fn reorder_items(folder_id: &str, conversation_ids: Vec<String>) -> Value {
155
json!({"action": "ReorderItems", "folderId": folder_id, "newItemsOrder": conversation_ids})
156
}
157
158
fn placed_order(conversation_ids: &[String], conversation_id: &str, index: usize) -> Vec<String> {
159
let mut order: Vec<String> = conversation_ids
160
.iter()
161
.filter(|id| *id != conversation_id)
162
.cloned()
163
.collect();
164
order.insert(index.min(order.len()), conversation_id.to_owned());
165
order
166
}
167
168
fn place_actions(
169
state: &Folders,
170
conversation_id: &str,
171
target_folder_id: &str,
172
index: Option<usize>,
173
) -> Result<Vec<Value>> {
174
let target = state.find(target_folder_id)?;
175
if target
176
.conversation_ids
177
.iter()
178
.any(|id| id == conversation_id)
179
{
180
let Some(index) = index else {
181
return Ok(Vec::new());
182
};
183
let order = placed_order(&target.conversation_ids, conversation_id, index);
184
if order == target.conversation_ids {
185
return Ok(Vec::new());
186
}
187
return Ok(vec![reorder_items(target_folder_id, order)]);
188
}
189
let mut actions = vec![add_item(target_folder_id, conversation_id, index)];
190
if let Some(current) = state.containing(conversation_id) {
191
actions.push(remove_item(¤t.id, conversation_id));
192
}
193
Ok(actions)
148
194
}
149
195
150
196
fn remove_item(folder_id: &str, conversation_id: &str) -> Value {
@@ -220,28 +266,10 @@ impl<T: CsaTransport> Pins<T> {
220
266
&self,
221
267
conversation_id: &str,
222
268
target_folder_id: &str,
269
index: Option<usize>,
223
270
) -> Result<Folders> {
224
self.write_folders(|state| {
225
let target = state
226
.folders
227
.iter()
228
.find(|folder| folder.id == target_folder_id)
229
.ok_or_else(|| Error::UnknownFolder(target_folder_id.to_owned()))?;
230
if target
231
.conversation_ids
232
.iter()
233
.any(|id| id == conversation_id)
234
{
235
return Ok(Vec::new());
236
}
237
let mut actions = Vec::new();
238
if let Some(current) = state.containing(conversation_id) {
239
actions.push(remove_item(¤t.id, conversation_id));
240
}
241
actions.push(add_item(target_folder_id, conversation_id));
242
Ok(actions)
243
})
244
.await
271
self.write_folders(|state| place_actions(state, conversation_id, target_folder_id, index))
272
.await
245
273
}
246
274
247
275
pub async fn remove_from_folder(
@@ -491,6 +519,72 @@ mod tests {
491
519
));
492
520
}
493
521
522
fn state(folders: &[(&str, FolderKind, &[&str])]) -> Folders {
523
Folders {
524
hierarchy_version: 1,
525
folders: folders
526
.iter()
527
.map(|(id, kind, ids)| Folder {
528
id: (*id).to_owned(),
529
name: (*id).to_owned(),
530
kind: *kind,
531
expanded: true,
532
conversation_ids: ids.iter().map(|id| (*id).to_owned()).collect(),
533
})
534
.collect(),
535
order: Vec::new(),
536
}
537
}
538
539
#[test]
540
fn placing_inside_the_same_folder_sends_the_full_new_order() {
541
let folders = state(&[("fav", FolderKind::Favorites, &["a", "b", "c", "d"])]);
542
assert_eq!(
543
place_actions(&folders, "a", "fav", Some(2)).unwrap(),
544
[
545
json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["b", "c", "a", "d"]})
546
]
547
);
548
assert_eq!(
549
place_actions(&folders, "d", "fav", Some(1)).unwrap(),
550
[
551
json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["a", "d", "b", "c"]})
552
]
553
);
554
assert!(
555
place_actions(&folders, "b", "fav", Some(1))
556
.unwrap()
557
.is_empty()
558
);
559
assert!(
560
place_actions(&folders, "b", "fav", None)
561
.unwrap()
562
.is_empty()
563
);
564
}
565
566
#[test]
567
fn moving_out_of_favorites_adds_at_the_index_then_removes_like_teams() {
568
let folders = state(&[
569
("fav", FolderKind::Favorites, &["a"]),
570
("work", FolderKind::UserCreated, &["b", "c"]),
571
]);
572
assert_eq!(
573
place_actions(&folders, "a", "work", Some(1)).unwrap(),
574
[
575
json!({"action": "AddItem", "folderId": "work", "itemId": "a", "itemIndex": 1}),
576
json!({"action": "RemoveItem", "folderId": "fav", "itemId": "a"}),
577
]
578
);
579
assert_eq!(
580
place_actions(&folders, "b", "fav", None).unwrap(),
581
[
582
json!({"action": "AddItem", "folderId": "fav", "itemId": "b"}),
583
json!({"action": "RemoveItem", "folderId": "work", "itemId": "b"}),
584
]
585
);
586
}
587
494
588
#[test]
495
589
fn write_body_puts_the_lock_before_the_fields() {
496
590
let write = FolderWrite::reread(Method::Put, Some("a"), json!({"folderName": "B"}));
@@ -51,7 +51,7 @@ impl Folders {51
51
52
fn containing(&self, conversation_id: &str) -> Option<&Folder> {52
fn containing(&self, conversation_id: &str) -> Option<&Folder> {53
self.folders.iter().find(|folder| {53
self.folders.iter().find(|folder| {54
folder.kind == FolderKind::UserCreated54
matches!(folder.kind, FolderKind::Favorites | FolderKind::UserCreated)55
&& folder55
&& folder56
.conversation_ids56
.conversation_ids57
.iter()57
.iter()@@ -143,8 +143,54 @@ fn expanded_body(hierarchy_version: i64, expanded: bool) -> Value {143
})143
})144
}144
}145
145
146
fn add_item(folder_id: &str, conversation_id: &str) -> Value {146
fn add_item(folder_id: &str, conversation_id: &str, index: Option<usize>) -> Value {147
json!({"action": "AddItem", "folderId": folder_id, "itemId": conversation_id})147
let mut action = json!({"action": "AddItem", "folderId": folder_id, "itemId": conversation_id});148
if let Some(index) = index {149
action["itemIndex"] = json!(index);150
}151
action152
}153
154
fn reorder_items(folder_id: &str, conversation_ids: Vec<String>) -> Value {155
json!({"action": "ReorderItems", "folderId": folder_id, "newItemsOrder": conversation_ids})156
}157
158
fn placed_order(conversation_ids: &[String], conversation_id: &str, index: usize) -> Vec<String> {159
let mut order: Vec<String> = conversation_ids160
.iter()161
.filter(|id| *id != conversation_id)162
.cloned()163
.collect();164
order.insert(index.min(order.len()), conversation_id.to_owned());165
order166
}167
168
fn place_actions(169
state: &Folders,170
conversation_id: &str,171
target_folder_id: &str,172
index: Option<usize>,173
) -> Result<Vec<Value>> {174
let target = state.find(target_folder_id)?;175
if target176
.conversation_ids177
.iter()178
.any(|id| id == conversation_id)179
{180
let Some(index) = index else {181
return Ok(Vec::new());182
};183
let order = placed_order(&target.conversation_ids, conversation_id, index);184
if order == target.conversation_ids {185
return Ok(Vec::new());186
}187
return Ok(vec![reorder_items(target_folder_id, order)]);188
}189
let mut actions = vec![add_item(target_folder_id, conversation_id, index)];190
if let Some(current) = state.containing(conversation_id) {191
actions.push(remove_item(¤t.id, conversation_id));192
}193
Ok(actions)148
}194
}149
195
150
fn remove_item(folder_id: &str, conversation_id: &str) -> Value {196
fn remove_item(folder_id: &str, conversation_id: &str) -> Value {@@ -220,28 +266,10 @@ impl<T: CsaTransport> Pins<T> {220
&self,266
&self,221
conversation_id: &str,267
conversation_id: &str,222
target_folder_id: &str,268
target_folder_id: &str,269
index: Option<usize>,223
) -> Result<Folders> {270
) -> Result<Folders> {224
self.write_folders(|state| {271
self.write_folders(|state| place_actions(state, conversation_id, target_folder_id, index))225
let target = state272
.await226
.folders227
.iter()228
.find(|folder| folder.id == target_folder_id)229
.ok_or_else(|| Error::UnknownFolder(target_folder_id.to_owned()))?;230
if target231
.conversation_ids232
.iter()233
.any(|id| id == conversation_id)234
{235
return Ok(Vec::new());236
}237
let mut actions = Vec::new();238
if let Some(current) = state.containing(conversation_id) {239
actions.push(remove_item(¤t.id, conversation_id));240
}241
actions.push(add_item(target_folder_id, conversation_id));242
Ok(actions)243
})244
.await245
}273
}246
274
247
pub async fn remove_from_folder(275
pub async fn remove_from_folder(@@ -491,6 +519,72 @@ mod tests {491
));519
));492
}520
}493
521
522
fn state(folders: &[(&str, FolderKind, &[&str])]) -> Folders {523
Folders {524
hierarchy_version: 1,525
folders: folders526
.iter()527
.map(|(id, kind, ids)| Folder {528
id: (*id).to_owned(),529
name: (*id).to_owned(),530
kind: *kind,531
expanded: true,532
conversation_ids: ids.iter().map(|id| (*id).to_owned()).collect(),533
})534
.collect(),535
order: Vec::new(),536
}537
}538
539
#[test]540
fn placing_inside_the_same_folder_sends_the_full_new_order() {541
let folders = state(&[("fav", FolderKind::Favorites, &["a", "b", "c", "d"])]);542
assert_eq!(543
place_actions(&folders, "a", "fav", Some(2)).unwrap(),544
[545
json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["b", "c", "a", "d"]})546
]547
);548
assert_eq!(549
place_actions(&folders, "d", "fav", Some(1)).unwrap(),550
[551
json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["a", "d", "b", "c"]})552
]553
);554
assert!(555
place_actions(&folders, "b", "fav", Some(1))556
.unwrap()557
.is_empty()558
);559
assert!(560
place_actions(&folders, "b", "fav", None)561
.unwrap()562
.is_empty()563
);564
}565
566
#[test]567
fn moving_out_of_favorites_adds_at_the_index_then_removes_like_teams() {568
let folders = state(&[569
("fav", FolderKind::Favorites, &["a"]),570
("work", FolderKind::UserCreated, &["b", "c"]),571
]);572
assert_eq!(573
place_actions(&folders, "a", "work", Some(1)).unwrap(),574
[575
json!({"action": "AddItem", "folderId": "work", "itemId": "a", "itemIndex": 1}),576
json!({"action": "RemoveItem", "folderId": "fav", "itemId": "a"}),577
]578
);579
assert_eq!(580
place_actions(&folders, "b", "fav", None).unwrap(),581
[582
json!({"action": "AddItem", "folderId": "fav", "itemId": "b"}),583
json!({"action": "RemoveItem", "folderId": "work", "itemId": "b"}),584
]585
);586
}587
494
#[test]588
#[test]495
fn write_body_puts_the_lock_before_the_fields() {589
fn write_body_puts_the_lock_before_the_fields() {496
let write = FolderWrite::reread(Method::Put, Some("a"), json!({"folderName": "B"}));590
let write = FolderWrite::reread(Method::Put, Some("a"), json!({"folderName": "B"}));@@ -384,20 +384,23 @@ fn sections(version: i64, custom: &[(&str, &[&str])], pinned: &[&str]) -> Value
384
384
}
385
385
386
386
#[tokio::test]
387
async fn move_to_folder_removes_from_the_current_custom_folder_then_adds_in_one_request() {
387
async fn move_to_folder_adds_then_removes_from_the_current_folder_in_one_request() {
388
388
let mock = Mock::new(vec![
389
389
(200, sections(10, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),
390
390
(200, sections(11, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),
391
391
]);
392
let state = pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();
392
let state = pins(&mock)
393
.move_to_folder("c", "t~u~B", None)
394
.await
395
.unwrap();
393
396
assert_eq!(state.folders[2].conversation_ids, ["c"]);
394
397
let body = sent(&mock)[1].2.clone().unwrap();
395
398
assert_eq!(body["folderHierarchyVersion"], 10);
396
399
assert_eq!(
397
400
body["actions"],
398
401
json!([
399
{"action": "RemoveItem", "folderId": "t~u~A", "itemId": "c"},
400
{"action": "AddItem", "folderId": "t~u~B", "itemId": "c"}
402
{"action": "AddItem", "folderId": "t~u~B", "itemId": "c"},
403
{"action": "RemoveItem", "folderId": "t~u~A", "itemId": "c"}
401
404
])
402
405
);
403
406
}
@@ -408,7 +411,10 @@ async fn move_to_folder_without_a_current_folder_only_adds() {
408
411
(200, sections(10, &[("t~u~B", &[])], &[])),
409
412
(200, sections(11, &[("t~u~B", &["c"])], &[])),
410
413
]);
411
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();
414
pins(&mock)
415
.move_to_folder("c", "t~u~B", None)
416
.await
417
.unwrap();
412
418
assert_eq!(
413
419
sent(&mock)[1].2.as_ref().unwrap()["actions"]
414
420
.as_array()
@@ -425,7 +431,10 @@ async fn move_to_folder_retries_once_and_replans_from_the_412_state() {
425
431
(412, sections(15, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),
426
432
(200, sections(16, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),
427
433
]);
428
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();
434
pins(&mock)
435
.move_to_folder("c", "t~u~B", None)
436
.await
437
.unwrap();
429
438
let retry = sent(&mock)[2].2.clone().unwrap();
430
439
assert_eq!(retry["folderHierarchyVersion"], 15);
431
440
assert_eq!(retry["actions"].as_array().unwrap().len(), 2);
@@ -434,14 +443,20 @@ async fn move_to_folder_retries_once_and_replans_from_the_412_state() {
434
443
#[tokio::test]
435
444
async fn move_into_the_folder_it_is_already_in_sends_nothing() {
436
445
let mock = Mock::new(vec![(200, sections(10, &[("t~u~B", &["c"])], &[]))]);
437
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();
446
pins(&mock)
447
.move_to_folder("c", "t~u~B", None)
448
.await
449
.unwrap();
438
450
assert_eq!(sent(&mock).len(), 1);
439
451
}
440
452
441
453
#[tokio::test]
442
454
async fn unknown_target_folder_is_typed() {
443
455
let mock = Mock::new(vec![(200, sections(10, &[], &[]))]);
444
let error = pins(&mock).move_to_folder("c", "nope").await.unwrap_err();
456
let error = pins(&mock)
457
.move_to_folder("c", "nope", None)
458
.await
459
.unwrap_err();
445
460
assert!(matches!(error, Error::UnknownFolder(_)));
446
461
}
447
462
@@ -584,13 +599,19 @@ async fn folder_action_posts_still_take_a_full_answer_as_state() {
584
599
(200, partial),
585
600
(200, sections(11, &[("t~u~B", &["c"])], &[])),
586
601
]);
587
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();
602
pins(&mock)
603
.move_to_folder("c", "t~u~B", None)
604
.await
605
.unwrap();
588
606
assert_eq!(sent(&mock).len(), 3);
589
607
let idle = Mock::new(vec![
590
608
(200, sections(10, &[("t~u~B", &[])], &[])),
591
609
(200, sections(11, &[("t~u~B", &["c"])], &[])),
592
610
]);
593
pins(&idle).move_to_folder("c", "t~u~B").await.unwrap();
611
pins(&idle)
612
.move_to_folder("c", "t~u~B", None)
613
.await
614
.unwrap();
594
615
assert_eq!(sent(&idle).len(), 2);
595
616
}
596
617
@@ -384,20 +384,23 @@ fn sections(version: i64, custom: &[(&str, &[&str])], pinned: &[&str]) -> Value384
}384
}385
385
386
#[tokio::test]386
#[tokio::test]387
async fn move_to_folder_removes_from_the_current_custom_folder_then_adds_in_one_request() {387
async fn move_to_folder_adds_then_removes_from_the_current_folder_in_one_request() {388
let mock = Mock::new(vec![388
let mock = Mock::new(vec![389
(200, sections(10, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),389
(200, sections(10, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),390
(200, sections(11, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),390
(200, sections(11, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),391
]);391
]);392
let state = pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();392
let state = pins(&mock)393
.move_to_folder("c", "t~u~B", None)394
.await395
.unwrap();393
assert_eq!(state.folders[2].conversation_ids, ["c"]);396
assert_eq!(state.folders[2].conversation_ids, ["c"]);394
let body = sent(&mock)[1].2.clone().unwrap();397
let body = sent(&mock)[1].2.clone().unwrap();395
assert_eq!(body["folderHierarchyVersion"], 10);398
assert_eq!(body["folderHierarchyVersion"], 10);396
assert_eq!(399
assert_eq!(397
body["actions"],400
body["actions"],398
json!([401
json!([399
{"action": "RemoveItem", "folderId": "t~u~A", "itemId": "c"},402
{"action": "AddItem", "folderId": "t~u~B", "itemId": "c"},400
{"action": "AddItem", "folderId": "t~u~B", "itemId": "c"}403
{"action": "RemoveItem", "folderId": "t~u~A", "itemId": "c"}401
])404
])402
);405
);403
}406
}@@ -408,7 +411,10 @@ async fn move_to_folder_without_a_current_folder_only_adds() {408
(200, sections(10, &[("t~u~B", &[])], &[])),411
(200, sections(10, &[("t~u~B", &[])], &[])),409
(200, sections(11, &[("t~u~B", &["c"])], &[])),412
(200, sections(11, &[("t~u~B", &["c"])], &[])),410
]);413
]);411
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();414
pins(&mock)415
.move_to_folder("c", "t~u~B", None)416
.await417
.unwrap();412
assert_eq!(418
assert_eq!(413
sent(&mock)[1].2.as_ref().unwrap()["actions"]419
sent(&mock)[1].2.as_ref().unwrap()["actions"]414
.as_array()420
.as_array()@@ -425,7 +431,10 @@ async fn move_to_folder_retries_once_and_replans_from_the_412_state() {425
(412, sections(15, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),431
(412, sections(15, &[("t~u~A", &["c"]), ("t~u~B", &[])], &[])),426
(200, sections(16, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),432
(200, sections(16, &[("t~u~A", &[]), ("t~u~B", &["c"])], &[])),427
]);433
]);428
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();434
pins(&mock)435
.move_to_folder("c", "t~u~B", None)436
.await437
.unwrap();429
let retry = sent(&mock)[2].2.clone().unwrap();438
let retry = sent(&mock)[2].2.clone().unwrap();430
assert_eq!(retry["folderHierarchyVersion"], 15);439
assert_eq!(retry["folderHierarchyVersion"], 15);431
assert_eq!(retry["actions"].as_array().unwrap().len(), 2);440
assert_eq!(retry["actions"].as_array().unwrap().len(), 2);@@ -434,14 +443,20 @@ async fn move_to_folder_retries_once_and_replans_from_the_412_state() {434
#[tokio::test]443
#[tokio::test]435
async fn move_into_the_folder_it_is_already_in_sends_nothing() {444
async fn move_into_the_folder_it_is_already_in_sends_nothing() {436
let mock = Mock::new(vec![(200, sections(10, &[("t~u~B", &["c"])], &[]))]);445
let mock = Mock::new(vec![(200, sections(10, &[("t~u~B", &["c"])], &[]))]);437
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();446
pins(&mock)447
.move_to_folder("c", "t~u~B", None)448
.await449
.unwrap();438
assert_eq!(sent(&mock).len(), 1);450
assert_eq!(sent(&mock).len(), 1);439
}451
}440
452
441
#[tokio::test]453
#[tokio::test]442
async fn unknown_target_folder_is_typed() {454
async fn unknown_target_folder_is_typed() {443
let mock = Mock::new(vec![(200, sections(10, &[], &[]))]);455
let mock = Mock::new(vec![(200, sections(10, &[], &[]))]);444
let error = pins(&mock).move_to_folder("c", "nope").await.unwrap_err();456
let error = pins(&mock)457
.move_to_folder("c", "nope", None)458
.await459
.unwrap_err();445
assert!(matches!(error, Error::UnknownFolder(_)));460
assert!(matches!(error, Error::UnknownFolder(_)));446
}461
}447
462
@@ -584,13 +599,19 @@ async fn folder_action_posts_still_take_a_full_answer_as_state() {584
(200, partial),599
(200, partial),585
(200, sections(11, &[("t~u~B", &["c"])], &[])),600
(200, sections(11, &[("t~u~B", &["c"])], &[])),586
]);601
]);587
pins(&mock).move_to_folder("c", "t~u~B").await.unwrap();602
pins(&mock)603
.move_to_folder("c", "t~u~B", None)604
.await605
.unwrap();588
assert_eq!(sent(&mock).len(), 3);606
assert_eq!(sent(&mock).len(), 3);589
let idle = Mock::new(vec![607
let idle = Mock::new(vec![590
(200, sections(10, &[("t~u~B", &[])], &[])),608
(200, sections(10, &[("t~u~B", &[])], &[])),591
(200, sections(11, &[("t~u~B", &["c"])], &[])),609
(200, sections(11, &[("t~u~B", &["c"])], &[])),592
]);610
]);593
pins(&idle).move_to_folder("c", "t~u~B").await.unwrap();611
pins(&idle)612
.move_to_folder("c", "t~u~B", None)613
.await614
.unwrap();594
assert_eq!(sent(&idle).len(), 2);615
assert_eq!(sent(&idle).len(), 2);595
}616
}596
617
@@ -81,6 +81,7 @@ pub trait FolderSource: Send + Sync {
81
81
&'a self,
82
82
conversation_id: &'a str,
83
83
target_folder_id: &'a str,
84
index: Option<usize>,
84
85
) -> BoxFuture<'a, Result<Vec<ChatFolder>>>;
85
86
fn remove_from_folder<'a>(
86
87
&'a self,
@@ -204,11 +205,12 @@ impl FolderSource for ChatsvcFolderSource {
204
205
&'a self,
205
206
conversation_id: &'a str,
206
207
target_folder_id: &'a str,
208
index: Option<usize>,
207
209
) -> BoxFuture<'a, Result<Vec<ChatFolder>>> {
208
210
Box::pin(async move {
209
211
Ok(convert(
210
212
self.pins
211
.move_to_folder(conversation_id, target_folder_id)
213
.move_to_folder(conversation_id, target_folder_id, index)
212
214
.await?,
213
215
))
214
216
})
@@ -543,10 +545,11 @@ impl<R: Remote> SyncEngine<R> {
543
545
&self,
544
546
conversation_id: &str,
545
547
target_folder_id: &str,
548
index: Option<usize>,
546
549
) -> Result<()> {
547
550
let folders = self
548
551
.folder_source()?
549
.move_to_folder(conversation_id, target_folder_id)
552
.move_to_folder(conversation_id, target_folder_id, index)
550
553
.await?;
551
554
self.store_folders(&folders, &self.store.pinned_channel_ids()?)
552
555
}
@@ -81,6 +81,7 @@ pub trait FolderSource: Send + Sync {81
&'a self,81
&'a self,82
conversation_id: &'a str,82
conversation_id: &'a str,83
target_folder_id: &'a str,83
target_folder_id: &'a str,84
index: Option<usize>,84
) -> BoxFuture<'a, Result<Vec<ChatFolder>>>;85
) -> BoxFuture<'a, Result<Vec<ChatFolder>>>;85
fn remove_from_folder<'a>(86
fn remove_from_folder<'a>(86
&'a self,87
&'a self,@@ -204,11 +205,12 @@ impl FolderSource for ChatsvcFolderSource {204
&'a self,205
&'a self,205
conversation_id: &'a str,206
conversation_id: &'a str,206
target_folder_id: &'a str,207
target_folder_id: &'a str,208
index: Option<usize>,207
) -> BoxFuture<'a, Result<Vec<ChatFolder>>> {209
) -> BoxFuture<'a, Result<Vec<ChatFolder>>> {208
Box::pin(async move {210
Box::pin(async move {209
Ok(convert(211
Ok(convert(210
self.pins212
self.pins211
.move_to_folder(conversation_id, target_folder_id)213
.move_to_folder(conversation_id, target_folder_id, index)212
.await?,214
.await?,213
))215
))214
})216
})@@ -543,10 +545,11 @@ impl<R: Remote> SyncEngine<R> {543
&self,545
&self,544
conversation_id: &str,546
conversation_id: &str,545
target_folder_id: &str,547
target_folder_id: &str,548
index: Option<usize>,546
) -> Result<()> {549
) -> Result<()> {547
let folders = self550
let folders = self548
.folder_source()?551
.folder_source()?549
.move_to_folder(conversation_id, target_folder_id)552
.move_to_folder(conversation_id, target_folder_id, index)550
.await?;553
.await?;551
self.store_folders(&folders, &self.store.pinned_channel_ids()?)554
self.store_folders(&folders, &self.store.pinned_channel_ids()?)552
}555
}@@ -3037,6 +3037,7 @@ impl FolderSource for FakeFolders {
3037
3037
for folder in state
3038
3038
.iter_mut()
3039
3039
.filter(|folder| folder.kind == FolderKind::UserCreated)
3040
_index: Option<usize>,
3040
3041
{
3041
3042
folder.conversation_ids.retain(|id| id != conversation_id);
3042
3043
}
@@ -3181,7 +3182,7 @@ async fn folders_refresh_move_and_remove_go_through_the_source_and_cache() {
3181
3182
engine.refresh_folders().await.unwrap();
3182
3183
assert!(events.try_recv().is_err());
3183
3184
3184
engine.move_to_folder("a", "work").await.unwrap();
3185
engine.move_to_folder("a", "work", None).await.unwrap();
3185
3186
assert_eq!(
3186
3187
engine.chat_folders().unwrap()[1].conversation_ids,
3187
3188
ids(&["c", "a"])
@@ -3445,7 +3446,7 @@ async fn folders_without_a_source_are_unsupported() {
3445
3446
Err(Error::Unsupported(_))
3446
3447
));
3447
3448
assert!(matches!(
3448
engine.move_to_folder("a", "b").await,
3449
engine.move_to_folder("a", "b", None).await,
3449
3450
Err(Error::Unsupported(_))
3450
3451
));
3451
3452
}
@@ -3037,6 +3037,7 @@ impl FolderSource for FakeFolders {3037
for folder in state3037
for folder in state3038
.iter_mut()3038
.iter_mut()3039
.filter(|folder| folder.kind == FolderKind::UserCreated)3039
.filter(|folder| folder.kind == FolderKind::UserCreated)3040
_index: Option<usize>,3040
{3041
{3041
folder.conversation_ids.retain(|id| id != conversation_id);3042
folder.conversation_ids.retain(|id| id != conversation_id);3042
}3043
}@@ -3181,7 +3182,7 @@ async fn folders_refresh_move_and_remove_go_through_the_source_and_cache() {3181
engine.refresh_folders().await.unwrap();3182
engine.refresh_folders().await.unwrap();3182
assert!(events.try_recv().is_err());3183
assert!(events.try_recv().is_err());3183
3184
3184
engine.move_to_folder("a", "work").await.unwrap();3185
engine.move_to_folder("a", "work", None).await.unwrap();3185
assert_eq!(3186
assert_eq!(3186
engine.chat_folders().unwrap()[1].conversation_ids,3187
engine.chat_folders().unwrap()[1].conversation_ids,3187
ids(&["c", "a"])3188
ids(&["c", "a"])@@ -3445,7 +3446,7 @@ async fn folders_without_a_source_are_unsupported() {3445
Err(Error::Unsupported(_))3446
Err(Error::Unsupported(_))3446
));3447
));3447
assert!(matches!(3448
assert!(matches!(3448
engine.move_to_folder("a", "b").await,3449
engine.move_to_folder("a", "b", None).await,3449
Err(Error::Unsupported(_))3450
Err(Error::Unsupported(_))3450
));3451
));3451
}3452
}