BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / folders.rs

folders.rs

folders.rs 23.7 KiB Download Raw
1use std::collections::HashMap;
2use std::future::Future;
3use std::pin::Pin;
4use std::sync::{Arc, Mutex};
5
6use chatsvc::settings::{parse_chat_sections, parse_skin_tone, parse_teams_order};
7use chatsvc::{
8 ChannelNotificationLevel, ChannelNotifications, ChannelSettings, ChatSection,
9 ChatSectionSettings, Pins, UserSettings, pins::SessionTransport,
10};
11use chrono::Utc;
12use serde_json::Value;
13use session::Session;
14use store::{ChannelLayoutRecord, ChannelTabRecord, FolderRecord, Store, TeamLayoutRecord};
15
16use crate::engine::SyncEngine;
17use crate::error::{Error, Result};
18use crate::events::CoreEvent;
19use crate::remote::Remote;
20
21pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum FolderKind {
25 Favorites,
26 UserCreated,
27 Recent,
28 Meeting,
29 Muted,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct ChatFolder {
34 pub id: String,
35 pub name: String,
36 pub kind: FolderKind,
37 pub expanded: bool,
38 pub conversation_ids: Vec<String>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum FolderChange {
43 Create {
44 name: String,
45 },
46 Rename {
47 folder_id: String,
48 name: String,
49 },
50 Delete {
51 folder_id: String,
52 },
53 Move {
54 folder_id: String,
55 target_folder_id: String,
56 },
57}
58
59pub trait FolderSource: Send + Sync {
60 fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>>;
61 fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>>;
62 fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>>;
63 fn section_settings(&self) -> BoxFuture<'_, Result<ChatSectionSettings>>;
64 fn teams_order(&self) -> BoxFuture<'_, Result<Vec<(String, i64)>>> {
65 Box::pin(async { Ok(Vec::new()) })
66 }
67 fn preferred_skin_tone(&self) -> Option<u8> {
68 None
69 }
70 fn set_skin_tone(&self, _tone: u8) -> BoxFuture<'_, Result<()>> {
71 Box::pin(async { Ok(()) })
72 }
73 fn set_section_enabled(&self, section: ChatSection, enabled: bool)
74 -> BoxFuture<'_, Result<()>>;
75 fn set_folder_expanded<'a>(
76 &'a self,
77 folder_id: &'a str,
78 expanded: bool,
79 ) -> BoxFuture<'a, Result<()>>;
80 fn move_to_folder<'a>(
81 &'a self,
82 conversation_id: &'a str,
83 target_folder_id: &'a str,
84 index: Option<usize>,
85 ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>;
86 fn remove_from_folder<'a>(
87 &'a self,
88 conversation_id: &'a str,
89 folder_id: &'a str,
90 ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>;
91 fn change_folders(&self, change: FolderChange) -> BoxFuture<'_, Result<Vec<ChatFolder>>>;
92}
93
94pub struct ChatsvcFolderSource {
95 pins: Pins<SessionTransport>,
96 settings: UserSettings,
97 channel_settings: ChannelSettings,
98 properties: Mutex<Option<Value>>,
99 skin_tone: Mutex<Option<u8>>,
100 group_ids: Mutex<HashMap<String, String>>,
101}
102
103impl ChatsvcFolderSource {
104 pub fn new(session: &Session) -> Self {
105 ChatsvcFolderSource {
106 pins: Pins::new(session),
107 settings: UserSettings::new(session),
108 channel_settings: ChannelSettings::new(session),
109 properties: Mutex::new(None),
110 skin_tone: Mutex::new(None),
111 group_ids: Mutex::new(HashMap::new()),
112 }
113 }
114}
115
116impl FolderSource for ChatsvcFolderSource {
117 fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>> {
118 Box::pin(async { Ok(convert(self.pins.folders().await?)) })
119 }
120
121 fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>> {
122 Box::pin(async { Ok(self.pins.pinned_channels().await?.channel_ids) })
123 }
124
125 fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>> {
126 Box::pin(async {
127 let layout = self.pins.team_layout().await?;
128 *self.group_ids.lock().unwrap() = group_ids_by_thread(&layout);
129 let unknown: Vec<String> = layout
130 .iter()
131 .flat_map(|team| &team.channels)
132 .filter(|channel| channel.notifications.is_none())
133 .map(|channel| channel.channel_id.clone())
134 .collect();
135 let fallback = if unknown.is_empty() {
136 HashMap::new()
137 } else {
138 self.channel_settings
139 .fetch(&unknown)
140 .await
141 .unwrap_or_default()
142 };
143 Ok(layout
144 .into_iter()
145 .map(|team| layout_record(team, &fallback))
146 .collect())
147 })
148 }
149
150 fn section_settings(&self) -> BoxFuture<'_, Result<ChatSectionSettings>> {
151 Box::pin(async {
152 let properties = self.settings.properties().await?;
153 let sections = parse_chat_sections(&properties);
154 *self.skin_tone.lock().unwrap() = parse_skin_tone(&properties);
155 *self.properties.lock().unwrap() = Some(properties);
156 Ok(sections)
157 })
158 }
159
160 fn preferred_skin_tone(&self) -> Option<u8> {
161 *self.skin_tone.lock().unwrap()
162 }
163
164 fn set_skin_tone(&self, tone: u8) -> BoxFuture<'_, Result<()>> {
165 Box::pin(async move { Ok(self.settings.set_skin_tone(tone).await?) })
166 }
167
168 fn teams_order(&self) -> BoxFuture<'_, Result<Vec<(String, i64)>>> {
169 Box::pin(async {
170 let cached = self.properties.lock().unwrap().take();
171 let properties = match cached {
172 Some(properties) => properties,
173 None => self.settings.properties().await?,
174 };
175 let known = self.group_ids.lock().unwrap().clone();
176 let group_ids = if known.is_empty() {
177 group_ids_by_thread(&self.pins.team_layout().await?)
178 } else {
179 known
180 };
181 Ok(order_by_group_id(
182 parse_teams_order(&properties),
183 &group_ids,
184 ))
185 })
186 }
187
188 fn set_section_enabled(
189 &self,
190 section: ChatSection,
191 enabled: bool,
192 ) -> BoxFuture<'_, Result<()>> {
193 Box::pin(async move { Ok(self.settings.set_chat_section(section, enabled).await?) })
194 }
195
196 fn set_folder_expanded<'a>(
197 &'a self,
198 folder_id: &'a str,
199 expanded: bool,
200 ) -> BoxFuture<'a, Result<()>> {
201 Box::pin(async move { Ok(self.pins.set_folder_expanded(folder_id, expanded).await?) })
202 }
203
204 fn move_to_folder<'a>(
205 &'a self,
206 conversation_id: &'a str,
207 target_folder_id: &'a str,
208 index: Option<usize>,
209 ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> {
210 Box::pin(async move {
211 Ok(convert(
212 self.pins
213 .move_to_folder(conversation_id, target_folder_id, index)
214 .await?,
215 ))
216 })
217 }
218
219 fn remove_from_folder<'a>(
220 &'a self,
221 conversation_id: &'a str,
222 folder_id: &'a str,
223 ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> {
224 Box::pin(async move {
225 Ok(convert(
226 self.pins
227 .remove_from_folder(conversation_id, folder_id)
228 .await?,
229 ))
230 })
231 }
232
233 fn change_folders(&self, change: FolderChange) -> BoxFuture<'_, Result<Vec<ChatFolder>>> {
234 Box::pin(async move {
235 let folders = match &change {
236 FolderChange::Create { name } => self.pins.create_folder(name).await?,
237 FolderChange::Rename { folder_id, name } => {
238 self.pins.rename_folder(folder_id, name).await?
239 }
240 FolderChange::Delete { folder_id } => self.pins.delete_folder(folder_id).await?,
241 FolderChange::Move {
242 folder_id,
243 target_folder_id,
244 } => self.pins.move_folder(folder_id, target_folder_id).await?,
245 };
246 Ok(convert(folders))
247 })
248 }
249}
250
251fn convert(folders: chatsvc::Folders) -> Vec<ChatFolder> {
252 folders
253 .folders
254 .into_iter()
255 .map(|folder| ChatFolder {
256 id: folder.id,
257 name: folder.name,
258 kind: match folder.kind {
259 chatsvc::FolderKind::Favorites => FolderKind::Favorites,
260 chatsvc::FolderKind::UserCreated => FolderKind::UserCreated,
261 chatsvc::FolderKind::Recent => FolderKind::Recent,
262 chatsvc::FolderKind::Meeting => FolderKind::Meeting,
263 chatsvc::FolderKind::Muted => FolderKind::Muted,
264 },
265 expanded: folder.expanded,
266 conversation_ids: folder.conversation_ids,
267 })
268 .collect()
269}
270
271fn stored_team_order(teams: &[store::SidebarTeam]) -> Vec<(String, i64)> {
272 teams
273 .iter()
274 .enumerate()
275 .map(|(position, team)| (team.team.id.clone(), position as i64))
276 .collect()
277}
278
279fn group_ids_by_thread(layout: &[chatsvc::TeamLayout]) -> HashMap<String, String> {
280 layout
281 .iter()
282 .filter_map(|team| Some((team.team_id.clone(), team.group_id.clone()?)))
283 .collect()
284}
285
286fn order_by_group_id(
287 order: Vec<(String, i64)>,
288 group_ids: &HashMap<String, String>,
289) -> Vec<(String, i64)> {
290 order
291 .into_iter()
292 .map(|(team_id, position)| match group_ids.get(&team_id) {
293 Some(group_id) => (group_id.clone(), position),
294 None => (team_id, position),
295 })
296 .collect()
297}
298
299fn sort_teams_by_order(teams: &mut [TeamLayoutRecord], order: &[(String, i64)]) {
300 let positions: HashMap<&str, i64> = order
301 .iter()
302 .map(|(team_id, position)| (team_id.as_str(), *position))
303 .collect();
304 teams.sort_by_key(|team| {
305 positions
306 .get(team.team_id.as_str())
307 .copied()
308 .unwrap_or(i64::MAX)
309 });
310}
311
312fn resolve_notifications(
313 channel: &chatsvc::ChannelLayout,
314 fallback: &HashMap<String, ChannelNotifications>,
315) -> ChannelNotifications {
316 channel
317 .notifications
318 .or_else(|| fallback.get(&channel.channel_id).copied())
319 .unwrap_or_else(|| ChannelNotifications::from_followed(channel.followed))
320}
321
322fn notification_record(notifications: ChannelNotifications) -> store::ChannelNotifications {
323 store::ChannelNotifications {
324 level: match notifications.level {
325 ChannelNotificationLevel::BannerAndFeed => {
326 store::ChannelNotificationLevel::BannerAndFeed
327 }
328 ChannelNotificationLevel::Feed => store::ChannelNotificationLevel::Feed,
329 ChannelNotificationLevel::Off => store::ChannelNotificationLevel::Off,
330 },
331 include_replies: notifications.include_replies,
332 }
333}
334
335pub(crate) fn chatsvc_notifications(
336 notifications: store::ChannelNotifications,
337) -> ChannelNotifications {
338 ChannelNotifications {
339 level: match notifications.level {
340 store::ChannelNotificationLevel::BannerAndFeed => {
341 ChannelNotificationLevel::BannerAndFeed
342 }
343 store::ChannelNotificationLevel::Feed => ChannelNotificationLevel::Feed,
344 store::ChannelNotificationLevel::Off => ChannelNotificationLevel::Off,
345 },
346 include_replies: notifications.include_replies,
347 }
348}
349
350fn layout_record(
351 team: chatsvc::TeamLayout,
352 fallback: &HashMap<String, ChannelNotifications>,
353) -> TeamLayoutRecord {
354 TeamLayoutRecord {
355 team_id: team.group_id.unwrap_or(team.team_id),
356 hidden: team.hidden,
357 channels: team
358 .channels
359 .into_iter()
360 .map(|channel| ChannelLayoutRecord {
361 notifications: notification_record(resolve_notifications(&channel, fallback)),
362 channel_id: channel.channel_id,
363 general: channel.general,
364 hidden: channel.hidden,
365 tabs: channel
366 .tabs
367 .into_iter()
368 .map(|tab| ChannelTabRecord {
369 tab_id: tab.id,
370 name: tab.name,
371 definition_id: tab.definition_id,
372 open_url: tab.open_url,
373 })
374 .collect(),
375 })
376 .collect(),
377 }
378}
379
380const META_TEAM_LAYOUT_AT: &str = "team_layout_refreshed_at";
381const KIND_FAVORITES: &str = "Favorites";
382const KIND_USER_CREATED: &str = "UserCreated";
383const KIND_RECENT: &str = "RecentChats";
384const KIND_MEETING: &str = "MeetingChats";
385const KIND_MUTED: &str = "MutedChats";
386const META_MUTED_SECTION: &str = "chat_section_muted";
387const META_MEETING_SECTION: &str = "chat_section_meeting";
388const META_ADAPTIVE_RAIL: &str = "adaptive_rail";
389
390fn to_record(folder: &ChatFolder) -> FolderRecord {
391 FolderRecord {
392 id: folder.id.clone(),
393 name: folder.name.clone(),
394 kind: match folder.kind {
395 FolderKind::Favorites => KIND_FAVORITES,
396 FolderKind::UserCreated => KIND_USER_CREATED,
397 FolderKind::Recent => KIND_RECENT,
398 FolderKind::Meeting => KIND_MEETING,
399 FolderKind::Muted => KIND_MUTED,
400 }
401 .to_owned(),
402 expanded: folder.expanded,
403 conversation_ids: folder.conversation_ids.clone(),
404 }
405}
406
407fn from_record(record: FolderRecord) -> ChatFolder {
408 ChatFolder {
409 id: record.id,
410 name: record.name,
411 kind: match record.kind.as_str() {
412 KIND_FAVORITES => FolderKind::Favorites,
413 KIND_RECENT => FolderKind::Recent,
414 KIND_MEETING => FolderKind::Meeting,
415 KIND_MUTED => FolderKind::Muted,
416 _ => FolderKind::UserCreated,
417 },
418 expanded: record.expanded,
419 conversation_ids: record.conversation_ids,
420 }
421}
422
423pub fn stored_section_settings(store: &Store) -> ChatSectionSettings {
424 let read = |key: &str| {
425 store
426 .meta(key)
427 .ok()
428 .flatten()
429 .and_then(|value| value.parse().ok())
430 };
431 ChatSectionSettings {
432 muted: read(META_MUTED_SECTION),
433 meeting: read(META_MEETING_SECTION),
434 adaptive_rail: read(META_ADAPTIVE_RAIL),
435 }
436}
437
438pub fn store_section_settings(store: &Store, settings: &ChatSectionSettings) -> Result<()> {
439 for (key, value) in [
440 (META_MUTED_SECTION, settings.muted),
441 (META_MEETING_SECTION, settings.meeting),
442 (META_ADAPTIVE_RAIL, settings.adaptive_rail),
443 ] {
444 let text = value.map(|enabled| enabled.to_string()).unwrap_or_default();
445 store.set_meta(key, &text)?;
446 }
447 Ok(())
448}
449
450impl<R: Remote> SyncEngine<R> {
451 pub fn with_folder_source(mut self, source: Arc<dyn FolderSource>) -> Self {
452 self.folder_source = Some(source);
453 self
454 }
455
456 pub fn chat_folders(&self) -> Result<Vec<ChatFolder>> {
457 Ok(self.store.folders()?.into_iter().map(from_record).collect())
458 }
459
460 pub fn pinned_channels(&self) -> Result<Vec<String>> {
461 Ok(self.store.pinned_channel_ids()?)
462 }
463
464 pub async fn refresh_folders(&self) -> Result<()> {
465 let source = self.folder_source()?;
466 let folders = match tokio::try_join!(source.folders(), source.pinned_channels()) {
467 Ok((folders, channels)) => self.store_folders(&folders, &channels),
468 Err(error) => Err(error),
469 };
470 self.refresh_section_settings(source).await;
471 let layout = self.refresh_team_layout(source).await;
472 match (folders, layout) {
473 (Err(folders_error), Err(layout_error)) => {
474 Err(Error::Folders(format!("{folders_error}; {layout_error}")))
475 }
476 (Err(error), Ok(())) | (Ok(()), Err(error)) => Err(error),
477 (Ok(()), Ok(())) => Ok(()),
478 }
479 }
480
481 async fn refresh_section_settings(&self, source: &Arc<dyn FolderSource>) {
482 let outcome = match source.section_settings().await {
483 Ok(settings) => self.apply_section_settings(&settings).and_then(|()| {
484 match source.preferred_skin_tone() {
485 Some(tone) => crate::skin_tone::store_account_skin_tone(&self.store, tone),
486 None => Ok(()),
487 }
488 }),
489 Err(error) => Err(error),
490 };
491 if let Err(error) = outcome {
492 self.report(format!("cannot refresh chat list settings: {error}"));
493 }
494 }
495
496 fn apply_section_settings(&self, settings: &ChatSectionSettings) -> Result<()> {
497 if stored_section_settings(&self.store) != *settings {
498 store_section_settings(&self.store, settings)?;
499 let _ = self.events.send(CoreEvent::FoldersChanged);
500 }
501 Ok(())
502 }
503
504 pub async fn set_section_enabled(&self, section: ChatSection, enabled: bool) -> Result<()> {
505 self.folder_source()?
506 .set_section_enabled(section, enabled)
507 .await
508 }
509
510 pub async fn set_skin_tone(&self, tone: u8) -> Result<()> {
511 self.folder_source()?.set_skin_tone(tone).await?;
512 crate::skin_tone::store_account_skin_tone(&self.store, tone)
513 }
514
515 pub async fn set_folder_expanded(&self, folder_id: &str, expanded: bool) -> Result<()> {
516 self.folder_source()?
517 .set_folder_expanded(folder_id, expanded)
518 .await
519 }
520
521 async fn refresh_team_layout(&self, source: &Arc<dyn FolderSource>) -> Result<()> {
522 let last = self.store.meta_time(META_TEAM_LAYOUT_AT)?;
523 if last.is_some_and(|last| Utc::now() - last < self.config.team_layout_refresh_interval) {
524 return Ok(());
525 }
526 let mut layout = source.team_layout().await?;
527 let before = self.store.sidebar()?.teams;
528 let order = match source.teams_order().await {
529 Ok(order) => order,
530 Err(error) => {
531 self.report(format!("cannot read the team order: {error}"));
532 stored_team_order(&before)
533 }
534 };
535 sort_teams_by_order(&mut layout, &order);
536 self.store.replace_team_layout(&layout)?;
537 self.store.set_meta_time(META_TEAM_LAYOUT_AT, Utc::now())?;
538 if self.store.sidebar()?.teams != before {
539 let _ = self.events.send(CoreEvent::SidebarChanged);
540 }
541 Ok(())
542 }
543
544 pub async fn move_to_folder(
545 &self,
546 conversation_id: &str,
547 target_folder_id: &str,
548 index: Option<usize>,
549 ) -> Result<()> {
550 let folders = self
551 .folder_source()?
552 .move_to_folder(conversation_id, target_folder_id, index)
553 .await?;
554 self.store_folders(&folders, &self.store.pinned_channel_ids()?)
555 }
556
557 pub async fn remove_from_folder(&self, conversation_id: &str, folder_id: &str) -> Result<()> {
558 let folders = self
559 .folder_source()?
560 .remove_from_folder(conversation_id, folder_id)
561 .await?;
562 self.store_folders(&folders, &self.store.pinned_channel_ids()?)
563 }
564
565 pub async fn change_folders(&self, change: FolderChange) -> Result<()> {
566 let folders = self.folder_source()?.change_folders(change).await?;
567 self.store_folders(&folders, &self.store.pinned_channel_ids()?)
568 }
569
570 fn folder_source(&self) -> Result<&Arc<dyn FolderSource>> {
571 self.folder_source
572 .as_ref()
573 .ok_or(Error::Unsupported("chat folders without a folder source"))
574 }
575
576 fn store_folders(&self, folders: &[ChatFolder], channel_ids: &[String]) -> Result<()> {
577 let previous = (self.chat_folders()?, self.store.pinned_channel_ids()?);
578 let records: Vec<FolderRecord> = folders.iter().map(to_record).collect();
579 self.store.replace_folders(&records, channel_ids)?;
580 if previous != (folders.to_vec(), channel_ids.to_vec()) {
581 let _ = self.events.send(CoreEvent::FoldersChanged);
582 }
583 Ok(())
584 }
585}
586
587#[cfg(test)]
588mod tests {
589 use super::*;
590
591 fn channel(
592 notifications: Option<ChannelNotifications>,
593 followed: bool,
594 ) -> chatsvc::ChannelLayout {
595 chatsvc::ChannelLayout {
596 channel_id: "19:a".into(),
597 general: false,
598 hidden: false,
599 tabs: Vec::new(),
600 notifications,
601 followed,
602 }
603 }
604
605 fn level(level: ChannelNotificationLevel, include_replies: bool) -> ChannelNotifications {
606 ChannelNotifications {
607 level,
608 include_replies,
609 }
610 }
611
612 fn team(team_id: &str) -> TeamLayoutRecord {
613 TeamLayoutRecord {
614 team_id: team_id.into(),
615 hidden: false,
616 channels: Vec::new(),
617 }
618 }
619
620 fn team_ids(teams: &[TeamLayoutRecord]) -> Vec<&str> {
621 teams.iter().map(|team| team.team_id.as_str()).collect()
622 }
623
624 #[test]
625 fn listed_teams_come_first_by_order_and_unlisted_follow_in_server_order() {
626 let mut teams = vec![team("x"), team("b"), team("y"), team("a"), team("z")];
627 let order = vec![
628 ("a".to_owned(), 0),
629 ("b".to_owned(), 1),
630 ("gone".to_owned(), 2),
631 ];
632 sort_teams_by_order(&mut teams, &order);
633 assert_eq!(team_ids(&teams), ["a", "b", "x", "y", "z"]);
634 }
635
636 #[test]
637 fn real_payloads_give_the_team_order_teams_web_shows() {
638 let fixture: Value =
639 serde_json::from_str(include_str!("../tests/fixtures/team_order.json")).unwrap();
640 let layout = chatsvc::pins::parse_team_layout(&fixture["csaTeams"]).unwrap();
641 let group_ids = group_ids_by_thread(&layout);
642 let order = order_by_group_id(parse_teams_order(&fixture["userProperties"]), &group_ids);
643 let mut records: Vec<TeamLayoutRecord> = layout
644 .into_iter()
645 .map(|team| layout_record(team, &HashMap::new()))
646 .collect();
647 sort_teams_by_order(&mut records, &order);
648 let store = Store::open_in_memory().unwrap();
649 let names: HashMap<String, String> = fixture["csaTeams"]["teams"]
650 .as_array()
651 .unwrap()
652 .iter()
653 .map(|team| {
654 let group_id = team["teamSiteInformation"]["groupId"].as_str().unwrap();
655 let name = team["displayName"].as_str().unwrap();
656 (group_id.to_owned(), name.to_owned())
657 })
658 .collect();
659 let graph_teams: Vec<store::TeamRecord> = names
660 .iter()
661 .map(|(group_id, name)| store::TeamRecord {
662 id: group_id.clone(),
663 name: name.clone(),
664 })
665 .collect();
666 store.upsert_teams(&graph_teams).unwrap();
667 store.replace_team_layout(&records).unwrap();
668
669 let shown: Vec<String> = store
670 .sidebar()
671 .unwrap()
672 .teams
673 .into_iter()
674 .filter(|team| !team.hidden)
675 .map(|team| team.team.id)
676 .collect();
677 let expected: Vec<String> = fixture["teamsWebShownTeams"]
678 .as_array()
679 .unwrap()
680 .iter()
681 .map(|team| team["groupId"].as_str().unwrap().to_owned())
682 .collect();
683 assert_eq!(shown, expected);
684 }
685
686 #[test]
687 fn an_empty_order_keeps_the_server_order() {
688 let mut teams = vec![team("c"), team("a"), team("b")];
689 sort_teams_by_order(&mut teams, &[]);
690 assert_eq!(team_ids(&teams), ["c", "a", "b"]);
691 }
692
693 #[test]
694 fn channel_value_beats_the_notification_service_beats_the_followed_flag() {
695 let off = level(ChannelNotificationLevel::Off, false);
696 let banner = level(ChannelNotificationLevel::BannerAndFeed, true);
697 let fallback = HashMap::from([("19:a".to_owned(), banner)]);
698 assert_eq!(
699 resolve_notifications(&channel(Some(off), true), &fallback),
700 off
701 );
702 assert_eq!(
703 resolve_notifications(&channel(None, false), &fallback),
704 banner
705 );
706 assert_eq!(
707 resolve_notifications(&channel(None, true), &HashMap::new()),
708 banner
709 );
710 assert_eq!(
711 resolve_notifications(&channel(None, false), &HashMap::new()),
712 ChannelNotifications::default()
713 );
714 }
715}