| 1 | use serde_json::Value; |
| 2 | use session::Request; |
| 3 | |
| 4 | use super::Pins; |
| 5 | use super::chats::ensure_success; |
| 6 | use super::transport::CsaTransport; |
| 7 | use crate::error::{Error, Result}; |
| 8 | |
| 9 | const TEAMS_QUERY: &str = "isPrefetch=false&enableMembershipSummary=true"; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub struct ChannelLayout { |
| 13 | pub channel_id: String, |
| 14 | pub general: bool, |
| 15 | pub hidden: bool, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct TeamLayout { |
| 20 | pub team_id: String, |
| 21 | pub hidden: bool, |
| 22 | pub channels: Vec<ChannelLayout>, |
| 23 | } |
| 24 | |
| 25 | impl<T: CsaTransport> Pins<T> { |
| 26 | pub async fn team_layout(&self) -> Result<Vec<TeamLayout>> { |
| 27 | let answer = self |
| 28 | .transport |
| 29 | .send(Request::get(format!("{}?{TEAMS_QUERY}", self.base_url))) |
| 30 | .await?; |
| 31 | ensure_success(&answer)?; |
| 32 | parse_team_layout(&answer.body) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub fn parse_team_layout(body: &Value) -> Result<Vec<TeamLayout>> { |
| 37 | let teams = body |
| 38 | .get("teams") |
| 39 | .and_then(Value::as_array) |
| 40 | .ok_or_else(|| Error::UnexpectedAnswer("teams missing".into()))?; |
| 41 | Ok(teams.iter().filter_map(parse_team).collect()) |
| 42 | } |
| 43 | |
| 44 | fn flag(value: &Value, key: &str) -> Option<bool> { |
| 45 | value.get(key).and_then(Value::as_bool) |
| 46 | } |
| 47 | |
| 48 | fn parse_team(team: &Value) -> Option<TeamLayout> { |
| 49 | if flag(team, "isDeleted") == Some(true) { |
| 50 | return None; |
| 51 | } |
| 52 | let general_shown = flag(team, "isGeneralChannelFavorite").unwrap_or(true); |
| 53 | let channels = team |
| 54 | .get("channels") |
| 55 | .and_then(Value::as_array) |
| 56 | .map(|channels| { |
| 57 | channels |
| 58 | .iter() |
| 59 | .filter(|channel| flag(channel, "isDeleted") != Some(true)) |
| 60 | .filter_map(|channel| { |
| 61 | let general = flag(channel, "isGeneral").unwrap_or(false); |
| 62 | let shown = flag(channel, "isFavorite") |
| 63 | .unwrap_or(if general { general_shown } else { true }); |
| 64 | Some(ChannelLayout { |
| 65 | channel_id: channel.get("id")?.as_str()?.to_owned(), |
| 66 | general, |
| 67 | hidden: !shown, |
| 68 | }) |
| 69 | }) |
| 70 | .collect() |
| 71 | }) |
| 72 | .unwrap_or_default(); |
| 73 | Some(TeamLayout { |
| 74 | team_id: team.get("id")?.as_str()?.to_owned(), |
| 75 | hidden: flag(team, "isFavorite") == Some(false), |
| 76 | channels, |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |
| 82 | use serde_json::json; |
| 83 | |
| 84 | use super::*; |
| 85 | |
| 86 | #[test] |
| 87 | fn keeps_the_service_order_and_reads_hidden_flags() { |
| 88 | let body = json!({"teams": [ |
| 89 | {"id": "19:b@thread.tacv2", "isFavorite": true, "channels": [ |
| 90 | {"id": "19:b2@thread.tacv2", "isFavorite": false}, |
| 91 | {"id": "19:b@thread.tacv2", "isGeneral": true, "isFavorite": true} |
| 92 | ]}, |
| 93 | {"id": "19:gone@thread.tacv2", "isDeleted": true, "channels": []}, |
| 94 | {"id": "19:a@thread.tacv2", "isFavorite": false, "isGeneralChannelFavorite": false, "channels": [ |
| 95 | {"id": "19:a@thread.tacv2", "isGeneral": true}, |
| 96 | {"id": "19:a2@thread.tacv2"} |
| 97 | ]} |
| 98 | ]}); |
| 99 | let layout = parse_team_layout(&body).unwrap(); |
| 100 | let ids: Vec<&str> = layout.iter().map(|team| team.team_id.as_str()).collect(); |
| 101 | assert_eq!(ids, ["19:b@thread.tacv2", "19:a@thread.tacv2"]); |
| 102 | assert!(!layout[0].hidden); |
| 103 | assert!(layout[1].hidden); |
| 104 | assert_eq!( |
| 105 | layout[0].channels, |
| 106 | [ |
| 107 | ChannelLayout { |
| 108 | channel_id: "19:b2@thread.tacv2".into(), |
| 109 | general: false, |
| 110 | hidden: true |
| 111 | }, |
| 112 | ChannelLayout { |
| 113 | channel_id: "19:b@thread.tacv2".into(), |
| 114 | general: true, |
| 115 | hidden: false |
| 116 | }, |
| 117 | ] |
| 118 | ); |
| 119 | assert!(layout[1].channels[0].hidden); |
| 120 | assert!(!layout[1].channels[1].hidden); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn rejects_an_answer_without_teams() { |
| 125 | assert!(parse_team_layout(&json!({"chats": []})).is_err()); |
| 126 | } |
| 127 | } |